dschandra commited on
Commit
408a4d8
·
verified ·
1 Parent(s): 5f81440

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +273 -0
app.py ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
14
+
15
+ # HTML code for the frontend
16
+ html_code = """
17
+ <!DOCTYPE html>
18
+ <html lang="en">
19
+ <head>
20
+ <meta charset="UTF-8">
21
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
22
+ <title>AI Dining Assistant</title>
23
+ <style>
24
+ body {
25
+ font-family: Arial, sans-serif;
26
+ background-color: #f4f4f9;
27
+ display: flex;
28
+ flex-direction: column;
29
+ align-items: center;
30
+ justify-content: center;
31
+ height: 100vh;
32
+ margin: 0;
33
+ }
34
+
35
+ h1 {
36
+ color: #333;
37
+ }
38
+
39
+ .mic-button {
40
+ width: 80px;
41
+ height: 80px;
42
+ border-radius: 50%;
43
+ background-color: #007bff;
44
+ color: white;
45
+ font-size: 24px;
46
+ border: none;
47
+ display: flex;
48
+ align-items: center;
49
+ justify-content: center;
50
+ cursor: pointer;
51
+ box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
52
+ transition: background-color 0.3s;
53
+ }
54
+
55
+ .mic-button:hover {
56
+ background-color: #0056b3;
57
+ }
58
+
59
+ .status {
60
+ margin-top: 20px;
61
+ font-size: 18px;
62
+ color: #666;
63
+ }
64
+
65
+ .listening {
66
+ color: green;
67
+ font-weight: bold;
68
+ }
69
+
70
+ .response {
71
+ margin-top: 20px;
72
+ padding: 10px;
73
+ background-color: #fff;
74
+ border: 1px solid #ddd;
75
+ border-radius: 5px;
76
+ box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
77
+ width: 300px;
78
+ text-align: center;
79
+ }
80
+ </style>
81
+ </head>
82
+ <body>
83
+ <h1>AI Dining Assistant</h1>
84
+ <button class="mic-button" id="mic-button">🎤</button>
85
+ <div class="status" id="status">Press the mic button to start listening...</div>
86
+ <div class="response" id="response" style="display: none;">Response will appear here...</div>
87
+
88
+ <script>
89
+ const micButton = document.getElementById('mic-button');
90
+ const status = document.getElementById('status');
91
+ const response = document.getElementById('response');
92
+
93
+ if (!window.MediaRecorder) {
94
+ alert("Your browser does not support audio recording.");
95
+ }
96
+
97
+ let mediaRecorder;
98
+ let audioChunks = [];
99
+
100
+ micButton.addEventListener('click', async () => {
101
+ navigator.mediaDevices.getUserMedia({ audio: true })
102
+ .then(stream => {
103
+ mediaRecorder = new MediaRecorder(stream);
104
+ mediaRecorder.start();
105
+ status.textContent = 'Listening...';
106
+ status.classList.add('listening');
107
+
108
+ mediaRecorder.ondataavailable = event => {
109
+ audioChunks.push(event.data);
110
+ };
111
+
112
+ mediaRecorder.onstop = async () => {
113
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
114
+ const formData = new FormData();
115
+ formData.append('audio', audioBlob);
116
+
117
+ status.textContent = 'Processing...';
118
+ status.classList.remove('listening');
119
+
120
+ try {
121
+ const result = await fetch('/process-audio', {
122
+ method: 'POST',
123
+ body: formData,
124
+ });
125
+
126
+ const data = await result.json();
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';
133
+ status.textContent = 'Press the mic button to start listening...';
134
+ }
135
+ };
136
+
137
+ setTimeout(() => {
138
+ mediaRecorder.stop();
139
+ }, 5000); // Stop recording after 5 seconds
140
+ })
141
+ .catch(err => {
142
+ status.textContent = 'Microphone access denied.';
143
+ });
144
+ });
145
+ </script>
146
+ </body>
147
+ </html>
148
+ """
149
+
150
+ @app.route('/')
151
+ def index():
152
+ return render_template_string(html_code)
153
+
154
+ @app.route('/process-audio', methods=['POST'])
155
+ def process_audio():
156
+ try:
157
+ audio_file = request.files['audio']
158
+ recognizer = sr.Recognizer()
159
+ with sr.AudioFile(audio_file) as source:
160
+ audio_data = recognizer.record(source)
161
+ command = recognizer.recognize_google(audio_data)
162
+ response = process_command(command)
163
+ return jsonify({"response": response})
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)