DSatishchandra commited on
Commit
b782a3b
1 Parent(s): 50dadbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -43
app.py CHANGED
@@ -1,59 +1,66 @@
1
- import gradio as gr
2
  from gtts import gTTS
3
  import os
 
4
 
5
- # Menu data from the second image (hardcoded for simplicity)
6
- menu = {
7
- "Appetizer": ["Veg Samosas", "Cut Mirchi", "Onion", "Spinach", "Mixed Vegetable"],
8
- "Pakodas": ["Veg Pakoda", "Chicken Pakoda", "Fish Pakoda"],
9
- "Manchurian": ["Vegetable", "Paneer", "Chicken", "Fish", "Jhinga"],
10
- "Chilly": ["Gobi", "Paneer", "Chicken", "Fish", "Shrimp"],
11
- "Chef's Special": ["Murgh (Chicken)", "Gosht (Goat)", "Jhinga (Shrimp)", "Fish Fry"],
12
- "Vegetarian Entree": ["Dal Fry", "Dal Makhani", "Channa Masala", "Aloo Gobi Masala", "Saag Paneer"],
13
- "Chettinad": ["Egg", "Murgh (Chicken)", "Gosht (Goat)", "Jhinga (Shrimp)", "Crab"],
14
- "Butter Masala": ["Chicken", "Shrimp", "Gosht (Goat)"]
15
- }
16
-
17
- # Function to speak a text using Google Text-to-Speech (gTTS)
18
  def speak(text):
19
  tts = gTTS(text=text, lang='en')
20
  tts.save("output.mp3")
21
- # Do not try to play the audio, just save it
22
- # os.system("mpg321 output.mp3") # Removed, as it is not supported in Hugging Face
23
 
24
- # Function to process the order and handle confirmation
25
- def process_order(order, confirmation):
26
- response = "You have ordered the following: "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  order = order.lower()
28
 
29
- # Check for matching menu items
30
  ordered_items = []
31
- for category, items in menu.items():
32
- for item in items:
33
- if item.lower() in order:
34
- ordered_items.append(item)
35
 
36
  if ordered_items:
37
- response += ', '.join(ordered_items) + ". Is that correct?"
38
- if confirmation.lower() == "yes":
39
- speak("Thank you for your order. It will be ready shortly!")
40
- return "Order confirmed. " + response
41
- else:
42
- speak("Please tell me again what you'd like to order.")
43
- return "Order not confirmed. Please try again."
44
  else:
45
  speak("Sorry, I couldn't find any items matching your order. Can you try again?")
46
- return "Sorry, I couldn't find any items matching your order. Can you try again?"
47
-
48
- # Create Gradio interface
49
- def start_assistant(order, confirmation):
50
- return process_order(order, confirmation)
51
 
52
- # Gradio interface setup
53
- iface = gr.Interface(fn=start_assistant, inputs=["text", "text"], outputs="text",
54
- live=True,
55
- title="Voice Food Ordering Assistant",
56
- description="Type your food order and confirm your order.")
 
 
 
 
57
 
58
- # Launch the interface
59
- iface.launch()
 
1
+ import speech_recognition as sr
2
  from gtts import gTTS
3
  import os
4
+ import time
5
 
6
+ # Function to speak using Google Text-to-Speech
 
 
 
 
 
 
 
 
 
 
 
 
7
  def speak(text):
8
  tts = gTTS(text=text, lang='en')
9
  tts.save("output.mp3")
10
+ os.system("start output.mp3") # For Windows
11
+ # os.system("mpg321 output.mp3") # Uncomment for Linux
12
 
13
+ # Function to listen and recognize speech using Google Speech Recognition
14
+ def listen():
15
+ recognizer = sr.Recognizer()
16
+
17
+ with sr.Microphone() as source:
18
+ print("Listening for your order...")
19
+ audio = recognizer.listen(source)
20
+
21
+ try:
22
+ # Using Google Speech Recognition to convert speech to text
23
+ order = recognizer.recognize_google(audio)
24
+ print(f"Recognized Order: {order}")
25
+ return order
26
+ except sr.UnknownValueError:
27
+ speak("Sorry, I could not understand that. Could you please repeat?")
28
+ return None
29
+ except sr.RequestError:
30
+ speak("Sorry, there was an issue with the service.")
31
+ return None
32
+
33
+ # Sample menu data
34
+ menu = ["Veg Samosas", "Chicken Pakoda", "Fish Fry", "Dal Fry", "Paneer Butter Masala"]
35
+
36
+ # Function to process the order
37
+ def process_order(order):
38
+ response = "You have ordered the following items: "
39
  order = order.lower()
40
 
 
41
  ordered_items = []
42
+ for item in menu:
43
+ if item.lower() in order:
44
+ ordered_items.append(item)
 
45
 
46
  if ordered_items:
47
+ response += ", ".join(ordered_items) + ". Is that correct?"
48
+ speak(response)
49
+ time.sleep(2)
50
+ return response
 
 
 
51
  else:
52
  speak("Sorry, I couldn't find any items matching your order. Can you try again?")
53
+ return "Sorry, I couldn't find any items matching your order."
 
 
 
 
54
 
55
+ # Main function
56
+ def start_assistant():
57
+ speak("Welcome to the Voice Food Ordering Assistant!")
58
+ time.sleep(1)
59
+ speak("Please tell me what you would like to order.")
60
+ while True:
61
+ order = listen()
62
+ if order:
63
+ process_order(order)
64
 
65
+ if __name__ == "__main__":
66
+ start_assistant()