DSatishchandra commited on
Commit
a2fa8f5
·
verified ·
1 Parent(s): 2866b97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -59
app.py CHANGED
@@ -3,82 +3,42 @@ 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
- os.system("play output.mp3 || mpg123 output.mp3")
11
 
12
- # Speech Recognition (Voice Input) Using Uploaded Audio
13
  def recognize_speech(audio_file):
14
  recognizer = sr.Recognizer()
15
  with sr.AudioFile(audio_file) as source:
16
  audio_data = recognizer.record(source)
17
  try:
18
- text = recognizer.recognize_google(audio_data)
19
- return text
20
  except sr.UnknownValueError:
21
- return "I didn't catch that. Could you please repeat?"
22
  except sr.RequestError:
23
- return "Sorry, there was an issue recognizing your speech. Try again later."
24
 
25
- # Voice Assistant Logic
26
- def voice_assistant(audio_file):
27
  user_input = recognize_speech(audio_file)
28
- if user_input.startswith("Sorry") or user_input.startswith("I didn't"):
29
- return user_input
30
-
31
- menu = {
32
- "Bread": ["Roti", "Naan", "Paratha"],
33
- "Veg Curries": ["Paneer Curry", "Mix Veg", "Dal Tadka"],
34
- "Non-Veg Curries": ["Chicken Curry", "Mutton Curry", "Fish Curry"],
35
- "Biryanis": ["Veg Biryani", "Chicken Biryani", "Mutton Biryani"]
36
- }
37
-
38
- if "menu" in user_input.lower() or "help" in user_input.lower():
39
- response = (
40
- "We have Bread, Veg Curries, Non-Veg Curries, and Biryanis. "
41
- "Please let me know which category you'd like to explore."
42
- )
43
- speak(response)
44
- return response
45
-
46
- for category, items in menu.items():
47
- if category.lower() in user_input.lower():
48
- response = f"In {category}, we have: {', '.join(items)}. What would you like to order?"
49
- speak(response)
50
- return response
51
-
52
- if "order" in user_input.lower():
53
- response = (
54
- "You have selected your items. Would you like to add any customizations, "
55
- "such as extra cheese, less spice, or something else?"
56
- )
57
- speak(response)
58
- return response
59
-
60
- if "confirm" in user_input.lower():
61
- response = (
62
- "Your order has been confirmed! Thank you for ordering with DynamicMenuApp. "
63
- "Your food will be ready soon. Have a great day!"
64
- )
65
- speak(response)
66
- return response
67
-
68
- response = "I didn't quite catch that. Could you please repeat?"
69
  speak(response)
70
  return response
71
 
72
- # Gradio Interface
73
  iface = gr.Interface(
74
- fn=voice_assistant,
75
- inputs=gr.Audio(type="filepath"), # Corrected syntax for Gradio Audio
76
  outputs="text",
77
- title="DynamicMenuApp Voice Assistant",
78
- description=(
79
- "Upload your voice commands to interact with the DynamicMenuApp Voice Assistant, "
80
- "navigate the menu, customize your food, and place orders seamlessly."
81
- )
82
  )
83
 
84
  if __name__ == "__main__":
 
3
  import os
4
  import speech_recognition as sr
5
 
6
+ menu = {
7
+ "Starters": ["Soup", "Spring Rolls"],
8
+ "Main Course": ["Paneer Butter Masala", "Chicken Curry", "Veg Biryani"],
9
+ "Breads": ["Roti", "Naan", "Paratha"],
10
+ "Desserts": ["Gulab Jamun", "Ice Cream"]
11
+ }
12
+
13
  def speak(text):
14
  tts = gTTS(text=text, lang='en')
15
+ tts.save("response.mp3")
16
+ os.system("play response.mp3 || mpg123 response.mp3")
17
 
 
18
  def recognize_speech(audio_file):
19
  recognizer = sr.Recognizer()
20
  with sr.AudioFile(audio_file) as source:
21
  audio_data = recognizer.record(source)
22
  try:
23
+ return recognizer.recognize_google(audio_data)
 
24
  except sr.UnknownValueError:
25
+ return "I didn't catch that. Could you repeat?"
26
  except sr.RequestError:
27
+ return "There was an issue processing your speech."
28
 
29
+ def process_order(audio_file):
 
30
  user_input = recognize_speech(audio_file)
31
+ response = f"You said: {user_input}\n"
32
+ response += "Menu items: " + ", ".join([item for category in menu.values() for item in category])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  speak(response)
34
  return response
35
 
 
36
  iface = gr.Interface(
37
+ fn=process_order,
38
+ inputs=gr.Audio(type="filepath"),
39
  outputs="text",
40
+ title="Restaurant Voice Assistant",
41
+ description="Speak your order and interact with our AI assistant!"
 
 
 
42
  )
43
 
44
  if __name__ == "__main__":