nagasurendra's picture
Update app.py
b766996 verified
raw
history blame
3.07 kB
import gradio as gr
from gtts import gTTS
import os
import speech_recognition as sr
from tempfile import NamedTemporaryFile
# Initialize the recognizer
recognizer = sr.Recognizer()
# Menu items and cart initialization
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 = []
def text_to_speech(text):
"""Convert text to speech and provide an audio file."""
tts = gTTS(text=text, lang='en')
audio_file = NamedTemporaryFile(delete=False, suffix=".mp3")
tts.save(audio_file.name)
return audio_file.name
def read_menu():
"""Generate and return the menu text."""
menu_text = "Here is the menu: \n"
for category, items in menu_items.items():
menu_text += f"{category.capitalize()}: \n" + ", ".join(items) + "\n"
menu_text += "Please tell me the items you want to add to your cart."
return menu_text
def process_audio_command(audio_path):
"""Process the user's audio command."""
global cart
try:
with sr.AudioFile(audio_path) as source:
audio = recognizer.record(source)
command = recognizer.recognize_google(audio).lower()
except Exception as e:
return "Sorry, I could not understand. Could you repeat?"
if "menu" in command:
return read_menu()
for category, items in menu_items.items():
for item in items:
if item.lower() in command:
cart.append(item)
return f"{item} has been added to your cart."
if "cart" in command:
if not cart:
return "Your cart is empty."
else:
return "Your cart contains: " + ", ".join(cart)
if "submit" in command or "finalize" in command:
if not cart:
return "Your cart is empty. Add some items before submitting."
else:
response = "Your final order is: " + ", ".join(cart) + ". Thank you for your order!"
cart.clear()
return response
return "Sorry, I didn't understand that. Please try again."
# Define the continuous conversation function
def conversation_loop(audio_path):
"""Continuous conversation handling for dynamic interactions."""
user_response = process_audio_command(audio_path)
return user_response, text_to_speech(user_response)
with gr.Blocks() as app:
gr.Markdown("# Voice-Activated Restaurant Menu System")
gr.Markdown("Speak your command to interact with the menu dynamically.")
with gr.Row():
audio_input = gr.Audio(label="Speak Your Command", type="filepath")
text_output = gr.Textbox(label="Transcribed Command")
response_text = gr.Textbox(label="Response Text")
audio_output = gr.Audio(label="Assistant Response")
audio_input.change(conversation_loop, inputs=audio_input, outputs=[response_text, audio_output])
if __name__ == "__main__":
app.launch()