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