I was trying to make a basic AI like Gpt 2 while chatting and voice capability. Also, perform simple commands. So here I made one with Python.It
import pyttsx3
import speech_recognition as sr
import os
import webbrowser
from transformers import AutoModelForCausalLM, AutoTokenizer
from PIL import Image
import pytesseract
import smtplib
from email.message import EmailMessage
engine = pyttsx3.init()
def speak(text):
engine.say(text)
engine.runAndWait()
def listen():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
try:
audio = recognizer.listen(source, timeout=5)
query = recognizer.recognize_google(audio)
return query
except:
return "Sorry, I didn't catch that."
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
def chat_with_ai(prompt):
inputs = tokenizer.encode(prompt, return_tensors="pt")
outputs = model.generate(inputs, max_length=150, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
def analyze_image(image_path):
try:
text = pytesseract.image_to_string(Image.open(image_path))
return text
except:
return None
def generate_image(prompt):
pass
def send_email(to_email, subject, body, attachment=None):
try:
sender_email = "[email protected]"
sender_password = "your_password"
msg = EmailMessage()
msg["Subject"] = subject
msg["From"] = sender_email
msg["To"] = to_email
msg.set_content(body)
if attachment:
with open(attachment, "rb") as file:
file_data = file.read()
file_name = os.path.basename(file.name)
msg.add_attachment(file_data, maintype="image", subtype="jpeg", filename=file_name)
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
smtp.login(sender_email, sender_password)
smtp.send_message(msg)
except:
pass
def handle_command(query):
if "open browser" in query.lower():
webbrowser.open("https://google.com")
elif "play music" in query.lower():
os.system("start spotify")
elif "generate image" in query.lower():
prompt = query.replace("generate image", "").strip()
generate_image(prompt)
elif "analyze image" in query.lower():
image_path = input("Enter the image path: ")
analyze_image(image_path)
elif "send email" in query.lower():
to_email = input("Enter recipient's email: ")
subject = input("Enter subject: ")
body = input("Enter email body: ")
attachment = input("Enter attachment path (or press Enter to skip): ")
send_email(to_email, subject, body, attachment if attachment else None)
elif "exit" in query.lower():
exit()
else:
ai_response = chat_with_ai(query)
speak(ai_response)
def main():
global command
mode = input("Type 'text' for text chat or 'voice' for voice chat: ").strip().lower()
speak("Hello! How can I assist you today?")
while True:
if mode == "text":
command = input("You: ")
elif mode == "voice":
command = listen()
if command.lower() == "switch to text":
mode = "text"
continue
elif command.lower() == "switch to voice":
mode = "voice"
continue
elif command.lower() == "exit":
break
handle_command(command)
if __name__ == "__main__":
main()
Supposed to do:
Starting the Program: When you run the program, it asks you to choose
between Text Mode or Voice Mode. Type ‘text’ for typing commands or
‘voice’ for speaking commands.Giving Commands:
Text Mode: Type commands like ‘open browser’, ‘play music’, ‘analyze
image’, or ‘send email’. Voice Mode: Speak commands like ‘open
browser’ or ‘play music’, and the program will execute them.
Switching Between Modes:In Text Mode, type ‘switch to voice’ to switch to Voice Mode. In Voice
Mode, say ‘switch to text’ to return to Text Mode. AI’s Response:The AI performs actions based on your command: opening the browser,
playing music, analyzing images, or sending emails. If the command
isn’t recognized, the AI responds with a default conversation from the
GPT model.
Exiting the Program: Type or say ‘exit’ at any time to close the
program.Example:
Start the program and choose ‘text’ mode. Type ‘open browser’, and the
browser opens. Switch to ‘voice’ mode by typing ‘switch to voice’. Say
‘play music’, and music plays. Type ‘exit’ to close the program.
but the issue is, that it’s not doing anything. It just takes input and doesn’t response. So please analyze the code and help me find the issue or correct the code so the AI will respond as it’s supposed to do.
1