First-Project / app.py
Rammohan0504's picture
Update app.py
270cfeb verified
raw
history blame
2.11 kB
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()