"""Trout Pixel Studio - live demo Space. Runs the real Trout model in this Space: text prompt -> 16x16 pixel-art sprites. The model and palette are downloaded from the FrameXlabs/Trout repo at startup and inference runs on CPU right here. """ from __future__ import annotations import os import time import gradio as gr import torch from huggingface_hub import hf_hub_download os.environ.setdefault("TROUT_CLIP_LOCAL", "") # always fetch CLIP from the hub here import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from trout.pipeline import TroutPipeline # noqa: E402 from trout.viz import make_grid # noqa: E402 from trout.config import DEFAULT_PROMPTS # noqa: E402 MODEL_REPO = "FrameXlabs/Trout" CKPT = "trout-small-fp16.pt" pipe = None def load_pipeline() -> TroutPipeline: global pipe if pipe is not None: return pipe t0 = time.time() ckpt_path = hf_hub_download(repo_id=MODEL_REPO, filename=CKPT) pipe = TroutPipeline.from_checkpoint(ckpt_path, device="cpu", use_ema=False) print(f"[trout] model loaded in {time.time()-t0:.1f}s (training step {getattr(pipe, 'step', '?')})", flush=True) return pipe def generate(prompt: str, num: int, steps: int, cfg_scale: float, seed: int): p = load_pipeline() if not prompt or not prompt.strip(): prompt = "red health potion bottle" t0 = time.time() images = p([prompt.strip()], num_per_prompt=num, steps=int(steps), cfg_scale=float(cfg_scale), seed=int(seed) if seed is not None and seed >= 0 else None, temperature=1.0, choice_temperature=2.0, spatial_nms=True) grid = make_grid(images, ncols=min(8, max(1, num)), scale=8) dt = time.time() - t0 print(f"[trout] generated {num} sprites in {dt:.1f}s ({steps} steps, cfg {cfg_scale})", flush=True) return grid TITLE = """ # 🐟 Trout — a 47.6M-parameter diffusion language model for pixel art **Trout** is a from-scratch ~47.6M-parameter **bidirectional masked-diffusion transformer** that turns text prompts into **16×16 pixel-art sprites**. Every pixel is one of **512 palette tokens**; the model unmasks all 256 pixels in parallel in ~16 steps, conditioned on a frozen **CLIP** text encoder. No VAE, no latent space, no autoregression — the same model family as [`thejagstudio/tessera-small`](https://huggingface.co/thejagstudio/tessera-small), trained from scratch on [Pixel Art Atlas 1.4M](https://huggingface.co/datasets/Angais/pixel-art-atlas-1.4m). ⚠️ **Honesty note:** this checkpoint was trained for **1,000 steps on a shared CPU** (~0.003% of the reference recipe's compute). It renders palette-plausible sprites and rough shapes, but it is **very undertrained** — expect blobs of reasonable colours more than sharp objects. The full training details are in the [model card](https://huggingface.co/FrameXlabs/Trout). """ FOOTER = ("Trout by FrameXlabs · weights + code on [FrameXlabs/Trout](https://huggingface.co/FrameXlabs/Trout) · " "trained on [Pixel Art Atlas 1.4M](https://huggingface.co/datasets/Angais/pixel-art-atlas-1.4m) by Angais · " "inspired by tessera-small (TheJagStudio) · frozen text encoder: CLIP ViT-B/32 (OpenAI, MIT)") with gr.Blocks(title="Trout — pixel art diffusion language model") as demo: gr.Markdown(TITLE) with gr.Row(): with gr.Column(scale=1): prompt = gr.Textbox(label="Prompt", value="red health potion bottle", placeholder="golden crown with blue gems", lines=1) num = gr.Slider(1, 16, value=4, step=1, label="Sprites") steps = gr.Slider(2, 32, value=12, step=1, label="Sampling steps") cfg = gr.Slider(1.0, 8.0, value=3.0, step=0.1, label="CFG scale") seed = gr.Number(label="Seed (-1 = random)", value=-1, precision=0) go = gr.Button("🎣 Generate", variant="primary") hint = gr.Markdown("_CPU inference: ~5-40s depending on settings. " "The model runs right here in this Space._") with gr.Column(scale=1): out = gr.Image(label="Sprites (upscaled 8×, transparency = checkerboard)", type="pil", height=420) gr.Examples(examples=[[p] for p in DEFAULT_PROMPTS], inputs=[prompt], label="Example prompts") gr.Markdown(FOOTER) go.click(generate, inputs=[prompt, num, steps, cfg, seed], outputs=[out]) if __name__ == "__main__": demo.queue(max_size=4).launch(server_name="0.0.0.0")