arpit13 commited on
Commit
f17ef40
·
verified ·
1 Parent(s): 961032c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -0
app.py CHANGED
@@ -51,6 +51,18 @@ def get_groq_response(message, history=[]):
51
  except Exception as e:
52
  return f"Error: {str(e)}"
53
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # Chatbot function
55
  def chatbot(user_input, history):
56
  # Load conversation history
@@ -150,6 +162,8 @@ with gr.Blocks(css="""
150
  # Chatbot UI
151
  chatbot_ui = gr.Chatbot()
152
  user_input = gr.Textbox(label="Type your message here:", placeholder="Ask me anything...", lines=1)
 
 
153
  clear_button = gr.Button("Clear History")
154
  system_message = gr.Textbox(label="System Message", interactive=False)
155
 
@@ -157,6 +171,10 @@ with gr.Blocks(css="""
157
 
158
  # Chat interaction
159
  user_input.submit(chatbot, inputs=[user_input, history_state], outputs=[chatbot_ui, history_state, user_input])
 
 
 
 
160
 
161
  # Clear history button action
162
  clear_button.click(clear_conversation_history, inputs=None, outputs=system_message)
 
51
  except Exception as e:
52
  return f"Error: {str(e)}"
53
 
54
+ # Text-to-Speech function
55
+ def text_to_speech(latest_response):
56
+ try:
57
+ tts = gTTS(latest_response, lang="en") # Generate speech from text
58
+ audio_file = "response_audio.mp3"
59
+ tts.save(audio_file)
60
+ print(f"Audio file saved at: {audio_file}") # Debugging print
61
+ return audio_file # Ensure correct file path is returned
62
+ except Exception as e:
63
+ print(f"Error generating audio: {e}")
64
+ return None
65
+
66
  # Chatbot function
67
  def chatbot(user_input, history):
68
  # Load conversation history
 
162
  # Chatbot UI
163
  chatbot_ui = gr.Chatbot()
164
  user_input = gr.Textbox(label="Type your message here:", placeholder="Ask me anything...", lines=1)
165
+ hear_button = gr.Button("Hear Response")
166
+ audio_output = gr.Audio(label="Bot's Voice", type="filepath", interactive=False)
167
  clear_button = gr.Button("Clear History")
168
  system_message = gr.Textbox(label="System Message", interactive=False)
169
 
 
171
 
172
  # Chat interaction
173
  user_input.submit(chatbot, inputs=[user_input, history_state], outputs=[chatbot_ui, history_state, user_input])
174
+ hear_button.click(
175
+ lambda latest: text_to_speech(latest[-1][1] if latest else ""), # Fetch latest bot response
176
+ inputs=[history_state], # Pass the conversation history
177
+ outputs=audio_output # Output the file to the audio player
178
 
179
  # Clear history button action
180
  clear_button.click(clear_conversation_history, inputs=None, outputs=system_message)