{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "ee4b18be-3bf0-4f7c-8ac5-ef68b7566750",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: lancedb in /opt/conda/lib/python3.10/site-packages (0.13.0)\n",
      "Collecting rerankers\n",
      "  Downloading rerankers-0.5.3-py3-none-any.whl.metadata (27 kB)\n",
      "Requirement already satisfied: deprecation in /opt/conda/lib/python3.10/site-packages (from lancedb) (2.1.0)\n",
      "Requirement already satisfied: pylance==0.17.0 in /opt/conda/lib/python3.10/site-packages (from lancedb) (0.17.0)\n",
      "Requirement already satisfied: requests>=2.31.0 in /opt/conda/lib/python3.10/site-packages (from lancedb) (2.32.3)\n",
      "Requirement already satisfied: retry>=0.9.2 in /opt/conda/lib/python3.10/site-packages (from lancedb) (0.9.2)\n",
      "Requirement already satisfied: tqdm>=4.27.0 in /opt/conda/lib/python3.10/site-packages (from lancedb) (4.66.5)\n",
      "Requirement already satisfied: pydantic>=1.10 in /opt/conda/lib/python3.10/site-packages (from lancedb) (2.9.2)\n",
      "Requirement already satisfied: attrs>=21.3.0 in /opt/conda/lib/python3.10/site-packages (from lancedb) (24.2.0)\n",
      "Requirement already satisfied: packaging in /opt/conda/lib/python3.10/site-packages (from lancedb) (24.1)\n",
      "Requirement already satisfied: cachetools in /opt/conda/lib/python3.10/site-packages (from lancedb) (5.5.0)\n",
      "Requirement already satisfied: overrides>=0.7 in /opt/conda/lib/python3.10/site-packages (from lancedb) (7.7.0)\n",
      "Requirement already satisfied: pyarrow>=12 in /opt/conda/lib/python3.10/site-packages (from pylance==0.17.0->lancedb) (15.0.2)\n",
      "Requirement already satisfied: numpy<2,>=1.22 in /opt/conda/lib/python3.10/site-packages (from pylance==0.17.0->lancedb) (1.26.4)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /opt/conda/lib/python3.10/site-packages (from pydantic>=1.10->lancedb) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.23.4 in /opt/conda/lib/python3.10/site-packages (from pydantic>=1.10->lancedb) (2.23.4)\n",
      "Requirement already satisfied: typing-extensions>=4.6.1 in /opt/conda/lib/python3.10/site-packages (from pydantic>=1.10->lancedb) (4.12.2)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.10/site-packages (from requests>=2.31.0->lancedb) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.10/site-packages (from requests>=2.31.0->lancedb) (3.8)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.10/site-packages (from requests>=2.31.0->lancedb) (2.2.3)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.10/site-packages (from requests>=2.31.0->lancedb) (2024.8.30)\n",
      "Requirement already satisfied: decorator>=3.4.2 in /opt/conda/lib/python3.10/site-packages (from retry>=0.9.2->lancedb) (5.1.1)\n",
      "Requirement already satisfied: py<2.0.0,>=1.4.26 in /opt/conda/lib/python3.10/site-packages (from retry>=0.9.2->lancedb) (1.11.0)\n",
      "Downloading rerankers-0.5.3-py3-none-any.whl (37 kB)\n",
      "Installing collected packages: rerankers\n",
      "Successfully installed rerankers-0.5.3\n"
     ]
    }
   ],
   "source": [
    "!pip install lancedb rerankers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3f3d4a75-bc50-46e2-ac26-2e4c975cfa9d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cloning into 'MM-Demo'...\n",
      "remote: Enumerating objects: 24, done.\u001b[K\n",
      "remote: Counting objects: 100% (20/20), done.\u001b[K\n",
      "remote: Compressing objects: 100% (20/20), done.\u001b[K\n",
      "remote: Total 24 (delta 4), reused 0 (delta 0), pack-reused 4 (from 1)\u001b[K\n",
      "Unpacking objects: 100% (24/24), 861.87 KiB | 5.04 MiB/s, done.\n"
     ]
    }
   ],
   "source": [
    "!git clone https://huggingface.co/datasets/Sanyam/MM-Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fd7f755b-c9cc-468d-b4f1-8d2f9e7b3d8d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2024-10-01 12:40:13--  https://huggingface.co/datasets/Sanyam/MM-Demo/resolve/main/archive.zip?download=true\n",
      "Resolving huggingface.co (huggingface.co)... 18.238.176.118, 18.238.176.103, 18.238.176.8, ...\n",
      "Connecting to huggingface.co (huggingface.co)|18.238.176.118|:443... connected.\n",
      "HTTP request sent, awaiting response... 302 Found\n",
      "Location: https://cdn-lfs-us-1.hf.co/repos/cd/e8/cde814535d30074dcdff9e1ffb015dfa1cc1e60be4b2906951b47df88d9571ed/89615a7f69ba75f09c19dc40e4214110134c6f533f950b2894440ee0741d8c53?response-content-disposition=attachment%3B+filename*%3DUTF-8%27%27archive.zip%3B+filename%3D%22archive.zip%22%3B&response-content-type=application%2Fzip&Expires=1728045614&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTcyODA0NTYxNH19LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy11cy0xLmhmLmNvL3JlcG9zL2NkL2U4L2NkZTgxNDUzNWQzMDA3NGRjZGZmOWUxZmZiMDE1ZGZhMWNjMWU2MGJlNGIyOTA2OTUxYjQ3ZGY4OGQ5NTcxZWQvODk2MTVhN2Y2OWJhNzVmMDljMTlkYzQwZTQyMTQxMTAxMzRjNmY1MzNmOTUwYjI4OTQ0NDBlZTA3NDFkOGM1Mz9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPSomcmVzcG9uc2UtY29udGVudC10eXBlPSoifV19&Signature=IqXQjBqfdISxsHxEvHZEo6jVeUJggBHfYU4q1yeMoCfzreQfMfQdcGE8bjFbUsmXBkZCV1BszKfB9yrSPnf0j5amsNFsioMPE0YNqakJ9H-vfo4OOeJNEFbzsdtA2St48LHF78pmNv9hTnXApnIf7j49O9qQvRBWF93CSxHD92E6A6gqEfADW28xz--qNnc5-9mM5P02pHk15pGsv3Nw9647e7M1GHneOXvRUrbJFVm8QKb9ro5-VBQjMXQbBhNbyK7YP79myXObocdD8NnVGeq7O8uskS9ddf6CrQdXrEAbIEglpvVKswI9zDtDgOI7zcV2YJAdCrkorYE3lQG0xg__&Key-Pair-Id=K24J24Z295AEI9 [following]\n",
      "--2024-10-01 12:40:14--  https://cdn-lfs-us-1.hf.co/repos/cd/e8/cde814535d30074dcdff9e1ffb015dfa1cc1e60be4b2906951b47df88d9571ed/89615a7f69ba75f09c19dc40e4214110134c6f533f950b2894440ee0741d8c53?response-content-disposition=attachment%3B+filename*%3DUTF-8%27%27archive.zip%3B+filename%3D%22archive.zip%22%3B&response-content-type=application%2Fzip&Expires=1728045614&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTcyODA0NTYxNH19LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy11cy0xLmhmLmNvL3JlcG9zL2NkL2U4L2NkZTgxNDUzNWQzMDA3NGRjZGZmOWUxZmZiMDE1ZGZhMWNjMWU2MGJlNGIyOTA2OTUxYjQ3ZGY4OGQ5NTcxZWQvODk2MTVhN2Y2OWJhNzVmMDljMTlkYzQwZTQyMTQxMTAxMzRjNmY1MzNmOTUwYjI4OTQ0NDBlZTA3NDFkOGM1Mz9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPSomcmVzcG9uc2UtY29udGVudC10eXBlPSoifV19&Signature=IqXQjBqfdISxsHxEvHZEo6jVeUJggBHfYU4q1yeMoCfzreQfMfQdcGE8bjFbUsmXBkZCV1BszKfB9yrSPnf0j5amsNFsioMPE0YNqakJ9H-vfo4OOeJNEFbzsdtA2St48LHF78pmNv9hTnXApnIf7j49O9qQvRBWF93CSxHD92E6A6gqEfADW28xz--qNnc5-9mM5P02pHk15pGsv3Nw9647e7M1GHneOXvRUrbJFVm8QKb9ro5-VBQjMXQbBhNbyK7YP79myXObocdD8NnVGeq7O8uskS9ddf6CrQdXrEAbIEglpvVKswI9zDtDgOI7zcV2YJAdCrkorYE3lQG0xg__&Key-Pair-Id=K24J24Z295AEI9\n",
      "Resolving cdn-lfs-us-1.hf.co (cdn-lfs-us-1.hf.co)... 18.238.176.56, 18.238.176.92, 18.238.176.83, ...\n",
      "Connecting to cdn-lfs-us-1.hf.co (cdn-lfs-us-1.hf.co)|18.238.176.56|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 164078460 (156M) [application/zip]\n",
      "Saving to: ‘archive.zip’\n",
      "\n",
      "archive.zip         100%[===================>] 156.48M   246MB/s    in 0.6s    \n",
      "\n",
      "2024-10-01 12:40:14 (246 MB/s) - ‘archive.zip’ saved [164078460/164078460]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget https://huggingface.co/datasets/Sanyam/MM-Demo/resolve/main/archive.zip?download=true -O archive.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3db34179-91c4-4377-b186-978bbe9d4fee",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!unzip archive.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a13b002d-26bc-4fb0-bd08-193163f2a3d7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "806b1ab5-e3ca-46ec-851d-85bb4e371795",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'end_header_id|>\\n\\n{\"Title\": \"Graphic T-Shirt\", \"Size\": \"S\", \"Category\": \"T-Shirt\", \"Gender\": \"U\", \"Type\": \"Casual\", \"Description\": \"This short-sleeved T-shirt features a gray color with a graphic print of a sailboat and a person in the middle. The graphic is primarily white, with yellow and blue accents, and includes a large \"6 6 8\" logo on the front, as well as additional smaller text that is difficult to decipher. The shirt is laid flat on a pink sheet, providing a contrasting background that highlights the shirt\\'s details.\"}<|eot_id|>'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"description\"][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "243f5eaf-dfb0-4ed8-9b41-5f2f20b5f46a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import re\n",
    "import json\n",
    "def fix_unescaped_quotes(json_string):\n",
    "    # Find the \"Description\" field and its content\n",
    "    pattern = r'\"Description\"\\s*:\\s*\"(.*?)\"(?=\\s*[,}])'\n",
    "    \n",
    "    def escape_quotes(match):\n",
    "        # Escape any unescaped quotes in the description content\n",
    "        content = match.group(1)\n",
    "        escaped_content = re.sub(r'(?<!\\\\)\"', r'\\\"', content)\n",
    "        return f'\"Description\":\"{escaped_content}\"'\n",
    "    \n",
    "    # Replace the Description field with properly escaped content\n",
    "    fixed_json = re.sub(pattern, escape_quotes, json_string)\n",
    "    # Now we can safely parse the JSON\n",
    "    try:\n",
    "        fixed_json = \"{\" + fixed_json.split(\"{\")[1].split(\"}\")[0] + \"}\"\n",
    "        return json.loads(fixed_json)\n",
    "    except:\n",
    "        return {\n",
    "        'Title': \"\", 'Size': \"\" , 'Category': \"\" , 'Gender': \"\" , 'Type': \"\" , 'Description': \"\"\n",
    "        }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "a5eb05e9-b453-44cd-b359-e96bfbfc3c9d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Title', 'Size', 'Category', 'Gender', 'Type', 'Description'])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = fix_unescaped_quotes(df[\"description\"][0])\n",
    "data.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "fca5bef6-934e-4962-8f6f-e0275d76d48f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[2024-10-01T13:54:24Z WARN  lance::dataset] No existing dataset at /home/jupyter/.lancedb/clothes.lance, it will be created\n"
     ]
    }
   ],
   "source": [
    "import lancedb\n",
    "from lancedb.pydantic import LanceModel, Vector\n",
    "from lancedb.embeddings import get_registry\n",
    "from lancedb.rerankers import ColbertReranker\n",
    "\n",
    "model = get_registry().get(\"sentence-transformers\").create(name=\"BAAI/bge-small-en-v1.5\", device=\"cuda\")\n",
    "\n",
    "\n",
    "class Schema(LanceModel):\n",
    "    filename: str\n",
    "    Description: str = model.SourceField()\n",
    "    vector: Vector(model.ndims()) = model.VectorField()\n",
    "    Title: str\n",
    "    Size: str\n",
    "    Category: str\n",
    "    Type: str\n",
    "    Gender: str\n",
    "    \n",
    "db = lancedb.connect(\"~/.lancedb\")\n",
    "tbl = db.create_table(name=\"clothes\", schema=Schema, mode=\"overwrite\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "bc09574c-b562-46fb-ab07-4a477479a9e0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data = [\n",
    "    {\n",
    "        \"filename\": row[\"filename\"],\n",
    "        **fix_unescaped_quotes(row[\"description\"])\n",
    "    } for index, row in df.iterrows()\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "1545c5fe-f4c6-4b65-857d-13b568f39394",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "empty_count = len([d for d in data if d.get('Description', '').strip() == ''])\n",
    "print(empty_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "e4917d40-3ade-49e5-aaf1-865a87bf50bf",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# automatically generate vectors\n",
    "tbl.add(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "c90f9005-ee3c-44ff-8bfd-b020bd4e6091",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>filename</th>\n",
       "      <th>Description</th>\n",
       "      <th>vector</th>\n",
       "      <th>Title</th>\n",
       "      <th>Size</th>\n",
       "      <th>Category</th>\n",
       "      <th>Type</th>\n",
       "      <th>Gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>751b23f7-973b-4ae1-943c-e6a94d0b98f0.jpg</td>\n",
       "      <td>This short-sleeved T-shirt features a gray col...</td>\n",
       "      <td>[-0.0042564264, 0.0655556, -0.014013509, -0.02...</td>\n",
       "      <td>Graphic T-Shirt</td>\n",
       "      <td>S</td>\n",
       "      <td>T-Shirt</td>\n",
       "      <td>Casual</td>\n",
       "      <td>U</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>97cddbc9-a59f-490e-a10d-7878b2aff826.jpg</td>\n",
       "      <td>A short-sleeved, solid pink blouse with a scoo...</td>\n",
       "      <td>[-0.022669869, 0.048171166, 0.003178434, -0.00...</td>\n",
       "      <td>Pink Blouse</td>\n",
       "      <td>M</td>\n",
       "      <td>Tops</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6190369c-a306-4fb6-801e-98ddcbe21435.jpg</td>\n",
       "      <td>These black sneakers feature yellow details on...</td>\n",
       "      <td>[-0.045041706, -0.011831723, 0.014727246, 0.00...</td>\n",
       "      <td>Black Sneakers with Yellow Details</td>\n",
       "      <td>S</td>\n",
       "      <td>Shoes</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>fd2d5fd1-2572-48d1-a089-f44039543b50.jpg</td>\n",
       "      <td>These are a pair of medium-washed denim pants ...</td>\n",
       "      <td>[-0.037305575, 0.08401107, 0.044987984, -0.013...</td>\n",
       "      <td>Denim Pants</td>\n",
       "      <td>M</td>\n",
       "      <td>Pants</td>\n",
       "      <td>Casual</td>\n",
       "      <td>U</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ee23222a-f46f-4044-abb2-0a5d1795da36.jpg</td>\n",
       "      <td>A long-sleeved shirt with a plaid pattern, fea...</td>\n",
       "      <td>[-1.7170775e-06, 0.07057334, -0.07675235, 0.06...</td>\n",
       "      <td>Long-Sleeved Shirt</td>\n",
       "      <td>L</td>\n",
       "      <td>Shirts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>18a573de-219f-44de-8dc9-0a64422e4f99.jpg</td>\n",
       "      <td></td>\n",
       "      <td>[0.048461664, -0.0012961804, 0.016879026, -0.0...</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>23636f41-67c0-4065-b82a-6fd0d3704c07.jpg</td>\n",
       "      <td>This vibrant top features a colorful fruit pat...</td>\n",
       "      <td>[-0.0075272345, -0.0083891535, -0.022784384, -...</td>\n",
       "      <td>Fruit printed top</td>\n",
       "      <td>S</td>\n",
       "      <td>T-Shirt</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>bd289469-607c-45df-bad3-ed1057e277bf.jpg</td>\n",
       "      <td>This is a red and black plaid skirt with a cla...</td>\n",
       "      <td>[-0.021971317, 0.12156065, -0.002515854, 0.034...</td>\n",
       "      <td>Red and Black Plaid Skirt</td>\n",
       "      <td>S</td>\n",
       "      <td>Skirts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9f747796-8b18-4019-ab9e-3a3e3cf60590.jpg</td>\n",
       "      <td></td>\n",
       "      <td>[0.048461664, -0.0012961804, 0.016879026, -0.0...</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>a4e185b1-0720-4553-bae0-76a2bda26dba.jpg</td>\n",
       "      <td>These light gray fleece shorts are a versatile...</td>\n",
       "      <td>[-0.05891305, 0.0793806, 0.017159712, -0.03977...</td>\n",
       "      <td>Fleece Shorts</td>\n",
       "      <td>S</td>\n",
       "      <td>Shorts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>U</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   filename  \\\n",
       "0  751b23f7-973b-4ae1-943c-e6a94d0b98f0.jpg   \n",
       "1  97cddbc9-a59f-490e-a10d-7878b2aff826.jpg   \n",
       "2  6190369c-a306-4fb6-801e-98ddcbe21435.jpg   \n",
       "3  fd2d5fd1-2572-48d1-a089-f44039543b50.jpg   \n",
       "4  ee23222a-f46f-4044-abb2-0a5d1795da36.jpg   \n",
       "5  18a573de-219f-44de-8dc9-0a64422e4f99.jpg   \n",
       "6  23636f41-67c0-4065-b82a-6fd0d3704c07.jpg   \n",
       "7  bd289469-607c-45df-bad3-ed1057e277bf.jpg   \n",
       "8  9f747796-8b18-4019-ab9e-3a3e3cf60590.jpg   \n",
       "9  a4e185b1-0720-4553-bae0-76a2bda26dba.jpg   \n",
       "\n",
       "                                         Description  \\\n",
       "0  This short-sleeved T-shirt features a gray col...   \n",
       "1  A short-sleeved, solid pink blouse with a scoo...   \n",
       "2  These black sneakers feature yellow details on...   \n",
       "3  These are a pair of medium-washed denim pants ...   \n",
       "4  A long-sleeved shirt with a plaid pattern, fea...   \n",
       "5                                                      \n",
       "6  This vibrant top features a colorful fruit pat...   \n",
       "7  This is a red and black plaid skirt with a cla...   \n",
       "8                                                      \n",
       "9  These light gray fleece shorts are a versatile...   \n",
       "\n",
       "                                              vector  \\\n",
       "0  [-0.0042564264, 0.0655556, -0.014013509, -0.02...   \n",
       "1  [-0.022669869, 0.048171166, 0.003178434, -0.00...   \n",
       "2  [-0.045041706, -0.011831723, 0.014727246, 0.00...   \n",
       "3  [-0.037305575, 0.08401107, 0.044987984, -0.013...   \n",
       "4  [-1.7170775e-06, 0.07057334, -0.07675235, 0.06...   \n",
       "5  [0.048461664, -0.0012961804, 0.016879026, -0.0...   \n",
       "6  [-0.0075272345, -0.0083891535, -0.022784384, -...   \n",
       "7  [-0.021971317, 0.12156065, -0.002515854, 0.034...   \n",
       "8  [0.048461664, -0.0012961804, 0.016879026, -0.0...   \n",
       "9  [-0.05891305, 0.0793806, 0.017159712, -0.03977...   \n",
       "\n",
       "                                Title Size Category    Type Gender  \n",
       "0                     Graphic T-Shirt    S  T-Shirt  Casual      U  \n",
       "1                         Pink Blouse    M     Tops  Casual      F  \n",
       "2  Black Sneakers with Yellow Details    S    Shoes  Casual      F  \n",
       "3                         Denim Pants    M    Pants  Casual      U  \n",
       "4                  Long-Sleeved Shirt    L   Shirts  Casual      M  \n",
       "5                                                                   \n",
       "6                   Fruit printed top    S  T-Shirt  Casual      F  \n",
       "7           Red and Black Plaid Skirt    S   Skirts  Casual      F  \n",
       "8                                                                   \n",
       "9                       Fleece Shorts    S   Shorts  Casual      U  "
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tbl.search().to_pandas()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "83a62a86-8fcf-4f7f-836d-40960f6d5ff5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from PIL import Image\n",
    "def show_image_from_response(rs):\n",
    "    names = rs[\"filename\"].to_list()\n",
    "    return [Image.open(Path(\"archive\")/ \"images_compressed\" / name) for name in  names]\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94652723-7813-48db-8089-cbee2ee8fc85",
   "metadata": {},
   "source": [
    "### Pattern 1: text input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "da307282-5c00-4f90-8e64-436269f37815",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " what clothes would go with A long-sleeved shirt with a plaid pattern\n"
     ]
    }
   ],
   "source": [
    "## TODO: turn this into a loop chat & also add image example\n",
    "prompt = input()\n",
    "\"\"\"\n",
    "what clothes would go with A long-sleeved shirt with a plaid pattern. Please answer within 100 words consisely and only provide the answer don't \n",
    "repeat any word from the input, start your response from the clothing items that can be paired with the input\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "84a9a7a0-8849-4cdc-b802-ab28b7438099",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "dummy_model_response = \"\"\"\n",
    "Jeans, trousers, or a skirt can be paired with a long-sleeved shirt with a plaid pattern. For a casual look, try pairing it with jeans and boots. \n",
    "For a more formal look, pair it with trousers and dress shoes. A skirt can also add a feminine touch to the outfit. \n",
    "To balance the boldness of the plaid pattern, consider adding a neutral-colored jacket or cardigan.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "5f0b36e0-761b-4e6f-a3df-e12c36caca0a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>filename</th>\n",
       "      <th>Description</th>\n",
       "      <th>vector</th>\n",
       "      <th>Title</th>\n",
       "      <th>Size</th>\n",
       "      <th>Category</th>\n",
       "      <th>Type</th>\n",
       "      <th>Gender</th>\n",
       "      <th>_distance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>bd289469-607c-45df-bad3-ed1057e277bf.jpg</td>\n",
       "      <td>This is a red and black plaid skirt with a cla...</td>\n",
       "      <td>[-0.021971317, 0.12156065, -0.002515854, 0.034...</td>\n",
       "      <td>Red and Black Plaid Skirt</td>\n",
       "      <td>S</td>\n",
       "      <td>Skirts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "      <td>0.366177</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10e51a41-3729-472e-b288-f3b8f895577a.jpg</td>\n",
       "      <td>This plaid shirt features a classic plaid patt...</td>\n",
       "      <td>[-0.044541422, 0.05006024, -0.04257077, 0.0401...</td>\n",
       "      <td>Plaid shirt with embroidered hem</td>\n",
       "      <td>L</td>\n",
       "      <td>Tops</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "      <td>0.366811</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b0c03127-9dfb-4573-8934-1958396937bf.jpg</td>\n",
       "      <td>This red flannel plaid shirt has a classic and...</td>\n",
       "      <td>[-0.03013145, 0.060508166, -0.03642542, 0.0301...</td>\n",
       "      <td>Red Flannel Plaid Shirt</td>\n",
       "      <td>S</td>\n",
       "      <td>Shirts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>M</td>\n",
       "      <td>0.448323</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   filename  \\\n",
       "0  bd289469-607c-45df-bad3-ed1057e277bf.jpg   \n",
       "1  10e51a41-3729-472e-b288-f3b8f895577a.jpg   \n",
       "2  b0c03127-9dfb-4573-8934-1958396937bf.jpg   \n",
       "\n",
       "                                         Description  \\\n",
       "0  This is a red and black plaid skirt with a cla...   \n",
       "1  This plaid shirt features a classic plaid patt...   \n",
       "2  This red flannel plaid shirt has a classic and...   \n",
       "\n",
       "                                              vector  \\\n",
       "0  [-0.021971317, 0.12156065, -0.002515854, 0.034...   \n",
       "1  [-0.044541422, 0.05006024, -0.04257077, 0.0401...   \n",
       "2  [-0.03013145, 0.060508166, -0.03642542, 0.0301...   \n",
       "\n",
       "                              Title Size Category    Type Gender  _distance  \n",
       "0         Red and Black Plaid Skirt    S   Skirts  Casual      F   0.366177  \n",
       "1  Plaid shirt with embroidered hem    L     Tops  Casual      F   0.366811  \n",
       "2           Red Flannel Plaid Shirt    S   Shirts  Casual      M   0.448323  "
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# automatically converted to vector respresentation\n",
    "rs = tbl.search(dummy_model_response).limit(3).to_pandas()\n",
    "rs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "e74a805a-28a9-4442-9477-65808321cb3c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "imgs = show_image_from_response(rs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "ffa32a2e-de10-4a68-b480-9639394564cc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 59548 characters]\n",
      "image/png": "[encoded data, 456644 characters]\n",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=533x400>"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "943d2358-d378-4446-860b-cda9f9154752",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 64544 characters]\n",
      "image/png": "[encoded data, 480376 characters]\n",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=400x533>"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "56efb82d-8e63-4ad1-97c2-29135ae361e2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 55448 characters]\n",
      "image/png": "[encoded data, 510812 characters]\n",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=400x533>"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4096bf87-a6fe-4ca2-bfe2-0381922dd6bd",
   "metadata": {},
   "source": [
    "### Limitations of semantic search\n",
    "-- "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "908e8c15-9e42-4457-b60d-b0b1fdca7b23",
   "metadata": {},
   "source": [
    "## Let's try Full-text search/ BM25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "141a5e42-32fc-4e8d-9efb-e0c1e00305ba",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "tbl.create_fts_index(\"Description\", replace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "c08a8699-24b2-4843-9437-842cb0d09c5b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>filename</th>\n",
       "      <th>Description</th>\n",
       "      <th>vector</th>\n",
       "      <th>Title</th>\n",
       "      <th>Size</th>\n",
       "      <th>Category</th>\n",
       "      <th>Type</th>\n",
       "      <th>Gender</th>\n",
       "      <th>_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0e20abb0-d56a-4f83-b254-9db70fb92794.jpg</td>\n",
       "      <td>This shirt is a long-sleeved, black and grey s...</td>\n",
       "      <td>[-0.045025624, 0.041662034, -0.0006505165, 0.0...</td>\n",
       "      <td>Black and Grey Striped Shirt with Pink Accents</td>\n",
       "      <td>S</td>\n",
       "      <td>Tops</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "      <td>52.089787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10e51a41-3729-472e-b288-f3b8f895577a.jpg</td>\n",
       "      <td>This plaid shirt features a classic plaid patt...</td>\n",
       "      <td>[-0.044541422, 0.05006024, -0.04257077, 0.0401...</td>\n",
       "      <td>Plaid shirt with embroidered hem</td>\n",
       "      <td>L</td>\n",
       "      <td>Tops</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "      <td>35.616638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>bd289469-607c-45df-bad3-ed1057e277bf.jpg</td>\n",
       "      <td>This is a red and black plaid skirt with a cla...</td>\n",
       "      <td>[-0.021971317, 0.12156065, -0.002515854, 0.034...</td>\n",
       "      <td>Red and Black Plaid Skirt</td>\n",
       "      <td>S</td>\n",
       "      <td>Skirts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "      <td>34.096905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>64c5d20e-7cec-4965-8c50-315389f44b22.jpg</td>\n",
       "      <td>These light washed blue jeans are distressed w...</td>\n",
       "      <td>[-0.07545003, 0.05375045, 0.00353413, -0.01551...</td>\n",
       "      <td>Light Washed Blue Ripped Jeans</td>\n",
       "      <td>L</td>\n",
       "      <td>Jeans</td>\n",
       "      <td>Casual</td>\n",
       "      <td>M</td>\n",
       "      <td>29.116760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b0c03127-9dfb-4573-8934-1958396937bf.jpg</td>\n",
       "      <td>This red flannel plaid shirt has a classic and...</td>\n",
       "      <td>[-0.03013145, 0.060508166, -0.03642542, 0.0301...</td>\n",
       "      <td>Red Flannel Plaid Shirt</td>\n",
       "      <td>S</td>\n",
       "      <td>Shirts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>M</td>\n",
       "      <td>28.444168</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   filename  \\\n",
       "0  0e20abb0-d56a-4f83-b254-9db70fb92794.jpg   \n",
       "1  10e51a41-3729-472e-b288-f3b8f895577a.jpg   \n",
       "2  bd289469-607c-45df-bad3-ed1057e277bf.jpg   \n",
       "3  64c5d20e-7cec-4965-8c50-315389f44b22.jpg   \n",
       "4  b0c03127-9dfb-4573-8934-1958396937bf.jpg   \n",
       "\n",
       "                                         Description  \\\n",
       "0  This shirt is a long-sleeved, black and grey s...   \n",
       "1  This plaid shirt features a classic plaid patt...   \n",
       "2  This is a red and black plaid skirt with a cla...   \n",
       "3  These light washed blue jeans are distressed w...   \n",
       "4  This red flannel plaid shirt has a classic and...   \n",
       "\n",
       "                                              vector  \\\n",
       "0  [-0.045025624, 0.041662034, -0.0006505165, 0.0...   \n",
       "1  [-0.044541422, 0.05006024, -0.04257077, 0.0401...   \n",
       "2  [-0.021971317, 0.12156065, -0.002515854, 0.034...   \n",
       "3  [-0.07545003, 0.05375045, 0.00353413, -0.01551...   \n",
       "4  [-0.03013145, 0.060508166, -0.03642542, 0.0301...   \n",
       "\n",
       "                                            Title Size Category    Type  \\\n",
       "0  Black and Grey Striped Shirt with Pink Accents    S     Tops  Casual   \n",
       "1                Plaid shirt with embroidered hem    L     Tops  Casual   \n",
       "2                       Red and Black Plaid Skirt    S   Skirts  Casual   \n",
       "3                  Light Washed Blue Ripped Jeans    L    Jeans  Casual   \n",
       "4                         Red Flannel Plaid Shirt    S   Shirts  Casual   \n",
       "\n",
       "  Gender     _score  \n",
       "0      F  52.089787  \n",
       "1      F  35.616638  \n",
       "2      F  34.096905  \n",
       "3      M  29.116760  \n",
       "4      M  28.444168  "
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rs = tbl.search(dummy_model_response, query_type=\"fts\").limit(5).to_pandas()\n",
    "rs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "a8b196b4-f526-41f5-95ad-08781b1d2f0d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "imgs = show_image_from_response(rs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "e631f244-3e99-43e3-878b-15ac639e5bb9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 55448 characters]\n",
      "image/png": "[encoded data, 510812 characters]\n",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=400x533>"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "e011f438-8899-4605-ba05-55df0ce95d01",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 35840 characters]\n",
      "image/png": "[encoded data, 314496 characters]\n",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=400x534>"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "1d6cbbd3-9fbb-4cfe-978d-4ece85a65f33",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 59548 characters]\n",
      "image/png": "[encoded data, 456644 characters]\n",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=533x400>"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs[-3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6370d9aa-3262-4234-915a-0459db9144ad",
   "metadata": {},
   "source": [
    "### Hybrid search\n",
    " \n",
    " \n",
    "-- Let's use ColBert Reranker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "46bcbeba-0f7b-4a84-a064-220ab61f1bbf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading ColBERTRanker model colbert-ir/colbertv2.0\n",
      "No device set\n",
      "Using device cuda\n",
      "No dtype set\n",
      "Using dtype torch.float16\n",
      "Loading model colbert-ir/colbertv2.0, this might take a while...\n",
      "Linear Dim set to: {linear_dim} for downcasting\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>filename</th>\n",
       "      <th>Description</th>\n",
       "      <th>vector</th>\n",
       "      <th>Title</th>\n",
       "      <th>Size</th>\n",
       "      <th>Category</th>\n",
       "      <th>Type</th>\n",
       "      <th>Gender</th>\n",
       "      <th>_relevance_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0e20abb0-d56a-4f83-b254-9db70fb92794.jpg</td>\n",
       "      <td>This shirt is a long-sleeved, black and grey s...</td>\n",
       "      <td>[-0.045025624, 0.041662034, -0.0006505165, 0.0...</td>\n",
       "      <td>Black and Grey Striped Shirt with Pink Accents</td>\n",
       "      <td>S</td>\n",
       "      <td>Tops</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "      <td>0.744141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10e51a41-3729-472e-b288-f3b8f895577a.jpg</td>\n",
       "      <td>This plaid shirt features a classic plaid patt...</td>\n",
       "      <td>[-0.044541422, 0.05006024, -0.04257077, 0.0401...</td>\n",
       "      <td>Plaid shirt with embroidered hem</td>\n",
       "      <td>L</td>\n",
       "      <td>Tops</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "      <td>0.735352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>300a4125-acec-46aa-b0b9-68b4b2142c70.jpg</td>\n",
       "      <td>These plaid shorts are a stylish and comfortab...</td>\n",
       "      <td>[-0.002765015, 0.030705247, -0.010424565, -0.0...</td>\n",
       "      <td>Plaid Shorts</td>\n",
       "      <td>S</td>\n",
       "      <td>Pants</td>\n",
       "      <td>Casual</td>\n",
       "      <td>M</td>\n",
       "      <td>0.728516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>bd289469-607c-45df-bad3-ed1057e277bf.jpg</td>\n",
       "      <td>This is a red and black plaid skirt with a cla...</td>\n",
       "      <td>[-0.021971317, 0.12156065, -0.002515854, 0.034...</td>\n",
       "      <td>Red and Black Plaid Skirt</td>\n",
       "      <td>S</td>\n",
       "      <td>Skirts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>F</td>\n",
       "      <td>0.699219</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b0c03127-9dfb-4573-8934-1958396937bf.jpg</td>\n",
       "      <td>This red flannel plaid shirt has a classic and...</td>\n",
       "      <td>[-0.03013145, 0.060508166, -0.03642542, 0.0301...</td>\n",
       "      <td>Red Flannel Plaid Shirt</td>\n",
       "      <td>S</td>\n",
       "      <td>Shirts</td>\n",
       "      <td>Casual</td>\n",
       "      <td>M</td>\n",
       "      <td>0.679199</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   filename  \\\n",
       "0  0e20abb0-d56a-4f83-b254-9db70fb92794.jpg   \n",
       "1  10e51a41-3729-472e-b288-f3b8f895577a.jpg   \n",
       "2  300a4125-acec-46aa-b0b9-68b4b2142c70.jpg   \n",
       "3  bd289469-607c-45df-bad3-ed1057e277bf.jpg   \n",
       "4  b0c03127-9dfb-4573-8934-1958396937bf.jpg   \n",
       "\n",
       "                                         Description  \\\n",
       "0  This shirt is a long-sleeved, black and grey s...   \n",
       "1  This plaid shirt features a classic plaid patt...   \n",
       "2  These plaid shorts are a stylish and comfortab...   \n",
       "3  This is a red and black plaid skirt with a cla...   \n",
       "4  This red flannel plaid shirt has a classic and...   \n",
       "\n",
       "                                              vector  \\\n",
       "0  [-0.045025624, 0.041662034, -0.0006505165, 0.0...   \n",
       "1  [-0.044541422, 0.05006024, -0.04257077, 0.0401...   \n",
       "2  [-0.002765015, 0.030705247, -0.010424565, -0.0...   \n",
       "3  [-0.021971317, 0.12156065, -0.002515854, 0.034...   \n",
       "4  [-0.03013145, 0.060508166, -0.03642542, 0.0301...   \n",
       "\n",
       "                                            Title Size Category    Type  \\\n",
       "0  Black and Grey Striped Shirt with Pink Accents    S     Tops  Casual   \n",
       "1                Plaid shirt with embroidered hem    L     Tops  Casual   \n",
       "2                                    Plaid Shorts    S    Pants  Casual   \n",
       "3                       Red and Black Plaid Skirt    S   Skirts  Casual   \n",
       "4                         Red Flannel Plaid Shirt    S   Shirts  Casual   \n",
       "\n",
       "  Gender  _relevance_score  \n",
       "0      F          0.744141  \n",
       "1      F          0.735352  \n",
       "2      M          0.728516  \n",
       "3      F          0.699219  \n",
       "4      M          0.679199  "
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reranker = ColbertReranker(column=\"Description\")\n",
    "rs = tbl.search(dummy_model_response, query_type=\"hybrid\").rerank(reranker=reranker).limit(5).to_pandas()\n",
    "rs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "965061ed-59ad-4ab3-a15d-fd7766321cf8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "imgs = show_image_from_response(rs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "1e397943-5a60-4067-96e1-2d4981193e8c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 55448 characters]\n",
      "image/png": "[encoded data, 510812 characters]\n",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=400x533>"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "06c35240-f9d8-4518-84e6-cd46bb567330",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 59548 characters]\n",
      "image/png": "[encoded data, 456644 characters]\n",
      "text/plain": [
       "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=533x400>"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e458bdda-4ac0-424a-8b04-a4f2261e0f57",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "environment": {
   "kernel": "conda-base-py",
   "name": "workbench-notebooks.m125",
   "type": "gcloud",
   "uri": "us-docker.pkg.dev/deeplearning-platform-release/gcr.io/workbench-notebooks:m125"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "conda-base-py"
  },
  "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.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
