Datasets:
Tags:
Not-For-All-Audiences
Upload sankaku_database_browse_and_caption.ipynb
Browse files
sankaku_database_browse_and_caption.ipynb
CHANGED
|
@@ -98,17 +98,17 @@
|
|
| 98 |
"# ============================================================\n",
|
| 99 |
"\n",
|
| 100 |
"SEARCH_QUERY = \"mario -ai-created \" #@param {type:\"string\"}\n",
|
| 101 |
-
"ADDITIONAL_WORDS = \"peach daisy\" #@param {type:\"string\"}\n",
|
| 102 |
"\n",
|
| 103 |
-
"IMAGE_COUNT =
|
| 104 |
"MIN_FAVOURITES = 105 #@param {type:\"slider\", min:0, max:1000, step:1}\n",
|
| 105 |
"\n",
|
| 106 |
"RANDOM_SEARCH_START = False #@param {type:\"boolean\"}\n",
|
| 107 |
"SEARCH_SEED_INPUT = \"banana123\" #@param {type:\"string\"}\n",
|
| 108 |
"\n",
|
| 109 |
-
"SEARCH_SKIP_AHEAD =
|
| 110 |
"\n",
|
| 111 |
-
"ENABLE_GEMMA_CAPTIONER =
|
| 112 |
"\n",
|
| 113 |
"GEMMA_MODEL_ID = \"coder3101/gemma-4-E2B-it-heretic\"\n",
|
| 114 |
"\n",
|
|
@@ -130,7 +130,7 @@
|
|
| 130 |
"\n",
|
| 131 |
"#@markdown ### Gemma VRAM management\n",
|
| 132 |
"\n",
|
| 133 |
-
"KEEP_GEMMA_IN_VRAM =
|
| 134 |
"\n",
|
| 135 |
"#@markdown When enabled, Gemma remains loaded after this cell finishes\n",
|
| 136 |
"#@markdown and can be reused if this cell is executed again.\n",
|
|
@@ -138,9 +138,9 @@
|
|
| 138 |
"\n",
|
| 139 |
"#@markdown ### Sankaku rating filters\n",
|
| 140 |
"\n",
|
| 141 |
-
"INCLUDE_SAFE =
|
| 142 |
-
"INCLUDE_QUESTIONABLE =
|
| 143 |
-
"INCLUDE_EXPLICIT =
|
| 144 |
"\n",
|
| 145 |
"\n",
|
| 146 |
"# ============================================================\n",
|
|
@@ -3448,12 +3448,513 @@
|
|
| 3448 |
"id": "zIr6sRbXASLp",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
],
|
| 3453 |
"metadata": {
|
| 3454 |
-
"accelerator": "GPU",
|
| 3455 |
"colab": {
|
| 3456 |
-
"gpuType": "T4",
|
| 3457 |
"provenance": [],
|
| 3458 |
"collapsed_sections": [
|
| 3459 |
"EytLQtu1OJ7A"
|
|
|
|
| 98 |
"# ============================================================\n",
|
| 99 |
"\n",
|
| 100 |
"SEARCH_QUERY = \"mario -ai-created \" #@param {type:\"string\"}\n",
|
| 101 |
+
"ADDITIONAL_WORDS = \"peach daisy \" #@param {type:\"string\"}\n",
|
| 102 |
"\n",
|
| 103 |
+
"IMAGE_COUNT = 1000 #@param {type:\"slider\", min:1, max:2000, step:1}\n",
|
| 104 |
"MIN_FAVOURITES = 105 #@param {type:\"slider\", min:0, max:1000, step:1}\n",
|
| 105 |
"\n",
|
| 106 |
"RANDOM_SEARCH_START = False #@param {type:\"boolean\"}\n",
|
| 107 |
"SEARCH_SEED_INPUT = \"banana123\" #@param {type:\"string\"}\n",
|
| 108 |
"\n",
|
| 109 |
+
"SEARCH_SKIP_AHEAD = 3 #@param {type:\"slider\", min:0, max:20, step:1}\n",
|
| 110 |
"\n",
|
| 111 |
+
"ENABLE_GEMMA_CAPTIONER = False #@param {type:\"boolean\"}\n",
|
| 112 |
"\n",
|
| 113 |
"GEMMA_MODEL_ID = \"coder3101/gemma-4-E2B-it-heretic\"\n",
|
| 114 |
"\n",
|
|
|
|
| 130 |
"\n",
|
| 131 |
"#@markdown ### Gemma VRAM management\n",
|
| 132 |
"\n",
|
| 133 |
+
"KEEP_GEMMA_IN_VRAM = False #@param {type:\"boolean\"}\n",
|
| 134 |
"\n",
|
| 135 |
"#@markdown When enabled, Gemma remains loaded after this cell finishes\n",
|
| 136 |
"#@markdown and can be reused if this cell is executed again.\n",
|
|
|
|
| 138 |
"\n",
|
| 139 |
"#@markdown ### Sankaku rating filters\n",
|
| 140 |
"\n",
|
| 141 |
+
"INCLUDE_SAFE = False #@param {type:\"boolean\"}\n",
|
| 142 |
+
"INCLUDE_QUESTIONABLE = True #@param {type:\"boolean\"}\n",
|
| 143 |
+
"INCLUDE_EXPLICIT = True #@param {type:\"boolean\"}\n",
|
| 144 |
"\n",
|
| 145 |
"\n",
|
| 146 |
"# ============================================================\n",
|
|
|
|
| 3448 |
"id": "zIr6sRbXASLp",
|
| 3449 |
"execution_count": null,
|
| 3450 |
"outputs": []
|
| 3451 |
+
},
|
| 3452 |
+
{
|
| 3453 |
+
"cell_type": "markdown",
|
| 3454 |
+
"source": [
|
| 3455 |
+
" # Upload zipped results to drive as encryped 7zip using password"
|
| 3456 |
+
],
|
| 3457 |
+
"metadata": {
|
| 3458 |
+
"id": "l3cD3WGpXoiU"
|
| 3459 |
+
},
|
| 3460 |
+
"id": "l3cD3WGpXoiU"
|
| 3461 |
+
},
|
| 3462 |
+
{
|
| 3463 |
+
"cell_type": "code",
|
| 3464 |
+
"source": [
|
| 3465 |
+
"from google.colab import drive\n",
|
| 3466 |
+
"drive.mount('/content/drive')"
|
| 3467 |
+
],
|
| 3468 |
+
"metadata": {
|
| 3469 |
+
"id": "eWymkqmoXqnF"
|
| 3470 |
+
},
|
| 3471 |
+
"id": "eWymkqmoXqnF",
|
| 3472 |
+
"execution_count": null,
|
| 3473 |
+
"outputs": []
|
| 3474 |
+
},
|
| 3475 |
+
{
|
| 3476 |
+
"cell_type": "code",
|
| 3477 |
+
"metadata": {
|
| 3478 |
+
"id": "48b0b131"
|
| 3479 |
+
},
|
| 3480 |
+
"source": [
|
| 3481 |
+
"#@title ✦ Encrypt and Upload Dataset to Google Drive ✦ { display-mode: \"form\" }\n",
|
| 3482 |
+
"\n",
|
| 3483 |
+
"import os\n",
|
| 3484 |
+
"import subprocess\n",
|
| 3485 |
+
"from google.colab import drive\n",
|
| 3486 |
+
"\n",
|
| 3487 |
+
"#@markdown ### 1. Google Drive Setup\n",
|
| 3488 |
+
"#@markdown Press play to run this cell. It will ask for permission to mount your Google Drive if it isn't already mounted.\n",
|
| 3489 |
+
"MOUNT_PATH = \"/content/drive\"\n",
|
| 3490 |
+
"target_drive_folder = \"/My Drive/SankakuDatasets\" #@param {type:\"string\"}\n",
|
| 3491 |
+
"\n",
|
| 3492 |
+
"#@markdown ### 2. Output 7z Configuration\n",
|
| 3493 |
+
"output_filename = \"/content/drive/MyDrive/sankaku_lora_training_completed.7z\" #@param {type:\"string\"}\n",
|
| 3494 |
+
"encryption_password = \"my_password\" #@param {type:\"string\"}\n",
|
| 3495 |
+
"\n",
|
| 3496 |
+
"# Ensure dependencies\n",
|
| 3497 |
+
"print(\"Installing p7zip-full...\")\n",
|
| 3498 |
+
"subprocess.run([\"apt-get\", \"install\", \"-y\", \"p7zip-full\"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n",
|
| 3499 |
+
"\n",
|
| 3500 |
+
"# Mount Google Drive\n",
|
| 3501 |
+
"if not os.path.exists(MOUNT_PATH):\n",
|
| 3502 |
+
" print(\"Mounting Google Drive...\")\n",
|
| 3503 |
+
" drive.mount(MOUNT_PATH)\n",
|
| 3504 |
+
"else:\n",
|
| 3505 |
+
" print(\"Google Drive is already mounted.\")\n",
|
| 3506 |
+
"\n",
|
| 3507 |
+
"# Define paths\n",
|
| 3508 |
+
"source_zip = \"/content/sankaku_lora_training_completed.zip\"\n",
|
| 3509 |
+
"if not os.path.exists(source_zip):\n",
|
| 3510 |
+
" # Fallback to template ZIP if completed ZIP has not been generated by the Gradio editor yet\n",
|
| 3511 |
+
" source_zip = \"/content/sankaku_lora_training_template.zip\"\n",
|
| 3512 |
+
"\n",
|
| 3513 |
+
"if not os.path.exists(source_zip):\n",
|
| 3514 |
+
" raise FileNotFoundError(\"No dataset ZIP archive found at '/content/sankaku_lora_training_completed.zip' or '/content/sankaku_lora_training_template.zip'. Please run the search/editor cell first.\")\n",
|
| 3515 |
+
"\n",
|
| 3516 |
+
"# Create Google Drive destination folder if it doesn't exist\n",
|
| 3517 |
+
"full_drive_path = MOUNT_PATH + target_drive_folder\n",
|
| 3518 |
+
"os.makedirs(full_drive_path, exist_ok=True)\n",
|
| 3519 |
+
"\n",
|
| 3520 |
+
"# Ensure output filename ends with .7z\n",
|
| 3521 |
+
"if not output_filename.endswith(\".7z\"):\n",
|
| 3522 |
+
" output_filename += \".7z\"\n",
|
| 3523 |
+
"\n",
|
| 3524 |
+
"destination_7z_path = os.path.join(full_drive_path, output_filename)\n",
|
| 3525 |
+
"\n",
|
| 3526 |
+
"if not encryption_password or encryption_password == \"your_secure_password_here\":\n",
|
| 3527 |
+
" print(\"WARNING: Please set a secure custom password in the form input.\")\n",
|
| 3528 |
+
"\n",
|
| 3529 |
+
"print(f\"Encrypting '{source_zip}' into '{destination_7z_path}'...\")\n",
|
| 3530 |
+
"\n",
|
| 3531 |
+
"# Compress with 7z using strong encryption (AES-256) and header encryption (-mhe=on)\n",
|
| 3532 |
+
"command = [\n",
|
| 3533 |
+
" \"7z\", \"a\",\n",
|
| 3534 |
+
" f\"-p{encryption_password}\",\n",
|
| 3535 |
+
" \"-mhe=on\",\n",
|
| 3536 |
+
" destination_7z_path,\n",
|
| 3537 |
+
" source_zip\n",
|
| 3538 |
+
"]\n",
|
| 3539 |
+
"\n",
|
| 3540 |
+
"result = subprocess.run(command, capture_output=True, text=True)\n",
|
| 3541 |
+
"\n",
|
| 3542 |
+
"if result.returncode == 0:\n",
|
| 3543 |
+
" print(\"\\n[SUCCESS] Encrypted 7z dataset created and saved to Google Drive!\")\n",
|
| 3544 |
+
" print(f\"Location: {destination_7z_path}\")\n",
|
| 3545 |
+
" print(f\"Size: {os.path.getsize(destination_7z_path) / (1024*1024):.2f} MB\")\n",
|
| 3546 |
+
"else:\n",
|
| 3547 |
+
" print(\"\\n[ERROR] Encryption failed.\")\n",
|
| 3548 |
+
" print(result.stderr)"
|
| 3549 |
+
],
|
| 3550 |
+
"id": "48b0b131",
|
| 3551 |
+
"execution_count": null,
|
| 3552 |
+
"outputs": []
|
| 3553 |
+
},
|
| 3554 |
+
{
|
| 3555 |
+
"cell_type": "markdown",
|
| 3556 |
+
"source": [
|
| 3557 |
+
"# Dataset Editor window\n",
|
| 3558 |
+
"\n"
|
| 3559 |
+
],
|
| 3560 |
+
"metadata": {
|
| 3561 |
+
"id": "RF6CbL6QXEYf"
|
| 3562 |
+
},
|
| 3563 |
+
"id": "RF6CbL6QXEYf"
|
| 3564 |
+
},
|
| 3565 |
+
{
|
| 3566 |
+
"cell_type": "code",
|
| 3567 |
+
"source": [
|
| 3568 |
+
"# ============================================================\n",
|
| 3569 |
+
"# SANKAKU LORA - IMAGE CAPTION EDITOR WITH AUTO-DECRYPT & DRIVE AUTO-OVERWRITE\n",
|
| 3570 |
+
"# Gradio interface | Dark / Orange / Purple theme\n",
|
| 3571 |
+
"# ============================================================\n",
|
| 3572 |
+
"encryption_password = \"my_password\" #@param {type:\"string\"}\n",
|
| 3573 |
+
"save_write_encrypted_filepath_7z = \"/content/drive/MyDrive/sankaku_lora_training_completed.7z\" #@param {type:\"string\"}\n",
|
| 3574 |
+
"\n",
|
| 3575 |
+
"\n",
|
| 3576 |
+
"# Mount Google Drive\n",
|
| 3577 |
+
"if not os.path.exists(MOUNT_PATH):\n",
|
| 3578 |
+
" print(\"Mounting Google Drive...\")\n",
|
| 3579 |
+
" drive.mount(MOUNT_PATH)\n",
|
| 3580 |
+
"else:\n",
|
| 3581 |
+
" print(\"Google Drive is already mounted.\")\n",
|
| 3582 |
+
"\n",
|
| 3583 |
+
"\n",
|
| 3584 |
+
"!pip -q install -U gradio\n",
|
| 3585 |
+
"\n",
|
| 3586 |
+
"import os\n",
|
| 3587 |
+
"import re\n",
|
| 3588 |
+
"import zipfile\n",
|
| 3589 |
+
"import shutil\n",
|
| 3590 |
+
"import subprocess\n",
|
| 3591 |
+
"from pathlib import Path\n",
|
| 3592 |
+
"import gradio as gr\n",
|
| 3593 |
+
"from google.colab import drive\n",
|
| 3594 |
+
"\n",
|
| 3595 |
+
"# ---------------- CONFIGURATION ----------------\n",
|
| 3596 |
+
"MOUNT_PATH = \"/content/drive\"\n",
|
| 3597 |
+
"TARGET_DRIVE_FOLDER = \"/My Drive/SankakuDatasets\"\n",
|
| 3598 |
+
"\n",
|
| 3599 |
+
"\n",
|
| 3600 |
+
"\n",
|
| 3601 |
+
"# Ensure dependencies\n",
|
| 3602 |
+
"print(\"Installing p7zip-full...\")\n",
|
| 3603 |
+
"subprocess.run([\"apt-get\", \"install\", \"-y\", \"p7zip-full\"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n",
|
| 3604 |
+
"\n",
|
| 3605 |
+
"# Ensure filepath ends with .7z\n",
|
| 3606 |
+
"if not save_write_encrypted_filepath_7z.endswith(\".7z\"):\n",
|
| 3607 |
+
" save_write_encrypted_filepath_7z += \".7z\"\n",
|
| 3608 |
+
"\n",
|
| 3609 |
+
"DRIVE_7Z_PATH = os.path.join(MOUNT_PATH + TARGET_DRIVE_FOLDER, save_write_encrypted_filepath_7z)\n",
|
| 3610 |
+
"LOCAL_ZIP_PATH = \"/content/sankaku_lora_training_template.zip\"\n",
|
| 3611 |
+
"WORK_DIR = \"/content/sankaku_caption_editor\"\n",
|
| 3612 |
+
"OUTPUT_ZIP = \"/content/sankaku_lora_training_completed.zip\"\n",
|
| 3613 |
+
"\n",
|
| 3614 |
+
"# ---------------- EXTRACT & DECRYPT DATASET ----------------\n",
|
| 3615 |
+
"if os.path.exists(WORK_DIR):\n",
|
| 3616 |
+
" shutil.rmtree(WORK_DIR)\n",
|
| 3617 |
+
"os.makedirs(WORK_DIR, exist_ok=True)\n",
|
| 3618 |
+
"\n",
|
| 3619 |
+
"# Decrypt or fallback\n",
|
| 3620 |
+
"if os.path.exists(DRIVE_7Z_PATH):\n",
|
| 3621 |
+
" print(f\"Found encrypted archive on Google Drive: {DRIVE_7Z_PATH}\")\n",
|
| 3622 |
+
" print(\"Decrypting and extracting directly into working directory...\")\n",
|
| 3623 |
+
" # -y answers yes to all prompts, -p specifies password, -o specifies output directory\n",
|
| 3624 |
+
" cmd = [\"7z\", \"x\", f\"-p{encryption_password}\", \"-y\", f\"-o{WORK_DIR}\", DRIVE_7Z_PATH]\n",
|
| 3625 |
+
" result = subprocess.run(cmd, capture_output=True, text=True)\n",
|
| 3626 |
+
" if result.returncode != 0:\n",
|
| 3627 |
+
" print(\"Decryption failed! Attempting to fallback to local unencrypted template zip...\")\n",
|
| 3628 |
+
" if os.path.exists(LOCAL_ZIP_PATH):\n",
|
| 3629 |
+
" with zipfile.ZipFile(LOCAL_ZIP_PATH, \"r\") as z:\n",
|
| 3630 |
+
" z.extractall(WORK_DIR)\n",
|
| 3631 |
+
" else:\n",
|
| 3632 |
+
" raise RuntimeError(\"Both encrypted Drive dataset decryption and local ZIP fallback failed.\")\n",
|
| 3633 |
+
"else:\n",
|
| 3634 |
+
" print(f\"No encrypted file found on Google Drive at '{DRIVE_7Z_PATH}'. Loading local template instead...\")\n",
|
| 3635 |
+
" if os.path.exists(LOCAL_ZIP_PATH):\n",
|
| 3636 |
+
" with zipfile.ZipFile(LOCAL_ZIP_PATH, \"r\") as z:\n",
|
| 3637 |
+
" z.extractall(WORK_DIR)\n",
|
| 3638 |
+
" else:\n",
|
| 3639 |
+
" raise FileNotFoundError(f\"Neither Google Drive 7z nor local fallback ZIP found: {LOCAL_ZIP_PATH}\")\n",
|
| 3640 |
+
"\n",
|
| 3641 |
+
"# Find numbered images\n",
|
| 3642 |
+
"image_extensions = {\".jpg\", \".jpeg\", \".png\", \".webp\"}\n",
|
| 3643 |
+
"images = []\n",
|
| 3644 |
+
"\n",
|
| 3645 |
+
"for root, dirs, filenames in os.walk(WORK_DIR):\n",
|
| 3646 |
+
" for filename in filenames:\n",
|
| 3647 |
+
" match = re.fullmatch(r\"(\\d+)\\.(jpg|jpeg|png|webp)\", filename, re.I)\n",
|
| 3648 |
+
" if match:\n",
|
| 3649 |
+
" number = int(match.group(1))\n",
|
| 3650 |
+
" images.append({\n",
|
| 3651 |
+
" \"number\": number,\n",
|
| 3652 |
+
" \"image\": str(Path(root) / filename),\n",
|
| 3653 |
+
" \"input\": str(Path(root) / f\"{number}_input.txt\"),\n",
|
| 3654 |
+
" \"output\": str(Path(root) / f\"{number}_output.txt\")\n",
|
| 3655 |
+
" })\n",
|
| 3656 |
+
"\n",
|
| 3657 |
+
"images.sort(key=lambda x: x[\"number\"])\n",
|
| 3658 |
+
"if not images:\n",
|
| 3659 |
+
" raise RuntimeError(\"No numbered images found in the working directory.\")\n",
|
| 3660 |
+
"\n",
|
| 3661 |
+
"print(f\"Loaded {len(images)} images successfully.\")\n",
|
| 3662 |
+
"\n",
|
| 3663 |
+
"# ---------------- CAPTION MANAGEMENT ----------------\n",
|
| 3664 |
+
"def read_caption(item):\n",
|
| 3665 |
+
" path = item[\"output\"]\n",
|
| 3666 |
+
" if os.path.exists(path):\n",
|
| 3667 |
+
" return Path(path).read_text(encoding=\"utf-8\")\n",
|
| 3668 |
+
" return \"\"\n",
|
| 3669 |
+
"\n",
|
| 3670 |
+
"def read_hints(item):\n",
|
| 3671 |
+
" path = item[\"input\"]\n",
|
| 3672 |
+
" if os.path.exists(path):\n",
|
| 3673 |
+
" return Path(path).read_text(encoding=\"utf-8\")\n",
|
| 3674 |
+
" return \"\"\n",
|
| 3675 |
+
"\n",
|
| 3676 |
+
"def save_caption(item, caption):\n",
|
| 3677 |
+
" Path(item[\"output\"]).write_text(caption or \"\", encoding=\"utf-8\")\n",
|
| 3678 |
+
"\n",
|
| 3679 |
+
"def get_view(index, caption=\"\", captions=None):\n",
|
| 3680 |
+
" index = max(0, min(index, len(images) - 1))\n",
|
| 3681 |
+
" if captions is None:\n",
|
| 3682 |
+
" captions = {}\n",
|
| 3683 |
+
" previous_index = captions.get(\"_current_index\")\n",
|
| 3684 |
+
" if previous_index is not None:\n",
|
| 3685 |
+
" save_caption(images[previous_index], caption)\n",
|
| 3686 |
+
" item = images[index]\n",
|
| 3687 |
+
" captions[\"_current_index\"] = index\n",
|
| 3688 |
+
" return (\n",
|
| 3689 |
+
" item[\"image\"],\n",
|
| 3690 |
+
" read_caption(item),\n",
|
| 3691 |
+
" read_hints(item),\n",
|
| 3692 |
+
" f\"IMAGE {index + 1:04d} / {len(images):04d} • FILE {item['number']}\",\n",
|
| 3693 |
+
" index,\n",
|
| 3694 |
+
" captions\n",
|
| 3695 |
+
" )\n",
|
| 3696 |
+
"\n",
|
| 3697 |
+
"# ---------------- NAVIGATION & SAVE ----------------\n",
|
| 3698 |
+
"def previous_image(index, caption, captions):\n",
|
| 3699 |
+
" return get_view(index - 1, caption, captions)\n",
|
| 3700 |
+
"\n",
|
| 3701 |
+
"def next_image(index, caption, captions):\n",
|
| 3702 |
+
" return get_view(index + 1, caption, captions)\n",
|
| 3703 |
+
"\n",
|
| 3704 |
+
"def jump_to_image(index, caption, captions):\n",
|
| 3705 |
+
" return get_view(index, caption, captions)\n",
|
| 3706 |
+
"\n",
|
| 3707 |
+
"def save_and_zip(index, caption, captions):\n",
|
| 3708 |
+
" # Save current caption\n",
|
| 3709 |
+
" save_caption(images[index], caption)\n",
|
| 3710 |
+
"\n",
|
| 3711 |
+
" # Save all other captions to working directory\n",
|
| 3712 |
+
" for item in images:\n",
|
| 3713 |
+
" if item[\"number\"] in captions:\n",
|
| 3714 |
+
" save_caption(item, captions[item[\"number\"]])\n",
|
| 3715 |
+
"\n",
|
| 3716 |
+
" # 1. Create local unencrypted ZIP\n",
|
| 3717 |
+
" if os.path.exists(OUTPUT_ZIP):\n",
|
| 3718 |
+
" os.remove(OUTPUT_ZIP)\n",
|
| 3719 |
+
" shutil.make_archive(OUTPUT_ZIP[:-4], \"zip\", WORK_DIR)\n",
|
| 3720 |
+
"\n",
|
| 3721 |
+
" # 2. Encrypt and overwrite Drive path using 7z\n",
|
| 3722 |
+
" print(f\"Encrypting active dataset back to Google Drive: {DRIVE_7Z_PATH}...\")\n",
|
| 3723 |
+
" if os.path.exists(DRIVE_7Z_PATH):\n",
|
| 3724 |
+
" os.remove(DRIVE_7Z_PATH)\n",
|
| 3725 |
+
"\n",
|
| 3726 |
+
" # Add files inside WORK_DIR to encrypted 7z archive\n",
|
| 3727 |
+
" cmd = [\n",
|
| 3728 |
+
" \"7z\", \"a\",\n",
|
| 3729 |
+
" f\"-p{encryption_password}\",\n",
|
| 3730 |
+
" \"-mhe=on\",\n",
|
| 3731 |
+
" DRIVE_7Z_PATH,\n",
|
| 3732 |
+
" os.path.join(WORK_DIR, \"*\")\n",
|
| 3733 |
+
" ]\n",
|
| 3734 |
+
" result = subprocess.run(cmd, capture_output=True, text=True)\n",
|
| 3735 |
+
"\n",
|
| 3736 |
+
" if result.returncode == 0:\n",
|
| 3737 |
+
" status_msg = f\"All captions saved! Locally zipped, encrypted, and backed up to Google Drive at: {save_write_encrypted_filepath_7z}\"\n",
|
| 3738 |
+
" else:\n",
|
| 3739 |
+
" status_msg = f\"Saved locally, but failed to encrypt to Google Drive: {result.stderr}\"\n",
|
| 3740 |
+
"\n",
|
| 3741 |
+
" return OUTPUT_ZIP, status_msg\n",
|
| 3742 |
+
"\n",
|
| 3743 |
+
"# ---------------- THEME ----------------\n",
|
| 3744 |
+
"theme = gr.themes.Base(\n",
|
| 3745 |
+
" primary_hue=gr.themes.colors.orange,\n",
|
| 3746 |
+
" secondary_hue=gr.themes.colors.purple,\n",
|
| 3747 |
+
" neutral_hue=gr.themes.colors.zinc,\n",
|
| 3748 |
+
" font=[gr.themes.GoogleFont(\"Inter\"), \"Arial\", \"sans-serif\"]\n",
|
| 3749 |
+
").set(\n",
|
| 3750 |
+
" body_background_fill=\"#111014\",\n",
|
| 3751 |
+
" body_text_color=\"#F3EDF7\",\n",
|
| 3752 |
+
" block_background_fill=\"#1D1924\",\n",
|
| 3753 |
+
" block_border_color=\"#393044\",\n",
|
| 3754 |
+
" input_background_fill=\"#17131D\",\n",
|
| 3755 |
+
" button_primary_background_fill=\"#F28C28\",\n",
|
| 3756 |
+
" button_primary_background_fill_hover=\"#FFAA50\",\n",
|
| 3757 |
+
" button_primary_text_color=\"#171014\"\n",
|
| 3758 |
+
")\n",
|
| 3759 |
+
"\n",
|
| 3760 |
+
"css = \"\"\"\n",
|
| 3761 |
+
".gradio-container {\n",
|
| 3762 |
+
" max-width: 1050px !important;\n",
|
| 3763 |
+
" margin: auto !important;\n",
|
| 3764 |
+
"}\n",
|
| 3765 |
+
"#app-title {\n",
|
| 3766 |
+
" text-align: center;\n",
|
| 3767 |
+
" padding: 12px;\n",
|
| 3768 |
+
"}\n",
|
| 3769 |
+
"#app-title h1 {\n",
|
| 3770 |
+
" color: #FF983D;\n",
|
| 3771 |
+
" letter-spacing: 2px;\n",
|
| 3772 |
+
"}\n",
|
| 3773 |
+
"#image-display img {\n",
|
| 3774 |
+
" max-height: 580px !important;\n",
|
| 3775 |
+
" object-fit: contain !important;\n",
|
| 3776 |
+
"}\n",
|
| 3777 |
+
"#caption textarea {\n",
|
| 3778 |
+
" font-size: 16px !important;\n",
|
| 3779 |
+
" line-height: 1.6 !important;\n",
|
| 3780 |
+
" background: #17131D !important;\n",
|
| 3781 |
+
" border: 1px solid #704B85 !important;\n",
|
| 3782 |
+
"}\n",
|
| 3783 |
+
"#counter {\n",
|
| 3784 |
+
" text-align: center;\n",
|
| 3785 |
+
" color: #FF983D;\n",
|
| 3786 |
+
" font-weight: bold;\n",
|
| 3787 |
+
" letter-spacing: 1px;\n",
|
| 3788 |
+
"}\n",
|
| 3789 |
+
".nav-button {\n",
|
| 3790 |
+
" min-height: 48px !important;\n",
|
| 3791 |
+
" font-size: 16px !important;\n",
|
| 3792 |
+
"}\n",
|
| 3793 |
+
"#download-button {\n",
|
| 3794 |
+
" min-height: 52px !important;\n",
|
| 3795 |
+
" font-weight: bold !important;\n",
|
| 3796 |
+
"}\n",
|
| 3797 |
+
"footer {\n",
|
| 3798 |
+
" display: none !important;\n",
|
| 3799 |
+
"}\n",
|
| 3800 |
+
"\"\"\"\n",
|
| 3801 |
+
"\n",
|
| 3802 |
+
"# ---------------- GRADIO APP ----------------\n",
|
| 3803 |
+
"with gr.Blocks(theme=theme, css=css, title=\"Sankaku Caption Studio\") as demo:\n",
|
| 3804 |
+
"\n",
|
| 3805 |
+
" gr.HTML(\"\"\"\n",
|
| 3806 |
+
" <div id=\"app-title\">\n",
|
| 3807 |
+
" <h1>✦ SANKAKU CAPTION STUDIO ✦</h1>\n",
|
| 3808 |
+
" <p style=\"color:#A99BB5\">\n",
|
| 3809 |
+
" LoRA Dataset Caption Editor & Secure Drive Sync\n",
|
| 3810 |
+
" </p>\n",
|
| 3811 |
+
" </div>\n",
|
| 3812 |
+
" \"\"\")\n",
|
| 3813 |
+
"\n",
|
| 3814 |
+
" current_index = gr.State(0)\n",
|
| 3815 |
+
" captions_state = gr.State({})\n",
|
| 3816 |
+
"\n",
|
| 3817 |
+
" counter = gr.Markdown(\n",
|
| 3818 |
+
" f\"IMAGE 0001 / {len(images):04d}\",\n",
|
| 3819 |
+
" elem_id=\"counter\"\n",
|
| 3820 |
+
" )\n",
|
| 3821 |
+
"\n",
|
| 3822 |
+
" image_display = gr.Image(\n",
|
| 3823 |
+
" value=images[0][\"image\"],\n",
|
| 3824 |
+
" label=\"\",\n",
|
| 3825 |
+
" type=\"filepath\",\n",
|
| 3826 |
+
" interactive=False,\n",
|
| 3827 |
+
" show_label=False,\n",
|
| 3828 |
+
" elem_id=\"image-display\",\n",
|
| 3829 |
+
" height=580\n",
|
| 3830 |
+
" )\n",
|
| 3831 |
+
"\n",
|
| 3832 |
+
" with gr.Accordion(\"▸ INPUT HINTS — click to expand\", open=False):\n",
|
| 3833 |
+
" hints_display = gr.Textbox(\n",
|
| 3834 |
+
" value=read_hints(images[0]),\n",
|
| 3835 |
+
" label=\"Original input / tag hints\",\n",
|
| 3836 |
+
" lines=5,\n",
|
| 3837 |
+
" interactive=False\n",
|
| 3838 |
+
" )\n",
|
| 3839 |
+
"\n",
|
| 3840 |
+
" caption_box = gr.Textbox(\n",
|
| 3841 |
+
" value=read_caption(images[0]),\n",
|
| 3842 |
+
" label=\"IMAGE CAPTION\",\n",
|
| 3843 |
+
" placeholder=\"Write your image description here...\",\n",
|
| 3844 |
+
" lines=7,\n",
|
| 3845 |
+
" max_lines=15,\n",
|
| 3846 |
+
" elem_id=\"caption\"\n",
|
| 3847 |
+
" )\n",
|
| 3848 |
+
"\n",
|
| 3849 |
+
" with gr.Row():\n",
|
| 3850 |
+
" prev_button = gr.Button(\n",
|
| 3851 |
+
" \"← PREVIOUS\",\n",
|
| 3852 |
+
" variant=\"secondary\",\n",
|
| 3853 |
+
" elem_classes=[\"nav-button\"]\n",
|
| 3854 |
+
" )\n",
|
| 3855 |
+
" next_button = gr.Button(\n",
|
| 3856 |
+
" \"NEXT →\",\n",
|
| 3857 |
+
" variant=\"primary\",\n",
|
| 3858 |
+
" elem_classes=[\"nav-button\"]\n",
|
| 3859 |
+
" )\n",
|
| 3860 |
+
"\n",
|
| 3861 |
+
" with gr.Row():\n",
|
| 3862 |
+
" jump_number = gr.Number(\n",
|
| 3863 |
+
" label=\"Go to image number\",\n",
|
| 3864 |
+
" value=1,\n",
|
| 3865 |
+
" precision=0,\n",
|
| 3866 |
+
" minimum=1,\n",
|
| 3867 |
+
" maximum=len(images)\n",
|
| 3868 |
+
" )\n",
|
| 3869 |
+
" jump_button = gr.Button(\"GO\", variant=\"secondary\")\n",
|
| 3870 |
+
"\n",
|
| 3871 |
+
" gr.Markdown(\"---\")\n",
|
| 3872 |
+
"\n",
|
| 3873 |
+
" with gr.Group():\n",
|
| 3874 |
+
" gr.Markdown(\"### EXPORT & SYNC DATASET\")\n",
|
| 3875 |
+
" download_button = gr.Button(\n",
|
| 3876 |
+
" \"✦ SAVE & BACKUP SECURE 7Z TO DRIVE\",\n",
|
| 3877 |
+
" variant=\"primary\",\n",
|
| 3878 |
+
" elem_id=\"download-button\"\n",
|
| 3879 |
+
" )\n",
|
| 3880 |
+
" download_file = gr.File(\n",
|
| 3881 |
+
" label=\"Completed dataset ZIP (Unencrypted copy)\",\n",
|
| 3882 |
+
" interactive=False\n",
|
| 3883 |
+
" )\n",
|
| 3884 |
+
" status = gr.Markdown(\"\")\n",
|
| 3885 |
+
"\n",
|
| 3886 |
+
" outputs = [\n",
|
| 3887 |
+
" image_display,\n",
|
| 3888 |
+
" caption_box,\n",
|
| 3889 |
+
" hints_display,\n",
|
| 3890 |
+
" counter,\n",
|
| 3891 |
+
" current_index,\n",
|
| 3892 |
+
" captions_state\n",
|
| 3893 |
+
" ]\n",
|
| 3894 |
+
"\n",
|
| 3895 |
+
" prev_button.click(\n",
|
| 3896 |
+
" previous_image,\n",
|
| 3897 |
+
" inputs=[current_index, caption_box, captions_state],\n",
|
| 3898 |
+
" outputs=outputs\n",
|
| 3899 |
+
" )\n",
|
| 3900 |
+
"\n",
|
| 3901 |
+
" next_button.click(\n",
|
| 3902 |
+
" next_image,\n",
|
| 3903 |
+
" inputs=[current_index, caption_box, captions_state],\n",
|
| 3904 |
+
" outputs=outputs\n",
|
| 3905 |
+
" )\n",
|
| 3906 |
+
"\n",
|
| 3907 |
+
" jump_button.click(\n",
|
| 3908 |
+
" lambda i, c, s: get_view(int(i) - 1, c, s),\n",
|
| 3909 |
+
" inputs=[jump_number, caption_box, captions_state],\n",
|
| 3910 |
+
" outputs=outputs\n",
|
| 3911 |
+
" )\n",
|
| 3912 |
+
"\n",
|
| 3913 |
+
" download_button.click(\n",
|
| 3914 |
+
" save_and_zip,\n",
|
| 3915 |
+
" inputs=[current_index, caption_box, captions_state],\n",
|
| 3916 |
+
" outputs=[download_file, status]\n",
|
| 3917 |
+
" )\n",
|
| 3918 |
+
"\n",
|
| 3919 |
+
"demo.launch(\n",
|
| 3920 |
+
" inline=True,\n",
|
| 3921 |
+
" share=True,\n",
|
| 3922 |
+
" debug=True\n",
|
| 3923 |
+
")"
|
| 3924 |
+
],
|
| 3925 |
+
"metadata": {
|
| 3926 |
+
"id": "hXhlVwn4GLi8"
|
| 3927 |
+
},
|
| 3928 |
+
"id": "hXhlVwn4GLi8",
|
| 3929 |
+
"execution_count": null,
|
| 3930 |
+
"outputs": []
|
| 3931 |
+
},
|
| 3932 |
+
{
|
| 3933 |
+
"cell_type": "markdown",
|
| 3934 |
+
"metadata": {
|
| 3935 |
+
"id": "e9c4035a"
|
| 3936 |
+
},
|
| 3937 |
+
"source": [
|
| 3938 |
+
"### Auto disconnect runtime session\n"
|
| 3939 |
+
],
|
| 3940 |
+
"id": "e9c4035a"
|
| 3941 |
+
},
|
| 3942 |
+
{
|
| 3943 |
+
"cell_type": "code",
|
| 3944 |
+
"metadata": {
|
| 3945 |
+
"id": "a45ebf2d"
|
| 3946 |
+
},
|
| 3947 |
+
"source": [
|
| 3948 |
+
"from google.colab import runtime\n",
|
| 3949 |
+
"runtime.unassign()"
|
| 3950 |
+
],
|
| 3951 |
+
"id": "a45ebf2d",
|
| 3952 |
+
"execution_count": null,
|
| 3953 |
+
"outputs": []
|
| 3954 |
}
|
| 3955 |
],
|
| 3956 |
"metadata": {
|
|
|
|
| 3957 |
"colab": {
|
|
|
|
| 3958 |
"provenance": [],
|
| 3959 |
"collapsed_sections": [
|
| 3960 |
"EytLQtu1OJ7A"
|