Raiff1982 commited on
Commit
246a8f4
·
verified ·
1 Parent(s): 4a7d3ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -1,6 +1,3 @@
1
- # This is a Gradio app that simulates the functionality of the provided Tkinter application.
2
- # It includes a text input, a voice input button, and a response area to display the AI's response.
3
-
4
  import gradio as gr
5
  import asyncio
6
  import speech_recognition as sr
@@ -20,29 +17,38 @@ async def process_query(query):
20
  return result['response']
21
 
22
  # Function to handle the text input submission
23
- def submit_query(query):
24
  if not query:
25
- return ""
26
  response = asyncio.run(process_query(query))
27
- return f"Response: {response}"
 
 
28
 
29
  # Function to handle voice input
30
  def listen_voice_command():
31
  recognizer = sr.Recognizer()
32
  with sr.Microphone() as source:
33
- print("Listening for voice command...")
34
  audio = recognizer.listen(source)
35
  try:
36
- command = recognizer.recognize_google(audio)
37
- return command
38
  except sr.UnknownValueError:
39
- return "Voice command not recognized."
40
- except sr.RequestError:
41
- return "Could not request results; check your network connection."
42
 
43
- # Create the Gradio interface
44
  with gr.Blocks() as demo:
45
- query_entry = gr.Textbox(label="Enter your query")
46
- response_area = gr.Textbox(label="Response")
47
- submit_button = gr.Button("Submit")
48
- voice_button = gr
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import asyncio
3
  import speech_recognition as sr
 
17
  return result['response']
18
 
19
  # Function to handle the text input submission
20
+ def submit_query(query, chat_history):
21
  if not query:
22
+ return chat_history
23
  response = asyncio.run(process_query(query))
24
+ chat_history.append({"role": "user", "content": query})
25
+ chat_history.append({"role": "assistant", "content": response})
26
+ return "", chat_history
27
 
28
  # Function to handle voice input
29
  def listen_voice_command():
30
  recognizer = sr.Recognizer()
31
  with sr.Microphone() as source:
32
+ print("Listening...")
33
  audio = recognizer.listen(source)
34
  try:
35
+ query = recognizer.recognize_google(audio)
36
+ return query
37
  except sr.UnknownValueError:
38
+ return "Could not understand audio"
39
+ except sr.RequestError as e:
40
+ return f"Could not request results; {e}"
41
 
42
+ # Gradio app
43
  with gr.Blocks() as demo:
44
+ chatbot = gr.Chatbot(type="messages")
45
+ msg = gr.Textbox(label="Enter your message")
46
+ voice_btn = gr.Button("Speak")
47
+ clear = gr.Button("Clear")
48
+
49
+ # Event listeners
50
+ msg.submit(submit_query, [msg, chatbot], [msg, chatbot], queue=False)
51
+ voice_btn.click(listen_voice_command, None, msg, queue=False)
52
+ clear.click(lambda: None, None, chatbot, queue=False)
53
+
54
+ demo.launch(show_error=True)