Download run_indepth.py from DexagonAI/indepth-10M: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/DexagonAI/indepth-10M/resolve/main/run_indepth.py
- Command line
-
hf download hf://DexagonAI/indepth-10M/run_indepth.py
-
curl -L -o run_indepth.py https://huggingface.co/DexagonAI/indepth-10M/resolve/main/run_indepth.py
2.97 kB
| #!/usr/bin/env python3 | |
| """Run InDepth from the command line. | |
| Usage: | |
| python run_indepth.py "cat wearing green crown" --steps 4 --cfg 1.5 --seed 13372000 | |
| python run_indepth.py --prompts a.txt --out sprites/ # one prompt per line | |
| python run_indepth.py --local /path/to/indepth-10M ... # local dir instead of the hub | |
| Requires: torch, safetensors, huggingface_hub (for hub download), pillow. | |
| """ | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from PIL import Image | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Generate 16x16 RGBA sprites with InDepth") | |
| parser.add_argument("prompt", nargs="?", help="text prompt (lowercase, short works best)") | |
| parser.add_argument("--prompts", help="text file with one prompt per line") | |
| parser.add_argument("--out", default="out/", help="output directory") | |
| parser.add_argument("--local", help="local model directory (skips hub download)") | |
| parser.add_argument("--steps", type=int, default=4, help="sampling steps (1-4)") | |
| parser.add_argument("--cfg", type=float, default=1.5, help="classifier-free guidance (1 = off)") | |
| parser.add_argument("--seed", type=int, default=13372000) | |
| parser.add_argument("--device", default="cpu", help="'cpu' or 'cuda'") | |
| parser.add_argument("--scale", type=int, default=8, help="PNG upscale factor (nearest)") | |
| args = parser.parse_args() | |
| if not args.prompt and not args.prompts: | |
| parser.error("give a prompt or --prompts file") | |
| from modeling_indepth import decode_to_rgba, generate, load_indepth | |
| from tokenizer import IndepthTokenizer | |
| model_dir = args.local or "DexagonAI/indepth-10M" | |
| model, config, resolved_dir = load_indepth(model_dir) | |
| tokenizer = IndepthTokenizer(resolved_dir) | |
| print(f"loaded {sum(p.numel() for p in model.parameters()):,} params, " | |
| f"vocab {len(tokenizer.stoi)} words, {config.subword_buckets} subword buckets") | |
| prompts = [args.prompt] if args.prompt else [l.strip() for l in Path(args.prompts).read_text().splitlines() if l.strip()] | |
| device = args.device | |
| model = model.to(device) | |
| token_rows, sub_rows = tokenizer.batch(prompts) | |
| import torch | |
| tokens = torch.tensor(token_rows, dtype=torch.long) | |
| subwords = torch.tensor(sub_rows, dtype=torch.long) | |
| image = generate(model, config, tokens, subwords, seed=args.seed, steps=args.steps, guidance=args.cfg, device=device) | |
| rgba = decode_to_rgba(image) | |
| out_dir = Path(args.out) | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| for i, prompt in enumerate(prompts): | |
| sprite = Image.fromarray(rgba[i], "RGBA").resize( | |
| (16 * args.scale, 16 * args.scale), Image.Resampling.NEAREST) | |
| slug = "-".join(w for w in __import__("re").split(r"[^a-z0-9]+", prompt.lower()) if w)[:40] or "sprite" | |
| path = out_dir / f"{slug}_{args.seed}_{i}.png" | |
| sprite.save(path) | |
| print("wrote", path) | |
| if __name__ == "__main__": | |
| main() | |