File size: 1,276 Bytes
04243d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()