Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import asyncio
|
| 4 |
-
from pydub import AudioSegment
|
| 5 |
-
import numpy as np
|
| 6 |
import edge_tts
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
def load_menu():
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
return menu_data
|
| 13 |
except Exception as e:
|
| 14 |
-
raise ValueError("
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
menu_data = load_menu()
|
| 19 |
-
|
| 20 |
-
for _,
|
| 21 |
-
|
| 22 |
-
return
|
| 23 |
|
| 24 |
-
# Text-to-
|
| 25 |
-
|
| 26 |
-
communicate = edge_tts.Communicate(text
|
| 27 |
-
with
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Handle user input and provide a response
|
| 32 |
-
async def handle_input(audio_path, cart):
|
| 33 |
-
menu_data = load_menu()
|
| 34 |
-
transcription = transcribe_audio(audio_path)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
else:
|
| 43 |
-
|
| 44 |
-
if not item.empty:
|
| 45 |
-
dish_name = item.iloc[0]["Dish Name"]
|
| 46 |
-
price = item.iloc[0]["Price ($)"]
|
| 47 |
-
response_text = f"{dish_name} is available for ${price}. Adding it to your cart."
|
| 48 |
-
cart.append(f"{dish_name} - ${price}")
|
| 49 |
-
else:
|
| 50 |
-
response_text = f"Sorry, {transcription} is not on the menu."
|
| 51 |
|
| 52 |
-
|
| 53 |
-
return
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
def transcribe_audio(audio_path):
|
| 57 |
-
# Mock transcription for now
|
| 58 |
-
return "veg samosa" # Replace this with the transcription from an audio processing library.
|
| 59 |
-
|
| 60 |
-
# Gradio UI setup
|
| 61 |
def app():
|
| 62 |
-
cart = []
|
| 63 |
-
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
gr.Markdown("
|
| 66 |
-
with gr.Row():
|
| 67 |
-
audio_input = gr.Audio(label="Speak your preference or order", source="microphone", type="filepath")
|
| 68 |
-
assistant_response = gr.Audio(label="Assistant Response")
|
| 69 |
-
cart_output = gr.Textbox(label="Cart", value="")
|
| 70 |
-
status = gr.Textbox(label="Status", value="")
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
return audio_response
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
#
|
|
|
|
|
|
|
|
|
|
| 80 |
audio_input.change(
|
| 81 |
-
|
| 82 |
-
inputs=[audio_input],
|
| 83 |
-
outputs=[
|
| 84 |
)
|
| 85 |
|
| 86 |
return demo
|
| 87 |
|
| 88 |
-
|
| 89 |
if __name__ == "__main__":
|
| 90 |
-
app()
|
|
|
|
|
|
| 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():
|
| 9 |
+
menu_file = "menu.xlsx"
|
| 10 |
try:
|
| 11 |
+
return pd.read_excel(menu_file)
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
+
raise ValueError(f"Error loading menu file: {e}")
|
| 14 |
|
| 15 |
+
# Filter menu items based on preference
|
| 16 |
+
def filter_menu(preference):
|
| 17 |
menu_data = load_menu()
|
| 18 |
+
menu_details = ""
|
| 19 |
+
for _, item in menu_data.iterrows():
|
| 20 |
+
menu_details += f"{item['Dish Name']} - ${item['Price ($)']}. "
|
| 21 |
+
return menu_details
|
| 22 |
|
| 23 |
+
# Text-to-speech response
|
| 24 |
+
def generate_tts_response(text):
|
| 25 |
+
communicate = edge_tts.Communicate(text)
|
| 26 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 27 |
+
tmp_path = tmp_file.name
|
| 28 |
+
asyncio.run(communicate.save(tmp_path))
|
| 29 |
+
return tmp_path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Process user audio input and respond
|
| 32 |
+
def process_audio(audio_file, preference):
|
| 33 |
+
if "menu" in preference.lower():
|
| 34 |
+
menu_details = filter_menu("All")
|
| 35 |
+
response_text = f"Here are the menu details: {menu_details}"
|
| 36 |
+
elif "veg samosa" in preference.lower():
|
| 37 |
+
response_text = "Order confirmed for Veg Samosa. It has been added to your cart."
|
| 38 |
else:
|
| 39 |
+
response_text = "I'm sorry, I didn't understand that. Please try again."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
tts_path = generate_tts_response(response_text)
|
| 42 |
+
return tts_path, response_text
|
| 43 |
|
| 44 |
+
# Build Gradio app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def app():
|
|
|
|
|
|
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# Welcome to the Menu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Audio input and output
|
| 50 |
+
audio_input = gr.Audio(label="Speak your preference or order", type="filepath")
|
| 51 |
+
audio_output = gr.Audio(label="Assistant Response", autoplay=True)
|
|
|
|
| 52 |
|
| 53 |
+
# Textbox for transcription
|
| 54 |
+
transcription = gr.Textbox(label="Transcription", placeholder="Detected text will appear here")
|
| 55 |
|
| 56 |
+
# Cart output
|
| 57 |
+
cart_output = gr.Textbox(label="Cart", placeholder="Your cart details will appear here")
|
| 58 |
+
|
| 59 |
+
# Audio processing
|
| 60 |
audio_input.change(
|
| 61 |
+
process_audio,
|
| 62 |
+
inputs=[audio_input, transcription],
|
| 63 |
+
outputs=[audio_output, transcription]
|
| 64 |
)
|
| 65 |
|
| 66 |
return demo
|
| 67 |
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
demo = app()
|
| 70 |
+
demo.launch()
|