Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,16 +12,15 @@ menu = {
|
|
| 12 |
"Drinks": ["Mango Lassi", "Soda", "Water"]
|
| 13 |
}
|
| 14 |
|
| 15 |
-
# Conversation history and context
|
| 16 |
conversation_history = []
|
| 17 |
context = {"last_action": None, "order": []}
|
| 18 |
|
| 19 |
# Text-to-Speech Function
|
| 20 |
def speak_and_save(text):
|
| 21 |
tts = gTTS(text=text, lang='en')
|
| 22 |
-
|
| 23 |
-
tts.save(
|
| 24 |
-
return
|
| 25 |
|
| 26 |
# Speech Recognition Function
|
| 27 |
def recognize_speech(audio_file_path):
|
|
@@ -49,47 +48,31 @@ def process_order(audio_file_path):
|
|
| 49 |
global context
|
| 50 |
response = ""
|
| 51 |
|
| 52 |
-
# Greet the customer
|
| 53 |
if context["last_action"] is None:
|
| 54 |
-
response = "
|
| 55 |
context["last_action"] = "greet"
|
| 56 |
-
|
| 57 |
-
# Show menu
|
| 58 |
elif "menu" in user_input.lower():
|
| 59 |
response = "Here is our menu:\n"
|
| 60 |
for category, items in menu.items():
|
| 61 |
-
response += f"{category}: {', '.join(items)}
|
| 62 |
response += "What would you like to order?"
|
| 63 |
context["last_action"] = "show_menu"
|
| 64 |
-
|
| 65 |
-
# Place an order
|
| 66 |
-
elif any(item.lower() in user_input.lower() for item in sum(menu.values(), [])):
|
| 67 |
for category, items in menu.items():
|
| 68 |
for item in items:
|
| 69 |
if item.lower() in user_input.lower():
|
| 70 |
context["order"].append(item)
|
| 71 |
break
|
| 72 |
-
response = f"I've added {', '.join(context['order'])} to your order. Would you like
|
| 73 |
context["last_action"] = "place_order"
|
| 74 |
-
|
| 75 |
-
# Customization
|
| 76 |
-
elif "customize" in user_input.lower() or "spice" in user_input.lower() or "topping" in user_input.lower():
|
| 77 |
-
response = "What customizations would you like? For example, less spicy, extra butter, or no onions?"
|
| 78 |
-
context["last_action"] = "customize_order"
|
| 79 |
-
|
| 80 |
-
# Finalize the order
|
| 81 |
elif "no" in user_input.lower() or "that's it" in user_input.lower():
|
| 82 |
response = f"Your final order is: {', '.join(context['order'])}. Thank you for your order. Your food will arrive shortly. Enjoy your meal!"
|
| 83 |
conversation_history.append(f"Order Summary: {context['order']}")
|
| 84 |
context["last_action"] = "final_order"
|
| 85 |
-
context["order"] = []
|
| 86 |
-
|
| 87 |
-
# Mid-meal check
|
| 88 |
elif "everything alright" in user_input.lower() or "anything else" in user_input.lower():
|
| 89 |
-
response = "I hope you're enjoying your meal. Can I bring you anything else
|
| 90 |
context["last_action"] = "check_meal"
|
| 91 |
-
|
| 92 |
-
# Default response
|
| 93 |
else:
|
| 94 |
response = "I'm not sure what you meant. Could you clarify?"
|
| 95 |
context["last_action"] = "clarify"
|
|
@@ -98,24 +81,34 @@ def process_order(audio_file_path):
|
|
| 98 |
audio_response_path = speak_and_save(response)
|
| 99 |
return response, audio_response_path
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
def
|
| 103 |
-
with
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
title="Restaurant Voice Assistant",
|
| 113 |
-
description="Talk to our voice assistant to place your order or ask about the menu!",
|
| 114 |
-
live=True
|
| 115 |
-
)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
finally:
|
| 121 |
-
save_conversation()
|
|
|
|
| 12 |
"Drinks": ["Mango Lassi", "Soda", "Water"]
|
| 13 |
}
|
| 14 |
|
|
|
|
| 15 |
conversation_history = []
|
| 16 |
context = {"last_action": None, "order": []}
|
| 17 |
|
| 18 |
# Text-to-Speech Function
|
| 19 |
def speak_and_save(text):
|
| 20 |
tts = gTTS(text=text, lang='en')
|
| 21 |
+
audio_path = "response.mp3"
|
| 22 |
+
tts.save(audio_path)
|
| 23 |
+
return audio_path
|
| 24 |
|
| 25 |
# Speech Recognition Function
|
| 26 |
def recognize_speech(audio_file_path):
|
|
|
|
| 48 |
global context
|
| 49 |
response = ""
|
| 50 |
|
|
|
|
| 51 |
if context["last_action"] is None:
|
| 52 |
+
response = "Welcome to our restaurant. How can I assist you today?"
|
| 53 |
context["last_action"] = "greet"
|
|
|
|
|
|
|
| 54 |
elif "menu" in user_input.lower():
|
| 55 |
response = "Here is our menu:\n"
|
| 56 |
for category, items in menu.items():
|
| 57 |
+
response += f"{category}: {', '.join(items)}\n"
|
| 58 |
response += "What would you like to order?"
|
| 59 |
context["last_action"] = "show_menu"
|
| 60 |
+
elif "order" in user_input.lower() or any(item.lower() in user_input.lower() for item in sum(menu.values(), [])):
|
|
|
|
|
|
|
| 61 |
for category, items in menu.items():
|
| 62 |
for item in items:
|
| 63 |
if item.lower() in user_input.lower():
|
| 64 |
context["order"].append(item)
|
| 65 |
break
|
| 66 |
+
response = f"I've added {', '.join(context['order'])} to your order. Would you like anything else?"
|
| 67 |
context["last_action"] = "place_order"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
elif "no" in user_input.lower() or "that's it" in user_input.lower():
|
| 69 |
response = f"Your final order is: {', '.join(context['order'])}. Thank you for your order. Your food will arrive shortly. Enjoy your meal!"
|
| 70 |
conversation_history.append(f"Order Summary: {context['order']}")
|
| 71 |
context["last_action"] = "final_order"
|
| 72 |
+
context["order"] = []
|
|
|
|
|
|
|
| 73 |
elif "everything alright" in user_input.lower() or "anything else" in user_input.lower():
|
| 74 |
+
response = "I hope you're enjoying your meal. Can I bring you anything else?"
|
| 75 |
context["last_action"] = "check_meal"
|
|
|
|
|
|
|
| 76 |
else:
|
| 77 |
response = "I'm not sure what you meant. Could you clarify?"
|
| 78 |
context["last_action"] = "clarify"
|
|
|
|
| 81 |
audio_response_path = speak_and_save(response)
|
| 82 |
return response, audio_response_path
|
| 83 |
|
| 84 |
+
# Gradio Interface with Auto-Play JavaScript Injection
|
| 85 |
+
def create_interface():
|
| 86 |
+
with gr.Blocks() as app:
|
| 87 |
+
gr.Markdown("## Restaurant Voice Assistant")
|
| 88 |
+
gr.Markdown("Talk to our voice assistant to place your order or ask about the menu!")
|
| 89 |
+
|
| 90 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="Your Voice Input")
|
| 91 |
+
text_response = gr.Textbox(label="Text Response")
|
| 92 |
+
audio_response = gr.Audio(label="Audio Response")
|
| 93 |
+
|
| 94 |
+
# Add JavaScript for auto-playing audio
|
| 95 |
+
gr.HTML("""
|
| 96 |
+
<script>
|
| 97 |
+
const audioPlayer = document.querySelector("audio");
|
| 98 |
+
if (audioPlayer) {
|
| 99 |
+
audioPlayer.onloadeddata = () => {
|
| 100 |
+
audioPlayer.play();
|
| 101 |
+
};
|
| 102 |
+
}
|
| 103 |
+
</script>
|
| 104 |
+
""")
|
| 105 |
|
| 106 |
+
def interact(audio_file_path):
|
| 107 |
+
return process_order(audio_file_path)
|
| 108 |
+
|
| 109 |
+
audio_input.change(interact, inputs=audio_input, outputs=[text_response, audio_response])
|
| 110 |
+
return app
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
+
app = create_interface()
|
| 114 |
+
app.launch()
|
|
|
|
|
|