import gradio as gr from gtts import gTTS import hashlib import os # Cache for storing generated audio audio_cache = {} # Chatbot Response Function def chatbot_response(user_input): responses = { "hello": "Hello! How can I help you?", "how are you": "I'm just a bot, but I'm doing great! What about you?", "bye": "Goodbye! Have a nice day!", } return responses.get(user_input.lower(), "Sorry, I didn't understand that.") # Text-to-Speech Function with Caching def speak(text): text_hash = hashlib.md5(text.encode()).hexdigest() audio_file = f"{text_hash}.mp3" if text_hash not in audio_cache: tts = gTTS(text=text, lang='en', slow=False) tts.save(audio_file) audio_cache[text_hash] = audio_file return audio_cache[text_hash] # Combined Function for Gradio Interface def chatbot(user_input): response = chatbot_response(user_input) audio_path = speak(response) return response, audio_path # Gradio Interface iface = gr.Interface( fn=chatbot, inputs=gr.Textbox(lines=2, placeholder="Type your message here..."), outputs=[gr.Textbox(), gr.Audio()], title="AI Female Voice Chatbot", description="Simple chatbot with text-to-speech functionality." ) iface.launch()