DSatishchandra commited on
Commit
f2acc6d
·
verified ·
1 Parent(s): 4854697

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -1,14 +1,31 @@
1
  import gradio as gr
2
  from gtts import gTTS
3
  import os
 
4
 
5
  # Text-to-Speech Function
6
  def speak(text):
7
  tts = gTTS(text=text, lang='en')
8
  tts.save("output.mp3")
9
- os.system("mpg123 output.mp3")
 
10
 
11
- # Menu and Voice Assistant Logic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def voice_assistant(user_input):
13
  menu = {
14
  "Bread": ["Roti", "Naan", "Paratha"],
@@ -52,18 +69,20 @@ def voice_assistant(user_input):
52
  return response
53
 
54
  # Gradio UI
55
- def interact_with_voice_assistant(user_input):
 
56
  return voice_assistant(user_input)
57
 
58
  iface = gr.Interface(
59
  fn=interact_with_voice_assistant,
60
- inputs="text",
61
  outputs="text",
62
  title="DynamicMenuApp Voice Assistant",
63
  description=(
64
  "Interact with the DynamicMenuApp Voice Assistant to navigate the menu, "
65
  "customize your food, and place orders seamlessly."
66
- )
 
67
  )
68
 
69
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from gtts import gTTS
3
  import os
4
+ import speech_recognition as sr
5
 
6
  # Text-to-Speech Function
7
  def speak(text):
8
  tts = gTTS(text=text, lang='en')
9
  tts.save("output.mp3")
10
+ # Use 'play' as a fallback if mpg123 is not installed
11
+ os.system("mpg123 output.mp3 || play output.mp3")
12
 
13
+ # Speech Recognition (Voice Input)
14
+ def recognize_speech():
15
+ recognizer = sr.Recognizer()
16
+ with sr.Microphone() as source:
17
+ print("Listening...")
18
+ try:
19
+ audio = recognizer.listen(source, timeout=5)
20
+ text = recognizer.recognize_google(audio)
21
+ print(f"You said: {text}")
22
+ return text
23
+ except sr.UnknownValueError:
24
+ return "I didn't catch that. Could you please repeat?"
25
+ except sr.RequestError:
26
+ return "Sorry, there was an issue recognizing your speech. Try again later."
27
+
28
+ # Voice Assistant Logic
29
  def voice_assistant(user_input):
30
  menu = {
31
  "Bread": ["Roti", "Naan", "Paratha"],
 
69
  return response
70
 
71
  # Gradio UI
72
+ def interact_with_voice_assistant():
73
+ user_input = recognize_speech()
74
  return voice_assistant(user_input)
75
 
76
  iface = gr.Interface(
77
  fn=interact_with_voice_assistant,
78
+ inputs=[],
79
  outputs="text",
80
  title="DynamicMenuApp Voice Assistant",
81
  description=(
82
  "Interact with the DynamicMenuApp Voice Assistant to navigate the menu, "
83
  "customize your food, and place orders seamlessly."
84
+ ),
85
+ live=True
86
  )
87
 
88
  if __name__ == "__main__":