Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
|
|
2 |
import openai
|
3 |
import os
|
4 |
import json
|
|
|
|
|
5 |
|
6 |
# OpenAI API setup
|
7 |
openai.api_key = os.getenv("GROQ_API_KEY")
|
@@ -81,6 +83,19 @@ def get_groq_response(message, category, history=[]):
|
|
81 |
except Exception as e:
|
82 |
return f"Error: {str(e)}"
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
# Chatbot function to handle user input and history
|
85 |
def chatbot(user_input, sub_category, history):
|
86 |
# Load conversation history
|
@@ -210,12 +225,21 @@ with gr.Blocks(css="""
|
|
210 |
user_input = gr.Textbox(label="Your Message", placeholder="Type something...", lines=1)
|
211 |
send_button = gr.Button("Send")
|
212 |
clear_button = gr.Button("Clear History")
|
|
|
|
|
213 |
# Chatbot UI
|
214 |
chatbot_ui = gr.Chatbot(type="messages")
|
215 |
|
216 |
history_state = gr.State(load_history())
|
217 |
|
|
|
218 |
send_button.click(chatbot, inputs=[user_input, sub_category, history_state], outputs=[chatbot_ui, history_state, user_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
# Clear input field after sending message
|
221 |
user_input.submit(chatbot, inputs=[user_input, sub_category, history_state], outputs=[chatbot_ui, history_state, user_input])
|
|
|
2 |
import openai
|
3 |
import os
|
4 |
import json
|
5 |
+
from gtts import gTTS
|
6 |
+
|
7 |
|
8 |
# OpenAI API setup
|
9 |
openai.api_key = os.getenv("GROQ_API_KEY")
|
|
|
83 |
except Exception as e:
|
84 |
return f"Error: {str(e)}"
|
85 |
|
86 |
+
def text_to_speech(latest_response):
|
87 |
+
try:
|
88 |
+
if not latest_response: # If there's no response
|
89 |
+
return None
|
90 |
+
tts = gTTS(latest_response, lang="en") # Generate speech from text
|
91 |
+
audio_file = "response_audio.mp3"
|
92 |
+
tts.save(audio_file)
|
93 |
+
return audio_file
|
94 |
+
except Exception as e:
|
95 |
+
print(f"Error generating audio: {e}")
|
96 |
+
return None
|
97 |
+
|
98 |
+
|
99 |
# Chatbot function to handle user input and history
|
100 |
def chatbot(user_input, sub_category, history):
|
101 |
# Load conversation history
|
|
|
225 |
user_input = gr.Textbox(label="Your Message", placeholder="Type something...", lines=1)
|
226 |
send_button = gr.Button("Send")
|
227 |
clear_button = gr.Button("Clear History")
|
228 |
+
hear_button = gr.Button("Hear Response")
|
229 |
+
audio_output = gr.Audio(label="Bot's Voice", type="filepath", interactive=False)
|
230 |
# Chatbot UI
|
231 |
chatbot_ui = gr.Chatbot(type="messages")
|
232 |
|
233 |
history_state = gr.State(load_history())
|
234 |
|
235 |
+
# Chat interaction
|
236 |
send_button.click(chatbot, inputs=[user_input, sub_category, history_state], outputs=[chatbot_ui, history_state, user_input])
|
237 |
+
hear_button.click(
|
238 |
+
lambda latest: text_to_speech(latest[-1][1] if latest else "No response yet."), # Gracefully handles empty history
|
239 |
+
inputs=[history_state],
|
240 |
+
outputs=audio_output
|
241 |
+
)
|
242 |
+
|
243 |
|
244 |
# Clear input field after sending message
|
245 |
user_input.submit(chatbot, inputs=[user_input, sub_category, history_state], outputs=[chatbot_ui, history_state, user_input])
|