geethareddy commited on
Commit
a054557
·
verified ·
1 Parent(s): 79b737e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -172
app.py CHANGED
@@ -1,106 +1,7 @@
 
1
  import gradio as gr
2
  import pandas as pd
3
- import edge_tts
4
- import asynciimport gradio as gr
5
- from gtts import gTTS
6
- import os
7
- import tempfile
8
- import json
9
- import speech_recognition as sr
10
-
11
- # Store cart in a temporary storage
12
- cart = []
13
-
14
- # Define the menu items dynamically
15
- menu_items = {
16
- "Pizza": 10.99,
17
- "Burger": 8.49,
18
- "Pasta": 12.99,
19
- "Salad": 7.99,
20
- "Soda": 2.49
21
- }
22
-
23
- def generate_voice_response(text):
24
- tts = gTTS(text)
25
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
26
- temp_file.close()
27
- tts.save(temp_file.name)
28
- return temp_file.name
29
-
30
- def calculate_total(cart):
31
- return sum(menu_items[item] for item in cart)
32
-
33
- def restaurant_voice_assistant(audio, state_json):
34
- global cart
35
- state = json.loads(state_json) if state_json else {}
36
- response = ""
37
- voice_path = None
38
-
39
- # Convert audio input to text
40
- if audio:
41
- recognizer = sr.Recognizer()
42
- with sr.AudioFile(audio) as source:
43
- try:
44
- input_text = recognizer.recognize_google(recognizer.record(source))
45
- except sr.UnknownValueError:
46
- input_text = ""
47
- else:
48
- input_text = ""
49
-
50
- if not state.get("menu_shown", False):
51
- # Show menu dynamically
52
- response = "Welcome to our restaurant! Here is our menu:\n"
53
- for item, price in menu_items.items():
54
- response += f"{item}: ${price:.2f}\n"
55
- response += "\nPlease tell me the item you would like to add to your cart."
56
- state["menu_shown"] = True
57
- elif any(item.lower() in input_text.lower() for item in menu_items):
58
- # Check if input matches a menu item
59
- for item in menu_items:
60
- if item.lower() in input_text.lower():
61
- cart.append(item)
62
- total = calculate_total(cart)
63
- response = f"{item} has been added to your cart. Your current cart includes:\n"
64
- for cart_item in cart:
65
- response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n"
66
- response += f"\nTotal: ${total:.2f}. Would you like to add anything else?"
67
- break
68
- elif "menu" in input_text.lower():
69
- response = "Here is our menu again:\n"
70
- for item, price in menu_items.items():
71
- response += f"{item}: ${price:.2f}\n"
72
- response += "\nWhat would you like to add to your cart?"
73
- elif "final order" in input_text.lower() or "submit order" in input_text.lower():
74
- if cart:
75
- total = calculate_total(cart)
76
- response = "Your final order includes:\n"
77
- for item in cart:
78
- response += f"- {item}: ${menu_items[item]:.2f}\n"
79
- response += f"\nTotal: ${total:.2f}.\nThank you for ordering!"
80
- cart = [] # Clear cart after finalizing order
81
- else:
82
- response = "Your cart is empty. Would you like to order something?"
83
- else:
84
- response = "I didn’t quite catch that. Please tell me what you’d like to order."
85
-
86
- voice_path = generate_voice_response(response)
87
- return response, voice_path, json.dumps(state)
88
-
89
- with gr.Blocks() as demo:
90
- state = gr.State(value=json.dumps({}))
91
-
92
- with gr.Row():
93
- user_audio = gr.Audio(type="filepath", label="Your Voice Input")
94
- output_text = gr.Textbox(label="Response Text")
95
-
96
- with gr.Row():
97
- voice_output = gr.Audio(label="Response Audio", autoplay=True)
98
-
99
- # Automatically process audio when recording stops
100
- user_audio.change(restaurant_voice_assistant, inputs=[user_audio, state], outputs=[output_text, voice_output, state])
101
-
102
- demo.launch()
103
- o
104
  import tempfile
105
 
106
  # Load menu data
@@ -111,91 +12,74 @@ def load_menu():
111
  except Exception as e:
112
  raise ValueError(f"Error loading menu file: {e}")
113
 
 
 
 
 
 
 
 
 
114
  # Generate TTS response
115
- async def generate_tts_response(text):
116
- communicate = edge_tts.Communicate(text)
117
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
118
  tmp_path = tmp_file.name
119
- await communicate.save(tmp_path)
120
  return tmp_path
121
 
122
- # Global cart to store orders
123
- cart = []
124
- menu_data = load_menu()
125
-
126
- # Add item to cart
127
- def add_to_cart(item_name):
128
- global cart
129
- item = menu_data[menu_data["Dish Name"].str.lower() == item_name.lower()]
130
- if not item.empty:
131
- cart.append({"Dish Name": item.iloc[0]["Dish Name"], "Price": item.iloc[0]["Price ($)"]})
132
- return f"{item_name} has been added to your cart."
133
- return f"{item_name} is not available on the menu."
134
-
135
- # Remove item from cart
136
- def remove_from_cart(item_name):
137
- global cart
138
- for i, item in enumerate(cart):
139
- if item["Dish Name"].lower() == item_name.lower():
140
- cart.pop(i)
141
- return f"{item_name} has been removed from your cart."
142
- return f"{item_name} is not in your cart."
143
-
144
- # Get cart summary and total bill
145
- def get_cart_summary():
146
- global cart
147
- if not cart:
148
- return "Your cart is empty.", 0
149
- summary = ", ".join(f"{item['Dish Name']} (${item['Price']})" for item in cart)
150
- total = sum(item["Price"] for item in cart)
151
- return summary, total
152
-
153
  # Process customer input
154
- async def process_customer_input(audio_file):
155
- # Simulated transcription (Replace with actual transcription logic)
156
- transcription = "veg samosa" # Replace with transcription logic
157
-
158
- if "menu" in transcription.lower():
159
- menu_details = "Here is our menu: " + ", ".join(
160
- f"{item['Dish Name']} - ${item['Price ($)']}" for _, item in menu_data.iterrows()
161
- )
162
- tts_path = await generate_tts_response(menu_details)
163
- return tts_path, menu_details
164
-
165
- elif "remove" in transcription.lower():
166
- item_name = transcription.replace("remove", "").strip()
167
- response_text = remove_from_cart(item_name)
168
- tts_path = await generate_tts_response(response_text)
169
- return tts_path, response_text
170
-
 
 
 
 
 
171
  else:
172
- response_text = add_to_cart(transcription)
173
- tts_path = await generate_tts_response(response_text)
174
- return tts_path, response_text
 
175
 
176
- # Build Gradio app
177
  def app():
178
- with gr.Blocks() as demo:
179
- gr.Markdown("# Welcome to the Biryani Hub")
180
 
181
- # Audio input and output
182
- customer_audio_input = gr.Audio(label="Click to speak your order", type="filepath")
183
- customer_audio_output = gr.Audio(label="Assistant Response", autoplay=True)
184
 
185
- # Cart and total bill outputs
 
 
 
186
  cart_output = gr.Textbox(label="Cart", placeholder="Your cart details will appear here")
187
- total_bill_output = gr.Textbox(label="Total Bill", placeholder="Your total bill will appear here")
188
 
189
- # Process customer input logic
190
- async def handle_customer_audio(audio):
191
- tts_path, response_text = await process_customer_input(audio)
192
- cart_summary, total_bill = get_cart_summary()
193
- return tts_path, response_text, cart_summary, f"${total_bill}"
194
 
195
- customer_audio_input.change(
196
- lambda audio: asyncio.run(handle_customer_audio(audio)),
197
- inputs=[customer_audio_input],
198
- outputs=[customer_audio_output, gr.Textbox(), cart_output, total_bill_output],
199
  )
200
 
201
  return demo
 
1
+ import asyncio
2
  import gradio as gr
3
  import pandas as pd
4
+ from edge_tts import Communicate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import tempfile
6
 
7
  # Load menu data
 
12
  except Exception as e:
13
  raise ValueError(f"Error loading menu file: {e}")
14
 
15
+ # Handle menu inquiries
16
+ def handle_menu_request():
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
+ return menu_details
22
+
23
  # Generate TTS response
24
+ def generate_tts_response(text):
25
+ communicate = 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 customer input
32
+ def process_customer_input(input_text, cart):
33
+ response_text = ""
34
+ menu_data = load_menu()
35
+ if "menu" in input_text.lower():
36
+ response_text = "Here is the menu:\n" + handle_menu_request()
37
+ elif any(dish.lower() in input_text.lower() for dish in menu_data['Dish Name']):
38
+ dish_name = next(dish for dish in menu_data['Dish Name'] if dish.lower() in input_text.lower())
39
+ cart.append(dish_name)
40
+ response_text = f"{dish_name} has been added to your cart."
41
+ elif "remove" in input_text.lower():
42
+ for dish in cart:
43
+ if dish.lower() in input_text.lower():
44
+ cart.remove(dish)
45
+ response_text = f"{dish} has been removed from your cart."
46
+ break
47
+ else:
48
+ response_text = "Item not found in cart to remove."
49
+ elif "cart" in input_text.lower():
50
+ if cart:
51
+ response_text = "Your cart contains:\n" + "\n".join(cart)
52
+ else:
53
+ response_text = "Your cart is empty."
54
  else:
55
+ response_text = "I'm sorry, I didn't understand that. Please try again."
56
+
57
+ tts_path = generate_tts_response(response_text)
58
+ return tts_path, response_text, cart
59
 
60
+ # Gradio app
61
  def app():
62
+ cart = []
 
63
 
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown("# Welcome to the Menu")
 
66
 
67
+ # Input and output components
68
+ audio_input = gr.Audio(source="microphone", type="filepath", label="Speak your preference or order")
69
+ audio_output = gr.Audio(label="Assistant Response", autoplay=True)
70
+ transcription = gr.Textbox(label="Transcription", placeholder="Detected text will appear here")
71
  cart_output = gr.Textbox(label="Cart", placeholder="Your cart details will appear here")
 
72
 
73
+ # Define interaction
74
+ def handle_interaction(audio_file):
75
+ input_text = "" # Placeholder for transcription logic
76
+ tts_path, response_text, updated_cart = process_customer_input(input_text, cart)
77
+ return tts_path, response_text, "\n".join(updated_cart)
78
 
79
+ audio_input.change(
80
+ handle_interaction,
81
+ inputs=[audio_input],
82
+ outputs=[audio_output, transcription, cart_output]
83
  )
84
 
85
  return demo