montassarTester commited on
Commit
8bcde54
·
verified ·
1 Parent(s): 5879024

update text to speech

Browse files
Files changed (1) hide show
  1. chat_app.py +36 -8
chat_app.py CHANGED
@@ -2,7 +2,11 @@ import os
2
  from groq import Groq
3
  import gradio as gr
4
  from dotenv import load_dotenv
 
 
 
5
  load_dotenv()
 
6
  # Load API key from environment variable
7
  API_KEY = os.getenv("GROQ_API_KEY")
8
  if not API_KEY:
@@ -45,6 +49,18 @@ def get_agriculture_response(input_text):
45
  except Exception as e:
46
  return f"Error: {e}"
47
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  def groq_chatbot(input_text, chat_history):
49
  validation_result = validate_input(input_text)
50
 
@@ -55,9 +71,16 @@ def groq_chatbot(input_text, chat_history):
55
  else:
56
  response = f"⚠️ Unexpected response: {validation_result}"
57
 
58
- # Append to chat history
59
  chat_history.append((input_text, response))
60
- return chat_history, "" # Clears input field after submission
 
 
 
 
 
 
 
 
61
 
62
  def launch_gradio_interface():
63
  with gr.Blocks() as demo:
@@ -67,16 +90,21 @@ def launch_gradio_interface():
67
  chatbot = gr.Chatbot(height=400)
68
  msg = gr.Textbox(placeholder="Ask a question about agriculture...", label="Your Question")
69
  clear = gr.Button("Clear Chat")
 
 
 
 
70
 
71
- chat_history_state = gr.State([]) # Stores chat history
 
72
 
73
- # Enter key submits the question
74
- msg.submit(fn=groq_chatbot, inputs=[msg, chat_history_state], outputs=[chatbot, msg])
75
 
76
- # Clicking "Clear Chat" resets history
77
- clear.click(lambda: ([], ""), None, [chatbot, msg], queue=False)
78
 
79
  demo.launch(share=True)
80
 
81
  if __name__ == "__main__":
82
- launch_gradio_interface()
 
2
  from groq import Groq
3
  import gradio as gr
4
  from dotenv import load_dotenv
5
+ from gtts import gTTS
6
+ import tempfile
7
+
8
  load_dotenv()
9
+
10
  # Load API key from environment variable
11
  API_KEY = os.getenv("GROQ_API_KEY")
12
  if not API_KEY:
 
49
  except Exception as e:
50
  return f"Error: {e}"
51
 
52
+ def text_to_speech(text):
53
+ if not text.strip():
54
+ return None
55
+ try:
56
+ tts = gTTS(text=text, lang='en')
57
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
58
+ tts.save(temp_file.name)
59
+ return temp_file.name
60
+ except Exception as e:
61
+ print(f"TTS error: {e}")
62
+ return None
63
+
64
  def groq_chatbot(input_text, chat_history):
65
  validation_result = validate_input(input_text)
66
 
 
71
  else:
72
  response = f"⚠️ Unexpected response: {validation_result}"
73
 
 
74
  chat_history.append((input_text, response))
75
+ # Return chat history, clear input, reset audio output
76
+ return chat_history, "", None
77
+
78
+ def on_tts_click(chat_history):
79
+ if chat_history and chat_history[-1][1]:
80
+ last_response = chat_history[-1][1]
81
+ audio_path = text_to_speech(last_response)
82
+ return audio_path
83
+ return None
84
 
85
  def launch_gradio_interface():
86
  with gr.Blocks() as demo:
 
90
  chatbot = gr.Chatbot(height=400)
91
  msg = gr.Textbox(placeholder="Ask a question about agriculture...", label="Your Question")
92
  clear = gr.Button("Clear Chat")
93
+ tts_button = gr.Button("Text-to-Speech")
94
+ audio_player = gr.Audio(label="Response Audio", interactive=False)
95
+
96
+ chat_history_state = gr.State([])
97
 
98
+ # Submit user question
99
+ msg.submit(fn=groq_chatbot, inputs=[msg, chat_history_state], outputs=[chatbot, msg, audio_player])
100
 
101
+ # Clear chat and reset audio
102
+ clear.click(lambda: ([], "", None), None, [chatbot, msg, audio_player], queue=False)
103
 
104
+ # Generate TTS audio of last response
105
+ tts_button.click(fn=on_tts_click, inputs=chat_history_state, outputs=audio_player)
106
 
107
  demo.launch(share=True)
108
 
109
  if __name__ == "__main__":
110
+ launch_gradio_interface()