Spaces:
Sleeping
Sleeping
File size: 3,070 Bytes
a941958 cf344c7 b766996 cf344c7 b766996 cf344c7 b766996 cf344c7 a941958 cf344c7 b766996 cf344c7 b766996 cf344c7 b766996 cf344c7 b766996 cf344c7 b766996 cf344c7 b766996 a941958 cf344c7 b766996 a941958 cf344c7 a941958 b766996 a941958 cf344c7 b766996 a941958 b766996 a941958 b766996 cf344c7 b766996 a941958 b766996 cf344c7 b766996 a941958 b766996 a941958 b766996 a941958 b766996 a941958 b766996 a941958 b766996 a941958 b766996 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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()
|