RASHID778's picture
upload train script publish.py
819f7a7 verified
Raw History Blame Contribute Delete
3.66 kB
import argparse
import json
import os
def get_token() -> str:
tok = os.environ.get("HF_TOKEN", "")
if tok:
return tok
for p in ("/content/king_hf_token.txt", "/content/hf_token.txt"):
if os.path.isfile(p):
return open(p).read().strip()
try:
from google.colab import userdata
return userdata.get("HF_TOKEN")
except Exception:
pass
raise RuntimeError("HF_TOKEN not found (env, /content/king_hf_token.txt or Colab secret 'HF_TOKEN')")
def build_section(cfg: dict) -> str:
base, steps, lr, res, rank = (
cfg.get("base_model", "stabilityai/stable-diffusion-xl-base-1.0"),
cfg.get("steps", 2000),
cfg.get("lr", 1e-4),
cfg.get("resolution", 512),
cfg.get("rank", 16),
)
return f"""
## [](#king2-image-v2--continual-fine-tune)🔄 KING2-IMAGE V2 — Continual Fine-Tune
**Second training run.** Continuation of `pytorch_lora_weights.safetensors` (the original
UNet-only rank-{rank} LoRA), further fine-tuned on a new 19,998-image dataset
(`indoor/` + `outdoor/` synthetic scenes) with auto-captions generated by
**Microsoft Florence-2** (`microsoft/Florence-2-base`, `DETAILED_CAPTION`).
| Property | Value |
|---|---|
| **Starting point** | `RASHID778/king2-image` LoRA (rank {rank}) |
| **New dataset** | [`RASHID778/king2-image-dataset`](https://huggingface.co/datasets/RASHID778/king2-image-dataset) |
| **Images** | 9,999 indoor (416×416) + 9,999 outdoor (150×150) |
| **Captions** | Microsoft Florence-2 `DETAILED_CAPTION` |
| **Resolution** | {res}×{res} (center-crop resize) |
| **LoRA rank / alpha** | {rank} / {rank} |
| **Optimizer** | AdamW |
| **Learning Rate** | {lr} |
| **Scheduler** | Cosine with warm-up |
| **Training Steps** | {steps} |
| **Batch / Gradient Accum** | 1 / 4 |
| **Mixed Precision** | fp16 |
| **UNet-only** | text encoders frozen |
Checkpoints of this run are kept under `checkpoint-continue-*`. The original
checkpoints (`checkpoint-500/1000/1500`) are untouched. The archive copy of the
pre-V2 LoRA lives in [`RASHID778/king2-image-dataset`](https://huggingface.co/datasets/RASHID778/king2-image-dataset)
under `king2-original/`.
"""
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--repo_id", default="RASHID778/king2-image")
ap.add_argument("--steps", type=int, default=2000)
ap.add_argument("--lr", type=float, default=1e-4)
ap.add_argument("--resolution", type=int, default=512)
ap.add_argument("--rank", type=int, default=16)
ap.add_argument("--base_model", default="stabilityai/stable-diffusion-xl-base-1.0")
args = ap.parse_args()
from huggingface_hub import HfApi, hf_hub_download, login
login(token=get_token())
api = HfApi()
readme_path = hf_hub_download(args.repo_id, "README.md")
readme = open(readme_path, encoding="utf-8").read()
marker = "KING2-IMAGE V2 — Continual Fine-Tune"
if marker in readme:
print("[skip] README already documents the V2 run", flush=True)
return
section = build_section(vars(args))
anchor = "## 📋 Overview"
if anchor in readme:
readme = readme.replace(anchor, section.strip() + "\n\n" + anchor, 1)
else:
readme = readme + "\n" + section
open("/content/README.md", "w", encoding="utf-8").write(readme)
api.upload_file(
path_or_fileobj="/content/README.md",
path_in_repo="README.md",
repo_id=args.repo_id,
commit_message="document king2-image V2 continual fine-tune",
)
print("[ok] README updated with V2 section", flush=True)
if __name__ == "__main__":
main()