from __future__ import annotations import io import os from dataclasses import dataclass import spaces from PIL import Image MODEL = os.getenv("PEERPIXEL_MODEL", "black-forest-labs/FLUX.2-klein-base-4B") PIPELINE = None EDIT_PIPELINE = None SAFETY = None def gpu_duration(task: dict, source: bytes | None = None) -> int: outputs = max(1, min(4, int(task.get("outputCount", 1)))) steps = max(1, min(50, int(task.get("steps", 16)))) return min(300, max(60, 35 + outputs * steps * 2)) def _pipeline(source: bytes | None): global PIPELINE, EDIT_PIPELINE if source is None and PIPELINE is not None: return PIPELINE import torch from diffusers import Flux2KleinInpaintPipeline, Flux2KleinPipeline if PIPELINE is None: PIPELINE = Flux2KleinPipeline.from_pretrained(MODEL, torch_dtype=torch.bfloat16) PIPELINE.to("cuda") PIPELINE.vae.enable_tiling() if source is not None: if EDIT_PIPELINE is None: EDIT_PIPELINE = Flux2KleinInpaintPipeline.from_pipe(PIPELINE) return EDIT_PIPELINE return PIPELINE def _classifier(): global SAFETY if SAFETY is None: from transformers import pipeline SAFETY = pipeline("image-classification", model="Falconsai/nsfw_image_detection", device=-1) return SAFETY def _generator(seed: int): import torch return torch.Generator(device="cpu").manual_seed(seed) def _jpeg(image: Image.Image) -> bytes: output = io.BytesIO() image.convert("RGB").save(output, "JPEG", quality=95, subsampling=0, optimize=True) return output.getvalue() def _evidence(image: Image.Image) -> dict: results = _classifier()(image.convert("RGB")) score = max((float(x["score"]) for x in results if "nsfw" in str(x.get("label", "")).lower()), default=0.0) return {"label": "nsfw" if score > .65 else "normal", "unsafe": score > .65, "score": score} def compose_grid(images: list[Image.Image]) -> Image.Image: canvas = Image.new("RGB", (1024, 1024), "white") for index, image in enumerate(images[:4]): canvas.paste(image.convert("RGB").resize((512, 512), Image.Resampling.LANCZOS), ((index % 2) * 512, (index // 2) * 512)) return canvas # ZeroGPU provides CUDA emulation during startup. Root-level placement lets it # prepare the model before a real allocation instead of loading inside it. if os.getenv("SPACE_ID"): _pipeline(None) @spaces.GPU(duration=gpu_duration) def render_task(task: dict, source: bytes | None): pipe = _pipeline(source) count = max(1, min(4, int(task.get("outputCount", 1)))) width, height = int(task.get("width", 512)), int(task.get("height", 512)) base_seed = int(task.get("seed", 0)) source_image = Image.open(io.BytesIO(source)).convert("RGB") if source else None images = [] for index in range(count): kwargs = {"prompt": task["prompt"], "width": width, "height": height, "num_inference_steps": int(task.get("steps", 16)), "guidance_scale": float(task.get("guidance", 4.0)), "generator": _generator(base_seed + index * 104729)} if source_image is not None: prepared = source_image.resize((width, height), Image.Resampling.LANCZOS) kwargs.update(image=prepared, mask_image=Image.new("L", (width, height), 255), strength=float(task.get("strength", .65))) image = pipe(**kwargs).images[0] images.append(image) packaged = [{"bytes": _jpeg(image), "localSafety": _evidence(image)} for image in images] grid = None if count == 4: composite = compose_grid(images) grid = {"bytes": _jpeg(composite), "localSafety": _evidence(composite)} return {"images": packaged, "grid": grid}