Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from gtts import gTTS
|
3 |
import os
|
|
|
|
|
4 |
import speech_recognition as sr
|
|
|
5 |
from tempfile import NamedTemporaryFile
|
6 |
-
import threading
|
7 |
-
import time
|
8 |
|
9 |
# Initialize the recognizer
|
10 |
recognizer = sr.Recognizer()
|
@@ -19,21 +18,21 @@ cart = []
|
|
19 |
|
20 |
# Helper Functions
|
21 |
def text_to_speech(text):
|
22 |
-
"""Convert text to speech and
|
23 |
tts = gTTS(text=text, lang='en')
|
24 |
audio_file = NamedTemporaryFile(delete=False, suffix=".mp3")
|
25 |
tts.save(audio_file.name)
|
|
|
26 |
return audio_file.name
|
27 |
|
28 |
def read_menu():
|
29 |
-
"""Generate
|
30 |
-
menu_text = "Here is the menu:
|
31 |
for category, items in menu_items.items():
|
32 |
-
menu_text += f"{category.capitalize()}:
|
33 |
menu_text += "Please tell me the items you want to add to your cart."
|
34 |
return menu_text
|
35 |
|
36 |
-
# Command Processing
|
37 |
def process_audio_command(audio_path):
|
38 |
"""Process the user's audio command."""
|
39 |
global cart
|
@@ -42,23 +41,27 @@ def process_audio_command(audio_path):
|
|
42 |
audio = recognizer.record(source)
|
43 |
command = recognizer.recognize_google(audio).lower()
|
44 |
except Exception as e:
|
45 |
-
return "Sorry, I
|
46 |
|
|
|
47 |
if "menu" in command:
|
48 |
return read_menu()
|
49 |
|
|
|
50 |
for category, items in menu_items.items():
|
51 |
for item in items:
|
52 |
if item.lower() in command:
|
53 |
cart.append(item)
|
54 |
return f"{item} has been added to your cart."
|
55 |
|
|
|
56 |
if "cart" in command:
|
57 |
if not cart:
|
58 |
return "Your cart is empty."
|
59 |
else:
|
60 |
return "Your cart contains: " + ", ".join(cart)
|
61 |
|
|
|
62 |
if "submit" in command or "finalize" in command:
|
63 |
if not cart:
|
64 |
return "Your cart is empty. Add some items before submitting."
|
@@ -69,7 +72,7 @@ def process_audio_command(audio_path):
|
|
69 |
|
70 |
return "Sorry, I didn't understand that. Please try again."
|
71 |
|
72 |
-
# Continuous
|
73 |
def continuous_conversation():
|
74 |
"""Continuously listen to and respond to user commands."""
|
75 |
while True:
|
@@ -82,28 +85,13 @@ def continuous_conversation():
|
|
82 |
with open(temp_audio.name, "wb") as f:
|
83 |
f.write(audio.get_wav_data())
|
84 |
response = process_audio_command(temp_audio.name)
|
85 |
-
print(response)
|
86 |
-
|
87 |
-
os.system(f"mpg123 {audio_response}")
|
88 |
except Exception as e:
|
89 |
-
print("Error processing audio input
|
90 |
-
|
91 |
-
# Gradio Interface
|
92 |
-
with gr.Blocks() as app:
|
93 |
-
gr.Markdown("# Voice-Activated Restaurant Menu System")
|
94 |
-
gr.Markdown("Speak your command to interact with the menu dynamically.")
|
95 |
-
|
96 |
-
with gr.Row():
|
97 |
-
audio_input = gr.Audio(label="Speak Your Command", type="filepath")
|
98 |
-
response_text = gr.Textbox(label="Assistant Response")
|
99 |
-
audio_output = gr.Audio(label="Assistant Voice Response")
|
100 |
-
|
101 |
-
def handle_conversation(audio_path):
|
102 |
-
response = process_audio_command(audio_path)
|
103 |
-
return response, text_to_speech(response)
|
104 |
-
|
105 |
-
audio_input.change(handle_conversation, inputs=audio_input, outputs=[response_text, audio_output])
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
+
import threading
|
4 |
import speech_recognition as sr
|
5 |
+
from gtts import gTTS
|
6 |
from tempfile import NamedTemporaryFile
|
|
|
|
|
7 |
|
8 |
# Initialize the recognizer
|
9 |
recognizer = sr.Recognizer()
|
|
|
18 |
|
19 |
# Helper Functions
|
20 |
def text_to_speech(text):
|
21 |
+
"""Convert text to speech and play the response."""
|
22 |
tts = gTTS(text=text, lang='en')
|
23 |
audio_file = NamedTemporaryFile(delete=False, suffix=".mp3")
|
24 |
tts.save(audio_file.name)
|
25 |
+
os.system(f"mpg123 {audio_file.name}")
|
26 |
return audio_file.name
|
27 |
|
28 |
def read_menu():
|
29 |
+
"""Generate the menu text."""
|
30 |
+
menu_text = "Here is the menu: "
|
31 |
for category, items in menu_items.items():
|
32 |
+
menu_text += f"{category.capitalize()}: " + ", ".join(items) + ". "
|
33 |
menu_text += "Please tell me the items you want to add to your cart."
|
34 |
return menu_text
|
35 |
|
|
|
36 |
def process_audio_command(audio_path):
|
37 |
"""Process the user's audio command."""
|
38 |
global cart
|
|
|
41 |
audio = recognizer.record(source)
|
42 |
command = recognizer.recognize_google(audio).lower()
|
43 |
except Exception as e:
|
44 |
+
return "Sorry, I couldn't understand that. Could you repeat?"
|
45 |
|
46 |
+
# Menu Request
|
47 |
if "menu" in command:
|
48 |
return read_menu()
|
49 |
|
50 |
+
# Adding Items to the Cart
|
51 |
for category, items in menu_items.items():
|
52 |
for item in items:
|
53 |
if item.lower() in command:
|
54 |
cart.append(item)
|
55 |
return f"{item} has been added to your cart."
|
56 |
|
57 |
+
# Cart Inquiry
|
58 |
if "cart" in command:
|
59 |
if not cart:
|
60 |
return "Your cart is empty."
|
61 |
else:
|
62 |
return "Your cart contains: " + ", ".join(cart)
|
63 |
|
64 |
+
# Submitting Order
|
65 |
if "submit" in command or "finalize" in command:
|
66 |
if not cart:
|
67 |
return "Your cart is empty. Add some items before submitting."
|
|
|
72 |
|
73 |
return "Sorry, I didn't understand that. Please try again."
|
74 |
|
75 |
+
# Continuous Listening and Responding
|
76 |
def continuous_conversation():
|
77 |
"""Continuously listen to and respond to user commands."""
|
78 |
while True:
|
|
|
85 |
with open(temp_audio.name, "wb") as f:
|
86 |
f.write(audio.get_wav_data())
|
87 |
response = process_audio_command(temp_audio.name)
|
88 |
+
print("Assistant:", response)
|
89 |
+
text_to_speech(response)
|
|
|
90 |
except Exception as e:
|
91 |
+
print("Error processing audio input:", e)
|
92 |
+
text_to_speech("I encountered an error. Please try again.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
+
print("Starting Voice-Activated Restaurant Menu System...")
|
96 |
+
text_to_speech("Welcome to the voice-activated restaurant menu system. You can ask for the menu, add items to your cart, check your cart, or submit your order.")
|
97 |
+
continuous_conversation()
|