codeShare commited on
Commit
3d3c621
·
verified ·
1 Parent(s): 199cd59

Upload sample_encrypted_dataset.ipynb

Browse files
gemma4_training/sample_encrypted_dataset.ipynb CHANGED
@@ -14,268 +14,275 @@
14
  }
15
  },
16
  "cells": [
17
- {
18
- "cell_type": "markdown",
19
- "source": [
20
- "# Sample Encrypted Dataset"
21
- ],
22
- "metadata": {
23
- "id": "DkphdgXC2gqu"
24
- }
25
- },
26
- {
27
- "cell_type": "code",
28
- "metadata": {
29
- "colab": {
30
- "base_uri": "https://localhost:8080/"
31
- },
32
- "id": "c67a868d",
33
- "outputId": "275fe56f-3b31-451e-bcb7-da39dcd139d1"
34
- },
35
- "source": [
36
- "import os\n",
37
- "from google.colab import userdata\n",
38
- "import requests\n",
39
- "\n",
40
- "# Install py7zr and pyAesCrypt for extracting and decrypting the files\n",
41
- "!pip install -q py7zr pyAesCrypt\n",
42
- "\n",
43
- "# Retrieve HF_TOKEN securely from Colab secrets\n",
44
- "try:\n",
45
- " hf_token = userdata.get('HF_TOKEN')\n",
46
- " print(\"HF_TOKEN retrieved successfully.\")\n",
47
- "except Exception as e:\n",
48
- " print(f\"Error retrieving HF_TOKEN: {e}\")\n",
49
- " hf_token = None\n",
50
- "\n",
51
- "# Define paths and URLs\n",
52
- "url = 'https://huggingface.co/codeShare/kromoss-lora-exports/resolve/main/gemma_train_set.7z'\n",
53
- "local_7z_path = 'gemma_train_set.7z'\n",
54
- "extract_dir = 'extracted_dataset'\n",
55
- "\n",
56
- "# Download the file using requests with HF authentication\n",
57
- "headers = {'Authorization': f'Bearer {hf_token}'} if hf_token else {}\n",
58
- "print(\"Downloading file...\")\n",
59
- "r = requests.get(url, headers=headers, stream=True)\n",
60
- "if r.status_code == 200:\n",
61
- " with open(local_7z_path, 'wb') as f:\n",
62
- " for chunk in r.iter_content(chunk_size=8192):\n",
63
- " f.write(chunk)\n",
64
- " print(\"Download completed.\")\n",
65
- "else:\n",
66
- " print(f\"Failed to download. Status code: {r.status_code}. Response: {r.text[:500]}\")"
67
- ],
68
- "execution_count": 1,
69
- "outputs": [
70
- {
71
- "output_type": "stream",
72
- "name": "stdout",
73
- "text": [
74
- "HF_TOKEN retrieved successfully.\n",
75
- "Downloading file...\n",
76
- "Download completed.\n"
77
- ]
78
- }
79
- ]
80
- },
81
  {
82
  "cell_type": "code",
 
83
  "metadata": {
84
- "colab": {
85
- "base_uri": "https://localhost:8080/"
86
- },
87
- "id": "856003c2",
88
- "outputId": "60dcd7ae-b0b4-415a-e60f-23877b4fc716"
89
  },
 
90
  "source": [
91
- "import py7zr\n",
 
 
 
 
 
 
 
92
  "\n",
93
- "print(\"Extracting 7z archive with password...\")\n",
94
- "try:\n",
95
- " with py7zr.SevenZipFile(local_7z_path, mode='r', password='blueberry') as z:\n",
96
- " z.extractall(path=extract_dir)\n",
97
- " print(f\"Extraction completed to {extract_dir}/\")\n",
98
- "except Exception as e:\n",
99
- " print(f\"Extraction failed: {e}\")"
100
- ],
101
- "execution_count": 3,
102
- "outputs": [
103
- {
104
- "output_type": "stream",
105
- "name": "stdout",
106
- "text": [
107
- "Extracting 7z archive with password...\n",
108
- "Extraction completed to extracted_dataset/\n"
109
- ]
110
- }
111
- ]
112
- },
113
- {
114
- "cell_type": "code",
115
- "source": [
116
  "\n",
117
  "import os\n",
118
- "import re\n",
119
- "import glob\n",
120
  "import io\n",
121
- "\n",
 
 
122
  "from pathlib import Path\n",
 
 
123
  "from PIL import Image\n",
124
  "from IPython.display import display\n",
 
 
125
  "from cryptography.hazmat.primitives.ciphers.aead import AESGCM\n",
126
  "from cryptography.hazmat.primitives.kdf.scrypt import Scrypt\n",
 
127
  "\n",
128
  "# ============================================================\n",
129
  "# CONFIGURATION\n",
130
  "# ============================================================\n",
131
  "\n",
132
- "PASSWORD = \"blueberry\"\n",
133
- "PCENC_MAGIC = b\"PCENC1\"\n",
 
 
 
 
 
 
 
 
134
  "NUM_EXAMPLES = 20\n",
135
  "\n",
 
 
 
 
 
 
 
 
 
 
136
  "# ============================================================\n",
137
- "# DECRYPTION\n",
138
  "# ============================================================\n",
139
  "\n",
140
- "def decrypt_file_bytes(data, password=PASSWORD):\n",
 
 
141
  "\n",
142
- " if not data.startswith(PCENC_MAGIC):\n",
143
- " return data\n",
144
  "\n",
145
- " if len(data) < 50:\n",
146
- " raise ValueError(\"Invalid or truncated PCENC1 file.\")\n",
 
147
  "\n",
148
- " salt = data[6:22]\n",
149
- " nonce = data[22:34]\n",
150
- " ciphertext = data[34:]\n",
 
 
 
 
 
151
  "\n",
152
- " key = Scrypt(\n",
153
- " salt=salt,\n",
154
- " length=32,\n",
155
- " n=2**14,\n",
156
- " r=8,\n",
157
- " p=1,\n",
158
- " ).derive(password.encode(\"utf-8\"))\n",
159
  "\n",
160
- " return AESGCM(key).decrypt(\n",
161
- " nonce,\n",
162
- " ciphertext,\n",
163
- " PCENC_MAGIC\n",
164
  " )\n",
165
  "\n",
 
 
 
 
 
 
166
  "\n",
167
  "# ============================================================\n",
168
- "# FILE READERS\n",
169
  "# ============================================================\n",
170
  "\n",
171
- "def read_bytes(path):\n",
172
- "\n",
173
- " with open(path, \"rb\") as f:\n",
174
- " data = f.read()\n",
 
175
  "\n",
176
- " return decrypt_file_bytes(data)\n",
 
 
 
 
177
  "\n",
 
 
 
 
 
 
 
178
  "\n",
179
- "def read_text(path):\n",
180
  "\n",
181
- " return read_bytes(path).decode(\n",
182
- " \"utf-8-sig\"\n",
183
- " ).strip()\n",
184
  "\n",
 
 
 
185
  "\n",
186
  "# ============================================================\n",
187
- "# FIND NUMERIC IMAGE FILES\n",
188
  "# ============================================================\n",
189
  "\n",
190
- "image_files = glob.glob(\n",
191
- " os.path.join(extract_dir, \"*.jpg\")\n",
192
- ")\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  "\n",
194
- "image_files = sorted(\n",
195
- " image_files,\n",
196
- " key=lambda p: int(\n",
197
- " Path(p).stem\n",
198
- " ) if Path(p).stem.isdigit() else float(\"inf\")\n",
199
- ")\n",
200
  "\n",
201
- "print(\"Total images:\", len(image_files))\n",
202
- "print(\"Displaying first\", NUM_EXAMPLES, \"examples\")\n",
 
203
  "\n",
 
 
 
 
 
 
 
 
 
204
  "\n",
205
  "# ============================================================\n",
206
- "# DISPLAY FIRST 20 EXAMPLES\n",
207
  "# ============================================================\n",
208
  "\n",
209
- "display_count = min(\n",
210
- " NUM_EXAMPLES,\n",
211
- " len(image_files)\n",
212
- ")\n",
213
  "\n",
214
- "for index, image_path in enumerate(\n",
215
- " image_files[:display_count],\n",
216
- " start=1\n",
217
- "):\n",
 
218
  "\n",
219
- " item_id = Path(image_path).stem\n",
 
220
  "\n",
221
- " input_path = os.path.join(\n",
222
- " extract_dir,\n",
223
- " f\"{item_id}_input.txt\"\n",
224
- " )\n",
225
  "\n",
226
- " output_path = os.path.join(\n",
227
- " extract_dir,\n",
228
- " f\"{item_id}_output.txt\"\n",
229
- " )\n",
 
 
230
  "\n",
231
  " print(\"\\n\" + \"=\" * 90)\n",
232
- " print(f\"DATASET EXAMPLE {index}/{display_count} — ID {item_id}\")\n",
 
 
233
  " print(\"=\" * 90)\n",
234
  "\n",
235
- " # --------------------------------------------------------\n",
236
- " # INPUT\n",
237
- " # --------------------------------------------------------\n",
238
- "\n",
239
- " if not os.path.isfile(input_path):\n",
240
- " raise FileNotFoundError(input_path)\n",
241
- "\n",
242
- " input_text = read_text(input_path)\n",
243
- "\n",
244
  " print(\"\\nINPUT:\")\n",
245
- " print(input_text)\n",
246
- "\n",
247
- " # --------------------------------------------------------\n",
248
- " # OUTPUT\n",
249
- " # --------------------------------------------------------\n",
250
- "\n",
251
- " if not os.path.isfile(output_path):\n",
252
- " raise FileNotFoundError(output_path)\n",
253
- "\n",
254
- " output_text = read_text(output_path)\n",
255
  "\n",
256
  " print(\"\\nOUTPUT:\")\n",
257
- " print(output_text)\n",
258
- "\n",
259
- " # --------------------------------------------------------\n",
260
- " # IMAGE\n",
261
- " # --------------------------------------------------------\n",
262
- "\n",
263
- " image_bytes = read_bytes(image_path)\n",
264
- "\n",
265
- " image = Image.open(\n",
266
- " io.BytesIO(image_bytes)\n",
267
- " ).convert(\"RGB\")\n",
268
  "\n",
269
  " print(\"\\nIMAGE:\")\n",
270
- " display(image.resize((350, 350)))\n",
271
- "\n",
272
- "print(\"\\nSuccessfully displayed\", display_count, \"examples.\")\n"
273
- ],
274
- "metadata": {
275
- "id": "ASorxaSJ2A41"
276
- },
277
- "execution_count": null,
278
- "outputs": []
 
 
 
 
279
  }
280
  ]
281
  }
 
14
  }
15
  },
16
  "cells": [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  {
18
  "cell_type": "code",
19
+ "execution_count": null,
20
  "metadata": {
21
+ "id": "0W3w2zmgi6cp"
 
 
 
 
22
  },
23
+ "outputs": [],
24
  "source": [
25
+ "# ============================================================\n",
26
+ "# Sample Encrypted Arrow Multimodal Dataset (gemma4_arrow_encrypted_v1)\n",
27
+ "# Google Colab\n",
28
+ "#\n",
29
+ "# Loads codeShare/kromoss-lora-exports/gemma4_arrow_encrypted_v1\n",
30
+ "# Decrypts image_enc + conversation_enc with the same scheme as the builder:\n",
31
+ "# AES-256-GCM, scrypt KDF, AAD = b\"gemma4-v1:{sample_id}:{field_name}\"\n",
32
+ "# ============================================================\n",
33
  "\n",
34
+ "!pip -q install -U datasets pyarrow cryptography pillow huggingface_hub tqdm\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  "\n",
36
  "import os\n",
 
 
37
  "import io\n",
38
+ "import json\n",
39
+ "import base64\n",
40
+ "import random\n",
41
  "from pathlib import Path\n",
42
+ "\n",
43
+ "from google.colab import userdata\n",
44
  "from PIL import Image\n",
45
  "from IPython.display import display\n",
46
+ "from datasets import load_from_disk\n",
47
+ "from huggingface_hub import snapshot_download, HfApi\n",
48
  "from cryptography.hazmat.primitives.ciphers.aead import AESGCM\n",
49
  "from cryptography.hazmat.primitives.kdf.scrypt import Scrypt\n",
50
+ "from tqdm.auto import tqdm\n",
51
  "\n",
52
  "# ============================================================\n",
53
  "# CONFIGURATION\n",
54
  "# ============================================================\n",
55
  "\n",
56
+ "REPO_ID = \"codeShare/kromoss-lora-exports\"\n",
57
+ "REPO_TYPE = \"model\"\n",
58
+ "SUBFOLDER = \"gemma4_arrow_encrypted_v1\"\n",
59
+ "\n",
60
+ "# Must match the password used when the encrypted dataset was built.\n",
61
+ "PASSWORD = userdata.get(\"ENC_PWD\")\n",
62
+ "if not PASSWORD:\n",
63
+ " raise ValueError(\"ENC_PWD is missing from Colab Secrets.\")\n",
64
+ "\n",
65
+ "# How many examples to show\n",
66
  "NUM_EXAMPLES = 20\n",
67
  "\n",
68
+ "# Sampling\n",
69
+ "RANDOM_SEED = 42\n",
70
+ "# True -> random sample of NUM_EXAMPLES\n",
71
+ "# False -> first NUM_EXAMPLES rows (idx order after shuffle is still shuffled)\n",
72
+ "RANDOM_SAMPLE = True\n",
73
+ "\n",
74
+ "# Local cache\n",
75
+ "LOCAL_DIR = Path(\"/content/encrypted_dataset_sample\")\n",
76
+ "LOCAL_DIR.mkdir(parents=True, exist_ok=True)\n",
77
+ "\n",
78
  "# ============================================================\n",
79
+ "# AUTH\n",
80
  "# ============================================================\n",
81
  "\n",
82
+ "HF_TOKEN = userdata.get(\"HF_TOKEN\")\n",
83
+ "if not HF_TOKEN:\n",
84
+ " raise ValueError(\"HF_TOKEN is missing from Colab Secrets.\")\n",
85
  "\n",
86
+ "api = HfApi(token=HF_TOKEN)\n",
87
+ "print(\"Authenticated as:\", api.whoami()[\"name\"])\n",
88
  "\n",
89
+ "# ============================================================\n",
90
+ "# DOWNLOAD DATASET FOLDER\n",
91
+ "# ============================================================\n",
92
  "\n",
93
+ "print(f\"\\nDownloading {REPO_ID}/{SUBFOLDER} ...\")\n",
94
+ "snapshot_download(\n",
95
+ " repo_id=REPO_ID,\n",
96
+ " repo_type=REPO_TYPE,\n",
97
+ " token=HF_TOKEN,\n",
98
+ " local_dir=str(LOCAL_DIR),\n",
99
+ " allow_patterns=[f\"{SUBFOLDER}/**\"],\n",
100
+ ")\n",
101
  "\n",
102
+ "dataset_dir = LOCAL_DIR / SUBFOLDER\n",
103
+ "manifest_path = dataset_dir / \"encryption_manifest.json\"\n",
 
 
 
 
 
104
  "\n",
105
+ "if not manifest_path.is_file():\n",
106
+ " raise FileNotFoundError(\n",
107
+ " f\"encryption_manifest.json not found under {dataset_dir}. \"\n",
108
+ " \"Check SUBFOLDER / repo path.\"\n",
109
  " )\n",
110
  "\n",
111
+ "with open(manifest_path, \"r\", encoding=\"utf-8\") as f:\n",
112
+ " manifest = json.load(f)\n",
113
+ "\n",
114
+ "print(\"Manifest sample_count:\", manifest.get(\"sample_count\"))\n",
115
+ "print(\"Cipher:\", manifest.get(\"encryption\", {}).get(\"cipher\"))\n",
116
+ "print(\"KDF:\", manifest.get(\"encryption\", {}).get(\"kdf\"))\n",
117
  "\n",
118
  "# ============================================================\n",
119
+ "# DERIVE DECRYPTION KEY FROM MANIFEST\n",
120
  "# ============================================================\n",
121
  "\n",
122
+ "enc = manifest[\"encryption\"]\n",
123
+ "if enc.get(\"cipher\") != \"AES-256-GCM\":\n",
124
+ " raise RuntimeError(f\"Unexpected cipher: {enc.get('cipher')}\")\n",
125
+ "if enc.get(\"kdf\") != \"scrypt\":\n",
126
+ " raise RuntimeError(f\"Unexpected KDF: {enc.get('kdf')}\")\n",
127
  "\n",
128
+ "salt = base64.b64decode(enc[\"kdf_salt_base64\"])\n",
129
+ "n = int(enc[\"scrypt_n\"])\n",
130
+ "r = int(enc[\"scrypt_r\"])\n",
131
+ "p = int(enc[\"scrypt_p\"])\n",
132
+ "key_len = int(enc.get(\"key_length_bytes\", 32))\n",
133
  "\n",
134
+ "KEY = Scrypt(\n",
135
+ " salt=salt,\n",
136
+ " length=key_len,\n",
137
+ " n=n,\n",
138
+ " r=r,\n",
139
+ " p=p,\n",
140
+ ").derive(PASSWORD.encode(\"utf-8\"))\n",
141
  "\n",
142
+ "print(\"Decryption key derived (scrypt).\")\n",
143
  "\n",
144
+ "# ============================================================\n",
145
+ "# LOAD ARROW DATASET\n",
146
+ "# ============================================================\n",
147
  "\n",
148
+ "ds = load_from_disk(str(dataset_dir))\n",
149
+ "print(\"Loaded rows:\", f\"{len(ds):,}\")\n",
150
+ "print(\"Columns:\", ds.column_names)\n",
151
  "\n",
152
  "# ============================================================\n",
153
+ "# DECRYPT HELPERS\n",
154
  "# ============================================================\n",
155
  "\n",
156
+ "def decrypt_payload(encrypted_payload, sample_id, field_name, key=KEY):\n",
157
+ " \"\"\"\n",
158
+ " Layout: nonce[12] || ciphertext\n",
159
+ " AAD: gemma4-v1:{sample_id}:{field_name}\n",
160
+ " \"\"\"\n",
161
+ " if not isinstance(encrypted_payload, (bytes, bytearray, memoryview)):\n",
162
+ " encrypted_payload = bytes(encrypted_payload)\n",
163
+ "\n",
164
+ " if len(encrypted_payload) <= 12:\n",
165
+ " raise ValueError(\"Encrypted payload too short.\")\n",
166
+ "\n",
167
+ " nonce = encrypted_payload[:12]\n",
168
+ " ciphertext = encrypted_payload[12:]\n",
169
+ " aad = f\"gemma4-v1:{sample_id}:{field_name}\".encode(\"utf-8\")\n",
170
+ " return AESGCM(key).decrypt(nonce, ciphertext, aad)\n",
171
+ "\n",
172
+ "\n",
173
+ "def parse_conversation(conversation_bytes):\n",
174
+ " data = json.loads(conversation_bytes.decode(\"utf-8\"))\n",
175
+ " messages = data[\"messages\"]\n",
176
+ " if len(messages) != 2:\n",
177
+ " raise ValueError(f\"Expected 2 messages, got {len(messages)}\")\n",
178
+ "\n",
179
+ " user_msg, asst_msg = messages[0], messages[1]\n",
180
+ " if user_msg[\"role\"] != \"user\" or asst_msg[\"role\"] != \"assistant\":\n",
181
+ " raise ValueError(\"Unexpected roles in conversation.\")\n",
182
+ "\n",
183
+ " # User: text + image placeholder\n",
184
+ " user_text = None\n",
185
+ " for part in user_msg[\"content\"]:\n",
186
+ " if part.get(\"type\") == \"text\":\n",
187
+ " user_text = part.get(\"text\", \"\")\n",
188
+ " break\n",
189
+ "\n",
190
+ " asst_text = None\n",
191
+ " for part in asst_msg[\"content\"]:\n",
192
+ " if part.get(\"type\") == \"text\":\n",
193
+ " asst_text = part.get(\"text\", \"\")\n",
194
+ " break\n",
195
+ "\n",
196
+ " if user_text is None or asst_text is None:\n",
197
+ " raise ValueError(\"Missing text in conversation content.\")\n",
198
+ "\n",
199
+ " return user_text, asst_text\n",
200
+ "\n",
201
+ "\n",
202
+ "def decrypt_row(row):\n",
203
+ " sample_id = row[\"sample_id\"]\n",
204
+ " image_bytes = decrypt_payload(row[\"image_enc\"], sample_id, \"image\")\n",
205
+ " conv_bytes = decrypt_payload(row[\"conversation_enc\"], sample_id, \"conversation\")\n",
206
+ " user_text, asst_text = parse_conversation(conv_bytes)\n",
207
+ " return {\n",
208
+ " \"sample_id\": sample_id,\n",
209
+ " \"idx\": row.get(\"idx\"),\n",
210
+ " \"width\": row[\"width\"],\n",
211
+ " \"height\": row[\"height\"],\n",
212
+ " \"image_format\": row[\"image_format\"],\n",
213
+ " \"input\": user_text,\n",
214
+ " \"output\": asst_text,\n",
215
+ " \"image_bytes\": image_bytes,\n",
216
+ " }\n",
217
  "\n",
 
 
 
 
 
 
218
  "\n",
219
+ "# ============================================================\n",
220
+ "# PASSWORD SANITY CHECK (first row)\n",
221
+ "# ============================================================\n",
222
  "\n",
223
+ "print(\"\\nVerifying password against row 0...\")\n",
224
+ "try:\n",
225
+ " _ = decrypt_row(ds[0])\n",
226
+ " print(\"Password verification: PASS\")\n",
227
+ "except Exception as e:\n",
228
+ " raise RuntimeError(\n",
229
+ " \"Failed to decrypt row 0. Wrong PASSWORD or incompatible dataset. \"\n",
230
+ " f\"Error: {e!r}\"\n",
231
+ " ) from e\n",
232
  "\n",
233
  "# ============================================================\n",
234
+ "# SELECT ROWS TO DISPLAY\n",
235
  "# ============================================================\n",
236
  "\n",
237
+ "n = len(ds)\n",
238
+ "k = min(NUM_EXAMPLES, n)\n",
 
 
239
  "\n",
240
+ "if RANDOM_SAMPLE:\n",
241
+ " rng = random.Random(RANDOM_SEED)\n",
242
+ " indices = rng.sample(range(n), k=k)\n",
243
+ "else:\n",
244
+ " indices = list(range(k))\n",
245
  "\n",
246
+ "print(f\"\\nDisplaying {k} example(s) \"\n",
247
+ " f\"({'random seed=' + str(RANDOM_SEED) if RANDOM_SAMPLE else 'first rows'})\")\n",
248
  "\n",
249
+ "# ============================================================\n",
250
+ "# DISPLAY\n",
251
+ "# ============================================================\n",
 
252
  "\n",
253
+ "# Resize for notebook display (longest side)\n",
254
+ "DISPLAY_MAX_SIDE = 350\n",
255
+ "\n",
256
+ "for i, row_i in enumerate(indices, start=1):\n",
257
+ " row = ds[int(row_i)]\n",
258
+ " item = decrypt_row(row)\n",
259
  "\n",
260
  " print(\"\\n\" + \"=\" * 90)\n",
261
+ " print(f\"DATASET EXAMPLE {i}/{k} | dataset_index={row_i} | idx={item['idx']}\")\n",
262
+ " print(f\"sample_id={item['sample_id']}\")\n",
263
+ " print(f\"image={item['width']}x{item['height']} {item['image_format']}\")\n",
264
  " print(\"=\" * 90)\n",
265
  "\n",
 
 
 
 
 
 
 
 
 
266
  " print(\"\\nINPUT:\")\n",
267
+ " print(item[\"input\"])\n",
 
 
 
 
 
 
 
 
 
268
  "\n",
269
  " print(\"\\nOUTPUT:\")\n",
270
+ " print(item[\"output\"])\n",
 
 
 
 
 
 
 
 
 
 
271
  "\n",
272
  " print(\"\\nIMAGE:\")\n",
273
+ " with Image.open(io.BytesIO(item[\"image_bytes\"])) as im:\n",
274
+ " im = im.convert(\"RGB\")\n",
275
+ " w, h = im.size\n",
276
+ " scale = DISPLAY_MAX_SIDE / max(w, h)\n",
277
+ " if scale < 1.0:\n",
278
+ " im = im.resize(\n",
279
+ " (max(1, round(w * scale)), max(1, round(h * scale))),\n",
280
+ " Image.Resampling.LANCZOS,\n",
281
+ " )\n",
282
+ " display(im)\n",
283
+ "\n",
284
+ "print(f\"\\nSuccessfully displayed {k} example(s).\")"
285
+ ]
286
  }
287
  ]
288
  }