File size: 3,089 Bytes
af704d1
63c3e87
af704d1
 
 
06188c6
cf344c7
af704d1
 
cf344c7
af704d1
cf344c7
af704d1
 
 
 
 
a941958
cf344c7
af704d1
 
 
 
 
 
cf344c7
ec6ab1a
b766996
af704d1
 
 
a941958
ec6ab1a
 
 
 
 
 
 
 
 
 
 
af704d1
 
 
 
 
 
 
ec6ab1a
af704d1
 
 
 
a941958
af704d1
 
 
 
 
 
 
 
 
 
 
 
 
a941958
af704d1
 
 
 
 
 
a941958
af704d1
 
0b9fd9a
af704d1
06188c6
af704d1
0b9fd9a
af704d1
0b9fd9a
a941958
af704d1
ec6ab1a
a941958
af704d1
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
import gradio as gr
from gtts import gTTS
import os
import tempfile
import json
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 restaurant_voice_assistant(audio, state_json):
    global cart
    state = json.loads(state_json) if state_json else {}
    response = ""
    voice_path = None

    # Convert audio input to text
    if audio:
        recognizer = sr.Recognizer()
        with sr.AudioFile(audio) as source:
            try:
                input_text = recognizer.recognize_google(recognizer.record(source))
            except sr.UnknownValueError:
                input_text = ""
    else:
        input_text = ""

    if not state.get("menu_shown", False):
        # Show menu dynamically
        response = "Welcome to our restaurant! Here is our menu:\n"
        for item, price in menu_items.items():
            response += f"{item}: ${price:.2f}\n"
        response += "\nPlease tell me the item you would like to add to your cart."
        state["menu_shown"] = True
    elif "add to cart" in input_text.lower():
        item = input_text.replace("add to cart", "").strip()
        if item in menu_items:
            cart.append(item)
            response = f"{item} has been added to your cart. Would you like to add anything else?"
        else:
            response = f"Sorry, {item} is not on the menu. Please choose an item from the menu."
    elif "menu" in input_text.lower():
        response = "Here is our menu again:\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 "final order" in input_text.lower():
        if cart:
            response = "Your final order includes:\n"
            for item in cart:
                response += f"- {item}\n"
            response += "\nThank you for ordering!"
            cart = []  # Clear cart after finalizing order
        else:
            response = "Your cart is empty. Would you like to order something?"
    else:
        response = "I didn’t quite catch that. Please tell me what you’d like to order."

    voice_path = generate_voice_response(response)
    return response, voice_path, json.dumps(state)

with gr.Blocks() as demo:
    state = gr.State(value=json.dumps({}))

    with gr.Row():
        user_audio = gr.Audio(type="filepath", label="Your Voice Input")
        output_text = gr.Textbox(label="Response Text")

    with gr.Row():
        voice_output = gr.Audio(label="Response Audio", autoplay=True)

    submit_btn = gr.Button("Submit")
    submit_btn.click(restaurant_voice_assistant, inputs=[user_audio, state], outputs=[output_text, voice_output, state])

demo.launch()