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62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pprint import pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from playground_prompt.version1 import prompts\n",
    "\n",
    "test_p = prompts[\"test_prompts\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.path.append(\"/home/kave/work/Elise/elise/src\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using /home/kave/.cache/torch_extensions/py310_cu117 as PyTorch extensions root...\n",
      "Detected CUDA files, patching ldflags\n",
      "Emitting ninja build file /home/kave/.cache/torch_extensions/py310_cu117/cuda_kernel/build.ninja...\n",
      "Building extension module cuda_kernel...\n",
      "Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)\n",
      "Loading extension module cuda_kernel...\n",
      "Failed to load CUDA kernels. Mra requires custom CUDA kernels. Please verify that compatible versions of PyTorch and CUDA Toolkit are installed: /home/kave/miniconda3/envs/afterhours_dev/lib/python3.10/site-packages/zmq/backend/cython/../../../../.././libstdc++.so.6: version `GLIBCXX_3.4.32' not found (required by /home/kave/.cache/torch_extensions/py310_cu117/cuda_kernel/cuda_kernel.so)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ninja: no work to do.\n"
     ]
    }
   ],
   "source": [
    "from parser import SentenceParser\n",
    "\n",
    "parser = SentenceParser.from_huggingface(\"BerserkerMother/restaurant_ner\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'Cuisine': ['fast -']},\n",
      " {'Amenity': ['lively']},\n",
      " {'Amenity': ['nice', 'fun'],\n",
      "  'Hours': ['dinner'],\n",
      "  'Rating': ['good', 'good'],\n",
      "  'Services': ['ambian']},\n",
      " {'Cuisine': ['authentic indian']},\n",
      " {'Amenity': ['romantic', 'nice']},\n",
      " {},\n",
      " {},\n",
      " {'Amenity': ['live music']},\n",
      " {'DS': ['gluten allergy', 'gluten - free']},\n",
      " {'Amenity': ['cozy']}]\n"
     ]
    }
   ],
   "source": [
    "ners = parser.get_ner(test_p)\n",
    "pprint(ners)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'Semantic': ['fast -']},\n",
      " {'Semantic': ['lively']},\n",
      " {'Hours': ['dinner'], 'Semantic': ['nice', 'fun']},\n",
      " {'Semantic': ['authentic indian']},\n",
      " {'Semantic': ['romantic', 'nice']},\n",
      " {},\n",
      " {},\n",
      " {'Semantic': ['live music']},\n",
      " {},\n",
      " {'Semantic': ['cozy']}]\n"
     ]
    }
   ],
   "source": [
    "parsed_prompts = parser.parse(ners)\n",
    "pprint(parsed_prompts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sentence_transformers import SentenceTransformer\n",
    "\n",
    "embedder = SentenceTransformer(\"all-MiniLM-L6-v2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                         Accessibility  \\\n",
      "0    Wheelchair accessible entrance, Wheelchair acc...   \n",
      "1    Wheelchair accessible entrance, Wheelchair acc...   \n",
      "2    Wheelchair accessible entrance, Wheelchair acc...   \n",
      "3    Wheelchair accessible entrance, Wheelchair acc...   \n",
      "4    Wheelchair accessible entrance, Wheelchair acc...   \n",
      "..                                                 ...   \n",
      "274  Wheelchair accessible entrance, Wheelchair acc...   \n",
      "275  Wheelchair accessible elevator, Wheelchair acc...   \n",
      "276  Wheelchair accessible entrance, Wheelchair acc...   \n",
      "277  Wheelchair accessible seating, Wheelchair acce...   \n",
      "278  Wheelchair accessible parking lot, Wheelchair ...   \n",
      "\n",
      "                         Amenities & Special featrures  \\\n",
      "0    Bar onsite, Good for kids, High chairs, Restro...   \n",
      "1                 Good for kids, High chairs, Restroom   \n",
      "2    Bar onsite, Dogs allowed, Good for kids, High ...   \n",
      "3                                             Restroom   \n",
      "4                                             Restroom   \n",
      "..                                                 ...   \n",
      "274  Bar onsite, Good for kids, High chairs, Restro...   \n",
      "275           Bar onsite, High chairs, Restroom, Wi-Fi   \n",
      "276  Bar onsite, Dogs allowed, Good for kids, High ...   \n",
      "277                  Bar onsite, High chairs, Restroom   \n",
      "278                  Bar onsite, High chairs, Restroom   \n",
      "\n",
      "                 Atmosphere  \\\n",
      "0              Casual, Cozy   \n",
      "1              Casual, Cozy   \n",
      "2              Casual, Cozy   \n",
      "3              Casual, Cozy   \n",
      "4    Casual, Cozy, Romantic   \n",
      "..                      ...   \n",
      "274            Casual, Cozy   \n",
      "275            Casual, Cozy   \n",
      "276  Casual, Cozy, Romantic   \n",
      "277            Casual, Cozy   \n",
      "278            Casual, Cozy   \n",
      "\n",
      "                                                 Crowd  \\\n",
      "0    Family friendly, Groups, LGBTQ+ friendly, Tran...   \n",
      "1                              Family friendly, Groups   \n",
      "2                              Family friendly, Groups   \n",
      "3                                                  NaN   \n",
      "4                                               Groups   \n",
      "..                                                 ...   \n",
      "274           Family friendly, Groups, LGBTQ+ friendly   \n",
      "275                                             Groups   \n",
      "276     Groups, LGBTQ+ friendly, Transgender safespace   \n",
      "277                                             Groups   \n",
      "278                                             Groups   \n",
      "\n",
      "                                        Dining options  \\\n",
      "0                      Lunch, Dinner, Dessert, Seating   \n",
      "1                      Lunch, Dinner, Dessert, Seating   \n",
      "2            Lunch, Dinner, Catering, Dessert, Seating   \n",
      "3                   Breakfast, Lunch, Dessert, Seating   \n",
      "4                             Dinner, Dessert, Seating   \n",
      "..                                                 ...   \n",
      "274          Dinner, Counter service, Dessert, Seating   \n",
      "275  Breakfast, Brunch, Lunch, Dinner, Dessert, Sea...   \n",
      "276  Breakfast, Brunch, Lunch, Dinner, Dessert, Sea...   \n",
      "277            Brunch, Lunch, Dinner, Dessert, Seating   \n",
      "278            Brunch, Lunch, Dinner, Dessert, Seating   \n",
      "\n",
      "                                             Offerings  \\\n",
      "0    Alcohol, All you can eat, Beer, Cocktails, Cof...   \n",
      "1    Alcohol, All you can eat, Beer, Coffee, Halal,...   \n",
      "2    Alcohol, Beer, Coffee, Halal, Healthy options,...   \n",
      "3                                               Coffee   \n",
      "4    Alcohol, Beer, Cocktails, Coffee, Hard liquor,...   \n",
      "..                                                 ...   \n",
      "274  Alcohol, Beer, Coffee, Healthy options, Kids' ...   \n",
      "275  Alcohol, Beer, Cocktails, Coffee, Hard liquor,...   \n",
      "276  Alcohol, Beer, Cocktails, Coffee, Hard liquor,...   \n",
      "277  Alcohol, Beer, Cocktails, Coffee, Hard liquor,...   \n",
      "278  Alcohol, Beer, Cocktails, Coffee, Hard liquor,...   \n",
      "\n",
      "                                            Payment              Planning  \\\n",
      "0    Debit cards, NFC mobile payments, Credit cards  Accepts reservations   \n",
      "1       Cash-only, Debit cards, NFC mobile payments  Accepts reservations   \n",
      "2    Debit cards, NFC mobile payments, Credit cards  Accepts reservations   \n",
      "3                                       Debit cards                   NaN   \n",
      "4                                               NaN  Accepts reservations   \n",
      "..                                              ...                   ...   \n",
      "274  Debit cards, NFC mobile payments, Credit cards                   NaN   \n",
      "275  Debit cards, NFC mobile payments, Credit cards  Accepts reservations   \n",
      "276  Debit cards, NFC mobile payments, Credit cards  Accepts reservations   \n",
      "277  Debit cards, NFC mobile payments, Credit cards  Accepts reservations   \n",
      "278                Debit cards, NFC mobile payments  Accepts reservations   \n",
      "\n",
      "                                               Service  \\\n",
      "0          Outdoor seating, Delivery, Takeout, Dine-in   \n",
      "1                             Outdoor seating, Dine-in   \n",
      "2    Outdoor seating, Curbside pickup, No-contact d...   \n",
      "3                    Outdoor seating, Takeout, Dine-in   \n",
      "4          Outdoor seating, Delivery, Takeout, Dine-in   \n",
      "..                                                 ...   \n",
      "274        Outdoor seating, Takeout, Dine-in, Delivery   \n",
      "275        Outdoor seating, Dine-in, Delivery, Takeout   \n",
      "276  Outdoor seating, Curbside pickup, Takeout, Din...   \n",
      "277        Outdoor seating, Takeout, Dine-in, Delivery   \n",
      "278        Outdoor seating, Dine-in, Delivery, Takeout   \n",
      "\n",
      "                                            categories  \\\n",
      "0                                                  NaN   \n",
      "1                           Korean barbecue restaurant   \n",
      "2    Indian restaurant, Asian restaurant, Health fo...   \n",
      "3    Cafe, Breakfast restaurant, Brunch restaurant,...   \n",
      "4                                      Thai restaurant   \n",
      "..                                                 ...   \n",
      "274                                                NaN   \n",
      "275  , Cocktail bar, Coffee shop, Coworking space, ...   \n",
      "276                                                NaN   \n",
      "277                                             , Cafe   \n",
      "278  , Culinary school, Event venue, Function room ...   \n",
      "\n",
      "                       Category  \\\n",
      "0                           NaN   \n",
      "1    Korean barbecue restaurant   \n",
      "2             Indian restaurant   \n",
      "3                          Cafe   \n",
      "4               Thai restaurant   \n",
      "..                          ...   \n",
      "274                         NaN   \n",
      "275                         NaN   \n",
      "276                         NaN   \n",
      "277                         NaN   \n",
      "278                         NaN   \n",
      "\n",
      "                                           description  \n",
      "0                                                  NaN  \n",
      "1                                                  NaN  \n",
      "2    Light-filled restaurant with colorful seating ...  \n",
      "3    Seattle-based coffeehouse chain known for its ...  \n",
      "4                                                  NaN  \n",
      "..                                                 ...  \n",
      "274                                                NaN  \n",
      "275                                                NaN  \n",
      "276                                                NaN  \n",
      "277                                                NaN  \n",
      "278                                                NaN  \n",
      "\n",
      "[279 rows x 12 columns]\n"
     ]
    }
   ],
   "source": [
    "from matcher import Matcher\n",
    "\n",
    "matcher = Matcher.from_path(\"/home/kave/work/Elise/elise/data/final_data.csv\", embedder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.0409, -0.0148,  0.0419,  ...,  0.0347, -0.0333,  0.0381],\n",
       "        [ 0.0090,  0.0329,  0.0421,  ...,  0.0387, -0.0885,  0.0523],\n",
       "        [ 0.0155, -0.0316,  0.0217,  ...,  0.0344, -0.0364,  0.0223],\n",
       "        ...,\n",
       "        [ 0.0363, -0.0182,  0.0596,  ...,  0.0276, -0.0117,  0.0335],\n",
       "        [ 0.0415, -0.0292,  0.0533,  ...,  0.0515, -0.0225,  0.0368],\n",
       "        [ 0.0540, -0.0334,  0.0415,  ...,  0.0756, -0.0274,  0.0454]],\n",
       "       device='cuda:0')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matcher.semantics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 331 ms, sys: 0 ns, total: 331 ms\n",
      "Wall time: 91.5 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "ners = parser.get_ner(test_p)\n",
    "parsed_prompts = parser.parse(ners)\n",
    "kk = matcher.handle_jobs(parsed_prompts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import df_to_json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\"I don't feel like cooking tonight. Where's a good place to get fast-food?\": [{'Name': 'Cafetaria Edison',\n",
       "   'Score': 20.519733428955078},\n",
       "  {'Name': 'Five Guys', 'Score': 20.5447940826416},\n",
       "  {'Name': 'Five Guys', 'Score': 20.5447940826416}],\n",
       " \"I'm planning a dinner with some friends. Any recommendations for a restaurant with a lively atmosphere?\": [{'Name': 'De Garde',\n",
       "   'Score': 25.97490882873535},\n",
       "  {'Name': 'Hemel & Aarde', 'Score': 26.467164993286133},\n",
       "  {'Name': 'The Thai Orchid', 'Score': 36.23405838012695}],\n",
       " \"I want to celebrate my graduation with a nice dinner out. What's a good restaurant with good food and a fun ambiance?\": [{'Name': 'Luc Utrecht',\n",
       "   'Score': 20.497264862060547},\n",
       "  {'Name': 'Spice Monkey', 'Score': 20.64722442626953},\n",
       "  {'Name': 'Ethiopian  Sunshine', 'Score': 20.84449005126953}],\n",
       " \"I want to try some new cuisines I've never had before. Can you recommend a restaurant with authentic Indian food?\": [{'Name': 'India Port',\n",
       "   'Score': 35.02317428588867},\n",
       "  {'Name': 'Kashmir Kitchen Utrecht', 'Score': 35.351844787597656},\n",
       "  {'Name': 'Surya Utrecht | Indiaas & Nepalees restaurant & bar',\n",
       "   'Score': 40.53645706176758}],\n",
       " \"I'm planning a special date night and want to go somewhere romantic. What's a good restaurant with a nice view?\": [{'Name': 'Sevilla',\n",
       "   'Score': 20.176685333251953},\n",
       "  {'Name': 'Pand 33 Utrecht', 'Score': 20.325525283813477},\n",
       "  {'Name': 'Hemel & Aarde', 'Score': 23.112428665161133}],\n",
       " \"I'm meeting a client for lunch. Can you recommend a good restaurant for a business meeting?\": [{'Name': 'Sevilla',\n",
       "   'Score': 20.176685333251953},\n",
       "  {'Name': 'Pand 33 Utrecht', 'Score': 20.325525283813477},\n",
       "  {'Name': 'Hemel & Aarde', 'Score': 23.112428665161133}],\n",
       " \"I'm traveling through this city and need to find a good place to eat. Any recommendations near the airport?\": [{'Name': 'Sevilla',\n",
       "   'Score': 20.176685333251953},\n",
       "  {'Name': 'Pand 33 Utrecht', 'Score': 20.325525283813477},\n",
       "  {'Name': 'Hemel & Aarde', 'Score': 23.112428665161133}],\n",
       " \"I'm looking for a restaurant with live music or other entertainment. Any suggestions?\": [{'Name': 'Silk Road Utrecht',\n",
       "   'Score': 19.077302932739258},\n",
       "  {'Name': 'Hemel & Aarde', 'Score': 20.864463806152344},\n",
       "  {'Name': 'The Thai Orchid', 'Score': 26.012712478637695}],\n",
       " 'I have a gluten allergy and need to find a restaurant with gluten-free options. Do you know any good ones in this area?': [{'Name': 'Silk Road Utrecht',\n",
       "   'Score': 19.077302932739258},\n",
       "  {'Name': 'Hemel & Aarde', 'Score': 20.864463806152344},\n",
       "  {'Name': 'The Thai Orchid', 'Score': 26.012712478637695}],\n",
       " \"I just want to relax and have a nice meal out. What's a good restaurant with a cozy atmosphere?\": [{'Name': 'De Garde',\n",
       "   'Score': 34.23313903808594},\n",
       "  {'Name': 'Asia Street Cooking', 'Score': 34.71759796142578},\n",
       "  {'Name': 'Hemel & Aarde', 'Score': 37.8213005065918}]}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_to_json(kk, test_p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "afterhours_dev",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}