nagasurendra's picture
Update app.py
fd08439 verified
raw
history blame
3.92 kB
import gradio as gr
from gtts import gTTS
import os
import speech_recognition as sr
from tempfile import NamedTemporaryFile
import threading
import time
# 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 = []
# Helper Functions
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
# Command Processing
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."
# Continuous Conversation Loop
def continuous_conversation():
"""Continuously listen to and respond to user commands."""
while True:
with NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
try:
print("Listening...")
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
with open(temp_audio.name, "wb") as f:
f.write(audio.get_wav_data())
response = process_audio_command(temp_audio.name)
print(response)
audio_response = text_to_speech(response)
os.system(f"mpg123 {audio_response}")
except Exception as e:
print("Error processing audio input.")
# Gradio Interface
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")
response_text = gr.Textbox(label="Assistant Response")
audio_output = gr.Audio(label="Assistant Voice Response")
def handle_conversation(audio_path):
response = process_audio_command(audio_path)
return response, text_to_speech(response)
audio_input.change(handle_conversation, inputs=audio_input, outputs=[response_text, audio_output])
if __name__ == "__main__":
threading.Thread(target=continuous_conversation, daemon=True).start()
app.launch()