nagasurendra's picture
Update app.py
cf344c7 verified
raw
history blame
3.38 kB
import gradio as gr
from gtts import gTTS
import os
import speech_recognition as sr
# Initialize 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 provide audio file."""
tts = gTTS(text=text, lang='en')
file_path = "response.mp3"
tts.save(file_path)
return file_path
# Read Menu Function
def read_menu():
"""Generate the menu text and read it aloud."""
menu_text = "Here is the menu. Starting with Biryani options: "
for item in menu_items["biryani"]:
menu_text += item + ". "
menu_text += "Now the 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, text_to_speech(menu_text)
# Process Voice Command
def process_command(audio_path):
"""Process the user's voice command."""
try:
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
command = recognizer.recognize_google(audio_data).lower()
except Exception as e:
error_text = "Sorry, I could not process the audio."
return "Error", text_to_speech(error_text)
if "menu" in command:
menu_text, menu_audio = read_menu()
return menu_text, menu_audio
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 response_text, text_to_speech(response_text)
if "cart" in command:
if not cart:
response_text = "Your cart is empty."
else:
response_text = "Your cart contains: " + ", ".join(cart)
return response_text, text_to_speech(response_text)
if "submit" in command or "done" in command:
if not cart:
response_text = "Your cart is empty. Add some items before submitting."
else:
response_text = "Your final order is: " + ", ".join(cart) + ". Thank you for your order!"
cart.clear()
return response_text, text_to_speech(response_text)
error_text = "Sorry, I couldn't understand your request."
return error_text, text_to_speech(error_text)
# Gradio App
def app():
"""Create the Gradio interface."""
with gr.Blocks() as demo:
gr.Markdown("# Voice-Activated Restaurant Menu System")
gr.Markdown("Speak your command to interact with the menu system dynamically.")
with gr.Row():
voice_input = gr.Audio(type="filepath", label="Speak Your Command")
transcribed_text = gr.Textbox(label="Transcribed Command")
response_text = gr.Textbox(label="Response Text")
audio_output = gr.Audio(label="Audio Response")
voice_input.change(fn=process_command, inputs=voice_input, outputs=[response_text, audio_output])
return demo
if __name__ == "__main__":
app().launch()