Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,79 +12,91 @@ def load_menu():
|
|
| 12 |
except Exception as e:
|
| 13 |
raise ValueError(f"Error loading menu file: {e}")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
def generate_tts_response(text):
|
| 17 |
communicate = edge_tts.Communicate(text)
|
| 18 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 19 |
tmp_path = tmp_file.name
|
| 20 |
-
|
| 21 |
return tmp_path
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
cart =
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
global cart
|
| 27 |
-
menu_data =
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
for _, item in menu_data.iterrows():
|
| 33 |
-
response_text += f"{item['Dish Name']} - ${item['Price ($)']}. "
|
| 34 |
-
|
| 35 |
-
elif any(item['Dish Name'].lower() in user_input.lower() for _, item in menu_data.iterrows()):
|
| 36 |
-
for _, item in menu_data.iterrows():
|
| 37 |
-
if item['Dish Name'].lower() in user_input.lower():
|
| 38 |
-
dish_name = item['Dish Name']
|
| 39 |
-
price = item['Price ($)']
|
| 40 |
-
if dish_name in cart:
|
| 41 |
-
cart[dish_name]['quantity'] += 1
|
| 42 |
-
else:
|
| 43 |
-
cart[dish_name] = {"price": price, "quantity": 1}
|
| 44 |
-
response_text = f"Order confirmed for {dish_name}. It has been added to your cart."
|
| 45 |
-
break
|
| 46 |
-
|
| 47 |
-
elif "remove" in user_input.lower():
|
| 48 |
-
for dish_name in list(cart.keys()):
|
| 49 |
-
if dish_name.lower() in user_input.lower():
|
| 50 |
-
del cart[dish_name]
|
| 51 |
-
response_text = f"{dish_name} has been removed from your cart."
|
| 52 |
-
break
|
| 53 |
-
else:
|
| 54 |
-
response_text = "The item you want to remove is not in the cart."
|
| 55 |
-
|
| 56 |
-
elif "cart" in user_input.lower():
|
| 57 |
-
if cart:
|
| 58 |
-
response_text = "Your cart contains: "
|
| 59 |
-
total = 0
|
| 60 |
-
for dish_name, details in cart.items():
|
| 61 |
-
response_text += f"{dish_name} (x{details['quantity']}) - ${details['price'] * details['quantity']}. "
|
| 62 |
-
total += details['price'] * details['quantity']
|
| 63 |
-
response_text += f"Total: ${total}"
|
| 64 |
-
else:
|
| 65 |
-
response_text = "Your cart is empty."
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Build Gradio app
|
| 74 |
def app():
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
gr.Markdown("# Welcome to the Biryani Hub")
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
outputs=[audio_output, response_text]
|
| 88 |
)
|
| 89 |
|
| 90 |
return demo
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
raise ValueError(f"Error loading menu file: {e}")
|
| 14 |
|
| 15 |
+
# Generate TTS response
|
| 16 |
+
async def generate_tts_response(text):
|
| 17 |
communicate = edge_tts.Communicate(text)
|
| 18 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 19 |
tmp_path = tmp_file.name
|
| 20 |
+
await communicate.save(tmp_path)
|
| 21 |
return tmp_path
|
| 22 |
|
| 23 |
+
# Global cart to store orders
|
| 24 |
+
cart = []
|
| 25 |
+
menu_data = load_menu()
|
| 26 |
+
|
| 27 |
+
# Add item to cart
|
| 28 |
+
def add_to_cart(item_name):
|
| 29 |
global cart
|
| 30 |
+
item = menu_data[menu_data["Dish Name"].str.lower() == item_name.lower()]
|
| 31 |
+
if not item.empty:
|
| 32 |
+
cart.append({"Dish Name": item.iloc[0]["Dish Name"], "Price": item.iloc[0]["Price ($)"]})
|
| 33 |
+
return f"{item_name} has been added to your cart."
|
| 34 |
+
return f"{item_name} is not available on the menu."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Remove item from cart
|
| 37 |
+
def remove_from_cart(item_name):
|
| 38 |
+
global cart
|
| 39 |
+
for i, item in enumerate(cart):
|
| 40 |
+
if item["Dish Name"].lower() == item_name.lower():
|
| 41 |
+
cart.pop(i)
|
| 42 |
+
return f"{item_name} has been removed from your cart."
|
| 43 |
+
return f"{item_name} is not in your cart."
|
| 44 |
+
|
| 45 |
+
# Get cart summary and total bill
|
| 46 |
+
def get_cart_summary():
|
| 47 |
+
global cart
|
| 48 |
+
if not cart:
|
| 49 |
+
return "Your cart is empty.", 0
|
| 50 |
+
summary = ", ".join(f"{item['Dish Name']} (${item['Price']})" for item in cart)
|
| 51 |
+
total = sum(item["Price"] for item in cart)
|
| 52 |
+
return summary, total
|
| 53 |
+
|
| 54 |
+
# Process customer input
|
| 55 |
+
async def process_customer_input(audio_file):
|
| 56 |
+
# Simulated transcription (Replace with actual transcription logic)
|
| 57 |
+
transcription = "veg samosa" # Replace with transcription logic
|
| 58 |
|
| 59 |
+
if "menu" in transcription.lower():
|
| 60 |
+
menu_details = "Here is our menu: " + ", ".join(
|
| 61 |
+
f"{item['Dish Name']} - ${item['Price ($)']}" for _, item in menu_data.iterrows()
|
| 62 |
+
)
|
| 63 |
+
tts_path = await generate_tts_response(menu_details)
|
| 64 |
+
return tts_path, menu_details
|
| 65 |
+
|
| 66 |
+
elif "remove" in transcription.lower():
|
| 67 |
+
item_name = transcription.replace("remove", "").strip()
|
| 68 |
+
response_text = remove_from_cart(item_name)
|
| 69 |
+
tts_path = await generate_tts_response(response_text)
|
| 70 |
+
return tts_path, response_text
|
| 71 |
+
|
| 72 |
+
else:
|
| 73 |
+
response_text = add_to_cart(transcription)
|
| 74 |
+
tts_path = await generate_tts_response(response_text)
|
| 75 |
+
return tts_path, response_text
|
| 76 |
|
| 77 |
# Build Gradio app
|
| 78 |
def app():
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
gr.Markdown("# Welcome to the Biryani Hub")
|
| 81 |
|
| 82 |
+
# Audio input and output
|
| 83 |
+
customer_audio_input = gr.Audio(label="Click to speak your order", type="filepath")
|
| 84 |
+
customer_audio_output = gr.Audio(label="Assistant Response", autoplay=True)
|
| 85 |
+
|
| 86 |
+
# Cart and total bill outputs
|
| 87 |
+
cart_output = gr.Textbox(label="Cart", placeholder="Your cart details will appear here")
|
| 88 |
+
total_bill_output = gr.Textbox(label="Total Bill", placeholder="Your total bill will appear here")
|
| 89 |
+
|
| 90 |
+
# Process customer input logic
|
| 91 |
+
async def handle_customer_audio(audio):
|
| 92 |
+
tts_path, response_text = await process_customer_input(audio)
|
| 93 |
+
cart_summary, total_bill = get_cart_summary()
|
| 94 |
+
return tts_path, response_text, cart_summary, f"${total_bill}"
|
| 95 |
|
| 96 |
+
customer_audio_input.change(
|
| 97 |
+
lambda audio: asyncio.run(handle_customer_audio(audio)),
|
| 98 |
+
inputs=[customer_audio_input],
|
| 99 |
+
outputs=[customer_audio_output, gr.Textbox(), cart_output, total_bill_output],
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
return demo
|