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
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"source": [
|
| 20 |
+
"# ============================================================\n",
|
| 21 |
+
"# Cell 1 β Install & setup\n",
|
| 22 |
+
"# ============================================================\n",
|
| 23 |
+
"!pip install -q cheesechaser pillow matplotlib\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"import os\n",
|
| 26 |
+
"from google.colab import userdata\n",
|
| 27 |
+
"from pathlib import Path\n",
|
| 28 |
+
"import random\n",
|
| 29 |
+
"import json\n",
|
| 30 |
+
"from PIL import Image\n",
|
| 31 |
+
"import matplotlib.pyplot as plt\n",
|
| 32 |
+
"from cheesechaser.datapool import SankakuWebpDataPool\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"# Load HF token from Colab secrets\n",
|
| 35 |
+
"HF_TOKEN = userdata.get('HF_TOKEN')\n",
|
| 36 |
+
"if not HF_TOKEN:\n",
|
| 37 |
+
" raise ValueError(\"Please add HF_TOKEN to Colab secrets (π icon on the left)\")\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"os.environ[\"HF_TOKEN\"] = HF_TOKEN\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"# ============================================================\n",
|
| 42 |
+
"# Cell 2 β Download a small sample (β 30β40 images)\n",
|
| 43 |
+
"# ============================================================\n",
|
| 44 |
+
"# Choose any range you like. IDs around 7Mβ36M are common.\n",
|
| 45 |
+
"# Using a small random-ish range so we don't download too much.\n",
|
| 46 |
+
"sample_ids = list(range(7000000, 7000040)) # 40 images β change freely\n",
|
| 47 |
+
"\n",
|
| 48 |
+
"DST_DIR = Path(\"/content/sankaku_sample\")\n",
|
| 49 |
+
"DST_DIR.mkdir(exist_ok=True, parents=True)\n",
|
| 50 |
+
"\n",
|
| 51 |
+
"pool = SankakuWebpDataPool(hf_token=HF_TOKEN)\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"print(f\"Downloading {len(sample_ids)} images β¦\")\n",
|
| 54 |
+
"pool.batch_download_to_directory(\n",
|
| 55 |
+
" resource_ids=sample_ids,\n",
|
| 56 |
+
" dst_dir=str(DST_DIR),\n",
|
| 57 |
+
" max_workers=8,\n",
|
| 58 |
+
" save_metainfo=True, # important β this creates the *_metainfo.json files\n",
|
| 59 |
+
")\n",
|
| 60 |
+
"print(\"Download finished.\")\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"# ============================================================\n",
|
| 63 |
+
"# Cell 3 β Pick ONE item and inspect its caption / tags\n",
|
| 64 |
+
"# ============================================================\n",
|
| 65 |
+
"# Find all downloaded images and their metainfo\n",
|
| 66 |
+
"images = list(DST_DIR.glob(\"*.webp\")) + list(DST_DIR.glob(\"*.jpg\")) + list(DST_DIR.glob(\"*.png\"))\n",
|
| 67 |
+
"if not images:\n",
|
| 68 |
+
" raise RuntimeError(\"No images were downloaded. Check the ID range or your HF access.\")\n",
|
| 69 |
+
"\n",
|
| 70 |
+
"# Pick one random image\n",
|
| 71 |
+
"one_img = random.choice(images)\n",
|
| 72 |
+
"meta_path = one_img.with_name(one_img.stem + \"_metainfo.json\")\n",
|
| 73 |
+
"\n",
|
| 74 |
+
"print(\"=\" * 60)\n",
|
| 75 |
+
"print(f\"Inspecting: {one_img.name}\")\n",
|
| 76 |
+
"print(\"=\" * 60)\n",
|
| 77 |
+
"\n",
|
| 78 |
+
"# Show the image\n",
|
| 79 |
+
"display(Image.open(one_img))\n",
|
| 80 |
+
"\n",
|
| 81 |
+
"# Load and pretty-print the caption / tags\n",
|
| 82 |
+
"if meta_path.exists():\n",
|
| 83 |
+
" with open(meta_path, \"r\", encoding=\"utf-8\") as f:\n",
|
| 84 |
+
" meta = json.load(f)\n",
|
| 85 |
+
"\n",
|
| 86 |
+
" print(\"\\nFull metainfo keys:\", list(meta.keys()))\n",
|
| 87 |
+
" print(\"\\n--- Caption / Tags ---\")\n",
|
| 88 |
+
"\n",
|
| 89 |
+
" # Sankaku metainfo usually contains a 'tags' list or a tag_string\n",
|
| 90 |
+
" if \"tags\" in meta:\n",
|
| 91 |
+
" # tags can be a list of dicts or a list of strings\n",
|
| 92 |
+
" tags = meta[\"tags\"]\n",
|
| 93 |
+
" if tags and isinstance(tags[0], dict):\n",
|
| 94 |
+
" tag_names = [t.get(\"name\") or t.get(\"tagName\") or str(t) for t in tags]\n",
|
| 95 |
+
" else:\n",
|
| 96 |
+
" tag_names = tags\n",
|
| 97 |
+
" print(\"Tags:\", \", \".join(tag_names))\n",
|
| 98 |
+
" elif \"tag_string\" in meta:\n",
|
| 99 |
+
" print(\"tag_string:\", meta[\"tag_string\"])\n",
|
| 100 |
+
" else:\n",
|
| 101 |
+
" # fallback β just dump the whole thing\n",
|
| 102 |
+
" print(json.dumps(meta, indent=2, ensure_ascii=False)[:2000])\n",
|
| 103 |
+
"else:\n",
|
| 104 |
+
" print(\"No metainfo JSON found for this image.\")\n",
|
| 105 |
+
"\n",
|
| 106 |
+
"# ============================================================\n",
|
| 107 |
+
"# Cell 4 β Show a random grid of the downloaded images\n",
|
| 108 |
+
"# ============================================================\n",
|
| 109 |
+
"random.shuffle(images)\n",
|
| 110 |
+
"n_show = min(16, len(images)) # up to 4Γ4 grid\n",
|
| 111 |
+
"selected = images[:n_show]\n",
|
| 112 |
+
"\n",
|
| 113 |
+
"cols = 4\n",
|
| 114 |
+
"rows = (n_show + cols - 1) // cols\n",
|
| 115 |
+
"\n",
|
| 116 |
+
"fig, axes = plt.subplots(rows, cols, figsize=(cols * 3.5, rows * 3.5))\n",
|
| 117 |
+
"axes = axes.flatten() if n_show > 1 else [axes]\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"for ax, img_path in zip(axes, selected):\n",
|
| 120 |
+
" img = Image.open(img_path)\n",
|
| 121 |
+
" ax.imshow(img)\n",
|
| 122 |
+
" ax.set_title(img_path.stem, fontsize=8)\n",
|
| 123 |
+
" ax.axis(\"off\")\n",
|
| 124 |
+
"\n",
|
| 125 |
+
"# Hide unused axes\n",
|
| 126 |
+
"for ax in axes[n_show:]:\n",
|
| 127 |
+
" ax.axis(\"off\")\n",
|
| 128 |
+
"\n",
|
| 129 |
+
"plt.tight_layout()\n",
|
| 130 |
+
"plt.show()\n",
|
| 131 |
+
"\n",
|
| 132 |
+
"print(f\"\\nShowing {n_show} random images from the sample.\")"
|
| 133 |
+
],
|
| 134 |
+
"metadata": {
|
| 135 |
+
"id": "fQeIjNHoap4Q"
|
| 136 |
+
},
|
| 137 |
+
"execution_count": null,
|
| 138 |
+
"outputs": []
|
| 139 |
+
}
|
| 140 |
+
]
|
| 141 |
+
}
|