File size: 3,117 Bytes
268b977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import speech_recognition as sr
import pyttsx3

# Initialize text-to-speech engine
engine = pyttsx3.init()

# 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 speak_response(response):
    """Generate voice output."""
    engine.say(response)
    engine.runAndWait()

def process_input(input_text):
    global cart
    response = ""

    if "price of" in input_text:
        matched_items = [item for item in menu_items if item.lower() in input_text]
        if len(matched_items) == 1:
            item = matched_items[0]
            response = f"The price of {item} is ${menu_items[item]:.2f}."
        elif len(matched_items) > 1:
            response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please ask for the price of one item at a time."
        else:
            response = "I couldn't find that item on the menu. Please ask for an item available in the menu."
    elif any(item.lower() in input_text for item in menu_items):
        matched_items = [item for item in menu_items if item.lower() in input_text]
        if len(matched_items) == 1:
            item = matched_items[0]
            cart.append(item)
            response = f"{item} has been added to your cart. Your current cart includes:\n"
            for cart_item in cart:
                response += f"- {cart_item}\n"
            response += "\nWould you like to add anything else?"
        elif len(matched_items) > 1:
            response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please mention one item at a time."
    elif "menu" in input_text:
        response = "Here is our menu:\n"
        for item in menu_items.keys():
            response += f"{item}\n"
        response += "\nWhat would you like to order?"
    elif "final order" in input_text or "submit order" in input_text:
        if cart:
            total = calculate_total(cart)
            response = "Your final order includes:\n"
            for item in cart:
                response += f"- {item}\n"
            response += f"\nTotal amount: ${total:.2f}. Thank you for ordering!"
            cart.clear()
        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 or ask about."

    return response

def voice_assistant(audio_file):
    recognizer = sr.Recognizer()
    try:
        with sr.AudioFile(audio_file) as source:
            audio = recognizer.record(source)
            input_text = recognizer.recognize_google(audio).lower()
            response = process_input(input_text)
            return response
    except sr.UnknownValueError:
        return "Sorry, I couldn't understand the audio. Please try again."

gr.Interface(
    fn=voice_assistant,
    inputs=gr.Audio(source="microphone", type="filepath"),
    outputs="text",
    live=True,
    title="Voice Assistant"
).launch()