{ "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 }