File size: 37,659 Bytes
752d595 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "e255d061-b87e-41d1-bdec-75717c311031",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"[15:15:29] Use pytorch device_name: mps\n",
"[15:15:29] Load pretrained SentenceTransformer: sentence-transformers/all-MiniLM-L6-v2\n",
"Batches: 100%|████████████████████████████████████| 1/1 [00:02<00:00, 2.42s/it]\n",
"[15:15:33] Shard 1/37 → loading tsmpd_public_000.parquet\n",
"[15:15:35] rows = 250,000 | load = 2.0s\n",
"[15:15:48] processed in 14.7s best_min=0.5414\n",
"[15:15:48] Shard 2/37 → loading tsmpd_public_001.parquet\n",
"[15:15:49] rows = 250,000 | load = 1.2s\n",
"[15:15:59] processed in 11.3s best_min=0.5506\n",
"[15:15:59] Shard 3/37 → loading tsmpd_public_002.parquet\n",
"[15:16:01] rows = 250,000 | load = 1.2s\n",
"[15:16:10] processed in 10.9s best_min=0.5990\n",
"[15:16:10] Shard 4/37 → loading tsmpd_public_003.parquet\n",
"[15:16:11] rows = 250,000 | load = 1.2s\n",
"[15:16:20] processed in 9.7s best_min=0.5990\n",
"[15:16:20] Shard 5/37 → loading tsmpd_public_004.parquet\n",
"[15:16:21] rows = 250,000 | load = 1.2s\n",
"[15:16:30] processed in 10.4s best_min=0.6122\n",
"[15:16:30] Shard 6/37 → loading tsmpd_public_005.parquet\n",
"[15:16:32] rows = 250,000 | load = 1.2s\n",
"[15:16:40] processed in 9.9s best_min=0.6491\n",
"[15:16:40] Shard 7/37 → loading tsmpd_public_006.parquet\n",
"[15:16:41] rows = 250,000 | load = 1.2s\n",
"[15:16:50] processed in 9.6s best_min=0.6491\n",
"[15:16:50] Shard 8/37 → loading tsmpd_public_007.parquet\n",
"[15:16:51] rows = 250,000 | load = 1.2s\n",
"[15:16:59] processed in 9.5s best_min=0.6491\n",
"[15:16:59] Shard 9/37 → loading tsmpd_public_008.parquet\n",
"[15:17:01] rows = 250,000 | load = 1.2s\n",
"[15:17:10] processed in 10.5s best_min=0.6528\n",
"[15:17:10] Shard 10/37 → loading tsmpd_public_009.parquet\n",
"[15:17:11] rows = 250,000 | load = 1.2s\n",
"[15:17:20] processed in 10.4s best_min=0.6528\n",
"[15:17:20] Shard 11/37 → loading tsmpd_public_010.parquet\n",
"[15:17:22] rows = 250,000 | load = 1.3s\n",
"[15:17:30] processed in 10.1s best_min=0.6528\n",
"[15:17:30] Shard 12/37 → loading tsmpd_public_011.parquet\n",
"[15:17:32] rows = 250,000 | load = 1.3s\n",
"[15:17:40] processed in 9.6s best_min=0.6741\n",
"[15:17:40] Shard 13/37 → loading tsmpd_public_012.parquet\n",
"[15:17:41] rows = 250,000 | load = 1.2s\n",
"[15:17:50] processed in 9.6s best_min=0.6859\n",
"[15:17:50] Shard 14/37 → loading tsmpd_public_013.parquet\n",
"[15:17:51] rows = 250,000 | load = 1.2s\n",
"[15:17:59] processed in 9.6s best_min=0.6859\n",
"[15:17:59] Shard 15/37 → loading tsmpd_public_014.parquet\n",
"[15:18:00] rows = 250,000 | load = 1.2s\n",
"[15:18:10] processed in 10.6s best_min=0.6859\n",
"[15:18:10] Shard 16/37 → loading tsmpd_public_015.parquet\n",
"[15:18:11] rows = 250,000 | load = 1.2s\n",
"[15:18:19] processed in 9.5s best_min=0.6980\n",
"[15:18:19] Shard 17/37 → loading tsmpd_public_016.parquet\n",
"[15:18:21] rows = 250,000 | load = 1.2s\n",
"[15:18:29] processed in 9.8s best_min=0.6980\n",
"[15:18:29] Shard 18/37 → loading tsmpd_public_017.parquet\n",
"[15:18:30] rows = 250,000 | load = 1.2s\n",
"[15:18:39] processed in 10.0s best_min=0.6980\n",
"[15:18:39] Shard 19/37 → loading tsmpd_public_018.parquet\n",
"[15:18:40] rows = 250,000 | load = 1.2s\n",
"[15:18:49] processed in 9.7s best_min=0.6980\n",
"[15:18:49] Shard 20/37 → loading tsmpd_public_019.parquet\n",
"[15:18:50] rows = 250,000 | load = 1.2s\n",
"[15:18:59] processed in 9.7s best_min=0.6980\n",
"[15:18:59] Shard 21/37 → loading tsmpd_public_020.parquet\n",
"[15:19:00] rows = 250,000 | load = 1.2s\n",
"[15:19:09] processed in 10.5s best_min=0.7070\n",
"[15:19:09] Shard 22/37 → loading tsmpd_public_021.parquet\n",
"[15:19:10] rows = 250,000 | load = 1.2s\n",
"[15:19:19] processed in 9.8s best_min=0.7070\n",
"[15:19:19] Shard 23/37 → loading tsmpd_public_022.parquet\n",
"[15:19:20] rows = 250,000 | load = 1.2s\n",
"[15:19:31] processed in 11.6s best_min=0.7070\n",
"[15:19:31] Shard 24/37 → loading tsmpd_public_023.parquet\n",
"[15:19:32] rows = 250,000 | load = 1.3s\n",
"[15:19:41] processed in 10.2s best_min=0.7070\n",
"[15:19:41] Shard 25/37 → loading tsmpd_public_024.parquet\n",
"[15:19:42] rows = 250,000 | load = 1.2s\n",
"[15:19:50] processed in 9.5s best_min=0.7070\n",
"[15:19:50] Shard 26/37 → loading tsmpd_public_025.parquet\n",
"[15:19:52] rows = 250,000 | load = 1.2s\n",
"[15:20:00] processed in 9.9s best_min=0.7070\n",
"[15:20:00] Shard 27/37 → loading tsmpd_public_026.parquet\n",
"[15:20:01] rows = 250,000 | load = 1.2s\n",
"[15:20:12] processed in 11.5s best_min=0.7070\n",
"[15:20:12] Shard 28/37 → loading tsmpd_public_027.parquet\n",
"[15:20:13] rows = 250,000 | load = 1.2s\n",
"[15:20:22] processed in 10.1s best_min=0.7070\n",
"[15:20:22] Shard 29/37 → loading tsmpd_public_028.parquet\n",
"[15:20:23] rows = 250,000 | load = 1.3s\n",
"[15:20:32] processed in 10.1s best_min=0.7070\n",
"[15:20:32] Shard 30/37 → loading tsmpd_public_029.parquet\n",
"[15:20:33] rows = 250,000 | load = 1.2s\n",
"[15:20:43] processed in 11.1s best_min=0.7070\n",
"[15:20:43] Shard 31/37 → loading tsmpd_public_030.parquet\n",
"[15:20:44] rows = 250,000 | load = 1.2s\n",
"[15:20:55] processed in 11.6s best_min=0.7070\n",
"[15:20:55] Shard 32/37 → loading tsmpd_public_031.parquet\n",
"[15:20:56] rows = 250,000 | load = 1.2s\n",
"[15:21:06] processed in 10.9s best_min=0.7070\n",
"[15:21:06] Shard 33/37 → loading tsmpd_public_032.parquet\n",
"[15:21:07] rows = 250,000 | load = 1.2s\n",
"[15:21:19] processed in 13.5s best_min=0.7070\n",
"[15:21:19] Shard 34/37 → loading tsmpd_public_033.parquet\n",
"[15:21:20] rows = 250,000 | load = 1.3s\n",
"[15:21:31] processed in 12.0s best_min=0.7070\n",
"[15:21:31] Shard 35/37 → loading tsmpd_public_034.parquet\n",
"[15:21:32] rows = 250,000 | load = 1.2s\n",
"[15:21:41] processed in 10.3s best_min=0.7070\n",
"[15:21:41] Shard 36/37 → loading tsmpd_public_035.parquet\n",
"[15:21:43] rows = 250,000 | load = 1.2s\n",
"[15:21:52] processed in 10.3s best_min=0.7070\n",
"[15:21:52] Shard 37/37 → loading tsmpd_public_036.parquet\n",
"[15:21:52] rows = 86 | load = 0.0s\n",
"[15:21:52] processed in 0.0s best_min=0.7070\n",
"[15:21:52] Finished query in 380.9s\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>score</th>\n",
" <th>uid</th>\n",
" <th>vendor</th>\n",
" <th>title</th>\n",
" <th>paragraph</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.751176</td>\n",
" <td>34985afc-0e36-4e1a-8494-1e9d06cf57ac</td>\n",
" <td>jxmdaudio</td>\n",
" <td>Active Noise Cancelling Headphones Wireless Over Ear Bluetooth Headphones</td>\n",
" <td>Active Noise Cancelling Headphones Wireless Over Ear Bluetooth Headphones by jxmdaudio - $39.99 . Limited quantityFirst come First served. Professional Active Noise Cancelling Technology Proprietary 40mm Large-aperture Drivers. High-quality Built-in Microphone and NFC Technology. The Professional Protein Earpad and 90 Swiveling Earcups. A built-in 600mAh battery won't allow your headphones power off, you can enjoy your world without noise for 30 hours' long time.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.741887</td>\n",
" <td>5ed011c5-8fb7-41b0-b6cc-b469f67e29dc</td>\n",
" <td>Distritech</td>\n",
" <td>SONY WIRELESS NOISE CANCELING OVERHEAD HEADPHONES, MIDNIGHT BLUE</td>\n",
" <td>SONY WIRELESS NOISE CANCELING OVERHEAD HEADPHONES, MIDNIGHT BLUE by Distritech - $173.00 . WIRELESS NOISE CANCELING OVERHEAD HEADPHONES</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.719547</td>\n",
" <td>ba329fd4-07c8-4fc6-bbc8-11c37ac7f615</td>\n",
" <td>likistor</td>\n",
" <td>Wireless Noise-Cancelling Headphones</td>\n",
" <td>Wireless Noise-Cancelling Headphones by likistor - $59.99 . SPECIFICATIONS Style: Headphone. Vocalism Principle: Dynamic Material: ABS Hign-concerned Chemical: none Communication: True Wireless Volume Control: Yes Category: Earphones &amp; Headphones Number Of Drivers: 2 Active Noise-Cancellation: Yes Waterproof: No Wireless Type: bluetooth Control Button: Yes Is wireless: Yes Charging Method: cable Battery Capacity[mAh]: 100 Maximum wireless range[m]: &lt;10m</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.708177</td>\n",
" <td>5994bc56-967a-494e-8011-553826b5a5b1</td>\n",
" <td>CatoGifts</td>\n",
" <td>Noise Cancelling Wireless Headset</td>\n",
" <td>Noise Cancelling Wireless Headset by CatoGifts - $42.04 . Experience Crystal Clear Sound with Our Noise Cancelling Wireless HeadsetImmerse yourself in superior audio quality with our noise cancelling wireless headset. Designed to block out external noise, it lets you enjoy your favorite music, podcasts, and calls with unparalleled clarity and precision.Elevate Your Listening Experience with Our Noise Cancelling Wireless HeadsetTake your audio experience to the next level with our noise cancelling wireless headset. Whether you're commuting, working, or relaxing at home, this headset provides an immersive soundscape free from distractions.Enjoy Uninterrupted Sound with Our Noise Cancelling Wireless HeadsetSay goodbye to background noise and hello to uninterrupted sound with our noise cancelling wireless headset. Its advanced technology ensures you hear every detail, making it perfect for music lovers and professionals alike.Stay Connected and Focused with Our Noise Cancelling Wireless HeadsetMaintain focus and stay connected wherever you are with our noise cancelling wireless headset. Its wireless design offers freedom of movement, while the noise cancelling feature ensures you stay immersed in your audio world without interruption.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.707036</td>\n",
" <td>b3291e0e-a5d1-4d08-9c3e-e4c0f85276f2</td>\n",
" <td>IVY DUCK</td>\n",
" <td>Wireless Bluetooth Noise Cancelling Headphones</td>\n",
" <td>Wireless Bluetooth Noise Cancelling Headphones by IVY DUCK - $59.99-$69.99 . Experience Uninterrupted Sound with Our Waterproof Wireless Bluetooth Noise-Cancelling Headphones Experience premium sound without the premium price with these advanced Wireless Bluetooth Noise-Cancelling Headphones. While other high-end brands may cost a fortune, our headphones deliver superior noise cancellation and crisp, clear audio at a fraction of the price. Enjoy all-day comfort with a lightweight design and long-lasting battery life, ensuring you get the most out of every listening session. Elevate your audio experience today with top-tier performance and unbeatable value! Advanced Noise-Cancelling Technology: Immerse yourself in your favourite tunes with state-of-the-art active noise-cancelling technology. Block out distractions and enjoy crystal-clear sound, whether youre on a noisy commute or in a bustling caf. High-Fidelity Sound: Experience rich, detailed audio with deep bass and clear highs. Our advanced drivers ensure you hear every note, making these headphones perfect for music lovers and audiophiles alike. Seamless Bluetooth Connectivity: Enjoy wireless freedom with the latest Bluetooth technology. Pair effortlessly with your devices and maintain a stable connection for uninterrupted listening. Long Battery Life: Stay powered throughout the day with an impressive battery life. Whether youre traveling or working, these headphones keep you connected and entertained for hours on end. Comfortable Fit: Designed with ergonomics in mind, these headphones feature plush ear cushions and an adjustable headband for a personalised, comfortable fit. Wear them all day without discomfort. Built-in Microphone &amp; Controls: Take calls and manage your music with ease using the integrated microphone and intuitive controls. Switch tracks, adjust volume, and answer calls with a simple touch. Premium Build Quality: Crafted with high-quality materials, these headphones offer durability and a sleek, modern design. They are built to withstand daily use while looking stylish. Foldable Design: For added convenience, the headphones feature a foldable design, making them easy to store and transport. Perfect for on-the-go lifestyles! Experience superior sound with our Wireless Bluetooth Noise-Cancelling Headphones. Engineered for immersive audio, these premium headphones offer crystal-clear sound and advanced noise cancellation, elevating your listening experience to new heights. Perfect for audiophiles and casual listeners alike, they blend luxury and performance effortlessly. Upgrade your audio with the ultimate in comfort and style today!</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" score uid vendor \\\n",
"0 0.751176 34985afc-0e36-4e1a-8494-1e9d06cf57ac jxmdaudio \n",
"1 0.741887 5ed011c5-8fb7-41b0-b6cc-b469f67e29dc Distritech \n",
"2 0.719547 ba329fd4-07c8-4fc6-bbc8-11c37ac7f615 likistor \n",
"3 0.708177 5994bc56-967a-494e-8011-553826b5a5b1 CatoGifts \n",
"4 0.707036 b3291e0e-a5d1-4d08-9c3e-e4c0f85276f2 IVY DUCK \n",
"\n",
" title \\\n",
"0 Active Noise Cancelling Headphones Wireless Over Ear Bluetooth Headphones \n",
"1 SONY WIRELESS NOISE CANCELING OVERHEAD HEADPHONES, MIDNIGHT BLUE \n",
"2 Wireless Noise-Cancelling Headphones \n",
"3 Noise Cancelling Wireless Headset \n",
"4 Wireless Bluetooth Noise Cancelling Headphones \n",
"\n",
" paragraph \n",
"0 Active Noise Cancelling Headphones Wireless Over Ear Bluetooth Headphones by jxmdaudio - $39.99 . Limited quantityFirst come First served. Professional Active Noise Cancelling Technology Proprietary 40mm Large-aperture Drivers. High-quality Built-in Microphone and NFC Technology. The Professional Protein Earpad and 90 Swiveling Earcups. A built-in 600mAh battery won't allow your headphones power off, you can enjoy your world without noise for 30 hours' long time. \n",
"1 SONY WIRELESS NOISE CANCELING OVERHEAD HEADPHONES, MIDNIGHT BLUE by Distritech - $173.00 . WIRELESS NOISE CANCELING OVERHEAD HEADPHONES \n",
"2 Wireless Noise-Cancelling Headphones by likistor - $59.99 . SPECIFICATIONS Style: Headphone. Vocalism Principle: Dynamic Material: ABS Hign-concerned Chemical: none Communication: True Wireless Volume Control: Yes Category: Earphones & Headphones Number Of Drivers: 2 Active Noise-Cancellation: Yes Waterproof: No Wireless Type: bluetooth Control Button: Yes Is wireless: Yes Charging Method: cable Battery Capacity[mAh]: 100 Maximum wireless range[m]: <10m \n",
"3 Noise Cancelling Wireless Headset by CatoGifts - $42.04 . Experience Crystal Clear Sound with Our Noise Cancelling Wireless HeadsetImmerse yourself in superior audio quality with our noise cancelling wireless headset. Designed to block out external noise, it lets you enjoy your favorite music, podcasts, and calls with unparalleled clarity and precision.Elevate Your Listening Experience with Our Noise Cancelling Wireless HeadsetTake your audio experience to the next level with our noise cancelling wireless headset. Whether you're commuting, working, or relaxing at home, this headset provides an immersive soundscape free from distractions.Enjoy Uninterrupted Sound with Our Noise Cancelling Wireless HeadsetSay goodbye to background noise and hello to uninterrupted sound with our noise cancelling wireless headset. Its advanced technology ensures you hear every detail, making it perfect for music lovers and professionals alike.Stay Connected and Focused with Our Noise Cancelling Wireless HeadsetMaintain focus and stay connected wherever you are with our noise cancelling wireless headset. Its wireless design offers freedom of movement, while the noise cancelling feature ensures you stay immersed in your audio world without interruption. \n",
"4 Wireless Bluetooth Noise Cancelling Headphones by IVY DUCK - $59.99-$69.99 . Experience Uninterrupted Sound with Our Waterproof Wireless Bluetooth Noise-Cancelling Headphones Experience premium sound without the premium price with these advanced Wireless Bluetooth Noise-Cancelling Headphones. While other high-end brands may cost a fortune, our headphones deliver superior noise cancellation and crisp, clear audio at a fraction of the price. Enjoy all-day comfort with a lightweight design and long-lasting battery life, ensuring you get the most out of every listening session. Elevate your audio experience today with top-tier performance and unbeatable value! Advanced Noise-Cancelling Technology: Immerse yourself in your favourite tunes with state-of-the-art active noise-cancelling technology. Block out distractions and enjoy crystal-clear sound, whether youre on a noisy commute or in a bustling caf. High-Fidelity Sound: Experience rich, detailed audio with deep bass and clear highs. Our advanced drivers ensure you hear every note, making these headphones perfect for music lovers and audiophiles alike. Seamless Bluetooth Connectivity: Enjoy wireless freedom with the latest Bluetooth technology. Pair effortlessly with your devices and maintain a stable connection for uninterrupted listening. Long Battery Life: Stay powered throughout the day with an impressive battery life. Whether youre traveling or working, these headphones keep you connected and entertained for hours on end. Comfortable Fit: Designed with ergonomics in mind, these headphones feature plush ear cushions and an adjustable headband for a personalised, comfortable fit. Wear them all day without discomfort. Built-in Microphone & Controls: Take calls and manage your music with ease using the integrated microphone and intuitive controls. Switch tracks, adjust volume, and answer calls with a simple touch. Premium Build Quality: Crafted with high-quality materials, these headphones offer durability and a sleek, modern design. They are built to withstand daily use while looking stylish. Foldable Design: For added convenience, the headphones feature a foldable design, making them easy to store and transport. Perfect for on-the-go lifestyles! Experience superior sound with our Wireless Bluetooth Noise-Cancelling Headphones. Engineered for immersive audio, these premium headphones offer crystal-clear sound and advanced noise cancellation, elevating your listening experience to new heights. Perfect for audiophiles and casual listeners alike, they blend luxury and performance effortlessly. Upgrade your audio with the ultimate in comfort and style today! "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import glob, heapq, logging, os, time\n",
"import polars as pl\n",
"import pandas as pd\n",
"import numpy as np\n",
"from sentence_transformers import SentenceTransformer\n",
"from IPython.display import display\n",
"\n",
"# ─── logging config ───────────────────────────────────────────────────────────\n",
"logging.basicConfig(\n",
" level=logging.INFO,\n",
" format=\"[%(asctime)s] %(message)s\",\n",
" datefmt=\"%H:%M:%S\"\n",
")\n",
"\n",
"# ─── helper: cosine similarity ────────────────────────────────────────────────\n",
"def cosine_batch(q_vec: np.ndarray, mat: np.ndarray) -> np.ndarray:\n",
" q_norm = q_vec / np.linalg.norm(q_vec)\n",
" mat_norm = mat / np.linalg.norm(mat, axis=1, keepdims=True)\n",
" return mat_norm @ q_norm\n",
"\n",
"# ─── model & shards ───────────────────────────────────────────────────────────\n",
"MODEL_NAME = \"sentence-transformers/all-MiniLM-L6-v2\"\n",
"encoder = SentenceTransformer(MODEL_NAME)\n",
"SHARDS = sorted(glob.glob(\"tsmpd_public_*.parquet\"))\n",
"\n",
"# ─── streaming search ─────────────────────────────────────────────────────────\n",
"def search_streaming(query: str, top_k: int = 10) -> pd.DataFrame:\n",
" q_vec = encoder.encode(query, convert_to_numpy=True).astype(\"float32\")\n",
"\n",
" best = [] # heap of (score, uid, vendor, title, paragraph)\n",
" heapq.heapify(best)\n",
"\n",
" for idx, shard in enumerate(SHARDS, 1):\n",
" t0 = time.perf_counter()\n",
" logging.info(f\"Shard {idx}/{len(SHARDS)} → loading {os.path.basename(shard)}\")\n",
"\n",
" tbl = pl.read_parquet(\n",
" shard,\n",
" columns=[\"uid\", \"vendor\", \"title\", \"paragraph\", \"embedding\"]\n",
" )\n",
" logging.info(f\" rows = {tbl.height:,} | load = {time.perf_counter() - t0:.1f}s\")\n",
"\n",
" embs = np.vstack(tbl[\"embedding\"].to_list()).astype(\"float32\")\n",
" sims = cosine_batch(q_vec, embs)\n",
"\n",
" for i, score in enumerate(sims):\n",
" item = (\n",
" float(score),\n",
" tbl[\"uid\"][i],\n",
" tbl[\"vendor\"][i],\n",
" tbl[\"title\"][i],\n",
" tbl[\"paragraph\"][i],\n",
" )\n",
" if len(best) < top_k:\n",
" heapq.heappush(best, item)\n",
" elif score > best[0][0]:\n",
" heapq.heapreplace(best, item)\n",
"\n",
" logging.info(\n",
" f\" processed in {time.perf_counter() - t0:.1f}s \"\n",
" f\"best_min={best[0][0]:.4f}\"\n",
" )\n",
" del tbl, embs, sims # free mem per shard\n",
"\n",
" if not best:\n",
" return pd.DataFrame()\n",
"\n",
" best_sorted = sorted(best, key=lambda x: x[0], reverse=True)\n",
" scores, uids, vendors, titles, paras = zip(*best_sorted)\n",
"\n",
" return pd.DataFrame({\n",
" \"score\": scores,\n",
" \"uid\": uids,\n",
" \"vendor\": vendors,\n",
" \"title\": titles,\n",
" \"paragraph\": paras\n",
" })\n",
"\n",
"# ─── run a test query ─────────────────────────────────────────────────────────\n",
"query = \"wireless noise cancelling headphones\"\n",
"start = time.perf_counter()\n",
"hits = search_streaming(query, top_k=5)\n",
"elapsed = time.perf_counter() - start\n",
"logging.info(f\"Finished query in {elapsed:.1f}s\")\n",
"\n",
"# display full text in each column\n",
"pd.set_option(\"display.max_colwidth\", None)\n",
"display(hits)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6261abe-ce70-43db-9cea-328fb12933d6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|