DSatishchandra's picture
Update app.py
901459b verified
Raw
History Blame Contribute Delete
5.35 kB
import gradio as gr
from gtts import gTTS
import os
import speech_recognition as sr
import time
# 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 = []
context = {"last_action": None, "order": {}}
# Text-to-Speech Function
def speak_and_save(text):
tts = gTTS(text=text, lang='en')
audio_path = "response.mp3"
tts.save(audio_path)
return audio_path
# Speech Recognition Function
def recognize_speech(audio_file_path):
if not audio_file_path or not os.path.exists(audio_file_path):
return "No valid audio input provided. Please try again."
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):
if not audio_file_path or not os.path.exists(audio_file_path):
response = "No audio input detected. Please record or upload your voice."
return response, speak_and_save(response)
user_input = recognize_speech(audio_file_path)
conversation_history.append(f"Customer: {user_input}")
if user_input.startswith("No audio input detected") 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"
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"
elif "order" in user_input.lower() or any(item.lower() in user_input.lower() for item in sum(menu.values(), [])):
words = user_input.split()
quantity = 1 # Default quantity
item_name = None
for word in words:
if word.isdigit():
quantity = int(word)
for category, items in menu.items():
if word in items:
item_name = word
break
if item_name:
context["order"][item_name] = context["order"].get(item_name, 0) + quantity
response = f"I've added {quantity} {item_name}(s) to your order. Would you like anything else?"
else:
response = "I couldn't identify the item. Could you please repeat your order?"
context["last_action"] = "place_order"
elif "no" in user_input.lower() or "that's it" in user_input.lower():
order_summary = ", ".join([f"{item} (x{qty})" for item, qty in context["order"].items()])
response = f"Your final order is: {order_summary}. 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"] = {}
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"
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
# Gradio Interface with Auto-Play After 3 Seconds
def create_interface():
with gr.Blocks() as app:
gr.Markdown("## Restaurant Voice Assistant")
gr.Markdown("Talk to our voice assistant to place your order or ask about the menu!")
audio_input = gr.Audio(type="filepath", label="Your Voice Input")
text_response = gr.Textbox(label="Text Response")
audio_response = gr.Audio(label="Audio Response", interactive=False)
# Add JavaScript for auto-playing audio 3 seconds after response
gr.HTML("""
<script>
document.addEventListener("change", function() {
setTimeout(function() {
const audioPlayer = document.querySelector("audio");
if (audioPlayer) {
audioPlayer.play();
}
}, 3000); // Delay of 3 seconds
});
</script>
""")
def interact(audio_file_path):
return process_order(audio_file_path)
audio_input.change(interact, inputs=audio_input, outputs=[text_response, audio_response])
return app
if __name__ == "__main__":
app = create_interface()
app.launch()