Spaces:
Build error
Build error
DSatishchandra
commited on
Commit
•
07de9e8
1
Parent(s):
abfa12b
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,59 @@
|
|
1 |
-
import
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
11 |
-
# os.system("mpg321 output.mp3") #
|
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 |
-
#
|
34 |
-
|
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
|
43 |
-
|
44 |
-
|
|
|
45 |
|
46 |
if ordered_items:
|
47 |
-
response +=
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
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 |
-
#
|
56 |
-
def start_assistant():
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
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 using Google Text-to-Speech
|
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."
|
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()
|