Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,12 @@ import gradio as gr
|
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
import speech_recognition as sr
|
|
|
5 |
|
6 |
-
# Initialize recognizer
|
7 |
recognizer = sr.Recognizer()
|
8 |
|
9 |
-
# Menu items
|
10 |
menu_items = {
|
11 |
"biryani": ["Chicken Biryani", "Mutton Biryani", "Vegetable Biryani", "Egg Biryani"],
|
12 |
"starters": ["Chicken Tikka", "Paneer Tikka", "Fish Fry", "Veg Manchurian"],
|
@@ -14,84 +15,73 @@ menu_items = {
|
|
14 |
}
|
15 |
cart = []
|
16 |
|
17 |
-
# Text-to-Speech Function
|
18 |
def text_to_speech(text):
|
19 |
-
"""Convert text to speech and provide audio file."""
|
20 |
tts = gTTS(text=text, lang='en')
|
21 |
-
|
22 |
-
tts.save(
|
23 |
-
return
|
24 |
|
25 |
-
# Read Menu Function
|
26 |
def read_menu():
|
27 |
-
"""Generate the menu text
|
28 |
-
menu_text = "Here is the menu
|
29 |
-
for
|
30 |
-
menu_text +=
|
31 |
-
menu_text += "
|
32 |
-
|
33 |
-
menu_text += item + ". "
|
34 |
-
menu_text += "Finally, Drinks: "
|
35 |
-
for item in menu_items["drinks"]:
|
36 |
-
menu_text += item + ". "
|
37 |
-
return menu_text, text_to_speech(menu_text)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
try:
|
43 |
with sr.AudioFile(audio_path) as source:
|
44 |
-
|
45 |
-
command = recognizer.recognize_google(
|
46 |
except Exception as e:
|
47 |
-
|
48 |
-
return "Error", text_to_speech(error_text)
|
49 |
|
50 |
if "menu" in command:
|
51 |
-
|
52 |
-
return menu_text, menu_audio
|
53 |
|
54 |
for category, items in menu_items.items():
|
55 |
for item in items:
|
56 |
if item.lower() in command:
|
57 |
cart.append(item)
|
58 |
-
|
59 |
-
return response_text, text_to_speech(response_text)
|
60 |
|
61 |
if "cart" in command:
|
62 |
if not cart:
|
63 |
-
|
64 |
else:
|
65 |
-
|
66 |
-
return response_text, text_to_speech(response_text)
|
67 |
|
68 |
-
if "submit" in command or "
|
69 |
if not cart:
|
70 |
-
|
71 |
else:
|
72 |
-
|
73 |
cart.clear()
|
74 |
-
|
75 |
|
76 |
-
|
77 |
-
return error_text, text_to_speech(error_text)
|
78 |
|
79 |
-
#
|
80 |
-
def
|
81 |
-
"""
|
82 |
-
|
83 |
-
|
84 |
-
gr.Markdown("Speak your command to interact with the menu system dynamically.")
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
response_text = gr.Textbox(label="Response Text")
|
90 |
-
audio_output = gr.Audio(label="Audio Response")
|
91 |
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
|
94 |
-
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
-
app
|
|
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
import speech_recognition as sr
|
5 |
+
from tempfile import NamedTemporaryFile
|
6 |
|
7 |
+
# Initialize the recognizer
|
8 |
recognizer = sr.Recognizer()
|
9 |
|
10 |
+
# Menu items and cart initialization
|
11 |
menu_items = {
|
12 |
"biryani": ["Chicken Biryani", "Mutton Biryani", "Vegetable Biryani", "Egg Biryani"],
|
13 |
"starters": ["Chicken Tikka", "Paneer Tikka", "Fish Fry", "Veg Manchurian"],
|
|
|
15 |
}
|
16 |
cart = []
|
17 |
|
|
|
18 |
def text_to_speech(text):
|
19 |
+
"""Convert text to speech and provide an audio file."""
|
20 |
tts = gTTS(text=text, lang='en')
|
21 |
+
audio_file = NamedTemporaryFile(delete=False, suffix=".mp3")
|
22 |
+
tts.save(audio_file.name)
|
23 |
+
return audio_file.name
|
24 |
|
|
|
25 |
def read_menu():
|
26 |
+
"""Generate and return the menu text."""
|
27 |
+
menu_text = "Here is the menu: \n"
|
28 |
+
for category, items in menu_items.items():
|
29 |
+
menu_text += f"{category.capitalize()}: \n" + ", ".join(items) + "\n"
|
30 |
+
menu_text += "Please tell me the items you want to add to your cart."
|
31 |
+
return menu_text
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
def process_audio_command(audio_path):
|
34 |
+
"""Process the user's audio command."""
|
35 |
+
global cart
|
36 |
try:
|
37 |
with sr.AudioFile(audio_path) as source:
|
38 |
+
audio = recognizer.record(source)
|
39 |
+
command = recognizer.recognize_google(audio).lower()
|
40 |
except Exception as e:
|
41 |
+
return "Sorry, I could not understand. Could you repeat?"
|
|
|
42 |
|
43 |
if "menu" in command:
|
44 |
+
return read_menu()
|
|
|
45 |
|
46 |
for category, items in menu_items.items():
|
47 |
for item in items:
|
48 |
if item.lower() in command:
|
49 |
cart.append(item)
|
50 |
+
return f"{item} has been added to your cart."
|
|
|
51 |
|
52 |
if "cart" in command:
|
53 |
if not cart:
|
54 |
+
return "Your cart is empty."
|
55 |
else:
|
56 |
+
return "Your cart contains: " + ", ".join(cart)
|
|
|
57 |
|
58 |
+
if "submit" in command or "finalize" in command:
|
59 |
if not cart:
|
60 |
+
return "Your cart is empty. Add some items before submitting."
|
61 |
else:
|
62 |
+
response = "Your final order is: " + ", ".join(cart) + ". Thank you for your order!"
|
63 |
cart.clear()
|
64 |
+
return response
|
65 |
|
66 |
+
return "Sorry, I didn't understand that. Please try again."
|
|
|
67 |
|
68 |
+
# Define the continuous conversation function
|
69 |
+
def conversation_loop(audio_path):
|
70 |
+
"""Continuous conversation handling for dynamic interactions."""
|
71 |
+
user_response = process_audio_command(audio_path)
|
72 |
+
return user_response, text_to_speech(user_response)
|
|
|
73 |
|
74 |
+
with gr.Blocks() as app:
|
75 |
+
gr.Markdown("# Voice-Activated Restaurant Menu System")
|
76 |
+
gr.Markdown("Speak your command to interact with the menu dynamically.")
|
|
|
|
|
77 |
|
78 |
+
with gr.Row():
|
79 |
+
audio_input = gr.Audio(label="Speak Your Command", type="filepath")
|
80 |
+
text_output = gr.Textbox(label="Transcribed Command")
|
81 |
+
response_text = gr.Textbox(label="Response Text")
|
82 |
+
audio_output = gr.Audio(label="Assistant Response")
|
83 |
|
84 |
+
audio_input.change(conversation_loop, inputs=audio_input, outputs=[response_text, audio_output])
|
85 |
|
86 |
if __name__ == "__main__":
|
87 |
+
app.launch()
|