CoolFace
Apppublic

nagasurendra/Voice_Menu_Ordering3

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py100 linesDownload Raw Back to root
1import gradio as gr2from gtts import gTTS3import os4import tempfile5import json6import speech_recognition as sr7 8# Store cart in a temporary storage9cart = []10 11# Define the menu items dynamically12menu_items = {13    "Pizza": 10.99,14    "Burger": 8.49,15    "Pasta": 12.99,16    "Salad": 7.99,17    "Soda": 2.4918}19 20def generate_voice_response(text):21    tts = gTTS(text)22    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")23    temp_file.close()24    tts.save(temp_file.name)25    return temp_file.name26 27def calculate_total(cart):28    return sum(menu_items[item] for item in cart)29 30def restaurant_voice_assistant(audio, state_json):31    global cart32    state = json.loads(state_json) if state_json else {}33    response = ""34    voice_path = None35 36    # Convert audio input to text37    if audio:38        recognizer = sr.Recognizer()39        with sr.AudioFile(audio) as source:40            try:41                input_text = recognizer.recognize_google(recognizer.record(source))42            except sr.UnknownValueError:43                input_text = ""44    else:45        input_text = ""46 47    if not state.get("menu_shown", False):48        # Show menu dynamically49        response = "Welcome to our restaurant! Here is our menu:\n"50        for item, price in menu_items.items():51            response += f"{item}: ${price:.2f}\n"52        response += "\nPlease tell me the item you would like to add to your cart."53        state["menu_shown"] = True54    elif any(item.lower() in input_text.lower() for item in menu_items):55        # Check if input matches a menu item56        for item in menu_items:57            if item.lower() in input_text.lower():58                cart.append(item)59                total = calculate_total(cart)60                response = f"{item} has been added to your cart. Your current cart includes:\n"61                for cart_item in cart:62                    response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n"63                response += f"\nTotal: ${total:.2f}. Would you like to add anything else?"64                break65    elif "menu" in input_text.lower():66        response = "Here is our menu again:\n"67        for item, price in menu_items.items():68            response += f"{item}: ${price:.2f}\n"69        response += "\nWhat would you like to add to your cart?"70    elif "final order" in input_text.lower() or "submit order" in input_text.lower():71        if cart:72            total = calculate_total(cart)73            response = "Your final order includes:\n"74            for item in cart:75                response += f"- {item}: ${menu_items[item]:.2f}\n"76            response += f"\nTotal: ${total:.2f}.\nThank you for ordering!"77            cart = []  # Clear cart after finalizing order78        else:79            response = "Your cart is empty. Would you like to order something?"80    else:81        response = "I didn’t quite catch that. Please tell me what you’d like to order."82 83    voice_path = generate_voice_response(response)84    return response, voice_path, json.dumps(state)85 86with gr.Blocks() as demo:87    state = gr.State(value=json.dumps({}))88 89    with gr.Row():90        user_audio = gr.Audio(type="filepath", label="Your Voice Input")91        output_text = gr.Textbox(label="Response Text")92 93    with gr.Row():94        voice_output = gr.Audio(label="Response Audio", autoplay=True)95 96    # Automatically process audio when recording stops97    user_audio.change(restaurant_voice_assistant, inputs=[user_audio, state], outputs=[output_text, voice_output, state])98 99demo.launch()100