Spaces:
Sleeping
Sleeping
File size: 3,379 Bytes
a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 a941958 cf344c7 |
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 89 90 91 92 93 94 95 96 97 98 |
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()
|