Spaces:
Sleeping
Sleeping
File size: 12,382 Bytes
17557b7 |
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 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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "e20d8915",
"metadata": {},
"source": [
"# π Notebook 1: Embed and Store Shot Data in Qdrant\n",
"\n",
"This notebook loads your cleaned shot data, embeds it using `bge-small-en-v1.5`, and stores the embeddings in Qdrant for use in retrieval and recommendation."
]
},
{
"cell_type": "markdown",
"id": "b11634a3",
"metadata": {},
"source": [
"# Initial Setup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "edc009c4",
"metadata": {},
"outputs": [],
"source": [
"# # Step 1: Initial Setup:\n",
"\n",
"# # Load sentence-transformers and GTE-small model\n",
"# from sentence_transformers import SentenceTransformer\n",
"# model = SentenceTransformer('thenlper/gte-small')"
]
},
{
"cell_type": "markdown",
"id": "8d5248e0",
"metadata": {},
"source": [
"## Load Shot Data From .csv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dce5090d",
"metadata": {},
"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>Date</th>\n",
" <th>Club Type</th>\n",
" <th>Club Description</th>\n",
" <th>Carry Distance</th>\n",
" <th>Total Distance</th>\n",
" <th>Ball Speed</th>\n",
" <th>Club Speed</th>\n",
" <th>Spin Rate</th>\n",
" <th>Attack Angle</th>\n",
" <th>Descent Angle</th>\n",
" <th>Shot Classification</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2025-02-04 12:41:00</td>\n",
" <td>Driver</td>\n",
" <td>TopGolf - Driver (+1; N; 2.75T)</td>\n",
" <td>124.33</td>\n",
" <td>171.19</td>\n",
" <td>122.16</td>\n",
" <td>85.92</td>\n",
" <td>1154</td>\n",
" <td>2.95</td>\n",
" <td>11.33</td>\n",
" <td>Hook</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2025-02-04 12:41:42</td>\n",
" <td>Driver</td>\n",
" <td>TopGolf - Driver (+1; N; 2.75T)</td>\n",
" <td>104.75</td>\n",
" <td>150.95</td>\n",
" <td>120.35</td>\n",
" <td>84.20</td>\n",
" <td>1666</td>\n",
" <td>2.45</td>\n",
" <td>8.19</td>\n",
" <td>Push Hook</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2025-02-04 12:42:17</td>\n",
" <td>Driver</td>\n",
" <td>TopGolf - Driver (+1; N; 2.75T)</td>\n",
" <td>163.45</td>\n",
" <td>195.51</td>\n",
" <td>115.05</td>\n",
" <td>86.28</td>\n",
" <td>1227</td>\n",
" <td>4.30</td>\n",
" <td>23.02</td>\n",
" <td>Push</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2025-02-04 12:43:05</td>\n",
" <td>Driver</td>\n",
" <td>TopGolf - Driver (+1; N; 2.75T)</td>\n",
" <td>162.57</td>\n",
" <td>192.56</td>\n",
" <td>110.91</td>\n",
" <td>81.96</td>\n",
" <td>1783</td>\n",
" <td>1.74</td>\n",
" <td>24.87</td>\n",
" <td>Push</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2025-02-04 12:44:18</td>\n",
" <td>Driver</td>\n",
" <td>TopGolf - Driver (+1; N; 2.75T)</td>\n",
" <td>105.30</td>\n",
" <td>152.00</td>\n",
" <td>118.83</td>\n",
" <td>80.78</td>\n",
" <td>1478</td>\n",
" <td>1.29</td>\n",
" <td>8.67</td>\n",
" <td>Push Draw</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Date Club Type Club Description \\\n",
"0 2025-02-04 12:41:00 Driver TopGolf - Driver (+1; N; 2.75T) \n",
"1 2025-02-04 12:41:42 Driver TopGolf - Driver (+1; N; 2.75T) \n",
"2 2025-02-04 12:42:17 Driver TopGolf - Driver (+1; N; 2.75T) \n",
"3 2025-02-04 12:43:05 Driver TopGolf - Driver (+1; N; 2.75T) \n",
"4 2025-02-04 12:44:18 Driver TopGolf - Driver (+1; N; 2.75T) \n",
"\n",
" Carry Distance Total Distance Ball Speed Club Speed Spin Rate \\\n",
"0 124.33 171.19 122.16 85.92 1154 \n",
"1 104.75 150.95 120.35 84.20 1666 \n",
"2 163.45 195.51 115.05 86.28 1227 \n",
"3 162.57 192.56 110.91 81.96 1783 \n",
"4 105.30 152.00 118.83 80.78 1478 \n",
"\n",
" Attack Angle Descent Angle Shot Classification \n",
"0 2.95 11.33 Hook \n",
"1 2.45 8.19 Push Hook \n",
"2 4.30 23.02 Push \n",
"3 1.74 24.87 Push \n",
"4 1.29 8.67 Push Draw "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Step 2: Load cleaned shot data\n",
"import pandas as pd\n",
"shot_data = pd.read_csv('../data/raw/cleaned_shot_data.csv')\n",
"shot_data.head()"
]
},
{
"cell_type": "markdown",
"id": "56e952f9",
"metadata": {},
"source": [
"## Embed Shot Data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a5052716",
"metadata": {},
"outputs": [],
"source": [
"# Step 3: Format shot data into text chunks for embedding\n",
"\n",
"def create_embedding_text(row):\n",
" return (\n",
" f\"On {row['Date']}, the golfer hit a shot {row['Total Distance']} yards with a carry of {row['Carry Distance']} yards \"\n",
" f\"using a {row['Club Type']} ({row['Club Description']}). \"\n",
" f\"The shot was classified as {row['Shot Classification']}. \"\n",
" f\"The known contributing factors to this result were: \"\n",
" f\"Ball speed: {row['Ball Speed']} mph. \"\n",
" f\"Club speed: {row['Club Speed']} mph. \"\n",
" f\"Spin rate: {row['Spin Rate']} rpm. \"\n",
" f\"Attack angle: {row['Attack Angle']} degrees. \"\n",
" f\"Descent angle: {row['Descent Angle']} degrees.\"\n",
" )\n",
"\n",
"texts = shot_data.apply(create_embedding_text, axis=1).tolist()\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9f23de1d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/mwalker/development/TAMARKDesigns/AI-Maker-Space/cohort-6/projects/session-05/AIE6-Golf-Agent/.venv/lib/python3.12/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"
]
}
],
"source": [
"# Select embedding model\n",
"# Options: 'e5-base-v2', 'bge-base-en-v1.5'\n",
"embedding_model_choice = 'bge-base-en-v1.5'\n",
"\n",
"# Use a model-specific collection name\n",
"descriptive_collection_name = f\"golf_shot_vectors_{embedding_model_choice.replace('-', '_')}\"\n",
"\n",
"from sentence_transformers import SentenceTransformer\n",
"\n",
"def load_embedding_model(name: str):\n",
" if name == 'e5-base-v2':\n",
" return SentenceTransformer('intfloat/e5-base-v2')\n",
" elif name == 'bge-base-en-v1.5':\n",
" return SentenceTransformer('BAAI/bge-base-en-v1.5')\n",
" else:\n",
" raise ValueError(f'Unsupported model: {name}')\n",
"\n",
"model = load_embedding_model(embedding_model_choice)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fbd10b62",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Batches: 100%|ββββββββββ| 16/16 [02:35<00:00, 9.73s/it]\n"
]
}
],
"source": [
"# Generate embeddings with model-specific query formatting\n",
"if embedding_model_choice.startswith('e5'):\n",
" texts_for_embedding = [f'passage: {t}' for t in texts]\n",
"else:\n",
" texts_for_embedding = texts\n",
"embeddings = model.encode(texts_for_embedding, show_progress_bar=True)"
]
},
{
"cell_type": "markdown",
"id": "b5744da3",
"metadata": {},
"source": [
"## Upload Embedded Shot Data to Qdrant"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d95b07f7",
"metadata": {},
"outputs": [],
"source": [
"# Collect the Qdrant API key\n",
"from getpass import getpass\n",
"\n",
"qdrant_api_key = getpass('π Enter your Qdrant API Key: ')\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c4a90e06",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/5p/gq47dsys3k5663k1r5z8s3c40000gn/T/ipykernel_40134/2265687801.py:11: DeprecationWarning: `recreate_collection` method is deprecated and will be removed in the future. Use `collection_exists` to check collection existence and `create_collection` instead.\n",
" client.recreate_collection(\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Qdrant setup\n",
"from qdrant_client import QdrantClient\n",
"from qdrant_client.models import VectorParams, PointStruct, Distance\n",
"\n",
"client = QdrantClient(\n",
" url='https://6f592f43-f667-4234-ad3a-4f15ed5882ef.us-west-2-0.aws.cloud.qdrant.io:6333',\n",
" api_key=qdrant_api_key\n",
")\n",
"\n",
"# Recreate the collection to flush old data\n",
"client.recreate_collection(\n",
" collection_name=descriptive_collection_name,\n",
" vectors_config=VectorParams(size=768, distance=Distance.COSINE)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f5c0f891",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"UpdateResult(operation_id=0, status=<UpdateStatus.COMPLETED: 'completed'>)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Upload embedded vectors to Qdrant\n",
"points = [\n",
" PointStruct(id=i, vector=embeddings[i], payload={'text': texts[i]})\n",
" for i in range(len(embeddings))\n",
"]\n",
"client.upsert(collection_name=descriptive_collection_name, points=points)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|