spammi commited on
Commit
a3353cf
·
verified ·
1 Parent(s): d94e740

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +631 -16
app.py CHANGED
@@ -2,20 +2,27 @@ import gradio as gr
2
  from sentence_transformers import SentenceTransformer, util
3
  import openai
4
  import os
 
5
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
 
 
6
  # Initialize paths and model identifiers for easy configuration and maintenance
7
- filename = "output_topic_details.txt" # Path to the file storing chess-specific details
8
  retrieval_model_name = 'output/sentence-transformer-finetuned/'
9
- openai.api_key = os.environ["OPENAI_API_KEY"]
 
10
  system_message = "You are a restaurant recommending chatbot that suggests one restaurant in Seattle from the restaurant database based on the criteria the user provides."
 
11
  # Initial system message to set the behavior of the assistant
12
  messages = [{"role": "system", "content": system_message}]
13
- # Attempt to load the necessary models and provide feedback on success or failure
 
14
  try:
15
  retrieval_model = SentenceTransformer(retrieval_model_name)
16
  print("Models loaded successfully.")
17
  except Exception as e:
18
  print(f"Failed to load models: {e}")
 
19
  def load_and_preprocess_text(filename):
20
  """
21
  Load and preprocess text from a file, removing empty lines and stripping whitespace.
@@ -28,37 +35,34 @@ def load_and_preprocess_text(filename):
28
  except Exception as e:
29
  print(f"Failed to load or preprocess text: {e}")
30
  return []
 
31
  segments = load_and_preprocess_text(filename)
 
32
  def find_relevant_segment(user_query, segments):
33
  """
34
  Find the most relevant text segment for a user's query using cosine similarity among sentence embeddings.
35
  This version finds the best match based on the content of the query.
36
  """
37
  try:
38
- # Lowercase the query for better matching
39
  lower_query = user_query.lower()
40
- # Encode the query and the segments
41
  query_embedding = retrieval_model.encode(lower_query)
42
  segment_embeddings = retrieval_model.encode(segments)
43
- # Compute cosine similarities between the query and the segments
44
  similarities = util.pytorch_cos_sim(query_embedding, segment_embeddings)[0]
45
- # Find the index of the most similar segment
46
  best_idx = similarities.argmax()
47
- # Return the most relevant segment
48
  return segments[best_idx]
49
  except Exception as e:
50
  print(f"Error in finding relevant segment: {e}")
51
  return ""
 
52
  def generate_response(user_query, relevant_segment):
53
  """
54
  Generate a response emphasizing the bot's capability in suggesting a restaurant.
55
  """
56
  try:
57
  user_message = f"Here is a local restaurant based on your information: {relevant_segment}"
58
- # Append user's message to messages list
59
  messages.append({"role": "user", "content": user_message})
60
  response = openai.ChatCompletion.create(
61
- model="gpt-4o",
62
  messages=messages,
63
  max_tokens=150,
64
  temperature=0.2,
@@ -66,25 +70,636 @@ def generate_response(user_query, relevant_segment):
66
  frequency_penalty=0,
67
  presence_penalty=0
68
  )
69
- # Extract the response text
70
  output_text = response['choices'][0]['message']['content'].strip()
71
- # Append assistant's message to messages list for context
72
  messages.append({"role": "assistant", "content": output_text})
73
  return output_text
74
  except Exception as e:
75
  print(f"Error in generating response: {e}")
76
  return f"Error in generating response: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  def query_model(question):
78
  """
79
  Process a question, find relevant information, and generate a response.
80
  """
81
  if question == "":
82
  return "Give me your preferences..."
83
- relevant_segment = find_relevant_segment(question, segments)
84
- if not relevant_segment:
85
- return "Could not find specific information. Please refine your question."
86
- response = generate_response(question, relevant_segment)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  return response
 
88
  # Define the welcome message and specific topics the chatbot can provide information about
89
  welcome_message = """
90
  # Welcome to Ethical Eats Explorer!
 
2
  from sentence_transformers import SentenceTransformer, util
3
  import openai
4
  import os
5
+
6
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
7
+ openai.api_key = os.environ["OPENAI_API_KEY"]
8
+
9
  # Initialize paths and model identifiers for easy configuration and maintenance
10
+ filename = "output_topic_details.txt" # Path to the file storing restaurant-specific details
11
  retrieval_model_name = 'output/sentence-transformer-finetuned/'
12
+
13
+ # Initialize the system message for the chatbot
14
  system_message = "You are a restaurant recommending chatbot that suggests one restaurant in Seattle from the restaurant database based on the criteria the user provides."
15
+
16
  # Initial system message to set the behavior of the assistant
17
  messages = [{"role": "system", "content": system_message}]
18
+
19
+ # Load the SentenceTransformer model
20
  try:
21
  retrieval_model = SentenceTransformer(retrieval_model_name)
22
  print("Models loaded successfully.")
23
  except Exception as e:
24
  print(f"Failed to load models: {e}")
25
+
26
  def load_and_preprocess_text(filename):
27
  """
28
  Load and preprocess text from a file, removing empty lines and stripping whitespace.
 
35
  except Exception as e:
36
  print(f"Failed to load or preprocess text: {e}")
37
  return []
38
+
39
  segments = load_and_preprocess_text(filename)
40
+
41
  def find_relevant_segment(user_query, segments):
42
  """
43
  Find the most relevant text segment for a user's query using cosine similarity among sentence embeddings.
44
  This version finds the best match based on the content of the query.
45
  """
46
  try:
 
47
  lower_query = user_query.lower()
 
48
  query_embedding = retrieval_model.encode(lower_query)
49
  segment_embeddings = retrieval_model.encode(segments)
 
50
  similarities = util.pytorch_cos_sim(query_embedding, segment_embeddings)[0]
 
51
  best_idx = similarities.argmax()
 
52
  return segments[best_idx]
53
  except Exception as e:
54
  print(f"Error in finding relevant segment: {e}")
55
  return ""
56
+
57
  def generate_response(user_query, relevant_segment):
58
  """
59
  Generate a response emphasizing the bot's capability in suggesting a restaurant.
60
  """
61
  try:
62
  user_message = f"Here is a local restaurant based on your information: {relevant_segment}"
 
63
  messages.append({"role": "user", "content": user_message})
64
  response = openai.ChatCompletion.create(
65
+ model="gpt-4",
66
  messages=messages,
67
  max_tokens=150,
68
  temperature=0.2,
 
70
  frequency_penalty=0,
71
  presence_penalty=0
72
  )
 
73
  output_text = response['choices'][0]['message']['content'].strip()
 
74
  messages.append({"role": "assistant", "content": output_text})
75
  return output_text
76
  except Exception as e:
77
  print(f"Error in generating response: {e}")
78
  return f"Error in generating response: {e}"
79
+
80
+ # Define a sample list of restaurants (replace this with your actual data source)
81
+ restaurants = [
82
+ "name": "Harvest Beat",
83
+ "price": "High",
84
+ "cuisine": "American",
85
+ "gluten_free": true,
86
+ "vegan": true,
87
+ "lactose_intolerant": true,
88
+ "pescatarian": true,
89
+ "allergen_friendly": true,
90
+ "halal": false,
91
+ "kosher": false,
92
+ "vegetarian": true,
93
+ "website": "https://www.harvestbeat.com/"
94
+ },
95
+ {
96
+ "name": "Plum Bistro",
97
+ "price": "Moderate",
98
+ "cuisine": "American",
99
+ "gluten_free": true,
100
+ "vegan": true,
101
+ "lactose_intolerant": true,
102
+ "pescatarian": true,
103
+ "allergen_friendly": true,
104
+ "halal": false,
105
+ "kosher": false,
106
+ "vegetarian": true,
107
+ "website": "https://plumbistro.com/"
108
+ },
109
+ {
110
+ "name": "Portage Bay Cafe",
111
+ "price": "Low",
112
+ "cuisine": "American",
113
+ "gluten_free": true,
114
+ "vegan": true,
115
+ "lactose_intolerant": true,
116
+ "pescatarian": true,
117
+ "allergen_friendly": false,
118
+ "halal": false,
119
+ "kosher": false,
120
+ "vegetarian": true,
121
+ "website": "https://www.portagebaycafe.com/"
122
+ },
123
+ {
124
+ "name": "Duke's Seafood",
125
+ "price": "High",
126
+ "cuisine": "American",
127
+ "gluten_free": true,
128
+ "vegan": false,
129
+ "lactose_intolerant": true,
130
+ "pescatarian": true,
131
+ "allergen_friendly": true,
132
+ "halal": false,
133
+ "kosher": false,
134
+ "vegetarian": false,
135
+ "website": "https://www.dukesseafood.com/"
136
+ },
137
+ {
138
+ "name": "Olmstead",
139
+ "price": "Moderate",
140
+ "cuisine": "American",
141
+ "gluten_free": false,
142
+ "vegan": false,
143
+ "lactose_intolerant": false,
144
+ "pescatarian": false,
145
+ "allergen_friendly": false,
146
+ "halal": false,
147
+ "kosher": false,
148
+ "vegetarian": false,
149
+ "website": "https://www.olmsteadseattle.com/home"
150
+ },
151
+ {
152
+ "name": "Veggie Grill",
153
+ "price": "Low",
154
+ "cuisine": "American",
155
+ "gluten_free": true,
156
+ "vegan": true,
157
+ "lactose_intolerant": true,
158
+ "pescatarian": true,
159
+ "allergen_friendly": true,
160
+ "halal": false,
161
+ "kosher": false,
162
+ "vegetarian": true,
163
+ "website": "https://www.veggiegrill.com/menus/"
164
+ },
165
+ {
166
+ "name": "Cafe Flora",
167
+ "price": "Moderate",
168
+ "cuisine": "American",
169
+ "gluten_free": false,
170
+ "vegan": true,
171
+ "lactose_intolerant": false,
172
+ "pescatarian": true,
173
+ "allergen_friendly": false,
174
+ "halal": false,
175
+ "kosher": false,
176
+ "vegetarian": true,
177
+ "website": "https://florarestaurantgroup.com/"
178
+ },
179
+ {
180
+ "name": "Bounty Kitchen",
181
+ "price": "Low",
182
+ "cuisine": "American",
183
+ "gluten_free": true,
184
+ "vegan": true,
185
+ "lactose_intolerant": false,
186
+ "pescatarian": true,
187
+ "allergen_friendly": true,
188
+ "halal": false,
189
+ "kosher": false,
190
+ "vegetarian": true,
191
+ "website": "http://www.bountykitchenseattle.com/"
192
+ },
193
+ {
194
+ "name": "No Bones Beach Club",
195
+ "price": "Low",
196
+ "cuisine": "American",
197
+ "gluten_free": true,
198
+ "vegan": true,
199
+ "lactose_intolerant": false,
200
+ "pescatarian": true,
201
+ "allergen_friendly": true,
202
+ "halal": false,
203
+ "kosher": false,
204
+ "vegetarian": true,
205
+ "website": "https://nobonesbeachclub.com/menu/"
206
+ },
207
+ {
208
+ "name": "Chiho Bistro",
209
+ "price": "Low",
210
+ "cuisine": "Chinese",
211
+ "gluten_free": false,
212
+ "vegan": true,
213
+ "lactose_intolerant": false,
214
+ "pescatarian": false,
215
+ "allergen_friendly": false,
216
+ "halal": false,
217
+ "kosher": false,
218
+ "vegetarian": false,
219
+ "website": "https://www.chihobistro.com/"
220
+ },
221
+ {
222
+ "name": "Uptown China",
223
+ "price": "Moderate",
224
+ "cuisine": "Chinese",
225
+ "gluten_free": false,
226
+ "vegan": true,
227
+ "lactose_intolerant": true,
228
+ "pescatarian": true,
229
+ "allergen_friendly": false,
230
+ "halal": false,
231
+ "kosher": false,
232
+ "vegetarian": true,
233
+ "website": "https://uptown-china.com/"
234
+ },
235
+ {
236
+ "name": "Meskel Ethiopian Restaurant",
237
+ "price": "Moderate",
238
+ "cuisine": "Ethiopian",
239
+ "gluten_free": true,
240
+ "vegan": true,
241
+ "lactose_intolerant": true,
242
+ "pescatarian": true,
243
+ "allergen_friendly": true,
244
+ "halal": false,
245
+ "kosher": false,
246
+ "vegetarian": true,
247
+ "website": "https://meskelethiopian.com/menu"
248
+ },
249
+ {
250
+ "name": "Habesha Cafe",
251
+ "price": "Moderate",
252
+ "cuisine": "Ethiopian",
253
+ "gluten_free": false,
254
+ "vegan": true,
255
+ "lactose_intolerant": false,
256
+ "pescatarian": true,
257
+ "allergen_friendly": false,
258
+ "halal": false,
259
+ "kosher": false,
260
+ "vegetarian": true,
261
+ "website": "https://habesha.cafe/"
262
+ },
263
+ {
264
+ "name": "Musang",
265
+ "price": "High",
266
+ "cuisine": "Filipino",
267
+ "gluten_free": true,
268
+ "vegan": false,
269
+ "lactose_intolerant": true,
270
+ "pescatarian": true,
271
+ "allergen_friendly": true,
272
+ "halal": false,
273
+ "kosher": false,
274
+ "vegetarian": false,
275
+ "website": "https://www.musangseattle.com/"
276
+ },
277
+ {
278
+ "name": "Musang Seattle",
279
+ "price": "Moderate",
280
+ "cuisine": "Filipino",
281
+ "gluten_free": true,
282
+ "vegan": true,
283
+ "lactose_intolerant": true,
284
+ "pescatarian": true,
285
+ "allergen_friendly": false,
286
+ "halal": false,
287
+ "kosher": false,
288
+ "vegetarian": true,
289
+ "website": "https://www.musangseattle.com/musang"
290
+ },
291
+ {
292
+ "name": "Gold Coast Ghal",
293
+ "price": "Moderate",
294
+ "cuisine": "Ghanaian",
295
+ "gluten_free": false,
296
+ "vegan": false,
297
+ "lactose_intolerant": false,
298
+ "pescatarian": false,
299
+ "allergen_friendly": false,
300
+ "halal": false,
301
+ "kosher": false,
302
+ "vegetarian": false,
303
+ "website": "https://www.goldcoastghal.com/"
304
+ },
305
+ {
306
+ "name": "Marination Ma Kai",
307
+ "price": "Low",
308
+ "cuisine": "Hawaiian-Korean",
309
+ "gluten_free": true,
310
+ "vegan": false,
311
+ "lactose_intolerant": true,
312
+ "pescatarian": true,
313
+ "allergen_friendly": true,
314
+ "halal": false,
315
+ "kosher": false,
316
+ "vegetarian": true,
317
+ "website": "https://marinationmobile.com/menu"
318
+ },
319
+ {
320
+ "name": "Pabla Indian Cuisine",
321
+ "price": "Low",
322
+ "cuisine": "Indian",
323
+ "gluten_free": true,
324
+ "vegan": true,
325
+ "lactose_intolerant": true,
326
+ "pescatarian": true,
327
+ "allergen_friendly": false,
328
+ "halal": true,
329
+ "kosher": false,
330
+ "vegetarian": true,
331
+ "website": "http://www.pablacuisine
332
+ 10:16
333
+ {
334
+ "name": "Mint Progressive Indian",
335
+ "price": "Moderate",
336
+ "cuisine": "Indian",
337
+ "gluten_free": true,
338
+ "vegan": true,
339
+ "lactose_intolerant": true,
340
+ "pescatarian": true,
341
+ "allergen_friendly": false,
342
+ "halal": false,
343
+ "kosher": false,
344
+ "vegetarian": true,
345
+ "website": "https://www.mintprogressive.com/"
346
+ },
347
+ {
348
+ "name": "Cantinetta",
349
+ "price": "Moderate",
350
+ "cuisine": "Italian",
351
+ "gluten_free": false,
352
+ "vegan": false,
353
+ "lactose_intolerant": false,
354
+ "pescatarian": false,
355
+ "allergen_friendly": false,
356
+ "halal": false,
357
+ "kosher": false,
358
+ "vegetarian": false,
359
+ "website": "https://www.cantinettausa.com/wallingfordYes"
360
+ },
361
+ {
362
+ "name": "The Pink Door",
363
+ "price": "High",
364
+ "cuisine": "Italian",
365
+ "gluten_free": true,
366
+ "vegan": true,
367
+ "lactose_intolerant": true,
368
+ "pescatarian": false,
369
+ "allergen_friendly": false,
370
+ "halal": false,
371
+ "kosher": false,
372
+ "vegetarian": true,
373
+ "website": "https://www.thepinkdoor.net/"
374
+ },
375
+ {
376
+ "name": "Mashiko",
377
+ "price": "High",
378
+ "cuisine": "Japanese",
379
+ "gluten_free": false,
380
+ "vegan": true,
381
+ "lactose_intolerant": true,
382
+ "pescatarian": true,
383
+ "allergen_friendly": false,
384
+ "halal": false,
385
+ "kosher": false,
386
+ "vegetarian": false,
387
+ "website": "https://www.mashikorestaurant.com/"
388
+ },
389
+ {
390
+ "name": "Ben's Fast Food",
391
+ "price": "Low",
392
+ "cuisine": "Mexican",
393
+ "gluten_free": true,
394
+ "vegan": true,
395
+ "lactose_intolerant": true,
396
+ "pescatarian": true,
397
+ "allergen_friendly": false,
398
+ "halal": false,
399
+ "kosher": false,
400
+ "vegetarian": true,
401
+ "website": "https://bensfastfood.com/"
402
+ },
403
+ {
404
+ "name": "Sal y Limon",
405
+ "price": "Moderate",
406
+ "cuisine": "Mexican",
407
+ "gluten_free": false,
408
+ "vegan": false,
409
+ "lactose_intolerant": false,
410
+ "pescatarian": false,
411
+ "allergen_friendly": false,
412
+ "halal": false,
413
+ "kosher": false,
414
+ "vegetarian": true,
415
+ "website": "https://www.salylimonseattle.com/"
416
+ },
417
+ {
418
+ "name": "El Borracho",
419
+ "price": "Low",
420
+ "cuisine": "Mexican",
421
+ "gluten_free": true,
422
+ "vegan": true,
423
+ "lactose_intolerant": true,
424
+ "pescatarian": true,
425
+ "allergen_friendly": false,
426
+ "halal": false,
427
+ "kosher": false,
428
+ "vegetarian": true,
429
+ "website": "https://www.elborracho.co/"
430
+ },
431
+ {
432
+ "name": "Tanoor",
433
+ "price": "Moderate",
434
+ "cuisine": "Middle Eastern",
435
+ "gluten_free": true,
436
+ "vegan": true,
437
+ "lactose_intolerant": true,
438
+ "pescatarian": true,
439
+ "allergen_friendly": true,
440
+ "halal": true,
441
+ "kosher": false,
442
+ "vegetarian": true,
443
+ "website": "https://www.tanoor.com/"
444
+ },
445
+ {
446
+ "name": "Mamnoon",
447
+ "price": "Moderate",
448
+ "cuisine": "Middle Eastern",
449
+ "gluten_free": true,
450
+ "vegan": true,
451
+ "lactose_intolerant": true,
452
+ "pescatarian": true,
453
+ "allergen_friendly": false,
454
+ "halal": false,
455
+ "kosher": false,
456
+ "vegetarian": true,
457
+ "website": "https://nadimama.com/mamnoon"
458
+ },
459
+ {
460
+ "name": "Gorgeous George's",
461
+ "price": "Moderate",
462
+ "cuisine": "Middle Eastern",
463
+ "gluten_free": true,
464
+ "vegan": true,
465
+ "lactose_intolerant": true,
466
+ "pescatarian": true,
467
+ "allergen_friendly": false,
468
+ "halal": false,
469
+ "kosher": false,
470
+ "vegetarian": true,
471
+ "website": "https://www.gorgeousgeorges.com/"
472
+ },
473
+ {
474
+ "name": "Zaina",
475
+ "price": "Low",
476
+ "cuisine": "Middle Eastern",
477
+ "gluten_free": true,
478
+ "vegan": true,
479
+ "lactose_intolerant": true,
480
+ "pescatarian": true,
481
+ "allergen_friendly": true,
482
+ "halal": true,
483
+ "kosher": false,
484
+ "vegetarian": true,
485
+ "website": "https://www.yelp.com/biz/zaina-food-drinks-and-friends-seattle-3"
486
+ },
487
+ {
488
+ "name": "Gold Schnitzel",
489
+ "price": "Moderate",
490
+ "cuisine": "Middle Eastern",
491
+ "gluten_free": false,
492
+ "vegan": false,
493
+ "lactose_intolerant": true,
494
+ "pescatarian": false,
495
+ "allergen_friendly": true,
496
+ "halal": false,
497
+ "kosher": false,
498
+ "vegetarian": false,
499
+ "website": "https://goldschnitzel.com/"
500
+ },
501
+ {
502
+ "name": "Maza Grill",
503
+ "price": "Moderate",
504
+ "cuisine": "Pakistani",
505
+ "gluten_free": false,
506
+ "vegan": false,
507
+ "lactose_intolerant": false,
508
+ "pescatarian": false,
509
+ "allergen_friendly": true,
510
+ "halal": false,
511
+ "kosher": false,
512
+ "vegetarian": false,
513
+ "website": "https://mazagrill.co/"
514
+ },
515
+ {
516
+ "name": "Terra Plata",
517
+ "price": "High",
518
+ "cuisine": "Spanish",
519
+ "gluten_free": false,
520
+ "vegan": false,
521
+ "lactose_intolerant": false,
522
+ "pescatarian": true,
523
+ "allergen_friendly": false,
524
+ "halal": false,
525
+ "kosher": false,
526
+ "vegetarian": false,
527
+ "website": "https://www.terraplata.com/"
528
+ },
529
+ {
530
+ "name": "Pestle Rock",
531
+ "price": "Moderate",
532
+ "cuisine": "Thai",
533
+ "gluten_free": true,
534
+ "vegan": true,
535
+ "lactose_intolerant": true,
536
+ "pescatarian": true,
537
+ "allergen_friendly": false,
538
+ "halal": false,
539
+ "kosher": false,
540
+ "vegetarian": true,
541
+ "website": "https://pestlerock.com/"
542
+ },
543
+ {
544
+ "name": "Araya's Place",
545
+ "price": "Low",
546
+ "cuisine": "Thai",
547
+ "gluten_free": false,
548
+ "vegan": true,
549
+ "lactose_intolerant": false,
550
+ "pescatarian": false,
551
+ "allergen_friendly": false,
552
+ "halal": false,
553
+ "kosher": false,
554
+ "vegetarian": true,
555
+ "website": "https://www.arayasplace.com/"
556
+ },
557
+ {
558
+ "name": "Cafe Turko",
559
+ "price": "Moderate",
560
+ "cuisine": "Turkish",
561
+ "gluten_free": true,
562
+ "vegan": true,
563
+ "lactose_intolerant": true,
564
+ "pescatarian": true,
565
+ "allergen_friendly": false,
566
+ "halal": true,
567
+ "kosher": false,
568
+ "vegetarian": true,
569
+ "website": "https://cafeturko.com/#"
570
+ },
571
+ {
572
+ "name": "Blossom Vegetarian",
573
+ "price": "Low",
574
+ "cuisine": "Vietnamese",
575
+ "gluten_free": true,
576
+ "vegan": true,
577
+ "lactose_intolerant": true,
578
+ "pescatarian": false,
579
+ "allergen_friendly": false,
580
+ "halal": false,
581
+ "kosher": false,
582
+ "vegetarian": true,
583
+ "website": "https://www.blossomrenton.com/"
584
+ },
585
+ {
586
+ "name": "Tilikum",
587
+ "price": "Moderate",
588
+ "cuisine": "European",
589
+ "gluten_free": false,
590
+ "vegan": false,
591
+ "lactose_intolerant": false,
592
+ "pescatarian": true,
593
+ "allergen_friendly": false,
594
+ "halal": false,
595
+ "kosher": false,
596
+ "vegetarian": false,
597
+ "website": "https://www.tilikumplacecafe.com/"
598
+ }
599
+ # Add more restaurant entries as needed
600
+ ]
601
+
602
+ def find_restaurants(criteria):
603
+ """
604
+ Finds restaurants based on the given criteria.
605
+ Parameters:
606
+ criteria (dict): Dictionary containing filtering criteria.
607
+ Returns:
608
+ List of restaurants that match the criteria.
609
+ """
610
+ matching_restaurants = []
611
+ for restaurant in restaurants:
612
+ match = True
613
+ for key, value in criteria.items():
614
+ if key in restaurant:
615
+ if isinstance(restaurant[key], bool):
616
+ if restaurant[key] != value:
617
+ match = False
618
+ break
619
+ elif restaurant[key].lower() != value.lower():
620
+ match = False
621
+ break
622
+ if match:
623
+ matching_restaurants.append(restaurant)
624
+ return matching_restaurants
625
+
626
+ def generate_recommendation(criteria):
627
+ """
628
+ Generates a recommendation based on the criteria.
629
+ Parameters:
630
+ criteria (dict): Dictionary containing filtering criteria.
631
+ Returns:
632
+ String with the recommendation or a message if no matches are found.
633
+ """
634
+ results = find_restaurants(criteria)
635
+ if results:
636
+ recommendations = []
637
+ for result in results:
638
+ recommendation = (
639
+ f"Based on your criteria, I recommend {result['name']}. "
640
+ f"It's a {result['price'].lower()} priced {result['cuisine'].lower()} restaurant with "
641
+ f"{'gluten-free options' if result['gluten_free'] else 'no gluten-free options'}, "
642
+ f"{'vegan options' if result['vegan'] else 'no vegan options'}, "
643
+ f"{'lactose-intolerant options' if result['lactose_intolerant'] else 'no lactose-intolerant options'}, "
644
+ f"{'pescatarian options' if result['pescatarian'] else 'no pescatarian options'}, "
645
+ f"{'allergen-friendly options' if result['allergen_friendly'] else 'no allergen-friendly options'}, "
646
+ f"{'halal options' if result['halal'] else 'no halal options'}, "
647
+ f"{'kosher options' if result['kosher'] else 'no kosher options'}, "
648
+ f"and { 'vegetarian options' if result['vegetarian'] else 'no vegetarian options'}. "
649
+ f"Visit their website for more details: {result['website']}"
650
+ )
651
+ recommendations.append(recommendation)
652
+ return "\n".join(recommendations)
653
+ else:
654
+ return "Sorry, no restaurants meet your criteria. Please try adjusting your filters."
655
+
656
  def query_model(question):
657
  """
658
  Process a question, find relevant information, and generate a response.
659
  """
660
  if question == "":
661
  return "Give me your preferences..."
662
+
663
+ if "restaurant" in question.lower():
664
+ # Extract criteria from the question
665
+ criteria = {}
666
+ if "gluten-free" in question.lower():
667
+ criteria["gluten_free"] = True
668
+ if "vegan" in question.lower():
669
+ criteria["vegan"] = True
670
+ if "lactose-intolerant" in question.lower():
671
+ criteria["lactose_intolerant"] = True
672
+ if "pescatarian" in question.lower():
673
+ criteria["pescatarian"] = True
674
+ if "allergen-friendly" in question.lower():
675
+ criteria["allergen_friendly"] = True
676
+ if "halal" in question.lower():
677
+ criteria["halal"] = True
678
+ if "kosher" in question.lower():
679
+ criteria["kosher"] = True
680
+ if "vegetarian" in question.lower():
681
+ criteria["vegetarian"] = True
682
+
683
+ # Extract price and cuisine
684
+ if "low" in question.lower():
685
+ criteria["price"] = "Low"
686
+ elif "moderate" in question.lower():
687
+ criteria["price"] = "Moderate"
688
+ elif "high" in question.lower():
689
+ criteria["price"] = "High"
690
+
691
+ if any(cuisine in question.lower() for cuisine in ["american", "indian", "middle eastern", "chinese", "italian", "thai", "hawaiian-korean", "japanese", "ethiopian", "pakistani", "mexican", "ghanaian", "vietnamese", "filipino", "spanish", "turkish"]):
692
+ criteria["cuisine"] = next(cuisine for cuisine in ["american", "indian", "middle eastern", "chinese", "italian", "thai", "hawaiian-korean", "japanese", "ethiopian", "pakistani", "mexican", "ghanaian", "vietnamese", "filipino", "spanish", "turkish"] if cuisine in question.lower())
693
+
694
+ response = generate_recommendation(criteria)
695
+ else:
696
+ relevant_segment = find_relevant_segment(question, segments)
697
+ if not relevant_segment:
698
+ return "Could not find specific information. Please refine your question."
699
+ response = generate_response(question, relevant_segment)
700
+
701
  return response
702
+
703
  # Define the welcome message and specific topics the chatbot can provide information about
704
  welcome_message = """
705
  # Welcome to Ethical Eats Explorer!