Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,9 @@ import asyncio
|
|
| 4 |
import tempfile
|
| 5 |
from edge_tts import Communicate
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
# Load Menu Data
|
| 8 |
def load_menu():
|
| 9 |
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
|
|
@@ -12,69 +15,92 @@ def load_menu():
|
|
| 12 |
except Exception as e:
|
| 13 |
raise ValueError(f"Error loading menu file: {e}")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
def filter_menu(preference):
|
| 17 |
-
menu_data = load_menu()
|
| 18 |
-
if preference == "Halal/Non-Veg":
|
| 19 |
-
filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 20 |
-
elif preference == "Vegetarian":
|
| 21 |
-
filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 22 |
-
elif preference == "Guilt-Free":
|
| 23 |
-
filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
|
| 24 |
-
else:
|
| 25 |
-
filtered_data = menu_data
|
| 26 |
-
|
| 27 |
-
html_content = ""
|
| 28 |
-
for _, item in filtered_data.iterrows():
|
| 29 |
-
html_content += f"""
|
| 30 |
-
<div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
|
| 31 |
-
<div style="flex: 1; margin-right: 15px;">
|
| 32 |
-
<h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
|
| 33 |
-
<p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
|
| 34 |
-
<p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
|
| 35 |
-
</div>
|
| 36 |
-
</div>
|
| 37 |
-
"""
|
| 38 |
-
return html_content
|
| 39 |
-
|
| 40 |
-
# Text-to-Speech Response
|
| 41 |
async def generate_tts_response(text):
|
| 42 |
communicate = Communicate(text, "en-US-JennyNeural")
|
| 43 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 44 |
await communicate.save(tmp_file.name)
|
| 45 |
return tmp_file.name
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# Handle Voice Commands
|
| 48 |
async def handle_voice_command(audio_path, preference):
|
| 49 |
transcription = transcribe_audio(audio_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
return await generate_tts_response(f"Order confirmed for {item_name}. Adding to cart."), preference, f"Added {item_name} to cart."
|
| 59 |
-
|
| 60 |
-
# Update menu preference
|
| 61 |
-
if "vegetarian" in transcription.lower():
|
| 62 |
preference = "Vegetarian"
|
| 63 |
-
elif "non-veg" in transcription
|
| 64 |
preference = "Halal/Non-Veg"
|
| 65 |
-
elif "guilt-free" in transcription
|
| 66 |
preference = "Guilt-Free"
|
| 67 |
else:
|
| 68 |
preference = "All"
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# Transcribe Audio Placeholder
|
| 75 |
def transcribe_audio(audio_path):
|
| 76 |
-
# Replace with
|
| 77 |
-
return "
|
| 78 |
|
| 79 |
# Gradio App
|
| 80 |
def app():
|
|
@@ -83,17 +109,33 @@ def app():
|
|
| 83 |
gr.Markdown("## Welcome to the Menu")
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
-
audio_input = gr.Audio(label="Speak your preference", type="filepath")
|
| 87 |
tts_output = gr.Audio(label="Assistant Response", autoplay=True)
|
| 88 |
preference = gr.Textbox(label="Preference", value="All", interactive=False)
|
| 89 |
-
menu_output = gr.
|
|
|
|
| 90 |
|
|
|
|
| 91 |
audio_input.change(
|
| 92 |
handle_voice_command,
|
| 93 |
inputs=[audio_input, preference],
|
| 94 |
outputs=[tts_output, preference, menu_output],
|
| 95 |
)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return demo
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import tempfile
|
| 5 |
from edge_tts import Communicate
|
| 6 |
|
| 7 |
+
# Global cart to store ordered items
|
| 8 |
+
cart = []
|
| 9 |
+
|
| 10 |
# Load Menu Data
|
| 11 |
def load_menu():
|
| 12 |
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
raise ValueError(f"Error loading menu file: {e}")
|
| 17 |
|
| 18 |
+
# Generate Text-to-Speech Response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
async def generate_tts_response(text):
|
| 20 |
communicate = Communicate(text, "en-US-JennyNeural")
|
| 21 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 22 |
await communicate.save(tmp_file.name)
|
| 23 |
return tmp_file.name
|
| 24 |
|
| 25 |
+
# Search for Dish
|
| 26 |
+
def search_dish(dish_name):
|
| 27 |
+
menu_data = load_menu()
|
| 28 |
+
dish = menu_data[menu_data["Dish Name"].str.contains(dish_name, case=False, na=False)]
|
| 29 |
+
return dish
|
| 30 |
+
|
| 31 |
# Handle Voice Commands
|
| 32 |
async def handle_voice_command(audio_path, preference):
|
| 33 |
transcription = transcribe_audio(audio_path)
|
| 34 |
+
transcription = transcription.lower()
|
| 35 |
+
|
| 36 |
+
if transcription.startswith("order"):
|
| 37 |
+
dish_name = transcription.replace("order", "").strip()
|
| 38 |
+
dish = search_dish(dish_name)
|
| 39 |
+
|
| 40 |
+
if not dish.empty:
|
| 41 |
+
# Add to cart
|
| 42 |
+
item_name = dish.iloc[0]["Dish Name"]
|
| 43 |
+
item_price = dish.iloc[0]["Price ($)"]
|
| 44 |
+
cart.append({"name": item_name, "price": item_price})
|
| 45 |
+
|
| 46 |
+
confirmation_text = f"{item_name} is available for ${item_price}. Order confirmed and added to cart."
|
| 47 |
+
audio_response = await generate_tts_response(confirmation_text)
|
| 48 |
+
return audio_response, preference, f"Order: {confirmation_text}"
|
| 49 |
+
else:
|
| 50 |
+
unavailable_text = f"Sorry, {dish_name} is not available in the menu."
|
| 51 |
+
audio_response = await generate_tts_response(unavailable_text)
|
| 52 |
+
return audio_response, preference, unavailable_text
|
| 53 |
+
|
| 54 |
+
if "menu items" in transcription:
|
| 55 |
+
menu_text = list_menu_items(preference)
|
| 56 |
+
audio_response = await generate_tts_response(menu_text)
|
| 57 |
+
return audio_response, preference, f"Menu: {menu_text}"
|
| 58 |
|
| 59 |
+
if "stop" in transcription:
|
| 60 |
+
return None, preference, "Stop command recognized."
|
| 61 |
+
elif "start" in transcription:
|
| 62 |
+
return None, preference, "Start command recognized."
|
| 63 |
+
|
| 64 |
+
# Update preference
|
| 65 |
+
if "vegetarian" in transcription:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
preference = "Vegetarian"
|
| 67 |
+
elif "non-veg" in transcription or "halal" in transcription:
|
| 68 |
preference = "Halal/Non-Veg"
|
| 69 |
+
elif "guilt-free" in transcription:
|
| 70 |
preference = "Guilt-Free"
|
| 71 |
else:
|
| 72 |
preference = "All"
|
| 73 |
|
| 74 |
+
preference_text = f"Updated preference to {preference}."
|
| 75 |
+
audio_response = await generate_tts_response(preference_text)
|
| 76 |
+
return audio_response, preference, preference_text
|
| 77 |
+
|
| 78 |
+
# List Menu Items
|
| 79 |
+
def list_menu_items(preference):
|
| 80 |
+
menu_data = load_menu()
|
| 81 |
+
if preference == "Halal/Non-Veg":
|
| 82 |
+
filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 83 |
+
elif preference == "Vegetarian":
|
| 84 |
+
filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 85 |
+
elif preference == "Guilt-Free":
|
| 86 |
+
filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
|
| 87 |
+
else:
|
| 88 |
+
filtered_data = menu_data
|
| 89 |
+
|
| 90 |
+
# Create a textual summary for speech
|
| 91 |
+
text_summary = ""
|
| 92 |
+
for _, item in filtered_data.iterrows():
|
| 93 |
+
text_summary += f"{item['Dish Name']} for ${item['Price ($)']}. "
|
| 94 |
+
|
| 95 |
+
if not text_summary:
|
| 96 |
+
text_summary = "No items available in this category."
|
| 97 |
+
|
| 98 |
+
return text_summary
|
| 99 |
|
| 100 |
# Transcribe Audio Placeholder
|
| 101 |
def transcribe_audio(audio_path):
|
| 102 |
+
# Replace with actual transcription logic (e.g., Whisper API or Google Speech-to-Text)
|
| 103 |
+
return "order veg biryani"
|
| 104 |
|
| 105 |
# Gradio App
|
| 106 |
def app():
|
|
|
|
| 109 |
gr.Markdown("## Welcome to the Menu")
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
+
audio_input = gr.Audio(label="Speak your preference or order", type="filepath")
|
| 113 |
tts_output = gr.Audio(label="Assistant Response", autoplay=True)
|
| 114 |
preference = gr.Textbox(label="Preference", value="All", interactive=False)
|
| 115 |
+
menu_output = gr.Textbox(label="Transcript", value="")
|
| 116 |
+
cart_output = gr.Textbox(label="Cart", value="Your cart is empty.", interactive=False)
|
| 117 |
|
| 118 |
+
# Update outputs dynamically
|
| 119 |
audio_input.change(
|
| 120 |
handle_voice_command,
|
| 121 |
inputs=[audio_input, preference],
|
| 122 |
outputs=[tts_output, preference, menu_output],
|
| 123 |
)
|
| 124 |
|
| 125 |
+
# Update cart content dynamically
|
| 126 |
+
def update_cart():
|
| 127 |
+
if cart:
|
| 128 |
+
cart_content = "\n".join([f"{item['name']} - ${item['price']}" for item in cart])
|
| 129 |
+
else:
|
| 130 |
+
cart_content = "Your cart is empty."
|
| 131 |
+
return cart_content
|
| 132 |
+
|
| 133 |
+
gr.Button("Update Cart").click(
|
| 134 |
+
update_cart,
|
| 135 |
+
inputs=[],
|
| 136 |
+
outputs=cart_output
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
return demo
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|