geethareddy commited on
Commit
a6d8b09
·
verified ·
1 Parent(s): 1ceb718

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -31
app.py CHANGED
@@ -20,31 +20,57 @@ async def generate_tts_response(text):
20
  await communicate.save(tmp_path)
21
  return tmp_path
22
 
23
- # Welcome Note Handler
24
- async def play_welcome_note():
25
- welcome_text = "Hello, welcome to Biryani Hub. What do you want to order?"
26
- return await generate_tts_response(welcome_text), welcome_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Process customer input
29
  async def process_customer_input(audio_file):
30
  # Simulated transcription (Replace with actual transcription logic)
31
- transcription = "menu" # This should be replaced with audio transcription logic
32
 
33
  if "menu" in transcription.lower():
34
- menu_data = load_menu()
35
  menu_details = "Here is our menu: " + ", ".join(
36
  f"{item['Dish Name']} - ${item['Price ($)']}" for _, item in menu_data.iterrows()
37
  )
38
  tts_path = await generate_tts_response(menu_details)
39
  return tts_path, menu_details
40
 
41
- elif "veg samosa" in transcription.lower():
42
- response_text = "Order confirmed for Veg Samosa. It has been added to your cart."
 
43
  tts_path = await generate_tts_response(response_text)
44
  return tts_path, response_text
45
 
46
  else:
47
- response_text = "I'm sorry, I didn't understand that. Please try again."
48
  tts_path = await generate_tts_response(response_text)
49
  return tts_path, response_text
50
 
@@ -53,39 +79,24 @@ def app():
53
  with gr.Blocks() as demo:
54
  gr.Markdown("# Welcome to the Menu")
55
 
56
- # Welcome note audio output
57
- welcome_audio_output = gr.Audio(label="Welcome Note", autoplay=True)
58
-
59
- # Customer audio input and response
60
  customer_audio_input = gr.Audio(label="Click to speak your order", type="filepath")
61
  customer_audio_output = gr.Audio(label="Assistant Response", autoplay=True)
62
- transcription_output = gr.Textbox(label="Transcription", placeholder="Detected text will appear here")
63
 
64
- # Cart details
65
  cart_output = gr.Textbox(label="Cart", placeholder="Your cart details will appear here")
 
66
 
67
- # Play welcome note
68
- def play_welcome():
69
- tts_path, welcome_text = asyncio.run(play_welcome_note())
70
- return tts_path, welcome_text
71
-
72
- # Process customer input
73
  async def handle_customer_audio(audio):
74
  tts_path, response_text = await process_customer_input(audio)
75
- return tts_path, response_text
 
76
 
77
- # Welcome button logic
78
- gr.Button("Play Welcome Note").click(
79
- play_welcome,
80
- inputs=[],
81
- outputs=[welcome_audio_output, transcription_output],
82
- )
83
-
84
- # Process customer input logic
85
  customer_audio_input.change(
86
  lambda audio: asyncio.run(handle_customer_audio(audio)),
87
  inputs=[customer_audio_input],
88
- outputs=[customer_audio_output, transcription_output],
89
  )
90
 
91
  return demo
 
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
 
 
79
  with gr.Blocks() as demo:
80
  gr.Markdown("# Welcome to the Menu")
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