Why AI is not responding? [closed]

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật