Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,12 +23,24 @@ def generate_voice_response(text):
|
|
23 |
tts.save(temp_file.name)
|
24 |
return temp_file.name
|
25 |
|
26 |
-
def restaurant_voice_assistant(
|
27 |
global cart
|
28 |
state = json.loads(state_json) if state_json else {}
|
29 |
response = ""
|
30 |
voice_path = None
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
if not state.get("menu_shown", False):
|
33 |
# Show menu dynamically
|
34 |
response = "Welcome to our restaurant! Here is our menu:\n"
|
@@ -36,7 +48,7 @@ def restaurant_voice_assistant(input_text, state_json):
|
|
36 |
response += f"{item}: ${price:.2f}\n"
|
37 |
response += "\nPlease tell me the item you would like to add to your cart."
|
38 |
state["menu_shown"] = True
|
39 |
-
elif "
|
40 |
item = input_text.replace("add to cart", "").strip()
|
41 |
if item in menu_items:
|
42 |
cart.append(item)
|
@@ -67,13 +79,13 @@ with gr.Blocks() as demo:
|
|
67 |
state = gr.State(value=json.dumps({}))
|
68 |
|
69 |
with gr.Row():
|
70 |
-
|
71 |
output_text = gr.Textbox(label="Response Text")
|
72 |
|
73 |
with gr.Row():
|
74 |
-
voice_output = gr.Audio(label="Response Audio")
|
75 |
|
76 |
submit_btn = gr.Button("Submit")
|
77 |
-
submit_btn.click(restaurant_voice_assistant, inputs=[
|
78 |
|
79 |
demo.launch()
|
|
|
23 |
tts.save(temp_file.name)
|
24 |
return temp_file.name
|
25 |
|
26 |
+
def restaurant_voice_assistant(audio, state_json):
|
27 |
global cart
|
28 |
state = json.loads(state_json) if state_json else {}
|
29 |
response = ""
|
30 |
voice_path = None
|
31 |
|
32 |
+
# Convert audio input to text
|
33 |
+
if audio:
|
34 |
+
import speech_recognition as sr
|
35 |
+
recognizer = sr.Recognizer()
|
36 |
+
with sr.AudioFile(audio) as source:
|
37 |
+
try:
|
38 |
+
input_text = recognizer.recognize_google(recognizer.record(source))
|
39 |
+
except sr.UnknownValueError:
|
40 |
+
input_text = ""
|
41 |
+
else:
|
42 |
+
input_text = ""
|
43 |
+
|
44 |
if not state.get("menu_shown", False):
|
45 |
# Show menu dynamically
|
46 |
response = "Welcome to our restaurant! Here is our menu:\n"
|
|
|
48 |
response += f"{item}: ${price:.2f}\n"
|
49 |
response += "\nPlease tell me the item you would like to add to your cart."
|
50 |
state["menu_shown"] = True
|
51 |
+
elif "add to cart" in input_text.lower():
|
52 |
item = input_text.replace("add to cart", "").strip()
|
53 |
if item in menu_items:
|
54 |
cart.append(item)
|
|
|
79 |
state = gr.State(value=json.dumps({}))
|
80 |
|
81 |
with gr.Row():
|
82 |
+
user_audio = gr.Audio(source="microphone", type="filepath", label="Your Voice Input")
|
83 |
output_text = gr.Textbox(label="Response Text")
|
84 |
|
85 |
with gr.Row():
|
86 |
+
voice_output = gr.Audio(label="Response Audio", autoplay=True)
|
87 |
|
88 |
submit_btn = gr.Button("Submit")
|
89 |
+
submit_btn.click(restaurant_voice_assistant, inputs=[user_audio, state], outputs=[output_text, voice_output, state])
|
90 |
|
91 |
demo.launch()
|