Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gtts import gTTS
|
3 |
+
import hashlib
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Cache for storing generated audio
|
7 |
+
audio_cache = {}
|
8 |
+
|
9 |
+
# Chatbot Response Function
|
10 |
+
def chatbot_response(user_input):
|
11 |
+
responses = {
|
12 |
+
"hello": "Hello! How can I help you?",
|
13 |
+
"how are you": "I'm just a bot, but I'm doing great! What about you?",
|
14 |
+
"bye": "Goodbye! Have a nice day!",
|
15 |
+
}
|
16 |
+
return responses.get(user_input.lower(), "Sorry, I didn't understand that.")
|
17 |
+
|
18 |
+
# Text-to-Speech Function with Caching
|
19 |
+
def speak(text):
|
20 |
+
text_hash = hashlib.md5(text.encode()).hexdigest()
|
21 |
+
audio_file = f"{text_hash}.mp3"
|
22 |
+
|
23 |
+
if text_hash not in audio_cache:
|
24 |
+
tts = gTTS(text=text, lang='en', slow=False)
|
25 |
+
tts.save(audio_file)
|
26 |
+
audio_cache[text_hash] = audio_file
|
27 |
+
|
28 |
+
return audio_cache[text_hash]
|
29 |
+
|
30 |
+
# Combined Function for Gradio Interface
|
31 |
+
def chatbot(user_input):
|
32 |
+
response = chatbot_response(user_input)
|
33 |
+
audio_path = speak(response)
|
34 |
+
return response, audio_path
|
35 |
+
|
36 |
+
# Gradio Interface
|
37 |
+
iface = gr.Interface(
|
38 |
+
fn=chatbot,
|
39 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your message here..."),
|
40 |
+
outputs=[gr.Textbox(), gr.Audio()],
|
41 |
+
title="AI Female Voice Chatbot",
|
42 |
+
description="Simple chatbot with text-to-speech functionality."
|
43 |
+
)
|
44 |
+
|
45 |
+
iface.launch()
|