Datasets:
Tags:
Not-For-All-Audiences
Upload sort_parquets_by_category.ipynb
Browse files- sort_parquets_by_category.ipynb +504 -373
sort_parquets_by_category.ipynb
CHANGED
|
@@ -4,7 +4,11 @@
|
|
| 4 |
"metadata": {
|
| 5 |
"colab": {
|
| 6 |
"provenance": [],
|
| 7 |
-
"gpuType": "T4"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
},
|
| 9 |
"kernelspec": {
|
| 10 |
"name": "python3",
|
|
@@ -19,10 +23,21 @@
|
|
| 19 |
{
|
| 20 |
"cell_type": "markdown",
|
| 21 |
"source": [
|
| 22 |
-
"Using https://huggingface.co/datasets/lodestones/booru-essence"
|
|
|
|
|
|
|
| 23 |
],
|
| 24 |
"metadata": {
|
| 25 |
-
"id": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
},
|
| 28 |
{
|
|
@@ -32,366 +47,467 @@
|
|
| 32 |
"drive.mount('/content/drive')"
|
| 33 |
],
|
| 34 |
"metadata": {
|
| 35 |
-
"id": "HFy5aDxM3G7O"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
},
|
| 37 |
"execution_count": null,
|
| 38 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
},
|
| 40 |
{
|
| 41 |
"cell_type": "code",
|
| 42 |
"source": [
|
| 43 |
-
"#
|
| 44 |
-
"#
|
| 45 |
-
"#
|
| 46 |
"\n",
|
| 47 |
-
"
|
| 48 |
"\n",
|
| 49 |
-
"
|
| 50 |
-
"import re\n",
|
| 51 |
-
"import json\n",
|
| 52 |
-
"import numpy as np\n",
|
| 53 |
"import pandas as pd\n",
|
| 54 |
-
"import torch\n",
|
| 55 |
-
"from transformers import T5Tokenizer, T5Model\n",
|
| 56 |
-
"from sklearn.cluster import KMeans\n",
|
| 57 |
-
"from sklearn.metrics import silhouette_score\n",
|
| 58 |
-
"from umap import UMAP\n",
|
| 59 |
-
"import matplotlib.pyplot as plt\n",
|
| 60 |
-
"from tqdm.auto import tqdm\n",
|
| 61 |
-
"import warnings\n",
|
| 62 |
-
"warnings.filterwarnings(\"ignore\")\n",
|
| 63 |
"\n",
|
| 64 |
-
"#
|
| 65 |
-
"
|
| 66 |
-
"# ============================================================\n",
|
| 67 |
-
"file_path = \"/content/drive/MyDrive/booru_essence.parquet\" #@param {type:\"string\"}\n",
|
| 68 |
"\n",
|
| 69 |
-
"
|
| 70 |
"df = pd.read_parquet(file_path)\n",
|
| 71 |
-
"print(f\"Loaded dataframe shape: {df.shape}\")\n",
|
| 72 |
-
"print(f\"Columns: {df.columns.tolist()}\")\n",
|
| 73 |
-
"\n",
|
| 74 |
-
"# ============================================================\n",
|
| 75 |
-
"# Configuration\n",
|
| 76 |
-
"# ============================================================\n",
|
| 77 |
-
"n_clusters = 100 #@param {type:\"slider\", min:2, max:100, step:1}\n",
|
| 78 |
-
"t5_model_name = \"t5-base\" #@param [\"t5-small\", \"t5-base\"]\n",
|
| 79 |
-
"max_length = 512 #@param {type:\"slider\", min:128, max:512, step:64}\n",
|
| 80 |
-
"batch_size = 16 #@param {type:\"slider\", min:4, max:32, step:4}\n",
|
| 81 |
-
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 82 |
-
"print(f\"Using device: {device}\")\n",
|
| 83 |
-
"\n",
|
| 84 |
-
"# Which text columns to combine (in order of priority)\n",
|
| 85 |
-
"TEXT_COLS = [\n",
|
| 86 |
-
" \"brief_summary\",\n",
|
| 87 |
-
" \"midjourney_style_summary\",\n",
|
| 88 |
-
" \"tags\",\n",
|
| 89 |
-
" \"trigger_tag\",\n",
|
| 90 |
-
" # \"10_word_summary\", # currently all-null in your file\n",
|
| 91 |
-
"]\n",
|
| 92 |
-
"\n",
|
| 93 |
-
"# ============================================================\n",
|
| 94 |
-
"# 1. Helper: clean & combine descriptive texts for one row\n",
|
| 95 |
-
"# ============================================================\n",
|
| 96 |
-
"def combine_texts(row):\n",
|
| 97 |
-
" parts = []\n",
|
| 98 |
-
" for col in TEXT_COLS:\n",
|
| 99 |
-
" val = row.get(col)\n",
|
| 100 |
-
" if pd.isna(val) or val is None:\n",
|
| 101 |
-
" continue\n",
|
| 102 |
-
" text = str(val).strip()\n",
|
| 103 |
-
" if text and text.lower() != \"none\":\n",
|
| 104 |
-
" # simple cleanup\n",
|
| 105 |
-
" text = re.sub(r'\\s+', ' ', text)\n",
|
| 106 |
-
" parts.append(text)\n",
|
| 107 |
-
" return \" | \".join(parts) if parts else \"\"\n",
|
| 108 |
-
"\n",
|
| 109 |
-
"# ============================================================\n",
|
| 110 |
-
"# 2. Load T5 encoder\n",
|
| 111 |
-
"# ============================================================\n",
|
| 112 |
-
"print(f\"Loading {t5_model_name}...\")\n",
|
| 113 |
-
"tokenizer = T5Tokenizer.from_pretrained(t5_model_name)\n",
|
| 114 |
-
"model = T5Model.from_pretrained(t5_model_name).to(device)\n",
|
| 115 |
-
"model.eval()\n",
|
| 116 |
-
"\n",
|
| 117 |
-
"def get_t5_embeddings(texts, batch_size=16):\n",
|
| 118 |
-
" \"\"\"Return mean-pooled encoder embeddings (N, hidden_size)\"\"\"\n",
|
| 119 |
-
" all_embs = []\n",
|
| 120 |
-
" for i in tqdm(range(0, len(texts), batch_size), desc=\"Encoding\"):\n",
|
| 121 |
-
" batch = texts[i:i+batch_size]\n",
|
| 122 |
-
" inputs = tokenizer(\n",
|
| 123 |
-
" batch,\n",
|
| 124 |
-
" return_tensors=\"pt\",\n",
|
| 125 |
-
" padding=True,\n",
|
| 126 |
-
" truncation=True,\n",
|
| 127 |
-
" max_length=max_length\n",
|
| 128 |
-
" ).to(device)\n",
|
| 129 |
"\n",
|
| 130 |
-
"
|
| 131 |
-
"
|
| 132 |
-
"
|
| 133 |
-
" last_hidden = outputs.last_hidden_state # (B, L, H)\n",
|
| 134 |
-
" mask = inputs.attention_mask.unsqueeze(-1).float()\n",
|
| 135 |
-
" summed = (last_hidden * mask).sum(dim=1)\n",
|
| 136 |
-
" counts = mask.sum(dim=1).clamp(min=1e-9)\n",
|
| 137 |
-
" mean_pooled = summed / counts # (B, H)\n",
|
| 138 |
-
" all_embs.append(mean_pooled.cpu().numpy())\n",
|
| 139 |
"\n",
|
| 140 |
-
"
|
|
|
|
| 141 |
"\n",
|
| 142 |
-
"
|
| 143 |
-
"
|
| 144 |
-
"# ============================================================\n",
|
| 145 |
-
"assert \"rating\" in df.columns, \"Column 'rating' not found!\"\n",
|
| 146 |
"\n",
|
| 147 |
-
"
|
| 148 |
-
"
|
| 149 |
-
"os.makedirs(output_base, exist_ok=True)\n",
|
| 150 |
"\n",
|
| 151 |
-
"
|
| 152 |
-
"print(
|
| 153 |
"\n",
|
| 154 |
-
"
|
| 155 |
-
"
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
"\n",
|
| 159 |
-
"
|
| 160 |
-
"
|
| 161 |
-
" print(f\" Rows: {len(group)}\")\n",
|
| 162 |
"\n",
|
| 163 |
-
"
|
| 164 |
-
" group[\"combined_text\"] = group.apply(combine_texts, axis=1)\n",
|
| 165 |
"\n",
|
| 166 |
-
"
|
| 167 |
-
" group = group[group[\"combined_text\"].str.len() > 5].reset_index(drop=True)\n",
|
| 168 |
-
" print(f\" After dropping near-empty texts: {len(group)}\")\n",
|
| 169 |
"\n",
|
| 170 |
-
"
|
| 171 |
-
" print(\" Skipping – not enough samples\")\n",
|
| 172 |
-
" continue\n",
|
| 173 |
"\n",
|
| 174 |
-
"
|
| 175 |
"\n",
|
| 176 |
-
"
|
| 177 |
-
" embeddings = get_t5_embeddings(texts, batch_size=batch_size)\n",
|
| 178 |
-
" print(f\" Embeddings shape: {embeddings.shape}\")\n",
|
| 179 |
"\n",
|
| 180 |
-
"
|
| 181 |
-
" k = min(n_clusters, len(group) // 2, 100) # safety\n",
|
| 182 |
-
" k = max(k, 2)\n",
|
| 183 |
-
" print(f\" Using k = {k} clusters\")\n",
|
| 184 |
"\n",
|
| 185 |
-
"
|
| 186 |
-
" kmeans = KMeans(n_clusters=k, random_state=42, n_init=10, verbose=0)\n",
|
| 187 |
-
" labels = kmeans.fit_predict(embeddings)\n",
|
| 188 |
-
" group[\"cluster\"] = labels\n",
|
| 189 |
"\n",
|
| 190 |
-
"
|
| 191 |
-
" try:\n",
|
| 192 |
-
" sil = silhouette_score(embeddings, labels, sample_size=min(5000, len(embeddings)))\n",
|
| 193 |
-
" print(f\" Silhouette score: {sil:.3f}\")\n",
|
| 194 |
-
" except Exception as e:\n",
|
| 195 |
-
" print(f\" Silhouette skipped: {e}\")\n",
|
| 196 |
"\n",
|
| 197 |
-
"
|
| 198 |
-
" rating_safe = re.sub(r'[^a-zA-Z0-9_]', '_', str(rating))\n",
|
| 199 |
-
" out_dir = os.path.join(output_base, f\"rating_{rating_safe}\")\n",
|
| 200 |
-
" os.makedirs(out_dir, exist_ok=True)\n",
|
| 201 |
"\n",
|
| 202 |
-
"
|
| 203 |
-
" group.to_parquet(os.path.join(out_dir, f\"all_with_clusters.parquet\"), index=False)\n",
|
| 204 |
-
" group.to_csv(os.path.join(out_dir, f\"all_with_clusters.csv\"), index=False)\n",
|
| 205 |
"\n",
|
| 206 |
-
"
|
| 207 |
-
"
|
| 208 |
-
" cluster_df = group[group[\"cluster\"] == c]\n",
|
| 209 |
-
" cluster_df.to_csv(\n",
|
| 210 |
-
" os.path.join(out_dir, f\"cluster_{c:03d}.csv\"),\n",
|
| 211 |
-
" index=False\n",
|
| 212 |
-
" )\n",
|
| 213 |
-
" # Optional: save only the combined text\n",
|
| 214 |
-
" with open(os.path.join(out_dir, f\"cluster_{c:03d}_texts.txt\"), \"w\", encoding=\"utf-8\") as f:\n",
|
| 215 |
-
" for t in cluster_df[\"combined_text\"]:\n",
|
| 216 |
-
" f.write(t + \"\\n\\n---\\n\\n\")\n",
|
| 217 |
-
"\n",
|
| 218 |
-
" print(f\" Saved to: {out_dir}\")\n",
|
| 219 |
-
"\n",
|
| 220 |
-
" # Optional 2-D UMAP plot\n",
|
| 221 |
-
" if len(embeddings) >= 10:\n",
|
| 222 |
-
" try:\n",
|
| 223 |
-
" reducer = UMAP(n_components=2, random_state=42, n_neighbors=15, min_dist=0.1)\n",
|
| 224 |
-
" emb_2d = reducer.fit_transform(embeddings)\n",
|
| 225 |
-
"\n",
|
| 226 |
-
" plt.figure(figsize=(10, 8))\n",
|
| 227 |
-
" scatter = plt.scatter(emb_2d[:, 0], emb_2d[:, 1],\n",
|
| 228 |
-
" c=labels, cmap=\"tab20\", s=20, alpha=0.7)\n",
|
| 229 |
-
" plt.colorbar(scatter, label=\"Cluster\")\n",
|
| 230 |
-
" plt.title(f\"T5 + KMeans – rating='{rating}' (k={k})\")\n",
|
| 231 |
-
" plt.xlabel(\"UMAP-1\")\n",
|
| 232 |
-
" plt.ylabel(\"UMAP-2\")\n",
|
| 233 |
-
" plt.tight_layout()\n",
|
| 234 |
-
" plt.savefig(os.path.join(out_dir, \"umap_plot.png\"), dpi=150)\n",
|
| 235 |
-
" plt.show()\n",
|
| 236 |
-
" except Exception as e:\n",
|
| 237 |
-
" print(f\" UMAP plot failed: {e}\")\n",
|
| 238 |
-
"\n",
|
| 239 |
-
"print(\"\\n✅ Done!\")\n",
|
| 240 |
-
"print(f\"All results are under: {output_base}\")\n",
|
| 241 |
-
"print(\"Structure:\")\n",
|
| 242 |
-
"print(\" /content/output/\")\n",
|
| 243 |
-
"print(\" rating_explicit/\")\n",
|
| 244 |
-
"print(\" all_with_clusters.parquet / .csv\")\n",
|
| 245 |
-
"print(\" cluster_000.csv ... cluster_099.csv\")\n",
|
| 246 |
-
"print(\" cluster_000_texts.txt ...\")\n",
|
| 247 |
-
"print(\" umap_plot.png\")\n",
|
| 248 |
-
"print(\" rating_questionable/\")\n",
|
| 249 |
-
"print(\" rating_general/\")\n",
|
| 250 |
-
"print(\" ...\")"
|
| 251 |
],
|
| 252 |
"metadata": {
|
| 253 |
-
"id": "
|
| 254 |
-
}
|
| 255 |
-
"execution_count": null,
|
| 256 |
-
"outputs": []
|
| 257 |
},
|
| 258 |
{
|
| 259 |
"cell_type": "code",
|
| 260 |
"source": [
|
| 261 |
-
"#@title
|
| 262 |
-
"#
|
| 263 |
-
"#
|
| 264 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
"\n",
|
| 266 |
"import os\n",
|
| 267 |
"import re\n",
|
| 268 |
"import zipfile\n",
|
|
|
|
|
|
|
|
|
|
| 269 |
"from pathlib import Path\n",
|
|
|
|
|
|
|
| 270 |
"from tqdm.auto import tqdm\n",
|
|
|
|
|
|
|
| 271 |
"\n",
|
| 272 |
"# ============================================================\n",
|
| 273 |
"# Configuration\n",
|
| 274 |
"# ============================================================\n",
|
| 275 |
-
"
|
| 276 |
-
"
|
| 277 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
"\n",
|
| 279 |
-
"
|
| 280 |
-
"
|
| 281 |
-
"
|
| 282 |
-
"showDemo = True\n",
|
| 283 |
"\n",
|
| 284 |
-
"
|
| 285 |
-
"
|
| 286 |
-
"
|
| 287 |
-
"placeholder_caption = \"This is text\"\n",
|
| 288 |
-
"meta_image = \"https://i.imgur.com/TPHIzaa.jpeg\"\n",
|
| 289 |
"\n",
|
| 290 |
"# ============================================================\n",
|
| 291 |
-
"#
|
| 292 |
"# ============================================================\n",
|
| 293 |
-
"
|
|
|
|
|
|
|
| 294 |
"\n",
|
| 295 |
-
"
|
| 296 |
-
"
|
| 297 |
-
"
|
| 298 |
-
"
|
| 299 |
-
"\n",
|
| 300 |
-
"
|
| 301 |
-
"
|
| 302 |
-
"
|
| 303 |
-
"
|
| 304 |
-
"
|
| 305 |
-
"
|
| 306 |
-
"
|
| 307 |
-
"
|
| 308 |
-
"
|
| 309 |
-
"
|
| 310 |
-
"\n",
|
| 311 |
-
"
|
| 312 |
-
"text_files = sorted(text_files, key=lambda x: (x[\"rating\"], x[\"cluster\"]))\n",
|
| 313 |
-
"\n",
|
| 314 |
-
"print(f\"Found {len(text_files)} cluster text files\")\n",
|
| 315 |
-
"if len(text_files) == 0:\n",
|
| 316 |
-
" raise FileNotFoundError(\"No cluster_*_texts.txt files found under /content/output/\")\n",
|
| 317 |
"\n",
|
| 318 |
"# ============================================================\n",
|
| 319 |
-
"#
|
| 320 |
"# ============================================================\n",
|
| 321 |
-
"def
|
| 322 |
-
"
|
| 323 |
-
"
|
| 324 |
-
"
|
| 325 |
-
"
|
| 326 |
-
"
|
| 327 |
-
"
|
| 328 |
-
"
|
| 329 |
-
"
|
| 330 |
-
"
|
| 331 |
-
"
|
| 332 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
"\n",
|
| 334 |
"# ============================================================\n",
|
| 335 |
-
"#
|
| 336 |
"# ============================================================\n",
|
| 337 |
-
"
|
| 338 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
"\n",
|
| 340 |
-
"
|
| 341 |
-
"
|
| 342 |
"\n",
|
| 343 |
-
"
|
| 344 |
-
" print(f\" Skipping empty cluster: {item['path']}\")\n",
|
| 345 |
-
" continue\n",
|
| 346 |
"\n",
|
| 347 |
-
"
|
| 348 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
"\n",
|
| 350 |
-
"
|
| 351 |
-
" if idx == 1:\n",
|
| 352 |
-
" backward = f'[\"fusion-t2i-{name}-1\"]'\n",
|
| 353 |
-
" else:\n",
|
| 354 |
-
" backward = f'[\"fusion-t2i-{name}-{idx-1}\"]'\n",
|
| 355 |
"\n",
|
| 356 |
-
"
|
| 357 |
-
"
|
| 358 |
-
"
|
| 359 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
"\n",
|
| 361 |
-
" # Full content matching your template\n",
|
| 362 |
" content = f\"\"\"//start\n",
|
| 363 |
-
"
|
| 364 |
-
"
|
| 365 |
-
"
|
| 366 |
-
"
|
| 367 |
-
"
|
| 368 |
-
"
|
| 369 |
-
"
|
|
|
|
|
|
|
|
|
|
| 370 |
"\n",
|
| 371 |
"tagBlackListRaw = [\"\"]\n",
|
| 372 |
-
"tagBlackList= [tagBlackListRaw.replaceText(' ' , ',')]\n",
|
| 373 |
"\n",
|
| 374 |
"// Generator browse destinations\n",
|
| 375 |
-
"backward=
|
| 376 |
-
"forward=
|
| 377 |
-
"backward_em= {emoji}\n",
|
| 378 |
-
"forward_em= {emoji}\n",
|
| 379 |
-
"numItemsStart=1\n",
|
| 380 |
"//----//\n",
|
| 381 |
"\n",
|
| 382 |
-
"\n",
|
| 383 |
-
"
|
| 384 |
-
"
|
| 385 |
-
"optional_image_caption= [\"{placeholder_caption}\"]\n",
|
| 386 |
"\n",
|
| 387 |
"$meta\n",
|
| 388 |
-
" title = [emoji] [name] [
|
| 389 |
-
" description =
|
| 390 |
-
" image =
|
| 391 |
"$output\n",
|
| 392 |
" [dataset]\n",
|
| 393 |
"\n",
|
| 394 |
-
"\n",
|
| 395 |
"data = [(($output.selectMany(document.numItems).join(\" \")+' '))]\n",
|
| 396 |
"\n",
|
| 397 |
"dataset\n",
|
|
@@ -399,109 +515,124 @@
|
|
| 399 |
"//----//\n",
|
| 400 |
"\"\"\"\n",
|
| 401 |
"\n",
|
| 402 |
-
"
|
| 403 |
-
"
|
| 404 |
-
" out_path = os.path.join(output_dir, out_name)\n",
|
| 405 |
-
" with open(out_path, \"w\", encoding=\"utf-8\") as f:\n",
|
| 406 |
" f.write(content)\n",
|
| 407 |
"\n",
|
| 408 |
-
" generated_files.append(out_path)\n",
|
| 409 |
-
"\n",
|
| 410 |
-
"print(f\"\\n✅ Generated {len(generated_files)} .txt files in: {output_dir}\")\n",
|
| 411 |
-
"\n",
|
| 412 |
"# ============================================================\n",
|
| 413 |
-
"#
|
| 414 |
"# ============================================================\n",
|
| 415 |
-
"
|
|
|
|
|
|
|
|
|
|
| 416 |
"\n",
|
| 417 |
-
"
|
| 418 |
-
"
|
| 419 |
-
"
|
| 420 |
-
"
|
| 421 |
-
"\n",
|
| 422 |
-
"print(f\"✅ ZIP created: {zip_path}\")\n",
|
| 423 |
-
"print(f\" Contains {len(generated_files)} files\")\n",
|
| 424 |
-
"print(\"\\nYou can download it from the Colab file browser or with:\")\n",
|
| 425 |
-
"print(f\"!files.download('{zip_path}')\")"
|
| 426 |
-
],
|
| 427 |
-
"metadata": {
|
| 428 |
-
"id": "vJh25rT9_14a"
|
| 429 |
-
},
|
| 430 |
-
"execution_count": null,
|
| 431 |
-
"outputs": []
|
| 432 |
-
},
|
| 433 |
-
{
|
| 434 |
-
"cell_type": "code",
|
| 435 |
-
"source": [
|
| 436 |
-
"#@title Clean \"|\" before every \"by ...\" in the generated TXT files + re-zip\n",
|
| 437 |
-
"# Removes the \"|\" that appears immediately before artist credits like:\n",
|
| 438 |
-
"# \"white sweater | by hyde (tabakko)|1girl\" → \"white sweater by hyde (tabakko)|1girl\"\n",
|
| 439 |
-
"# \"eyebrows | by alexmaster|1girl\" → \"eyebrows by alexmaster|1girl\"\n",
|
| 440 |
"\n",
|
| 441 |
-
"
|
| 442 |
-
"
|
| 443 |
-
"
|
| 444 |
-
"from pathlib import Path\n",
|
| 445 |
-
"from tqdm.auto import tqdm\n",
|
| 446 |
-
"\n",
|
| 447 |
-
"# ============================================================\n",
|
| 448 |
-
"# Paths\n",
|
| 449 |
-
"# ============================================================\n",
|
| 450 |
-
"txt_dir = Path(\"/content/output/js_files\")\n",
|
| 451 |
-
"zip_path = \"/content/js_files.zip\" # ← final zip now saved directly to /content/\n",
|
| 452 |
-
"\n",
|
| 453 |
-
"if not txt_dir.exists():\n",
|
| 454 |
-
" raise FileNotFoundError(f\"Directory not found: {txt_dir}\")\n",
|
| 455 |
-
"\n",
|
| 456 |
-
"txt_files = sorted(txt_dir.glob(\"fusion-t2i-*.txt\"))\n",
|
| 457 |
-
"print(f\"Found {len(txt_files)} files to clean\")\n",
|
| 458 |
"\n",
|
| 459 |
-
"
|
| 460 |
-
"# Cleaning function\n",
|
| 461 |
-
"# ============================================================\n",
|
| 462 |
-
"def clean_pipe_before_by(text: str) -> str:\n",
|
| 463 |
-
" \"\"\"\n",
|
| 464 |
-
" Remove the '|' that sits right before an artist credit starting with 'by '.\n",
|
| 465 |
-
" Examples:\n",
|
| 466 |
-
" \"... sweater | by hyde (tabakko)|1girl\"\n",
|
| 467 |
-
" → \"... sweater by hyde (tabakko)|1girl\"\n",
|
| 468 |
-
" \"\"\"\n",
|
| 469 |
-
" # Pattern: optional whitespace + | + optional whitespace + \"by \" (case-insensitive)\n",
|
| 470 |
-
" # We keep the \"by \" and just remove the preceding |\n",
|
| 471 |
-
" cleaned = re.sub(r'\\s*\\|\\s*(by\\s)', r' \\1', text, flags=re.IGNORECASE)\n",
|
| 472 |
-
"\n",
|
| 473 |
-
" # Also clean any double spaces that may have been created\n",
|
| 474 |
-
" cleaned = re.sub(r' {2,}', ' ', cleaned)\n",
|
| 475 |
-
"\n",
|
| 476 |
-
" return cleaned\n",
|
| 477 |
"\n",
|
| 478 |
-
"#
|
| 479 |
-
"
|
| 480 |
-
"
|
| 481 |
-
"cleaned_count = 0\n",
|
| 482 |
"\n",
|
| 483 |
-
"
|
| 484 |
-
"
|
| 485 |
-
"
|
|
|
|
| 486 |
"\n",
|
| 487 |
-
"
|
| 488 |
-
"
|
| 489 |
-
"
|
| 490 |
"\n",
|
| 491 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
"\n",
|
| 493 |
"# ============================================================\n",
|
| 494 |
-
"#
|
| 495 |
"# ============================================================\n",
|
| 496 |
-
"
|
| 497 |
-
" for txt_path in sorted(txt_dir.glob(\"fusion-t2i-*.txt\")):\n",
|
| 498 |
-
" zipf.write(txt_path, arcname=txt_path.name)\n",
|
| 499 |
"\n",
|
| 500 |
-
"
|
| 501 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
],
|
| 503 |
"metadata": {
|
| 504 |
-
"id": "
|
| 505 |
},
|
| 506 |
"execution_count": null,
|
| 507 |
"outputs": []
|
|
|
|
| 4 |
"metadata": {
|
| 5 |
"colab": {
|
| 6 |
"provenance": [],
|
| 7 |
+
"gpuType": "T4",
|
| 8 |
+
"collapsed_sections": [
|
| 9 |
+
"PvAtznhla3jS",
|
| 10 |
+
"fB-SLVdVQvA4"
|
| 11 |
+
]
|
| 12 |
},
|
| 13 |
"kernelspec": {
|
| 14 |
"name": "python3",
|
|
|
|
| 23 |
{
|
| 24 |
"cell_type": "markdown",
|
| 25 |
"source": [
|
| 26 |
+
"Using https://huggingface.co/datasets/lodestones/booru-essence\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"* Dowload the parquet from the huggingface huggingface repo and save to your google drive as /content/drive/MyDrive/booru_essence.parquet and /content/drive/MyDrive/e621_essence.parquet respectively"
|
| 29 |
],
|
| 30 |
"metadata": {
|
| 31 |
+
"id": "oSQKdE5VYMoS"
|
| 32 |
+
}
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "markdown",
|
| 36 |
+
"source": [
|
| 37 |
+
"# Parquet Explorer"
|
| 38 |
+
],
|
| 39 |
+
"metadata": {
|
| 40 |
+
"id": "PvAtznhla3jS"
|
| 41 |
}
|
| 42 |
},
|
| 43 |
{
|
|
|
|
| 47 |
"drive.mount('/content/drive')"
|
| 48 |
],
|
| 49 |
"metadata": {
|
| 50 |
+
"id": "HFy5aDxM3G7O",
|
| 51 |
+
"outputId": "c64bc8f1-8416-4511-9927-04a8167402e5",
|
| 52 |
+
"colab": {
|
| 53 |
+
"base_uri": "https://localhost:8080/"
|
| 54 |
+
}
|
| 55 |
},
|
| 56 |
"execution_count": null,
|
| 57 |
+
"outputs": [
|
| 58 |
+
{
|
| 59 |
+
"output_type": "stream",
|
| 60 |
+
"name": "stdout",
|
| 61 |
+
"text": [
|
| 62 |
+
"Mounted at /content/drive\n"
|
| 63 |
+
]
|
| 64 |
+
}
|
| 65 |
+
]
|
| 66 |
},
|
| 67 |
{
|
| 68 |
"cell_type": "code",
|
| 69 |
"source": [
|
| 70 |
+
"# Step 1: Mount Google Drive\n",
|
| 71 |
+
"#from google.colab import drive\n",
|
| 72 |
+
"#drive.mount('/content/drive')\n",
|
| 73 |
"\n",
|
| 74 |
+
"#@markdown paste .parquet file stored on your Google Drive folder to see its characteristics\n",
|
| 75 |
"\n",
|
| 76 |
+
"# Step 2: Import required libraries\n",
|
|
|
|
|
|
|
|
|
|
| 77 |
"import pandas as pd\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
"\n",
|
| 79 |
+
"# Step 3: Define the path to the Parquet file\n",
|
| 80 |
+
"file_path = '/content/drive/MyDrive/booru_essence.parquet' #@param {type:'string'}\n",
|
|
|
|
|
|
|
| 81 |
"\n",
|
| 82 |
+
"# Step 4: Read the Parquet file\n",
|
| 83 |
"df = pd.read_parquet(file_path)\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
"\n",
|
| 85 |
+
"# Step 5: Basic exploration of the Parquet file\n",
|
| 86 |
+
"print(\"First 5 rows of the dataset:\")\n",
|
| 87 |
+
"print(df.head())\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
"\n",
|
| 89 |
+
"print(\"\\nDataset Info:\")\n",
|
| 90 |
+
"print(df.info())\n",
|
| 91 |
"\n",
|
| 92 |
+
"print(\"\\nBasic Statistics:\")\n",
|
| 93 |
+
"print(df.describe())\n",
|
|
|
|
|
|
|
| 94 |
"\n",
|
| 95 |
+
"print(\"\\nColumn Names:\")\n",
|
| 96 |
+
"print(df.columns.tolist())\n",
|
|
|
|
| 97 |
"\n",
|
| 98 |
+
"print(\"\\nMissing Values:\")\n",
|
| 99 |
+
"print(df.isnull().sum())\n",
|
| 100 |
"\n",
|
| 101 |
+
"# Optional: Display number of rows and columns\n",
|
| 102 |
+
"print(f\"\\nShape of the dataset: {df.shape}\")"
|
| 103 |
+
],
|
| 104 |
+
"metadata": {
|
| 105 |
+
"id": "So-PKtbo5AVA",
|
| 106 |
+
"colab": {
|
| 107 |
+
"base_uri": "https://localhost:8080/"
|
| 108 |
+
},
|
| 109 |
+
"outputId": "f029c8f6-d8b7-424b-ec0e-a98fda652b34"
|
| 110 |
+
},
|
| 111 |
+
"execution_count": null,
|
| 112 |
+
"outputs": [
|
| 113 |
+
{
|
| 114 |
+
"output_type": "stream",
|
| 115 |
+
"name": "stdout",
|
| 116 |
+
"text": [
|
| 117 |
+
"First 5 rows of the dataset:\n",
|
| 118 |
+
" url image_width image_height \\\n",
|
| 119 |
+
"0 1c3e0a16ca474016d2d870605e607c14 2507 3541 \n",
|
| 120 |
+
"1 1d97251e72ac672625531d6da2754e0a 706 1000 \n",
|
| 121 |
+
"2 ce657d922d6b3689d759c5e1a7a0136d 644 1000 \n",
|
| 122 |
+
"3 2bf4eed7892f89f70f4f77ca8bc5fe5a 1000 690 \n",
|
| 123 |
+
"4 3eb89d65e51046de960e50913ad15065 850 1200 \n",
|
| 124 |
+
"\n",
|
| 125 |
+
" brief_summary \\\n",
|
| 126 |
+
"0 None \n",
|
| 127 |
+
"1 NSFW anime image of Reimu Hakurei receiving an... \n",
|
| 128 |
+
"2 None \n",
|
| 129 |
+
"3 Flandre and Remilia Scarlet from *Touhou*, alo... \n",
|
| 130 |
+
"4 None \n",
|
| 131 |
+
"\n",
|
| 132 |
+
" midjourney_style_summary \\\n",
|
| 133 |
+
"0 None \n",
|
| 134 |
+
"1 Reimu Hakurei, Touhou character, anal sex, cum... \n",
|
| 135 |
+
"2 Nami, One Piece character, lifting white shirt... \n",
|
| 136 |
+
"3 Three Touhou Project characters, Flandre Scarl... \n",
|
| 137 |
+
"4 None \n",
|
| 138 |
+
"\n",
|
| 139 |
+
" tags 10_word_summary \\\n",
|
| 140 |
+
"0 1boy, 1girl, absurdres, ass, ass grab, bar cen... None \n",
|
| 141 |
+
"1 1boy, 1girl, ascot, ass, bar censor, bow, brow... None \n",
|
| 142 |
+
"2 1girl, :o, ?, areola slip, bad id, bad pixiv i... None \n",
|
| 143 |
+
"3 2girls, :d, ascot, bat wings, bed, blonde hair... None \n",
|
| 144 |
+
"4 1boy, 1girl, barefoot, blue archive, blush, bo... None \n",
|
| 145 |
+
"\n",
|
| 146 |
+
" subreddit rating fav_count ... is_ai_generated \\\n",
|
| 147 |
+
"0 None explicit 574 ... <NA> \n",
|
| 148 |
+
"1 None explicit 177 ... <NA> \n",
|
| 149 |
+
"2 None questionable 575 ... <NA> \n",
|
| 150 |
+
"3 None general 199 ... <NA> \n",
|
| 151 |
+
"4 None questionable 114 ... <NA> \n",
|
| 152 |
+
"\n",
|
| 153 |
+
" created_at solo_subject multi_subject \\\n",
|
| 154 |
+
"0 2020-03-15 02:46:12.630000-07:00 True False \n",
|
| 155 |
+
"1 2023-02-11 10:11:05.214000-08:00 True False \n",
|
| 156 |
+
"2 2022-03-16 06:31:10.380000-07:00 True False \n",
|
| 157 |
+
"3 2011-05-27 03:03:52.099000-07:00 False True \n",
|
| 158 |
+
"4 2023-10-06 12:51:35.622000-07:00 True False \n",
|
| 159 |
+
"\n",
|
| 160 |
+
" artist_weights \\\n",
|
| 161 |
+
"0 {\"ebifurya\": 7.542241240977748e-06} \n",
|
| 162 |
+
"1 {\"hammer_(sunset_beach)\": 7.783193106820236e-06} \n",
|
| 163 |
+
"2 {\"yaegashi_nan\": 8.593942374516996e-06} \n",
|
| 164 |
+
"3 {\"ruu_(tksymkw)\": 8.518144291024462e-06} \n",
|
| 165 |
+
"4 {\"kouji_(campus_life)\": 8.428515205822908e-06} \n",
|
| 166 |
+
"\n",
|
| 167 |
+
" character_weights species_weights \\\n",
|
| 168 |
+
"0 {\"hibiki_(kancolle)\": 8.120579857377395e-06} {} \n",
|
| 169 |
+
"1 {\"hakurei_reimu\": 4.829851527251942e-06} {} \n",
|
| 170 |
+
"2 {\"nami_(one_piece)\": 1.105513713098661e-05} {} \n",
|
| 171 |
+
"3 {\"bowser\": 8.348764728924828e-06, \"flandre_sca... {} \n",
|
| 172 |
+
"4 {\"yuzu_(blue_archive)\": 1.4766504894334119e-05} {} \n",
|
| 173 |
+
"\n",
|
| 174 |
+
" sampling_probability source trigger_tag \n",
|
| 175 |
+
"0 0.233719 danbooru by ebifurya \n",
|
| 176 |
+
"1 0.196160 danbooru by hammer (sunset beach) \n",
|
| 177 |
+
"2 0.284688 danbooru by yaegashi nan \n",
|
| 178 |
+
"3 0.228091 danbooru by ruu (tksymkw) \n",
|
| 179 |
+
"4 0.313498 danbooru by kouji (campus life) \n",
|
| 180 |
+
"\n",
|
| 181 |
+
"[5 rows x 21 columns]\n",
|
| 182 |
+
"\n",
|
| 183 |
+
"Dataset Info:\n",
|
| 184 |
+
"<class 'pandas.core.frame.DataFrame'>\n",
|
| 185 |
+
"RangeIndex: 19987 entries, 0 to 19986\n",
|
| 186 |
+
"Data columns (total 21 columns):\n",
|
| 187 |
+
" # Column Non-Null Count Dtype \n",
|
| 188 |
+
"--- ------ -------------- ----- \n",
|
| 189 |
+
" 0 url 19987 non-null object \n",
|
| 190 |
+
" 1 image_width 19987 non-null int64 \n",
|
| 191 |
+
" 2 image_height 19987 non-null int64 \n",
|
| 192 |
+
" 3 brief_summary 11230 non-null object \n",
|
| 193 |
+
" 4 midjourney_style_summary 12797 non-null object \n",
|
| 194 |
+
" 5 tags 19987 non-null object \n",
|
| 195 |
+
" 6 10_word_summary 0 non-null object \n",
|
| 196 |
+
" 7 subreddit 0 non-null object \n",
|
| 197 |
+
" 8 rating 19987 non-null object \n",
|
| 198 |
+
" 9 fav_count 19987 non-null int64 \n",
|
| 199 |
+
" 10 is_nsfw 0 non-null boolean \n",
|
| 200 |
+
" 11 is_ai_generated 0 non-null boolean \n",
|
| 201 |
+
" 12 created_at 19987 non-null datetime64[us, America/Los_Angeles]\n",
|
| 202 |
+
" 13 solo_subject 19987 non-null bool \n",
|
| 203 |
+
" 14 multi_subject 19987 non-null bool \n",
|
| 204 |
+
" 15 artist_weights 19987 non-null object \n",
|
| 205 |
+
" 16 character_weights 19987 non-null object \n",
|
| 206 |
+
" 17 species_weights 19987 non-null object \n",
|
| 207 |
+
" 18 sampling_probability 19987 non-null float32 \n",
|
| 208 |
+
" 19 source 19987 non-null object \n",
|
| 209 |
+
" 20 trigger_tag 19987 non-null object \n",
|
| 210 |
+
"dtypes: bool(2), boolean(2), datetime64[us, America/Los_Angeles](1), float32(1), int64(3), object(12)\n",
|
| 211 |
+
"memory usage: 2.6+ MB\n",
|
| 212 |
+
"None\n",
|
| 213 |
+
"\n",
|
| 214 |
+
"Basic Statistics:\n",
|
| 215 |
+
" image_width image_height fav_count sampling_probability\n",
|
| 216 |
+
"count 19987.000000 19987.000000 19987.000000 19987.000000\n",
|
| 217 |
+
"mean 1631.031120 1929.382298 209.043678 0.616547\n",
|
| 218 |
+
"std 1016.239287 1201.701401 193.320715 0.178348\n",
|
| 219 |
+
"min 250.000000 240.000000 0.000000 0.106295\n",
|
| 220 |
+
"25% 952.000000 1097.000000 79.000000 0.492193\n",
|
| 221 |
+
"50% 1311.000000 1540.000000 152.000000 0.593897\n",
|
| 222 |
+
"75% 2048.000000 2400.000000 277.000000 0.722180\n",
|
| 223 |
+
"max 29700.000000 27772.000000 2633.000000 1.382085\n",
|
| 224 |
+
"\n",
|
| 225 |
+
"Column Names:\n",
|
| 226 |
+
"['url', 'image_width', 'image_height', 'brief_summary', 'midjourney_style_summary', 'tags', '10_word_summary', 'subreddit', 'rating', 'fav_count', 'is_nsfw', 'is_ai_generated', 'created_at', 'solo_subject', 'multi_subject', 'artist_weights', 'character_weights', 'species_weights', 'sampling_probability', 'source', 'trigger_tag']\n",
|
| 227 |
+
"\n",
|
| 228 |
+
"Missing Values:\n",
|
| 229 |
+
"url 0\n",
|
| 230 |
+
"image_width 0\n",
|
| 231 |
+
"image_height 0\n",
|
| 232 |
+
"brief_summary 8757\n",
|
| 233 |
+
"midjourney_style_summary 7190\n",
|
| 234 |
+
"tags 0\n",
|
| 235 |
+
"10_word_summary 19987\n",
|
| 236 |
+
"subreddit 19987\n",
|
| 237 |
+
"rating 0\n",
|
| 238 |
+
"fav_count 0\n",
|
| 239 |
+
"is_nsfw 19987\n",
|
| 240 |
+
"is_ai_generated 19987\n",
|
| 241 |
+
"created_at 0\n",
|
| 242 |
+
"solo_subject 0\n",
|
| 243 |
+
"multi_subject 0\n",
|
| 244 |
+
"artist_weights 0\n",
|
| 245 |
+
"character_weights 0\n",
|
| 246 |
+
"species_weights 0\n",
|
| 247 |
+
"sampling_probability 0\n",
|
| 248 |
+
"source 0\n",
|
| 249 |
+
"trigger_tag 0\n",
|
| 250 |
+
"dtype: int64\n",
|
| 251 |
+
"\n",
|
| 252 |
+
"Shape of the dataset: (19987, 21)\n"
|
| 253 |
+
]
|
| 254 |
+
}
|
| 255 |
+
]
|
| 256 |
+
},
|
| 257 |
+
{
|
| 258 |
+
"cell_type": "markdown",
|
| 259 |
+
"source": [
|
| 260 |
+
"# Create text files from parquet\n",
|
| 261 |
"\n",
|
| 262 |
+
"Goals:\n",
|
| 263 |
+
"1) First , sort each parquet item inte separate groups. First by the parquet columns 'solo_subject' vs 'multi_subject rating'. These columns are boolean values with True / False possible values. You will now have four boolean groups.\n",
|
|
|
|
| 264 |
"\n",
|
| 265 |
+
"2) Then for each of these four boolean groups , sort the contents by its 'rating' category. The ratings are a string , and can be 'explicit' , 'sensitive' , 'questionable' or 'general'. You should now have 16 groups. Assign a name to each group.\n",
|
|
|
|
| 266 |
"\n",
|
| 267 |
+
"3) For each of the 16 groups , append the texts from the parquet columns 'brief_summary' , 'midjourney_style_summary' , and 'tags' into a single text for each item. \n",
|
|
|
|
|
|
|
| 268 |
"\n",
|
| 269 |
+
"4) Once each item has the full descriptive text , replace all double quations \" with single quotations ' . \n",
|
|
|
|
|
|
|
| 270 |
"\n",
|
| 271 |
+
"5) Make sure all commas , have one space before and one space after them.\n",
|
| 272 |
"\n",
|
| 273 |
+
"5) For each text , remove all symbols of the following types: { , } ,] , [ , | , ^ , / , \\\n",
|
|
|
|
|
|
|
| 274 |
"\n",
|
| 275 |
+
"6) Replace all captilized letters for lowercase. Except for at the start of a sentence , or at the start of a quatation , for example 'Tree'\n",
|
|
|
|
|
|
|
|
|
|
| 276 |
"\n",
|
| 277 |
+
"7) Once the T5 model is loaded , truncate the text for each item to 500 tokens in length\n",
|
|
|
|
|
|
|
|
|
|
| 278 |
"\n",
|
| 279 |
+
"8) Encode the full descriptive texts using the T5 for each of the 12 group.\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
"\n",
|
| 281 |
+
"9) For each of the 12 groups , put similiar texts into folders using K-means. Each group may contain a maximum of 100 folders of similiar texts.\n",
|
|
|
|
|
|
|
|
|
|
| 282 |
"\n",
|
| 283 |
+
"10) For each folder , create a txt file with the contents , using the format shown in the code. The dataset string should be enclosed with the {} symbols and each text should be separated by the | symbol , {text1|text2|....|text1000} . This txt file is javascript for a website. Make sure the indentation is correct. The name of the group and folder id should be included in the javascript as strings. Each txt file may contain a maximum of 1000 texts. Should the amount be exceeded , create more txt files until all texts within that similarity category are listed.\n",
|
|
|
|
|
|
|
| 284 |
"\n",
|
| 285 |
+
"11) Package the txt files into a single zip file. The folder structure within the zip file should be sorted by its 12 groups , then by the similiar texts found within each folder.\n",
|
| 286 |
+
"\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
],
|
| 288 |
"metadata": {
|
| 289 |
+
"id": "fB-SLVdVQvA4"
|
| 290 |
+
}
|
|
|
|
|
|
|
| 291 |
},
|
| 292 |
{
|
| 293 |
"cell_type": "code",
|
| 294 |
"source": [
|
| 295 |
+
"#@title Complete Pipeline – All groups → T5 → KMeans → JS TXT files → ZIP\n",
|
| 296 |
+
"# Merges booru_essence.parquet + e621_essence.parquet\n",
|
| 297 |
+
"# NEW: any txt file < 300 items is merged with other small clusters\n",
|
| 298 |
+
"\n",
|
| 299 |
+
"!pip install -q transformers scikit-learn tqdm\n",
|
| 300 |
+
"\n",
|
| 301 |
+
"from google.colab import drive\n",
|
| 302 |
+
"drive.mount('/content/drive')\n",
|
| 303 |
"\n",
|
| 304 |
"import os\n",
|
| 305 |
"import re\n",
|
| 306 |
"import zipfile\n",
|
| 307 |
+
"import numpy as np\n",
|
| 308 |
+
"import pandas as pd\n",
|
| 309 |
+
"import torch\n",
|
| 310 |
"from pathlib import Path\n",
|
| 311 |
+
"from transformers import T5Tokenizer, T5Model\n",
|
| 312 |
+
"from sklearn.cluster import KMeans\n",
|
| 313 |
"from tqdm.auto import tqdm\n",
|
| 314 |
+
"import warnings\n",
|
| 315 |
+
"warnings.filterwarnings(\"ignore\")\n",
|
| 316 |
"\n",
|
| 317 |
"# ============================================================\n",
|
| 318 |
"# Configuration\n",
|
| 319 |
"# ============================================================\n",
|
| 320 |
+
"file_path_booru = \"/content/drive/MyDrive/booru_essence.parquet\"\n",
|
| 321 |
+
"file_path_e621 = \"/content/drive/MyDrive/e621_essence.parquet\"\n",
|
| 322 |
+
"t5_model_name = \"t5-base\" #@param [\"t5-small\", \"t5-base\"]\n",
|
| 323 |
+
"max_tokens = 500 #@param {type:\"slider\", min:128, max:512, step:32}\n",
|
| 324 |
+
"batch_size = 1 # fixed to 1\n",
|
| 325 |
+
"max_clusters_per_group = 10\n",
|
| 326 |
+
"max_texts_per_file = 1000\n",
|
| 327 |
+
"min_texts_per_file = 300 # NEW: minimum size before merging\n",
|
| 328 |
"\n",
|
| 329 |
+
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 330 |
+
"print(f\"Device: {device}\")\n",
|
| 331 |
+
"print(f\"Batch size: {batch_size}\")\n",
|
|
|
|
| 332 |
"\n",
|
| 333 |
+
"output_base = Path(\"/content/output\")\n",
|
| 334 |
+
"js_root = output_base / \"js_files\"\n",
|
| 335 |
+
"os.makedirs(js_root, exist_ok=True)\n",
|
|
|
|
|
|
|
| 336 |
"\n",
|
| 337 |
"# ============================================================\n",
|
| 338 |
+
"# 0. Load both Parquets and combine\n",
|
| 339 |
"# ============================================================\n",
|
| 340 |
+
"print(f\"Loading: {file_path_booru}\")\n",
|
| 341 |
+
"df_booru = pd.read_parquet(file_path_booru)\n",
|
| 342 |
+
"print(f\" Shape: {df_booru.shape}\")\n",
|
| 343 |
"\n",
|
| 344 |
+
"print(f\"Loading: {file_path_e621}\")\n",
|
| 345 |
+
"df_e621 = pd.read_parquet(file_path_e621)\n",
|
| 346 |
+
"print(f\" Shape: {df_e621.shape}\")\n",
|
| 347 |
+
"\n",
|
| 348 |
+
"assert list(df_booru.columns) == list(df_e621.columns), \\\n",
|
| 349 |
+
" \"Column structures differ between the two Parquet files!\"\n",
|
| 350 |
+
"\n",
|
| 351 |
+
"df = pd.concat([df_booru, df_e621], ignore_index=True)\n",
|
| 352 |
+
"print(f\"\\nCombined shape: {df.shape}\")\n",
|
| 353 |
+
"print(f\"Columns: {df.columns.tolist()}\")\n",
|
| 354 |
+
"\n",
|
| 355 |
+
"assert \"solo_subject\" in df.columns and df[\"solo_subject\"].dtype == bool\n",
|
| 356 |
+
"assert \"multi_subject\" in df.columns and df[\"multi_subject\"].dtype == bool\n",
|
| 357 |
+
"assert \"rating\" in df.columns\n",
|
| 358 |
+
"print(\"Unique ratings:\", df[\"rating\"].unique().tolist())\n",
|
| 359 |
+
"print(\"solo_subject value counts:\\n\", df[\"solo_subject\"].value_counts())\n",
|
| 360 |
+
"print(\"multi_subject value counts:\\n\", df[\"multi_subject\"].value_counts())\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
"\n",
|
| 362 |
"# ============================================================\n",
|
| 363 |
+
"# Helper: Text cleaning (Goals 3 → 6)\n",
|
| 364 |
"# ============================================================\n",
|
| 365 |
+
"def clean_text(text: str) -> str:\n",
|
| 366 |
+
" if not isinstance(text, str) or not text.strip():\n",
|
| 367 |
+
" return \"\"\n",
|
| 368 |
+
"\n",
|
| 369 |
+
" text = text.strip()\n",
|
| 370 |
+
" text = text.replace('\"', \"'\")\n",
|
| 371 |
+
" text = re.sub(r'\\s*,\\s*', ' , ', text)\n",
|
| 372 |
+
" text = re.sub(r'[{}\\[\\]\\|\\^/\\\\]', '', text)\n",
|
| 373 |
+
" text = re.sub(r'\\s+', ' ', text).strip()\n",
|
| 374 |
+
"\n",
|
| 375 |
+
" def smart_case(s):\n",
|
| 376 |
+
" parts = re.split(r'([.!?]\\s+)', s)\n",
|
| 377 |
+
" result = []\n",
|
| 378 |
+
" capitalize_next = True\n",
|
| 379 |
+
"\n",
|
| 380 |
+
" for part in parts:\n",
|
| 381 |
+
" if not part:\n",
|
| 382 |
+
" continue\n",
|
| 383 |
+
"\n",
|
| 384 |
+
" if capitalize_next and part and part[0].isalpha():\n",
|
| 385 |
+
" part = part[0].upper() + part[1:].lower()\n",
|
| 386 |
+
" else:\n",
|
| 387 |
+
" part = part.lower()\n",
|
| 388 |
+
"\n",
|
| 389 |
+
" part = re.sub(\n",
|
| 390 |
+
" r\"(['‘’])([a-z])\",\n",
|
| 391 |
+
" lambda m: m.group(1) + m.group(2).upper(),\n",
|
| 392 |
+
" part\n",
|
| 393 |
+
" )\n",
|
| 394 |
+
"\n",
|
| 395 |
+
" result.append(part)\n",
|
| 396 |
+
" capitalize_next = part.endswith(('.', '!', '?')) or bool(re.search(r'[.!?]\\s*$', part))\n",
|
| 397 |
+
"\n",
|
| 398 |
+
" return ''.join(result)\n",
|
| 399 |
+
"\n",
|
| 400 |
+
" return smart_case(text)\n",
|
| 401 |
+
"\n",
|
| 402 |
+
"\n",
|
| 403 |
+
"def combine_and_clean(row) -> str:\n",
|
| 404 |
+
" parts = []\n",
|
| 405 |
+
" for col in [\"brief_summary\", \"midjourney_style_summary\", \"tags\"]:\n",
|
| 406 |
+
" val = row.get(col)\n",
|
| 407 |
+
" if pd.isna(val) or val is None:\n",
|
| 408 |
+
" continue\n",
|
| 409 |
+
" t = str(val).strip()\n",
|
| 410 |
+
" if t and t.lower() != \"none\":\n",
|
| 411 |
+
" parts.append(t)\n",
|
| 412 |
+
" combined = \" \".join(parts)\n",
|
| 413 |
+
" return clean_text(combined)\n",
|
| 414 |
"\n",
|
| 415 |
"# ============================================================\n",
|
| 416 |
+
"# 1 + 2. Create the groups (includes 'sensitive')\n",
|
| 417 |
"# ============================================================\n",
|
| 418 |
+
"boolean_masks = {\n",
|
| 419 |
+
" \"solo_true__multi_false\": (df[\"solo_subject\"] == True) & (df[\"multi_subject\"] == False),\n",
|
| 420 |
+
" \"solo_false__multi_true\": (df[\"solo_subject\"] == False) & (df[\"multi_subject\"] == True),\n",
|
| 421 |
+
" \"solo_true__multi_true\": (df[\"solo_subject\"] == True) & (df[\"multi_subject\"] == True),\n",
|
| 422 |
+
" \"solo_false__multi_false\": (df[\"solo_subject\"] == False) & (df[\"multi_subject\"] == False),\n",
|
| 423 |
+
"}\n",
|
| 424 |
"\n",
|
| 425 |
+
"ratings = [\"explicit\", \"questionable\", \"general\", \"sensitive\"]\n",
|
| 426 |
+
"ratings = [r for r in ratings if r in df[\"rating\"].unique()]\n",
|
| 427 |
"\n",
|
| 428 |
+
"groups = {}\n",
|
|
|
|
|
|
|
| 429 |
"\n",
|
| 430 |
+
"print(\"\\nCreating groups...\")\n",
|
| 431 |
+
"for bname, mask in boolean_masks.items():\n",
|
| 432 |
+
" subset = df[mask]\n",
|
| 433 |
+
" for rating in ratings:\n",
|
| 434 |
+
" gname = f\"{bname}__{rating}\"\n",
|
| 435 |
+
" gdf = subset[subset[\"rating\"] == rating].copy().reset_index(drop=True)\n",
|
| 436 |
+
" groups[gname] = gdf\n",
|
| 437 |
+
" print(f\" {gname:45s} → {len(gdf):5d} rows\")\n",
|
| 438 |
"\n",
|
| 439 |
+
"print(f\"\\nTotal groups: {len(groups)}\")\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
"\n",
|
| 441 |
+
"# ============================================================\n",
|
| 442 |
+
"# Load T5 encoder\n",
|
| 443 |
+
"# ============================================================\n",
|
| 444 |
+
"print(f\"\\nLoading {t5_model_name}...\")\n",
|
| 445 |
+
"tokenizer = T5Tokenizer.from_pretrained(t5_model_name)\n",
|
| 446 |
+
"model = T5Model.from_pretrained(t5_model_name).to(device)\n",
|
| 447 |
+
"model.eval()\n",
|
| 448 |
+
"\n",
|
| 449 |
+
"def get_t5_embeddings(texts):\n",
|
| 450 |
+
" all_embs = []\n",
|
| 451 |
+
" for i in tqdm(range(0, len(texts), batch_size), desc=\"Encoding\", leave=False):\n",
|
| 452 |
+
" batch = texts[i : i + batch_size]\n",
|
| 453 |
+
" inputs = tokenizer(\n",
|
| 454 |
+
" batch,\n",
|
| 455 |
+
" return_tensors=\"pt\",\n",
|
| 456 |
+
" padding=True,\n",
|
| 457 |
+
" truncation=True,\n",
|
| 458 |
+
" max_length=max_tokens,\n",
|
| 459 |
+
" ).to(device)\n",
|
| 460 |
+
"\n",
|
| 461 |
+
" with torch.no_grad():\n",
|
| 462 |
+
" outputs = model.encoder(**inputs)\n",
|
| 463 |
+
" last_hidden = outputs.last_hidden_state\n",
|
| 464 |
+
" mask = inputs.attention_mask.unsqueeze(-1).float()\n",
|
| 465 |
+
" summed = (last_hidden * mask).sum(dim=1)\n",
|
| 466 |
+
" counts = mask.sum(dim=1).clamp(min=1e-9)\n",
|
| 467 |
+
" mean_pooled = summed / counts\n",
|
| 468 |
+
" all_embs.append(mean_pooled.cpu().numpy())\n",
|
| 469 |
+
" return np.vstack(all_embs)\n",
|
| 470 |
+
"\n",
|
| 471 |
+
"# ============================================================\n",
|
| 472 |
+
"# Helper: write one JS file\n",
|
| 473 |
+
"# ============================================================\n",
|
| 474 |
+
"def write_js_file(cluster_dir, gname, folder_id, part_idx, chunk):\n",
|
| 475 |
+
" dataset_str = \"{\" + \"|\".join(chunk) + \"}\"\n",
|
| 476 |
"\n",
|
|
|
|
| 477 |
" content = f\"\"\"//start\n",
|
| 478 |
+
"group = \"{gname}\"\n",
|
| 479 |
+
"folder_id = \"{folder_id}\"\n",
|
| 480 |
+
"file_index = {part_idx + 1}\n",
|
| 481 |
+
"name = \"booru_essence\"\n",
|
| 482 |
+
"emoji = \"🏖️\"\n",
|
| 483 |
+
"showComments = true\n",
|
| 484 |
+
"showDemo = true\n",
|
| 485 |
+
"\n",
|
| 486 |
+
"pageTitle = [emoji] fusion-t2i-[name]-[group]-[folder_id]-[file_index]\n",
|
| 487 |
+
"pageDescription = Captioned works from booru parquet – {gname} – {folder_id}\n",
|
| 488 |
"\n",
|
| 489 |
"tagBlackListRaw = [\"\"]\n",
|
| 490 |
+
"tagBlackList = [tagBlackListRaw.replaceText(' ' , ',')]\n",
|
| 491 |
"\n",
|
| 492 |
"// Generator browse destinations\n",
|
| 493 |
+
"backward = []\n",
|
| 494 |
+
"forward = []\n",
|
| 495 |
+
"backward_em = {{emoji}}\n",
|
| 496 |
+
"forward_em = {{emoji}}\n",
|
| 497 |
+
"numItemsStart = 1\n",
|
| 498 |
"//----//\n",
|
| 499 |
"\n",
|
| 500 |
+
"image_url = [\"https://www.iguides.ru/upload/iblock/ccf/ccf3e83b95a8b350d5089424687fc133.jpg\"]\n",
|
| 501 |
+
"image_description = [\"placeholder\"]\n",
|
| 502 |
+
"optional_image_caption = [\"placeholder\"]\n",
|
|
|
|
| 503 |
"\n",
|
| 504 |
"$meta\n",
|
| 505 |
+
" title = [emoji] [name] [group] [folder_id] [file_index]\n",
|
| 506 |
+
" description = Subset of {gname} – {folder_id} – part {part_idx+1}\n",
|
| 507 |
+
" image = https://i.imgur.com/TPHIzaa.jpeg\n",
|
| 508 |
"$output\n",
|
| 509 |
" [dataset]\n",
|
| 510 |
"\n",
|
|
|
|
| 511 |
"data = [(($output.selectMany(document.numItems).join(\" \")+' '))]\n",
|
| 512 |
"\n",
|
| 513 |
"dataset\n",
|
|
|
|
| 515 |
"//----//\n",
|
| 516 |
"\"\"\"\n",
|
| 517 |
"\n",
|
| 518 |
+
" filename = f\"fusion-t2i-booru_essence-{gname}-{folder_id}-part{part_idx+1:02d}.txt\"\n",
|
| 519 |
+
" with open(cluster_dir / filename, \"w\", encoding=\"utf-8\") as f:\n",
|
|
|
|
|
|
|
| 520 |
" f.write(content)\n",
|
| 521 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
"# ============================================================\n",
|
| 523 |
+
"# Process every group (Goals 3–10 + merge small files)\n",
|
| 524 |
"# ============================================================\n",
|
| 525 |
+
"for gname, group in groups.items():\n",
|
| 526 |
+
" print(f\"\\n{'='*70}\")\n",
|
| 527 |
+
" print(f\"Processing: {gname}\")\n",
|
| 528 |
+
" print(f\"{'='*70}\")\n",
|
| 529 |
"\n",
|
| 530 |
+
" group = group.copy()\n",
|
| 531 |
+
" group[\"cleaned_text\"] = group.apply(combine_and_clean, axis=1)\n",
|
| 532 |
+
" group = group[group[\"cleaned_text\"].str.len() > 10].reset_index(drop=True)\n",
|
| 533 |
+
" print(f\" After cleaning: {len(group)} usable rows\")\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
"\n",
|
| 535 |
+
" if len(group) < 2:\n",
|
| 536 |
+
" print(\" → Skipping (too few samples)\")\n",
|
| 537 |
+
" continue\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
"\n",
|
| 539 |
+
" texts = group[\"cleaned_text\"].tolist()\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
"\n",
|
| 541 |
+
" # Goal 8: T5 embeddings\n",
|
| 542 |
+
" embeddings = get_t5_embeddings(texts)\n",
|
| 543 |
+
" print(f\" Embeddings shape: {embeddings.shape}\")\n",
|
|
|
|
| 544 |
"\n",
|
| 545 |
+
" # Goal 9: K-Means\n",
|
| 546 |
+
" k = min(max_clusters_per_group, max(2, len(group) // 8))\n",
|
| 547 |
+
" k = min(k, len(group))\n",
|
| 548 |
+
" print(f\" Using k = {k}\")\n",
|
| 549 |
"\n",
|
| 550 |
+
" kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)\n",
|
| 551 |
+
" labels = kmeans.fit_predict(embeddings)\n",
|
| 552 |
+
" group[\"cluster\"] = labels\n",
|
| 553 |
"\n",
|
| 554 |
+
" # Collect texts per cluster\n",
|
| 555 |
+
" cluster_texts_list = []\n",
|
| 556 |
+
" for c in range(k):\n",
|
| 557 |
+
" cluster_texts = group.loc[group[\"cluster\"] == c, \"cleaned_text\"].tolist()\n",
|
| 558 |
+
" if cluster_texts:\n",
|
| 559 |
+
" cluster_texts_list.append(cluster_texts)\n",
|
| 560 |
+
"\n",
|
| 561 |
+
" # ----------------------------------------------------------\n",
|
| 562 |
+
" # NEW: Separate large (≥300) and small (<300) clusters\n",
|
| 563 |
+
" # ----------------------------------------------------------\n",
|
| 564 |
+
" large_clusters = []\n",
|
| 565 |
+
" small_pool = []\n",
|
| 566 |
+
"\n",
|
| 567 |
+
" for texts in cluster_texts_list:\n",
|
| 568 |
+
" if len(texts) >= min_texts_per_file:\n",
|
| 569 |
+
" large_clusters.append(texts)\n",
|
| 570 |
+
" else:\n",
|
| 571 |
+
" small_pool.extend(texts)\n",
|
| 572 |
+
"\n",
|
| 573 |
+
" print(f\" Large clusters (≥{min_texts_per_file}): {len(large_clusters)}\")\n",
|
| 574 |
+
" print(f\" Texts moved to small pool: {len(small_pool)}\")\n",
|
| 575 |
+
"\n",
|
| 576 |
+
" # Re-pack the small pool into chunks of size [min_texts_per_file .. max_texts_per_file]\n",
|
| 577 |
+
" merged_small_clusters = []\n",
|
| 578 |
+
" if small_pool:\n",
|
| 579 |
+
" for start in range(0, len(small_pool), max_texts_per_file):\n",
|
| 580 |
+
" chunk = small_pool[start : start + max_texts_per_file]\n",
|
| 581 |
+
" # If the last chunk is still tiny and we have previous merged chunks,\n",
|
| 582 |
+
" # try to attach it to the previous one (if it doesn't exceed max)\n",
|
| 583 |
+
" if (len(chunk) < min_texts_per_file and merged_small_clusters\n",
|
| 584 |
+
" and len(merged_small_clusters[-1]) + len(chunk) <= max_texts_per_file):\n",
|
| 585 |
+
" merged_small_clusters[-1].extend(chunk)\n",
|
| 586 |
+
" else:\n",
|
| 587 |
+
" merged_small_clusters.append(chunk)\n",
|
| 588 |
+
"\n",
|
| 589 |
+
" # Final list of folders to write\n",
|
| 590 |
+
" final_folders = large_clusters + merged_small_clusters\n",
|
| 591 |
+
" print(f\" Final folders after merging: {len(final_folders)}\")\n",
|
| 592 |
+
"\n",
|
| 593 |
+
" # Create group directory\n",
|
| 594 |
+
" group_dir = js_root / gname\n",
|
| 595 |
+
" os.makedirs(group_dir, exist_ok=True)\n",
|
| 596 |
+
"\n",
|
| 597 |
+
" for folder_idx, folder_texts in enumerate(final_folders):\n",
|
| 598 |
+
" folder_id = f\"cluster_{folder_idx:03d}\"\n",
|
| 599 |
+
" cluster_dir = group_dir / folder_id\n",
|
| 600 |
+
" os.makedirs(cluster_dir, exist_ok=True)\n",
|
| 601 |
+
"\n",
|
| 602 |
+
" # Split this folder into files of ≤ 1000 (already mostly handled, but keep safe)\n",
|
| 603 |
+
" for part_idx, start in enumerate(range(0, len(folder_texts), max_texts_per_file)):\n",
|
| 604 |
+
" chunk = folder_texts[start : start + max_texts_per_file]\n",
|
| 605 |
+
" write_js_file(cluster_dir, gname, folder_id, part_idx, chunk)\n",
|
| 606 |
+
"\n",
|
| 607 |
+
" print(f\" → Saved under {group_dir}\")\n",
|
| 608 |
"\n",
|
| 609 |
"# ============================================================\n",
|
| 610 |
+
"# Goal 11: Create ZIP with correct hierarchical structure\n",
|
| 611 |
"# ============================================================\n",
|
| 612 |
+
"zip_path = \"/content/output/booru_essence_js_files.zip\"\n",
|
|
|
|
|
|
|
| 613 |
"\n",
|
| 614 |
+
"with zipfile.ZipFile(zip_path, \"w\", zipfile.ZIP_DEFLATED) as zipf:\n",
|
| 615 |
+
" for root, dirs, files in os.walk(js_root):\n",
|
| 616 |
+
" for file in files:\n",
|
| 617 |
+
" if file.endswith(\".txt\"):\n",
|
| 618 |
+
" full = os.path.join(root, file)\n",
|
| 619 |
+
" arcname = os.path.relpath(full, js_root)\n",
|
| 620 |
+
" zipf.write(full, arcname=arcname)\n",
|
| 621 |
+
"\n",
|
| 622 |
+
"print(\"\\n\" + \"=\"*70)\n",
|
| 623 |
+
"print(\"✅ ALL DONE\")\n",
|
| 624 |
+
"print(f\"ZIP file: {zip_path}\")\n",
|
| 625 |
+
"print(\"Structure inside ZIP:\")\n",
|
| 626 |
+
"print(\" solo_true__multi_false__explicit/\")\n",
|
| 627 |
+
"print(\" cluster_000/\")\n",
|
| 628 |
+
"print(\" fusion-t2i-....txt\")\n",
|
| 629 |
+
"print(\" ...\")\n",
|
| 630 |
+
"print(\" ... (all groups including *.__sensitive)\")\n",
|
| 631 |
+
"print(\"=\"*70)\n",
|
| 632 |
+
"print(\"Download the ZIP from the Colab file browser (left panel).\")"
|
| 633 |
],
|
| 634 |
"metadata": {
|
| 635 |
+
"id": "SYVWeJZUf6SC"
|
| 636 |
},
|
| 637 |
"execution_count": null,
|
| 638 |
"outputs": []
|