geethareddy commited on
Commit
24b4bee
·
verified ·
1 Parent(s): 0e2779f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -43
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
- # Process user audio input and respond
16
- def process_audio(audio_file, transcription):
17
  menu_data = load_menu()
18
- cart = []
19
- response_text = ""
20
-
21
- if "menu" in transcription.lower():
22
- menu_details = "\n".join(
23
- [f"{item['Dish Name']} - ${item['Price ($)']}" for _, item in menu_data.iterrows()]
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
- # Audio input and output
60
- audio_input = gr.Audio(type="filepath", label="Speak your preference or order")
61
- audio_output = gr.Audio(label="Assistant Response", autoplay=True)
62
-
63
- # Textbox for transcription
64
- transcription = gr.Textbox(label="Transcription", placeholder="Detected text will appear here")
65
 
66
- # Cart output
67
- cart_output = gr.Textbox(label="Cart", placeholder="Your cart details will appear here")
68
- total_bill_output = gr.Textbox(label="Total Bill", placeholder="Your total bill will appear here")
69
 
70
- # Audio processing
71
- audio_input.change(
72
- process_audio,
73
- inputs=[audio_input, transcription],
74
- outputs=[audio_output, transcription, cart_output, total_bill_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