DynamicMenuApp1 / app.py
DSatishchandra's picture
Update app.py
f65fa0a verified
Raw
History Blame Contribute Delete
4.16 kB
import gradio as gr
from gtts import gTTS
import os
import speech_recognition as sr
# Menu for the restaurant
menu = {
"Starters": ["Soup", "Spring Rolls"],
"Main Course": ["Paneer Butter Masala", "Chicken Curry", "Veg Biryani"],
"Breads": ["Roti", "Naan", "Paratha"],
"Desserts": ["Gulab Jamun", "Ice Cream"],
"Drinks": ["Mango Lassi", "Soda", "Water"]
}
# Conversation history and context
conversation_history = []
context = {"last_action": None, "order": []}
# Text-to-Speech Function
def speak_and_save(text, filename="response.mp3"):
tts = gTTS(text=text, lang='en')
tts.save(filename)
return filename
# Speech Recognition Function
def recognize_speech(audio_file_path):
recognizer = sr.Recognizer()
try:
with sr.AudioFile(audio_file_path) as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data)
return text
except sr.UnknownValueError:
return "I didn't catch that. Could you repeat?"
except sr.RequestError:
return "There was an issue processing your speech. Try again later."
# Voice Assistant Logic
def process_order(audio_file_path):
user_input = recognize_speech(audio_file_path)
conversation_history.append(f"Customer: {user_input}")
if user_input.startswith("Sorry") or user_input.startswith("I didn't"):
response = user_input
conversation_history.append(f"AI: {response}")
return response, speak_and_save(response)
global context
response = ""
# Greet the customer
if context["last_action"] is None:
response = "Welcome to our restaurant. How can I assist you today?"
context["last_action"] = "greet"
# Show menu
elif "menu" in user_input.lower():
response = "Here is our menu:\n"
for category, items in menu.items():
response += f"{category}: {', '.join(items)}\n"
response += "What would you like to order?"
context["last_action"] = "show_menu"
# Place an order
elif "order" in user_input.lower() or any(item.lower() in user_input.lower() for item in sum(menu.values(), [])):
for category, items in menu.items():
for item in items:
if item.lower() in user_input.lower():
context["order"].append(item)
break
response = f"I've added {', '.join(context['order'])} to your order. Would you like anything else?"
context["last_action"] = "place_order"
# Finalize the order
elif "no" in user_input.lower() or "that's it" in user_input.lower():
response = f"Your final order is: {', '.join(context['order'])}. Thank you for your order. Your food will arrive shortly. Enjoy your meal!"
conversation_history.append(f"Order Summary: {context['order']}")
context["last_action"] = "final_order"
context["order"] = [] # Reset the order
# Mid-meal check
elif "everything alright" in user_input.lower() or "anything else" in user_input.lower():
response = "I hope you're enjoying your meal. Can I bring you anything else?"
context["last_action"] = "check_meal"
# Default response
else:
response = "I'm not sure what you meant. Could you clarify?"
context["last_action"] = "clarify"
conversation_history.append(f"AI: {response}")
audio_response_path = speak_and_save(response)
return response, audio_response_path
# Save Conversation History
def save_conversation():
with open("conversation_history.txt", "w") as f:
f.write("\n".join(conversation_history))
return "Conversation history saved successfully!"
# Gradio Interface
iface = gr.Interface(
fn=process_order,
inputs=gr.Audio(type="filepath"),
outputs=[gr.Textbox(label="Text Response"), gr.Audio(label="Audio Response")],
title="Restaurant Voice Assistant",
description="Talk to our voice assistant to place your order or ask about the menu!",
live=True
)
if __name__ == "__main__":
try:
iface.launch()
finally:
save_conversation()