Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,36 +12,15 @@ def load_menu():
|
|
| 12 |
except Exception as e:
|
| 13 |
raise ValueError(f"Error loading menu file: {e}")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
def
|
| 17 |
menu_data = load_menu()
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
menu_details = "\n"
|
| 23 |
-
|
| 24 |
-
)
|
| 25 |
-
response_text = f"Here is our menu:\n{menu_details}"
|
| 26 |
-
elif any(dish.lower() in transcription.lower() for dish in menu_data['Dish Name']):
|
| 27 |
-
for _, item in menu_data.iterrows():
|
| 28 |
-
if item['Dish Name'].lower() in transcription.lower():
|
| 29 |
-
cart.append(f"{item['Dish Name']} - ${item['Price ($)']}")
|
| 30 |
-
response_text = f"Order confirmed for {item['Dish Name']}. It has been added to your cart."
|
| 31 |
-
break
|
| 32 |
-
elif "remove" in transcription.lower():
|
| 33 |
-
if cart:
|
| 34 |
-
removed_item = cart.pop()
|
| 35 |
-
response_text = f"{removed_item} has been removed from your cart."
|
| 36 |
-
else:
|
| 37 |
-
response_text = "Your cart is empty."
|
| 38 |
-
else:
|
| 39 |
-
response_text = "I'm sorry, I didn't understand that. Please try again."
|
| 40 |
-
|
| 41 |
-
total_bill = sum(float(item.split("$")[1]) for item in cart)
|
| 42 |
-
tts_path = generate_tts_response(response_text)
|
| 43 |
-
|
| 44 |
-
return tts_path, response_text, "\n".join(cart), f"Total Bill: ${total_bill}"
|
| 45 |
|
| 46 |
# Text-to-speech response
|
| 47 |
def generate_tts_response(text):
|
|
@@ -51,27 +30,62 @@ def generate_tts_response(text):
|
|
| 51 |
asyncio.run(communicate.save(tmp_path))
|
| 52 |
return tmp_path
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Build Gradio app
|
| 55 |
def app():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
gr.Markdown("# Welcome to the Menu")
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Textbox for transcription
|
| 64 |
-
transcription = gr.Textbox(label="Transcription", placeholder="Detected text will appear here")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
cart_output = gr.Textbox(label="Cart"
|
| 68 |
-
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
inputs=
|
| 74 |
-
outputs=[audio_output,
|
| 75 |
)
|
| 76 |
|
| 77 |
return demo
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
raise ValueError(f"Error loading menu file: {e}")
|
| 14 |
|
| 15 |
+
# Generate menu details
|
| 16 |
+
def generate_menu_details():
|
| 17 |
menu_data = load_menu()
|
| 18 |
+
menu_details = ""
|
| 19 |
+
for _, item in menu_data.iterrows():
|
| 20 |
+
menu_details += f"{item['Dish Name']} - ${item['Price ($)']}\n"
|
| 21 |
+
menu_details += f"{item['Description']}\n"
|
| 22 |
+
menu_details += f"[Energy: {item['Energy (kcal)']} kcal, Protein: {item['Protein (g)']}g, Carbohydrates: {item['Carbohydrates (g)']}g, Fiber: {item['Fiber (g)']}g, Fat: {item['Fat (g)']}g, Sugar: {item['Sugar (g)']}g]\n\n"
|
| 23 |
+
return menu_details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Text-to-speech response
|
| 26 |
def generate_tts_response(text):
|
|
|
|
| 30 |
asyncio.run(communicate.save(tmp_path))
|
| 31 |
return tmp_path
|
| 32 |
|
| 33 |
+
# Process user input and respond
|
| 34 |
+
def process_user_input(preference, cart):
|
| 35 |
+
menu_data = load_menu()
|
| 36 |
+
response_text = ""
|
| 37 |
+
|
| 38 |
+
if "menu" in preference.lower():
|
| 39 |
+
response_text = "Here are the menu details:\n" + generate_menu_details()
|
| 40 |
+
else:
|
| 41 |
+
matched_item = menu_data[menu_data['Dish Name'].str.contains(preference, case=False, na=False)]
|
| 42 |
+
if not matched_item.empty:
|
| 43 |
+
dish_name = matched_item.iloc[0]['Dish Name']
|
| 44 |
+
price = matched_item.iloc[0]['Price ($)']
|
| 45 |
+
cart.append((dish_name, price))
|
| 46 |
+
response_text = f"Order confirmed for {dish_name}. It has been added to your cart."
|
| 47 |
+
elif "remove" in preference.lower():
|
| 48 |
+
item_to_remove = preference.lower().replace("remove", "").strip()
|
| 49 |
+
cart = [item for item in cart if item[0].lower() != item_to_remove]
|
| 50 |
+
response_text = f"Removed {item_to_remove} from your cart."
|
| 51 |
+
else:
|
| 52 |
+
response_text = "I'm sorry, I didn't understand that. Please try again."
|
| 53 |
+
|
| 54 |
+
tts_path = generate_tts_response(response_text)
|
| 55 |
+
return tts_path, response_text, cart
|
| 56 |
+
|
| 57 |
+
# Calculate total bill
|
| 58 |
+
def calculate_total(cart):
|
| 59 |
+
total = sum(item[1] for item in cart)
|
| 60 |
+
return f"Total: ${total:.2f}"
|
| 61 |
+
|
| 62 |
# Build Gradio app
|
| 63 |
def app():
|
| 64 |
+
cart = []
|
| 65 |
+
|
| 66 |
+
def handle_interaction(preference):
|
| 67 |
+
tts_path, response_text, updated_cart = process_user_input(preference, cart)
|
| 68 |
+
cart_display = "\n".join([f"{item[0]} - ${item[1]}" for item in updated_cart])
|
| 69 |
+
total_bill = calculate_total(updated_cart)
|
| 70 |
+
return tts_path, response_text, cart_display, total_bill
|
| 71 |
+
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
gr.Markdown("# Welcome to the Menu")
|
| 74 |
|
| 75 |
+
# Input and outputs
|
| 76 |
+
with gr.Row():
|
| 77 |
+
preference_input = gr.Textbox(label="Your Request", placeholder="Ask for menu or order items")
|
| 78 |
+
audio_output = gr.Audio(label="Assistant Response", autoplay=True)
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
transcription_output = gr.Textbox(label="Response Text")
|
| 81 |
+
cart_output = gr.Textbox(label="Cart")
|
| 82 |
+
total_output = gr.Textbox(label="Total Bill")
|
| 83 |
|
| 84 |
+
# Button for interaction
|
| 85 |
+
preference_input.submit(
|
| 86 |
+
handle_interaction,
|
| 87 |
+
inputs=preference_input,
|
| 88 |
+
outputs=[audio_output, transcription_output, cart_output, total_output],
|
| 89 |
)
|
| 90 |
|
| 91 |
return demo
|