Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
| 3 |
-
import speech_recognition as sr
|
| 4 |
import tempfile
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
| 8 |
menu_items = {
|
| 9 |
"Pizza": 10.99,
|
| 10 |
"Burger": 8.49,
|
|
@@ -13,79 +16,91 @@ menu_items = {
|
|
| 13 |
"Soda": 2.49
|
| 14 |
}
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
def process_audio(
|
| 20 |
global cart
|
| 21 |
recognizer = sr.Recognizer()
|
| 22 |
response = ""
|
| 23 |
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
with sr.AudioFile(audio_file) as source:
|
| 27 |
audio = recognizer.record(source)
|
| 28 |
-
|
| 29 |
-
print("User said:",
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
response +=
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
response += f"
|
| 46 |
-
|
| 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 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
except sr.UnknownValueError:
|
| 64 |
-
response = "Sorry, I
|
| 65 |
except Exception as e:
|
| 66 |
response = f"An error occurred: {str(e)}"
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 71 |
-
tts.save(temp_file.name)
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 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 |
-
|
| 85 |
-
output_text = gr.Textbox(label="Assistant Response")
|
| 86 |
-
output_audio = gr.Audio(label="Audio Response", type="filepath")
|
| 87 |
|
| 88 |
-
voice_input.change(
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gtts import gTTS
|
|
|
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
+
import speech_recognition as sr
|
| 6 |
|
| 7 |
+
# Store cart in a temporary storage
|
| 8 |
+
cart = []
|
| 9 |
+
|
| 10 |
+
# Define the menu items dynamically
|
| 11 |
menu_items = {
|
| 12 |
"Pizza": 10.99,
|
| 13 |
"Burger": 8.49,
|
|
|
|
| 16 |
"Soda": 2.49
|
| 17 |
}
|
| 18 |
|
| 19 |
+
def generate_voice_response(text):
|
| 20 |
+
tts = gTTS(text)
|
| 21 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 22 |
+
temp_file.close()
|
| 23 |
+
tts.save(temp_file.name)
|
| 24 |
+
return temp_file.name
|
| 25 |
|
| 26 |
+
def process_audio(audio_path):
|
| 27 |
global cart
|
| 28 |
recognizer = sr.Recognizer()
|
| 29 |
response = ""
|
| 30 |
|
| 31 |
try:
|
| 32 |
+
with sr.AudioFile(audio_path) as source:
|
|
|
|
| 33 |
audio = recognizer.record(source)
|
| 34 |
+
input_text = recognizer.recognize_google(audio)
|
| 35 |
+
print("User said:", input_text)
|
| 36 |
+
|
| 37 |
+
if "menu" in input_text.lower():
|
| 38 |
+
response = "Here is our menu:\n"
|
| 39 |
+
for item, price in menu_items.items():
|
| 40 |
+
response += f"{item}: ${price:.2f}\n"
|
| 41 |
+
response += "\nWhat would you like to add to your cart?"
|
| 42 |
+
|
| 43 |
+
elif any(item.lower() in input_text.lower() for item in menu_items):
|
| 44 |
+
for item in menu_items:
|
| 45 |
+
if item.lower() in input_text.lower():
|
| 46 |
+
cart.append(item)
|
| 47 |
+
total = sum(menu_items[cart_item] for cart_item in cart)
|
| 48 |
+
response = f"{item} has been added to your cart. Your current cart includes:\n"
|
| 49 |
+
for cart_item in cart:
|
| 50 |
+
response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n"
|
| 51 |
+
response += f"\nTotal: ${total:.2f}. Would you like to add anything else?"
|
| 52 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
elif "final order" in input_text.lower() or "submit order" in input_text.lower():
|
| 55 |
+
if cart:
|
| 56 |
+
total = sum(menu_items[cart_item] for cart_item in cart)
|
| 57 |
+
response = "Your final order includes:\n"
|
| 58 |
+
for item in cart:
|
| 59 |
+
response += f"- {item}: ${menu_items[item]:.2f}\n"
|
| 60 |
+
response += f"\nTotal: ${total:.2f}. Thank you for ordering!"
|
| 61 |
+
cart = [] # Clear cart after finalizing order
|
| 62 |
+
else:
|
| 63 |
+
response = "Your cart is empty. Would you like to order something?"
|
| 64 |
+
|
| 65 |
+
elif "stop" in input_text.lower():
|
| 66 |
+
response = "Stopping voice assistant."
|
| 67 |
+
|
| 68 |
+
else:
|
| 69 |
+
response = "I didn’t quite catch that. Please tell me what you’d like to order."
|
| 70 |
|
| 71 |
except sr.UnknownValueError:
|
| 72 |
+
response = "Sorry, I didn’t catch that. Could you please repeat?"
|
| 73 |
except Exception as e:
|
| 74 |
response = f"An error occurred: {str(e)}"
|
| 75 |
|
| 76 |
+
audio_response_path = generate_voice_response(response)
|
| 77 |
+
return response, audio_response_path
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
def run_voice_assistant(audio):
|
| 80 |
+
response, audio_path = process_audio(audio)
|
| 81 |
+
return response, (audio_path,)
|
| 82 |
+
|
| 83 |
+
def javascript_autoplay():
|
| 84 |
+
return """<script>
|
| 85 |
+
var audio = document.getElementsByTagName('audio')[0];
|
| 86 |
+
if (audio) {
|
| 87 |
+
audio.play();
|
| 88 |
+
}
|
| 89 |
+
</script>"""
|
| 90 |
+
|
| 91 |
+
with gr.Blocks() as demo:
|
| 92 |
+
gr.Markdown("### Voice-Activated Restaurant Assistant")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
voice_input = gr.Audio(source="microphone", type="filepath", label="Speak Now")
|
| 96 |
+
assistant_response = gr.Textbox(label="Assistant Response")
|
| 97 |
|
| 98 |
+
gr.Markdown("Note: The audio will play automatically.")
|
|
|
|
|
|
|
|
|
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
with gr.Row():
|
| 101 |
+
audio_output = gr.Audio(label="Audio Response")
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
voice_input.change(run_voice_assistant, inputs=voice_input, outputs=[assistant_response, audio_output])
|
| 104 |
+
gr.HTML(javascript_autoplay())
|
| 105 |
|
| 106 |
+
demo.launch()
|
|
|