File size: 3,417 Bytes
376c1a3
e15cee6
 
376c1a3
cf63b7f
a7115da
e15cee6
 
7f426ee
e15cee6
 
 
 
 
cf63b7f
4b14f15
cfa3de5
e15cee6
 
 
 
 
 
376c1a3
50f0956
 
 
e15cee6
 
 
50f0956
e15cee6
 
 
 
 
50f0956
4b14f15
e15cee6
 
 
 
 
 
 
 
 
 
 
 
76e55bf
 
50f0956
 
 
eb8327c
e15cee6
 
cf63b7f
 
e15cee6
76e55bf
eb8327c
e15cee6
76e55bf
4b14f15
e15cee6
 
 
376c1a3
eef6a53
76e55bf
e15cee6
 
eef6a53
e15cee6
76e55bf
e15cee6
 
 
76e55bf
e15cee6
 
 
 
 
 
 
 
76e55bf
e15cee6
76e55bf
376c1a3
 
4b14f15
eef6a53
4b14f15
50f0956
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
import gradio as gr
from gtts import gTTS
import os
import speech_recognition as sr

# Initialize the recognizer
recognizer = sr.Recognizer()

# Menu items
menu_items = {
    "biryani": ["Chicken Biryani", "Mutton Biryani", "Vegetable Biryani", "Egg Biryani"],
    "starters": ["Chicken Tikka", "Paneer Tikka", "Fish Fry", "Veg Manchurian"],
    "drinks": ["Coke", "Pepsi", "Lemonade", "Mango Juice", "Water"]
}
cart = []

# Text-to-Speech Function
def text_to_speech(text):
    """Convert text to speech and save as response.mp3."""
    tts = gTTS(text=text, lang='en')
    file_path = "response.mp3"
    tts.save(file_path)
    return file_path

# Generate Menu Details
def generate_menu_details():
    """Generate the full menu details as text."""
    menu_text = "Here is the menu. Starting with Biryani options: "
    for item in menu_items["biryani"]:
        menu_text += item + ". "
    menu_text += "Next, we have Starters: "
    for item in menu_items["starters"]:
        menu_text += item + ". "
    menu_text += "Finally, Drinks: "
    for item in menu_items["drinks"]:
        menu_text += item + ". "
    return menu_text

# Process Voice Command
def process_command(audio_path):
    """Process the user's voice command and return the audio response."""
    try:
        print(f"Processing audio file: {audio_path}")
        with sr.AudioFile(audio_path) as source:
            audio_data = recognizer.record(source)
            command = recognizer.recognize_google(audio_data).lower()
            print(f"Recognized command: {command}")
    except Exception as e:
        print(f"Error during voice processing: {e}")
        error_text = "Sorry, I could not process the audio."
        return text_to_speech(error_text)
    
    if "menu" in command or "menu items" in command or "tell me menu details" in command:
        menu_text = generate_menu_details()
        return text_to_speech(menu_text)

    for category, items in menu_items.items():
        for item in items:
            if item.lower() in command:
                cart.append(item)
                response_text = f"{item} has been added to your cart."
                return text_to_speech(response_text)

    error_text = "Sorry, I couldn't find that item on the menu."
    return text_to_speech(error_text)

# Gradio App
def app():
    """Create the Gradio interface."""
    with gr.Blocks() as demo:
        gr.Markdown("# Voice Assistant for Menu")
        gr.Markdown("Press the microphone to speak your command. The response will be provided as audio.")

        with gr.Row():
            voice_input = gr.Audio(type="filepath", label="🎤 Speak Command")
            # Output for autoplay audio
            audio_output = gr.HTML()

        # Process and autoplay
        def process_and_autoplay(audio_path):
            audio_file = process_command(audio_path)
            # Generate autoplay HTML for the audio
            autoplay_html = f"""
            <audio autoplay>
                <source src="{audio_file}" type="audio/mpeg">
                Your browser does not support the audio element. Please download the file:
                <a href="{audio_file}" download>Download Audio</a>
            </audio>
            """
            return autoplay_html

        voice_input.change(process_and_autoplay, inputs=voice_input, outputs=audio_output)

    return demo


if __name__ == "__main__":
    app().launch()