dschandra commited on
Commit
26eed15
·
verified ·
1 Parent(s): e5d21e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -106
app.py CHANGED
@@ -1,13 +1,8 @@
1
- import pyttsx3
2
- import speech_recognition as sr
3
- from datetime import datetime
4
  from flask import Flask, render_template_string, request, jsonify
 
5
 
6
  app = Flask(__name__)
7
 
8
- # Initialize text-to-speech engine
9
- engine = pyttsx3.init()
10
-
11
  # Initialize an empty list to store orders
12
  orders = []
13
  user_preferences = {"diet": "all"} # Default to all
@@ -127,6 +122,10 @@ html_code = """
127
  response.textContent = data.response;
128
  response.style.display = 'block';
129
  status.textContent = 'Press the mic button to start listening...';
 
 
 
 
130
  } catch (error) {
131
  response.textContent = 'Error occurred. Please try again.';
132
  response.style.display = 'block';
@@ -164,110 +163,15 @@ def process_audio():
164
  except Exception as e:
165
  return jsonify({"response": f"An error occurred: {str(e)}"})
166
 
167
- def speak_text(text):
168
- """Speak the provided text."""
169
- engine.say(text)
170
- engine.runAndWait()
171
-
172
- def get_greeting():
173
- """Return a greeting based on the current time."""
174
- current_hour = datetime.now().hour
175
- if current_hour < 12:
176
- return "Good morning!"
177
- elif 12 <= current_hour < 18:
178
- return "Good afternoon!"
179
- else:
180
- return "Good evening!"
181
-
182
- def filter_menu(menu):
183
- """Filter menu based on user preferences."""
184
- filtered_menu = {}
185
- for dish, details in menu.items():
186
- if user_preferences["diet"] in details["type"] or user_preferences["diet"] == "all":
187
- filtered_menu[dish] = details
188
- return filtered_menu
189
-
190
  def process_command(command):
191
  """Process the user's voice command and return a response."""
192
- global orders, user_preferences
193
  command = command.lower()
194
-
195
- # Menu options
196
- menu = {
197
- # Veg items
198
- "paneer butter masala": {"type": "veg"},
199
- "dal makhani": {"type": "veg"},
200
- "masala dosa": {"type": "veg"},
201
- "idli sambhar": {"type": "veg"},
202
- "fried rice": {"type": "veg"},
203
- "hakka noodles": {"type": "veg"},
204
- # Non-veg items
205
- "butter chicken": {"type": "non-veg"},
206
- "hyderabadi biryani": {"type": "non-veg"},
207
- "chilli chicken": {"type": "non-veg"},
208
- "fish curry": {"type": "non-veg"},
209
- "tandoori chicken": {"type": "non-veg"},
210
- "prawns masala": {"type": "non-veg"},
211
- # Drinks
212
- "masala chai": {"type": "all"},
213
- "lassi": {"type": "all"},
214
- "cold coffee": {"type": "all"},
215
- "fresh lime soda": {"type": "all"}
216
- }
217
-
218
- # Handle preferences
219
- if "veg" in command or "vegetarian" in command:
220
- user_preferences["diet"] = "veg"
221
- return "Vegetarian preference set."
222
- elif "non-veg" in command or "non-vegetarian" in command:
223
- user_preferences["diet"] = "non-veg"
224
- return "Non-vegetarian preference set."
225
- elif "mix" in command:
226
- user_preferences["diet"] = "all"
227
- return "Mixed preference set."
228
-
229
- # Filtered menu based on preferences
230
- filtered_menu = filter_menu(menu)
231
-
232
- # Show menu
233
  if "menu" in command:
234
- if filtered_menu:
235
- return "Our menu includes: " + ", ".join(filtered_menu.keys())
236
- else:
237
- return "No items match your preferences."
238
-
239
- # Add to order
240
- elif "order" in command or "add" in command:
241
- for item in filtered_menu:
242
- if item in command:
243
- orders.append(item)
244
- return f"{item} has been added to your order. Anything else you'd like to add?"
245
- return "I'm sorry, I couldn't recognize the dish. Please try again."
246
-
247
- # Remove from order
248
- elif "remove" in command:
249
- for item in orders:
250
- if item in command:
251
- orders.remove(item)
252
- return f"{item} has been removed from your order. Anything else you'd like to modify?"
253
- return "I'm sorry, I couldn't recognize the dish to remove. Please try again."
254
-
255
- # Show order
256
- elif "show order" in command or "what's my order" in command:
257
- if orders:
258
- return f"Your current order includes: {', '.join(orders)}."
259
- else:
260
- return "You haven't ordered anything yet."
261
-
262
- # Thank you and exit
263
- elif "thank you" in command or "bye" in command:
264
- if orders:
265
- return f"Thank you for your order! You have ordered: {', '.join(orders)}. Your food will arrive shortly. Have a great day!"
266
- else:
267
- return "Thank you! Your food will arrive shortly. Have a great day!"
268
-
269
- else:
270
- return "I'm sorry, I don't understand. Could you please repeat?"
271
 
272
  if __name__ == "__main__":
273
  app.run(debug=True, port=5000)
 
 
 
 
1
  from flask import Flask, render_template_string, request, jsonify
2
+ from datetime import datetime
3
 
4
  app = Flask(__name__)
5
 
 
 
 
6
  # Initialize an empty list to store orders
7
  orders = []
8
  user_preferences = {"diet": "all"} # Default to all
 
122
  response.textContent = data.response;
123
  response.style.display = 'block';
124
  status.textContent = 'Press the mic button to start listening...';
125
+
126
+ // Use browser text-to-speech
127
+ const utterance = new SpeechSynthesisUtterance(data.response);
128
+ speechSynthesis.speak(utterance);
129
  } catch (error) {
130
  response.textContent = 'Error occurred. Please try again.';
131
  response.style.display = 'block';
 
163
  except Exception as e:
164
  return jsonify({"response": f"An error occurred: {str(e)}"})
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  def process_command(command):
167
  """Process the user's voice command and return a response."""
168
+ global orders
169
  command = command.lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  if "menu" in command:
171
+ return "Our menu includes paneer butter masala, fried rice, and cold coffee."
172
+ elif "order" in command:
173
+ return "Your order has been placed."
174
+ return "Sorry, I didn't understand your request."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  if __name__ == "__main__":
177
  app.run(debug=True, port=5000)