@@ -0,0 +1,351 @@
1+#!/usr/bin/env python3
2+"""Generate raw SDXL LoRA panels from comic specs.
3+
4+This script intentionally generates only raw image panels. It does not compose speech
5+bubbles, captions, terminal text, borders, or final strips.
6+"""
7+
8+from __future__ import annotations
9+
10+import argparse
11+import json
12+import os
13+import random
14+from dataclasses import dataclass
15+from datetime import datetime, timezone
16+from pathlib import Path
17+from typing import Any
18+
19+DEFAULT_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
20+DEFAULT_VAE = "madebyollin/sdxl-vae-fp16-fix"
21+DEFAULT_STYLE_PROMPT = "ctdstyle, neon green outline doodle, pure black background, simple sparse comic"
22+DEFAULT_CHARACTER_PROMPT = (
23+ "Dev: stick figure developer, round head, rectangular glasses, thin limbs. "
24+ "Clanker: small boxy robot, screen head, antenna, claw hands"
25+)
26+DEFAULT_PANEL_SUFFIX = "raw art only, no text, no speech bubbles"
27+DEFAULT_NEGATIVE_PROMPT = (
28+ "anime, manga, cyberpunk, armored suit, helmet, realistic human, hoodie, detailed clothing, "
29+ "text, words, letters, speech bubble, caption, watermark, logo, panel border, white background, "
30+ "gray background, complex background, photorealistic, 3d render, clutter, extra limbs, "
31+ "malformed limbs, blurry, low quality"
32+)
33+
34+
35+@dataclass(frozen=True)
36+class PanelJob:
37+ index: int
38+ art_prompt: str
39+ output_path: Path
40+ seed: int
41+
42+
43+def project_paths() -> tuple[Path, Path]:
44+ comic_root = Path(__file__).resolve().parent
45+ site_root = comic_root.parent
46+ return comic_root, site_root
47+
48+
49+def display_path(path: Path) -> str:
50+ """Return repo-relative metadata paths when possible."""
51+ _, site_root = project_paths()
52+ try:
53+ return path.resolve().relative_to(site_root).as_posix()
54+ except ValueError:
55+ return str(path)
56+
57+
58+def parse_args() -> argparse.Namespace:
59+ comic_root, site_root = project_paths()
60+
61+ parser = argparse.ArgumentParser(description="Generate raw image panels from comic specs.")
62+ parser.add_argument("spec", nargs="?", type=Path, help="Spec JSON path. If omitted, use --slug or generate all specs.")
63+ parser.add_argument("--slug", help="Generate comic/specs/<slug>.json")
64+ parser.add_argument("--all", action="store_true", help="Generate every JSON spec in comic/specs")
65+
66+ parser.add_argument("--specs-dir", type=Path, default=comic_root / "specs")
67+ parser.add_argument("--assets-dir", type=Path, default=site_root / "assets" / "comics")
68+ parser.add_argument("--output-dir", type=Path, default=None, help="Override output directory for a single spec")
69+
70+ parser.add_argument("--pretrained-model-name-or-path", default=DEFAULT_MODEL)
71+ parser.add_argument("--vae-model-name-or-path", default=DEFAULT_VAE)
72+ parser.add_argument("--lora-path", type=Path, default=comic_root / "models" / "ctdstyle-sdxl-lora")
73+ parser.add_argument("--no-lora", action="store_true")
74+ parser.add_argument("--lora-scale", type=float, default=0.85)
75+ parser.add_argument("--hf-home", type=Path, default=comic_root / ".cache" / "huggingface")
76+
77+ parser.add_argument("--style-prompt", default=DEFAULT_STYLE_PROMPT)
78+ parser.add_argument("--character-prompt", default=DEFAULT_CHARACTER_PROMPT)
79+ parser.add_argument("--character-prompt-file", type=Path, help="Optional text file overriding --character-prompt")
80+ parser.add_argument("--no-character-prompt", action="store_true", help="Do not inject shared Dev/Clanker descriptions")
81+ parser.add_argument("--panel-suffix", default=DEFAULT_PANEL_SUFFIX)
82+ parser.add_argument(
83+ "--prompt-2-mode",
84+ choices=["split", "same", "none"],
85+ default="split",
86+ help="How to populate SDXL prompt_2: split keeps reusable character context there, same mirrors prompt, none leaves it empty.",
87+ )
88+ parser.add_argument("--negative-prompt", default=DEFAULT_NEGATIVE_PROMPT)
89+ parser.add_argument("--guidance-scale", type=float, default=7.0)
90+ parser.add_argument("--num-inference-steps", type=int, default=35)
91+ parser.add_argument("--width", type=int, default=1024)
92+ parser.add_argument("--height", type=int, default=1024)
93+ parser.add_argument("--seed", type=int, default=1234)
94+ parser.add_argument("--seed-stride", type=int, default=101)
95+
96+ parser.add_argument("--panel", type=int, action="append", help="1-based panel number to generate. Repeat for multiple panels.")
97+ parser.add_argument("--num-images-per-panel", type=int, default=1)
98+ parser.add_argument("--overwrite", action="store_true")
99+ parser.add_argument(
100+ "--clean-stale",
101+ action=argparse.BooleanOptionalAction,
102+ default=True,
103+ help="When generating a full spec with --overwrite, remove stale raw-panel-*.png files for panels no longer in the spec.",
104+ )
105+ parser.add_argument("--dry-run", action="store_true")
106+ parser.add_argument("--print-prompts", action="store_true", help="Print full prompts as panels are planned")
107+ parser.add_argument("--cpu-offload", action=argparse.BooleanOptionalAction, default=True)
108+ parser.add_argument("--mixed-precision", choices=["fp16", "bf16", "no"], default="fp16")
109+ return parser.parse_args()
110+
111+
112+def resolve_specs(args: argparse.Namespace) -> list[Path]:
113+ if args.all:
114+ return sorted(args.specs_dir.glob("*.json"))
115+ if args.spec is not None:
116+ return [args.spec]
117+ if args.slug:
118+ return [args.specs_dir / f"{args.slug}.json"]
119+ raise SystemExit("Provide a spec path, --slug <slug>, or --all")
120+
121+
122+def load_spec(spec_path: Path) -> dict[str, Any]:
123+ if not spec_path.exists():
124+ raise FileNotFoundError(f"Spec not found: {spec_path}")
125+ spec = json.loads(spec_path.read_text(encoding="utf-8"))
126+ if not isinstance(spec.get("panels"), list) or not spec["panels"]:
127+ raise ValueError(f"Spec has no panels: {spec_path}")
128+ for index, panel in enumerate(spec["panels"], start=1):
129+ if not str(panel.get("artPrompt", "")).strip():
130+ raise ValueError(f"Panel {index} in {spec_path} is missing artPrompt")
131+ if not str(spec.get("slug", "")).strip():
132+ spec["slug"] = spec_path.stem
133+ return spec
134+
135+
136+def resolve_character_prompt(args: argparse.Namespace) -> str:
137+ if args.no_character_prompt:
138+ return ""
139+ if args.character_prompt_file:
140+ return args.character_prompt_file.read_text(encoding="utf-8").strip()
141+ return args.character_prompt.strip()
142+
143+
144+def join_prompt_parts(*parts: str) -> str:
145+ return ", ".join(part.strip() for part in parts if part and part.strip())
146+
147+
148+def build_prompts(
149+ art_prompt: str,
150+ style_prompt: str,
151+ character_prompt: str,
152+ panel_suffix: str,
153+ prompt_2_mode: str,
154+) -> tuple[str, str]:
155+ # SDXL has two text encoders, each with a short CLIP token limit. The default
156+ # split mode keeps panel/action direction in prompt and reusable character
157+ # definitions in prompt_2. For small LoRAs, mirroring prompt into prompt_2 is
158+ # useful because training captions conditioned both text encoders on the same
159+ # caption text.
160+ prompt = join_prompt_parts(style_prompt, art_prompt, panel_suffix)
161+ if prompt_2_mode == "same":
162+ prompt_2 = prompt
163+ elif prompt_2_mode == "none":
164+ prompt_2 = ""
165+ else:
166+ prompt_2 = join_prompt_parts(style_prompt, character_prompt, panel_suffix)
167+ return prompt, prompt_2
168+
169+
170+def output_name(panel_index: int, variant_index: int, num_variants: int) -> str:
171+ if num_variants == 1:
172+ return f"raw-panel-{panel_index:02d}.png"
173+ return f"raw-panel-{panel_index:02d}-{variant_index:02d}.png"
174+
175+
176+def plan_jobs(spec: dict[str, Any], spec_output_dir: Path, args: argparse.Namespace) -> list[PanelJob]:
177+ requested_panels = set(args.panel or [])
178+ jobs: list[PanelJob] = []
179+
180+ for panel_index, panel in enumerate(spec["panels"], start=1):
181+ if requested_panels and panel_index not in requested_panels:
182+ continue
183+ for variant_index in range(1, args.num_images_per_panel + 1):
184+ seed = args.seed + (panel_index - 1) * args.seed_stride + (variant_index - 1)
185+ jobs.append(
186+ PanelJob(
187+ index=panel_index,
188+ art_prompt=panel["artPrompt"],
189+ output_path=spec_output_dir / output_name(panel_index, variant_index, args.num_images_per_panel),
190+ seed=seed,
191+ )
192+ )
193+
194+ if not jobs:
195+ raise ValueError("No panels selected")
196+ return jobs
197+
198+
199+def load_pipeline(args: argparse.Namespace) -> Any:
200+ import torch
201+ from diffusers import AutoencoderKL, DPMSolverMultistepScheduler, StableDiffusionXLPipeline
202+
203+ dtype = {"fp16": torch.float16, "bf16": torch.bfloat16, "no": torch.float32}[args.mixed_precision]
204+
205+ vae = None
206+ if args.vae_model_name_or_path:
207+ vae = AutoencoderKL.from_pretrained(args.vae_model_name_or_path, torch_dtype=dtype)
208+
209+ pipe = StableDiffusionXLPipeline.from_pretrained(
210+ args.pretrained_model_name_or_path,
211+ vae=vae,
212+ torch_dtype=dtype,
213+ variant="fp16" if args.mixed_precision == "fp16" else None,
214+ use_safetensors=True,
215+ )
216+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
217+
218+ if not args.no_lora:
219+ if not args.lora_path.exists():
220+ raise FileNotFoundError(f"LoRA path not found: {args.lora_path}")
221+ if args.lora_path.is_file():
222+ pipe.load_lora_weights(str(args.lora_path.parent), weight_name=args.lora_path.name, adapter_name="ctdstyle")
223+ else:
224+ lora_kwargs: dict[str, Any] = {}
225+ default_weight = args.lora_path / "pytorch_lora_weights.safetensors"
226+ if default_weight.exists():
227+ lora_kwargs["weight_name"] = default_weight.name
228+ pipe.load_lora_weights(str(args.lora_path), adapter_name="ctdstyle", **lora_kwargs)
229+ if hasattr(pipe, "set_adapters"):
230+ pipe.set_adapters(["ctdstyle"], adapter_weights=[args.lora_scale])
231+
232+ if args.cpu_offload:
233+ pipe.enable_model_cpu_offload()
234+ else:
235+ pipe.to("cuda" if torch.cuda.is_available() else "cpu")
236+
237+ return pipe
238+
239+
240+def remove_stale_raw_panels(spec_output_dir: Path, jobs: list[PanelJob], args: argparse.Namespace) -> None:
241+ if args.dry_run or not args.overwrite or not args.clean_stale or args.panel:
242+ return
243+
244+ expected = {job.output_path.resolve() for job in jobs}
245+ for path in sorted(spec_output_dir.glob("raw-panel-*.png")):
246+ if path.resolve() not in expected:
247+ print(f"remove stale {path}")
248+ path.unlink()
249+
250+
251+def generate_spec(spec_path: Path, pipe: Any | None, args: argparse.Namespace) -> None:
252+ import torch
253+
254+ spec = load_spec(spec_path)
255+ slug = spec["slug"]
256+ spec_output_dir = args.output_dir if args.output_dir else args.assets_dir / slug
257+ spec_output_dir.mkdir(parents=True, exist_ok=True)
258+
259+ jobs = plan_jobs(spec, spec_output_dir, args)
260+ remove_stale_raw_panels(spec_output_dir, jobs, args)
261+ character_prompt = resolve_character_prompt(args)
262+ records: list[dict[str, Any]] = []
263+
Cut short: this commit's diff is over 1000 lines.