File size: 16,545 Bytes
f04b033 251c856 f04b033 86cac40 24fa864 86cac40 72f859f 24fa864 72f859f 86cac40 72f859f 24fa864 72f859f 86cac40 24fa864 72f859f 24fa864 72f859f 24fa864 72f859f 24fa864 72f859f 24fa864 72f859f 24fa864 72f859f 24fa864 72f859f 24fa864 72f859f f04b033 251c856 24fa864 f04b033 72f859f f04b033 24fa864 f04b033 24fa864 f04b033 24fa864 f04b033 24fa864 f04b033 24fa864 f04b033 72f859f f04b033 9599ba8 f04b033 9599ba8 f04b033 24fa864 f04b033 24fa864 27c3570 86cac40 27c3570 9599ba8 72f859f 9599ba8 72f859f 24fa864 72f859f 9599ba8 72f859f 86cac40 27c3570 24fa864 27c3570 24fa864 f04b033 |
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"source": [
"This notebook processes a JSON file of N items into chunks of 1000 items. The items are stored as a JSON + Safetensor pair. The name of the safestensor file is written within the JSON file at index 1"
],
"metadata": {
"id": "T7pqzVAFcPoK"
}
},
{
"cell_type": "code",
"source": [
"\n",
"import json\n",
"import pandas as pd\n",
"import os\n",
"import shelve\n",
"import torch\n",
"from safetensors.torch import save_file\n",
"import json\n",
"\n",
"# Determine if this notebook is running on Colab or Kaggle\n",
"#Use https://www.kaggle.com/ if Google Colab GPU is busy\n",
"home_directory = '/content/'\n",
"using_Kaggle = os.environ.get('KAGGLE_URL_BASE','')\n",
"if using_Kaggle : home_directory = '/kaggle/working/'\n",
"%cd {home_directory}\n",
"#-------#\n",
"# Load the data if not already loaded\n",
"try:\n",
" loaded\n",
"except:\n",
" %cd {home_directory}\n",
" !git clone https://huggingface.co/datasets/codeShare/workspace\n",
" loaded = True\n",
"#--------#"
],
"metadata": {
"id": "xow5kaB2SgPs",
"outputId": "e11edb8a-98a3-4de8-b5b7-139d5db959a5",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/content\n",
"/content\n",
"Cloning into 'workspace'...\n",
"remote: Enumerating objects: 19, done.\u001b[K\n",
"remote: Counting objects: 100% (16/16), done.\u001b[K\n",
"remote: Compressing objects: 100% (10/10), done.\u001b[K\n",
"remote: Total 19 (delta 0), reused 0 (delta 0), pack-reused 3 (from 1)\u001b[K\n",
"Unpacking objects: 100% (19/19), 4.84 KiB | 412.00 KiB/s, done.\n"
]
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cskYkw0zXHEm",
"outputId": "b60e3914-84c4-4660-ba85-f8522ba18558",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/content\n",
"/content/workspace/civit9/raw\n",
"/content/output/civit9/raw\n",
"Saving segment civit9.json to /content/output/civit9/raw/...\n",
"0\n",
"100\n",
"200\n",
"300\n",
"400\n",
"500\n",
"600\n",
"700\n",
"800\n",
"900\n",
"/content/output/civit9/text\n",
"Saving segment civit9-1.json to /content/output/civit9/text/...\n",
"/content/output/civit9/text_encodings\n",
"Saving segment civit9-1.safetensors to /content/output/civit9/text_encodings/...\n",
"0\n",
"100\n",
"200\n",
"300\n",
"400\n",
"500\n",
"600\n",
"700\n",
"800\n",
"900\n",
"/content/output/civit9/text\n",
"Saving segment civit9-2.json to /content/output/civit9/text/...\n",
"/content/output/civit9/text_encodings\n",
"Saving segment civit9-2.safetensors to /content/output/civit9/text_encodings/...\n",
"0\n",
"100\n",
"200\n",
"300\n",
"400\n",
"500\n",
"600\n",
"700\n",
"800\n",
"900\n",
"/content/output/civit9/text\n",
"Saving segment civit9-3.json to /content/output/civit9/text/...\n",
"/content/output/civit9/text_encodings\n",
"Saving segment civit9-3.safetensors to /content/output/civit9/text_encodings/...\n",
"0\n",
"100\n",
"200\n",
"300\n",
"400\n",
"500\n",
"600\n",
"700\n",
"800\n",
"900\n",
"/content/output/civit9/text\n",
"Saving segment civit9-4.json to /content/output/civit9/text/...\n",
"/content/output/civit9/text_encodings\n",
"Saving segment civit9-4.safetensors to /content/output/civit9/text_encodings/...\n",
"0\n",
"100\n",
"200\n",
"300\n",
"400\n",
"500\n",
"600\n",
"700\n",
"800\n",
"900\n",
"/content/output/civit9/text\n",
"Saving segment civit9-5.json to /content/output/civit9/text/...\n",
"/content/output/civit9/text_encodings\n",
"Saving segment civit9-5.safetensors to /content/output/civit9/text_encodings/...\n",
"0\n",
"100\n",
"200\n",
"300\n",
"400\n",
"500\n",
"600\n",
"700\n",
"800\n",
"900\n",
"/content/output/civit9/text\n",
"Saving segment civit9-6.json to /content/output/civit9/text/...\n",
"/content/output/civit9/text_encodings\n",
"Saving segment civit9-6.safetensors to /content/output/civit9/text_encodings/...\n",
"0\n",
"100\n",
"200\n",
"300\n",
"400\n",
"500\n",
"600\n",
"700\n",
"800\n"
]
}
],
"source": [
"\n",
"\n",
"# @title Make your own text_encodings .safetensor file for later use (using GPU is recommended to speed things up)\n",
"# User input\n",
"target = home_directory + 'workspace/civit9/'\n",
"output_folder = home_directory + 'output/civit9/'\n",
"root_filename = 'civit9'\n",
"NUM_FILES = 1\n",
"#--------#\n",
"\n",
"# Setup environment\n",
"def my_mkdirs(folder):\n",
" if os.path.exists(folder)==False:\n",
" os.makedirs(folder)\n",
"#--------#\n",
"output_folder_text = output_folder + 'text/'\n",
"output_folder_text = output_folder + 'text/'\n",
"output_folder_text_encodings = output_folder + 'text_encodings/'\n",
"output_folder_raw = output_folder + 'raw/'\n",
"target_raw = target + 'raw/'\n",
"\n",
"%cd {home_directory}\n",
"my_mkdirs(output_folder)\n",
"my_mkdirs(output_folder_text)\n",
"my_mkdirs(output_folder_text_encodings)\n",
"my_mkdirs(output_folder_raw)\n",
"#-------#\n",
"\n",
"%cd {target_raw}\n",
"with open(root_filename + '.json', 'r') as f:\n",
" data = json.load(f)\n",
"_df = pd.DataFrame({'count': data})['count']\n",
"_prompts = {\n",
" key : value for key, value in _df.items()\n",
"}\n",
"\n",
"index = 0\n",
"for key in _prompts:\n",
" index = index + 1\n",
"#-----#\n",
"NUM_ITEMS = index-1\n",
"\n",
"prompts = {}\n",
"import random\n",
"for key in _prompts:\n",
" prompts[key] = _prompts[key] + ' ' + _prompts[f'{random.randint(0,NUM_ITEMS)}']\n",
"#------#\n",
"\n",
"%cd {output_folder_raw}\n",
"print(f'Saving segment {root_filename}.json to {output_folder_raw}...')\n",
"with open(f'{root_filename}.json', 'w') as f:\n",
" json.dump(prompts, f)\n",
"#-------#\n",
"\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"from transformers import AutoTokenizer\n",
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
"from transformers import CLIPProcessor, CLIPModel\n",
"processor = CLIPProcessor.from_pretrained(\"openai/clip-vit-large-patch14\" , clean_up_tokenization_spaces = True)\n",
"model = CLIPModel.from_pretrained(\"openai/clip-vit-large-patch14\").to(device)\n",
"\n",
"#---------#\n",
"for file_index in range(NUM_FILES + 1):\n",
" if (file_index < 1): continue\n",
" filename = f'{root_filename}-{file_index}'\n",
" if (NUM_FILES == 1) : filename = f'{root_filename}'\n",
" index = 0\n",
" for key in prompts:\n",
" index = index + 1\n",
" #----------#\n",
" NUM_ITEMS = index\n",
" #------#\n",
"\n",
" # Calculate text_encoding for .json file contents and results as .db file\n",
" names_dict = {}\n",
" text_encoding_dict = {}\n",
" segments = {}\n",
" index = 0;\n",
" subby = 1;\n",
" NUM_HEADERS = 2\n",
" CHUNKS_SIZE = 1000\n",
" _filename = ''\n",
" for _index in range(NUM_ITEMS):\n",
" if (index % 100 == 0) : print(index)\n",
" if (index == 0 and _index>0) : index = index + 2 #make space for headers\n",
" if (_index % (CHUNKS_SIZE-NUM_HEADERS) == 0 and _index > 0) :\n",
"\n",
" # Write headers in the .json\n",
" names_dict[f'{0}'] = f'{_index}'\n",
" names_dict[f'{1}'] = f'{filename}-{subby}'\n",
"\n",
" # Encode the headers into text_encoding\n",
" inputs = tokenizer(text = '' + names_dict[f'{0}'], padding=True, truncation = True , return_tensors=\"pt\").to(device)\n",
" text_features = model.get_text_features(**inputs).to(device)\n",
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
" text_encoding_dict[f'{0}'] = text_features.to(torch.device('cpu'))\n",
" inputs = tokenizer(text = '' + names_dict[f'{1}'], padding=True, truncation = True , return_tensors=\"pt\").to(device)\n",
" text_features = model.get_text_features(**inputs).to(device)\n",
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
" text_encoding_dict[f'{1}'] = text_features.to(torch.device('cpu'))\n",
" #-------#\n",
"\n",
" # Write .json\n",
" _filename = f'{filename}-{subby}.json'\n",
" %cd {output_folder_text}\n",
" print(f'Saving segment {_filename} to {output_folder_text}...')\n",
" with open(_filename, 'w') as f:\n",
" json.dump(names_dict, f)\n",
" #-------#\n",
"\n",
" # Write .safetensors\n",
" _filename = f'{filename}-{subby}.safetensors'\n",
" %cd {output_folder_text_encodings}\n",
" print(f'Saving segment {_filename} to {output_folder_text_encodings}...')\n",
" save_file(text_encoding_dict, _filename)\n",
" #--------#\n",
"\n",
" #Iterate\n",
" subby = subby + 1\n",
" segments[f'{subby}'] = _filename\n",
" text_encoding_dict = {}\n",
" names_dict = {}\n",
" index = 0\n",
" #------#\n",
" #------#\n",
" else: index = index + 1\n",
" #--------#\n",
" inputs = tokenizer(text = '' + prompts[f'{_index}'], padding=True, truncation = True , return_tensors=\"pt\").to(device)\n",
" text_features = model.get_text_features(**inputs).to(device)\n",
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
" text_encoding_dict[f'{index}'] = text_features.to(torch.device('cpu'))\n",
" names_dict[f'{index}'] = prompts[f'{_index}']\n",
" continue\n",
" #-----#\n",
" #-----#\n",
" # Write headers in the .json\n",
" names_dict[f'{0}'] = f'{_index}'\n",
" names_dict[f'{1}'] = f'{filename}-{subby}'\n",
"\n",
" # Encode the headers into text_encoding\n",
" inputs = tokenizer(text = '' + names_dict[f'{0}'], padding=True, truncation = True , return_tensors=\"pt\").to(device)\n",
" text_features = model.get_text_features(**inputs).to(device)\n",
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
" text_encoding_dict[f'{0}'] = text_features.to(torch.device('cpu'))\n",
" inputs = tokenizer(text = '' + names_dict[f'{1}'], padding=True, truncation = True , return_tensors=\"pt\").to(device)\n",
" text_features = model.get_text_features(**inputs).to(device)\n",
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
" text_encoding_dict[f'{1}'] = text_features.to(torch.device('cpu'))\n",
" #-------#\n",
"\n",
" # Write .json\n",
" _filename = f'{filename}-{subby}.json'\n",
" %cd {output_folder_text}\n",
" print(f'Saving segment {_filename} to {output_folder_text}...')\n",
" with open(_filename, 'w') as f:\n",
" json.dump(names_dict, f)\n",
" #-------#\n",
"\n",
" # Write .safetensors\n",
" _filename = f'{filename}-{subby}.safetensors'\n",
" %cd {output_folder_text_encodings}\n",
" print(f'Saving segment {_filename} to {output_folder_text_encodings}...')\n",
" save_file(text_encoding_dict, _filename)\n",
" #--------#\n",
"\n",
" #Iterate\n",
" subby = subby + 1\n",
" segments[f'{subby}'] = _filename\n",
" text_encoding_dict = {}\n",
" names_dict = {}\n",
" index = 0\n",
" #------#\n",
" #----#"
]
},
{
"cell_type": "code",
"source": [
"# @title Download the text_encodings as .zip\n",
"# UP TO FILE INDEX 250 FOR fanfic tags out of a total 1622\n",
"import os\n",
"%cd {home_directory}\n",
"output_folder = '/content/output'\n",
"#os.remove(f'{home_directory}results.zip')\n",
"zip_dest = f'{home_directory}results.zip'\n",
"!zip -r {zip_dest} '/content/output'"
],
"metadata": {
"id": "cR-ed0CGhekk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# @title Download the text_encodings to google drive as .zip\n",
"from google.colab import drive\n",
"\n",
"output_folder = '/content/output'\n",
"#-----#\n",
"try: mounted\n",
"except:\n",
" mounted = True\n",
" drive.mount('/content/drive')\n",
"#------#\n"
],
"metadata": {
"id": "zTRmgabymGI1",
"outputId": "0071e88d-c55b-43d8-bf51-9055ef59ce7f",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"\n",
"zip_dest = '/content/drive/MyDrive/fanfic1.zip'\n",
"!zip -r {zip_dest} {output_folder}"
],
"metadata": {
"id": "wk3KETWdZv1h"
},
"execution_count": null,
"outputs": []
}
]
} |