geethareddy's picture
Update app.py
50f0956 verified
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()