File size: 2,114 Bytes
f916939
5d82ef0
 
 
261a3f3
5d82ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f07fbb
ac70e91
270cfeb
 
5d82ef0
 
270cfeb
 
 
 
 
 
 
 
 
 
1e5aca5
5d82ef0
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
import gradio as gr
import speech_recognition as sr
from gtts import gTTS
import os

# Voice assistant function
def voice_assistant(audio_file):
    """Handles voice input and responds dynamically."""
    recognizer = sr.Recognizer()
    menu_keywords = {
        "biryani": "We have Chicken Biryani, Mutton Biryani, and Veg Biryani.",
        "starters": "Our starters include Samosa, Chicken 65, and Paneer Tikka.",
        "drinks": "We offer refreshing drinks like Coke, Lemonade, and Iced Tea.",
    }
    try:
        # Process the audio input
        with sr.AudioFile(audio_file) as source:
            audio = recognizer.record(source)
            query = recognizer.recognize_google(audio).lower()

        # Respond based on the query
        for keyword, response in menu_keywords.items():
            if keyword in query:
                tts = gTTS(text=response, lang="en")
                tts.save("response.mp3")
                return "response.mp3"
        
        # Default response
        default_response = "I didn't catch that. Please ask about biryani, starters, or drinks."
        tts = gTTS(text=default_response, lang="en")
        tts.save("response.mp3")
        return "response.mp3"
    except Exception:
        error_response = "Sorry, I couldn't understand your request. Please try again."
        tts = gTTS(text=error_response, lang="en")
        tts.save("response.mp3")
        return "response.mp3"

# Main Gradio App
with gr.Blocks() as demo:
    gr.HTML("<h1 style='text-align: center; color: #333;'>🎤 Voice Assistant</h1>")
    
    # Voice Assistant Section
    with gr.Row():
        mic_button = gr.Image(
            value="/mnt/data/Screenshot 2024-12-28 101858.jpg", 
            interactive=True, 
            label="Click to Speak"
        )
        mic_input = gr.Audio(type="filepath", label="Record Your Query", visible=False)
        mic_response = gr.Audio(type="filepath", label="Response", visible=False)

    # Link the microphone button to the function
    mic_button.click(voice_assistant, inputs=[mic_input], outputs=[mic_response])

demo.launch()