Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import edge_tts
|
| 4 |
import asyncio
|
| 5 |
import tempfile
|
|
|
|
| 6 |
|
| 7 |
# Load Menu Data
|
| 8 |
def load_menu():
|
|
@@ -27,39 +27,53 @@ def filter_menu(preference):
|
|
| 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;">
|
| 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 style="flex-shrink: 0; text-align: center;">
|
| 37 |
-
<button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;"
|
| 38 |
-
onclick="addItemToCart('{item['Dish Name']}', '{item['Price ($)']}')">Add</button>
|
| 39 |
-
</div>
|
| 40 |
</div>
|
| 41 |
"""
|
| 42 |
return html_content
|
| 43 |
|
| 44 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
async def handle_voice_command(audio_path, preference):
|
| 46 |
-
|
| 47 |
|
| 48 |
# Process commands
|
| 49 |
-
if "stop" in
|
| 50 |
-
return "Audio playback stopped.", preference, ""
|
| 51 |
-
elif "start" in
|
| 52 |
-
return "Audio playback started.", preference, ""
|
| 53 |
-
elif "order" in
|
| 54 |
-
item_name =
|
| 55 |
-
return f"Order confirmed for {item_name}.", preference, f"Added {item_name} to cart."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
#
|
| 61 |
def transcribe_audio(audio_path):
|
| 62 |
-
# Replace with actual transcription
|
| 63 |
return "Sample transcription text"
|
| 64 |
|
| 65 |
# Gradio App
|
|
@@ -67,14 +81,18 @@ def app():
|
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
with gr.Row():
|
| 69 |
gr.Markdown("## Welcome to the Menu")
|
|
|
|
| 70 |
with gr.Row():
|
| 71 |
audio_input = gr.Audio(label="Speak your preference", type="filepath")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
audio_input.change(
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
return demo
|
| 80 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import asyncio
|
| 4 |
import tempfile
|
| 5 |
+
from edge_tts import Communicate
|
| 6 |
|
| 7 |
# Load Menu Data
|
| 8 |
def load_menu():
|
|
|
|
| 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 |
# Process commands
|
| 52 |
+
if "stop" in transcription.lower():
|
| 53 |
+
return "Audio playback stopped.", preference, "Stop command recognized."
|
| 54 |
+
elif "start" in transcription.lower():
|
| 55 |
+
return "Audio playback started.", preference, "Start command recognized."
|
| 56 |
+
elif "order" in transcription.lower():
|
| 57 |
+
item_name = transcription.replace("order", "").strip()
|
| 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.lower() or "halal" in transcription.lower():
|
| 64 |
+
preference = "Halal/Non-Veg"
|
| 65 |
+
elif "guilt-free" in transcription.lower():
|
| 66 |
+
preference = "Guilt-Free"
|
| 67 |
+
else:
|
| 68 |
+
preference = "All"
|
| 69 |
|
| 70 |
+
# Generate a response for the updated menu
|
| 71 |
+
response_text = f"Updated menu preference to {preference}."
|
| 72 |
+
return await generate_tts_response(response_text), preference, filter_menu(preference)
|
| 73 |
|
| 74 |
+
# Transcribe Audio Placeholder
|
| 75 |
def transcribe_audio(audio_path):
|
| 76 |
+
# Replace with an actual transcription service (e.g., Whisper, Azure STT, etc.)
|
| 77 |
return "Sample transcription text"
|
| 78 |
|
| 79 |
# Gradio App
|
|
|
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
with gr.Row():
|
| 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.HTML(value=filter_menu("All"), label="Menu Suggestions")
|
| 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 |
|