Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
|
|
3 |
import tempfile
|
4 |
import os
|
5 |
-
import speech_recognition as sr
|
6 |
-
import threading
|
7 |
-
import time
|
8 |
|
9 |
-
#
|
10 |
-
cart = []
|
11 |
-
|
12 |
-
# Define the menu items dynamically
|
13 |
menu_items = {
|
14 |
"Pizza": 10.99,
|
15 |
"Burger": 8.49,
|
@@ -18,96 +13,79 @@ menu_items = {
|
|
18 |
"Soda": 2.49
|
19 |
}
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
24 |
-
temp_file.close()
|
25 |
-
tts.save(temp_file.name)
|
26 |
-
return temp_file.name
|
27 |
|
28 |
-
def process_audio(
|
29 |
global cart
|
30 |
recognizer = sr.Recognizer()
|
31 |
response = ""
|
32 |
|
33 |
try:
|
34 |
-
|
|
|
35 |
audio = recognizer.record(source)
|
36 |
-
|
37 |
-
print("User said:",
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
response += "\
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
response += f"
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
response += f"
|
63 |
-
|
64 |
-
|
65 |
-
response = "Your cart is empty. Would you like to order something?"
|
66 |
-
|
67 |
-
elif "stop" in input_text.lower():
|
68 |
-
response = "Stopping voice assistant."
|
69 |
-
|
70 |
else:
|
71 |
-
response = "
|
|
|
|
|
|
|
72 |
|
73 |
except sr.UnknownValueError:
|
74 |
-
response = "Sorry, I
|
75 |
except Exception as e:
|
76 |
response = f"An error occurred: {str(e)}"
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
def run_assistant_continuous():
|
83 |
-
recognizer = sr.Recognizer()
|
84 |
-
mic = sr.Microphone()
|
85 |
-
|
86 |
-
with mic as source:
|
87 |
-
recognizer.adjust_for_ambient_noise(source)
|
88 |
-
print("Listening...")
|
89 |
|
90 |
-
|
91 |
-
try:
|
92 |
-
with mic as source:
|
93 |
-
audio = recognizer.listen(source)
|
94 |
-
input_text = recognizer.recognize_google(audio)
|
95 |
-
print("User said:", input_text)
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
100 |
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
|
105 |
-
print("Sorry, I didn’t catch that. Please repeat.")
|
106 |
-
except KeyboardInterrupt:
|
107 |
-
print("Stopping voice assistant.")
|
108 |
-
break
|
109 |
-
except Exception as e:
|
110 |
-
print(f"An error occurred: {str(e)}")
|
111 |
|
112 |
-
|
113 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
+
import speech_recognition as sr
|
4 |
import tempfile
|
5 |
import os
|
|
|
|
|
|
|
6 |
|
7 |
+
# Menu items
|
|
|
|
|
|
|
8 |
menu_items = {
|
9 |
"Pizza": 10.99,
|
10 |
"Burger": 8.49,
|
|
|
13 |
"Soda": 2.49
|
14 |
}
|
15 |
|
16 |
+
# Global cart
|
17 |
+
cart = []
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
def process_audio(audio_file):
|
20 |
global cart
|
21 |
recognizer = sr.Recognizer()
|
22 |
response = ""
|
23 |
|
24 |
try:
|
25 |
+
# Transcribe user input
|
26 |
+
with sr.AudioFile(audio_file) as source:
|
27 |
audio = recognizer.record(source)
|
28 |
+
user_input = recognizer.recognize_google(audio)
|
29 |
+
print("User said:", user_input)
|
30 |
+
|
31 |
+
# Process user commands
|
32 |
+
if "menu" in user_input.lower():
|
33 |
+
response = "Here is our menu:\n"
|
34 |
+
for item, price in menu_items.items():
|
35 |
+
response += f"{item}: ${price:.2f}\n"
|
36 |
+
response += "What would you like to add to your cart?"
|
37 |
+
|
38 |
+
elif any(item.lower() in user_input.lower() for item in menu_items):
|
39 |
+
for item in menu_items:
|
40 |
+
if item.lower() in user_input.lower():
|
41 |
+
cart.append(item)
|
42 |
+
total = sum(menu_items[i] for i in cart)
|
43 |
+
response = f"{item} added to your cart. Your current cart includes:\n"
|
44 |
+
for i in cart:
|
45 |
+
response += f"- {i}: ${menu_items[i]:.2f}\n"
|
46 |
+
response += f"Total: ${total:.2f}. Anything else?"
|
47 |
+
break
|
48 |
+
|
49 |
+
elif "final order" in user_input.lower():
|
50 |
+
if cart:
|
51 |
+
total = sum(menu_items[i] for i in cart)
|
52 |
+
response = "Your final order includes:\n"
|
53 |
+
for i in cart:
|
54 |
+
response += f"- {i}: ${menu_items[i]:.2f}\n"
|
55 |
+
response += f"Total: ${total:.2f}. Thank you for ordering!"
|
56 |
+
cart.clear()
|
|
|
|
|
|
|
|
|
|
|
57 |
else:
|
58 |
+
response = "Your cart is empty. Please add items to your cart."
|
59 |
+
|
60 |
+
else:
|
61 |
+
response = "Sorry, I didn't understand that. Please try again."
|
62 |
|
63 |
except sr.UnknownValueError:
|
64 |
+
response = "Sorry, I couldn't understand your voice. Please try again."
|
65 |
except Exception as e:
|
66 |
response = f"An error occurred: {str(e)}"
|
67 |
|
68 |
+
# Generate voice response
|
69 |
+
tts = gTTS(response)
|
70 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
71 |
+
tts.save(temp_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
return response, temp_file.name
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# Gradio Interface
|
76 |
+
def voice_assistant(audio):
|
77 |
+
response_text, response_audio = process_audio(audio)
|
78 |
+
return response_text, response_audio
|
79 |
|
80 |
+
# Gradio App
|
81 |
+
with gr.Blocks() as app:
|
82 |
+
gr.Markdown("# Voice-Activated Restaurant Assistant")
|
83 |
+
with gr.Row():
|
84 |
+
voice_input = gr.Audio(source="microphone", type="filepath", label="Speak Now")
|
85 |
+
output_text = gr.Textbox(label="Assistant Response")
|
86 |
+
output_audio = gr.Audio(label="Audio Response", type="filepath")
|
87 |
|
88 |
+
voice_input.change(voice_assistant, inputs=voice_input, outputs=[output_text, output_audio])
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
# Run the app
|
91 |
+
app.launch()
|