CoolFace
Apppublic

nagasurendra/Voice_Menu_Ordering2

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py95 linesDownload Raw Back to root
1import gradio as gr2from gtts import gTTS3import tempfile4import os5import speech_recognition as sr6import threading7import time8 9# Store cart in a temporary storage10cart = []11 12# Define the menu items dynamically13menu_items = {14    "Pizza": 10.99,15    "Burger": 8.49,16    "Pasta": 12.99,17    "Salad": 7.99,18    "Soda": 2.4919}20 21def generate_voice_response(text):22    tts = gTTS(text)23    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")24    temp_file.close()25    tts.save(temp_file.name)26    return temp_file.name27 28def process_audio(audio_path):29    global cart30    recognizer = sr.Recognizer()31    response = ""32 33    try:34        with sr.AudioFile(audio_path) as source:35            audio = recognizer.record(source)36            input_text = recognizer.recognize_google(audio)37            print("User said:", input_text)38 39            if "menu" in input_text.lower():40                response = "Here is our menu:\n"41                for item, price in menu_items.items():42                    response += f"{item}: ${price:.2f}\n"43                response += "\nWhat would you like to add to your cart?"44 45            elif any(item.lower() in input_text.lower() for item in menu_items):46                for item in menu_items:47                    if item.lower() in input_text.lower():48                        cart.append(item)49                        total = sum(menu_items[cart_item] for cart_item in cart)50                        response = f"{item} has been added to your cart. Your current cart includes:\n"51                        for cart_item in cart:52                            response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n"53                        response += f"\nTotal: ${total:.2f}. Would you like to add anything else?"54                        break55 56            elif "final order" in input_text.lower() or "submit order" in input_text.lower():57                if cart:58                    total = sum(menu_items[cart_item] for cart_item in cart)59                    response = "Your final order includes:\n"60                    for item in cart:61                        response += f"- {item}: ${menu_items[item]:.2f}\n"62                    response += f"\nTotal: ${total:.2f}. Thank you for ordering!"63                    cart = []  # Clear cart after finalizing order64                else: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 = "I didn’t quite catch that. Please tell me what you’d like to order."72 73    except sr.UnknownValueError:74        response = "Sorry, I didn’t catch that. Could you please repeat?"75    except Exception as e:76        response = f"An error occurred: {str(e)}"77 78    audio_response_path = generate_voice_response(response)79    return response, audio_response_path80 81def run_assistant(audio):82    transcription, audio_response_path = process_audio(audio)83    os.system(f"start {audio_response_path}")  # Automatically play the response audio84    return transcription85 86with gr.Blocks() as demo:87    gr.Markdown("# Voice-Activated Restaurant Assistant")88    audio_input = gr.Audio(type="filepath", label="Speak Now", streaming=True)89 90    output_text = gr.Textbox(label="Transcription")91 92    audio_input.change(fn=run_assistant, inputs=audio_input, outputs=output_text)93 94    demo.launch()95