"""OpenAI-compatible image server for SenseNova-U1.5-8B-MoT. sensenova/SenseNova-U1.5-8B-MoT (Apache-2.0, OpenSenseNova / SenseTime) is a native unified multimodal model on the NEO-unify architecture: ONE checkpoint does text-to-image, image editing and visual question answering. Internally it is a Qwen3-8B LLM backbone with MoT — Mixture of Transformers — generation branches (the *_mot_gen weights), and it is a PIXEL-SPACE generator, so there is no separate VAE to load. Apache-2.0 matters here: most of the strong image models on this rig are non-commercial (qwen-image-2.1 is Qwen Research, tinydit-256 is CC BY-NC). This one and Ming-Image are the two that can back something shipped. WHY THE VENDOR REPO AND NOT ComfyUI: ComfyUI master does carry a SenseNovaU15 class, and its detection keys are the official checkpoint's own key names, so a merged single-file repack does load there. We deliberately run the vendor's own inference code instead — it is the path the model was tested on, it keeps us on official Apache-2.0 weights with no community repack in the trust chain, and it gives us think_mode and the official 8-step LoRA, neither of which the ComfyUI path exposes. The code is baked into an image (machines/gx10/docker/Dockerfile.sensenova-u1) rather than pip-installed at boot, because sensenova_u1 must be importable before transformers sees the checkpoint: it self-registers NEOChatModel at import time, which is what lets this run without trust_remote_code. Generation defaults are hardcoded in the request models below and echoed per call under "params_used"; query them via GET /v1/config. """ import base64, io, os, threading, time from typing import Optional import torch from fastapi import FastAPI, HTTPException from fastapi.responses import JSONResponse from PIL import Image from pydantic import BaseModel, Field import uvicorn import sensenova_u1 # noqa: F401 — registers NEOChatModel with transformers on import from sensenova_u1.utils import load_and_merge_lora_weight_from_safetensors SERVED_MODEL_ID = os.environ.get("SERVED_MODEL_ID", "sensenova-u1.5-8b-mot") PORT = int(os.environ.get("PORT", "9204")) MODEL_DIR = os.environ.get("MODEL_DIR", "/model") LORA_PATH = os.environ.get("SENSENOVA_LORA", "").strip() VRAM_MODE = os.environ.get("SENSENOVA_VRAM_MODE", "") DEVICE_MAP = os.environ.get("SENSENOVA_DEVICE_MAP", "").strip() or None # The vendor's trained resolution set. Anything else triggers their own # "outside the trained resolution set" warning; we expose the buckets by name so # callers do not have to memorise the pixel pairs. SUPPORTED_RESOLUTIONS = { "1:1": (2048, 2048), "16:9": (2720, 1536), "9:16": (1536, 2720), "3:2": (2496, 1664), "2:3": (1664, 2496), "4:3": (2368, 1760), "3:4": (1760, 2368), "1:2": (1440, 2880), "2:1": (2880, 1440), "1:3": (1152, 3456), "3:1": (3456, 1152), } # With the official 8-step distillation LoRA merged, the step count and CFG both # change — a few-step model sampled at 50 steps / cfg 4 is wrong, not just slow. LORA_DEFAULTS = {"num_inference_steps": 8, "guidance_scale": 1.0} BASE_DEFAULTS = {"num_inference_steps": 50, "guidance_scale": 4.0} app = FastAPI() _lock = threading.Lock() _state = {} def _defaults(): return LORA_DEFAULTS if _state.get("lora_merged") else BASE_DEFAULTS def _load(): t0 = time.time() # Imported lazily so the module can be read without the vendor tree present. import sys sys.path.insert(0, "/opt/sensenova-u1/examples/t2i") from inference import SenseNovaU1T2I # vendor's engine wrapper kwargs = dict(model_path=MODEL_DIR, device="cuda", dtype=torch.bfloat16) if VRAM_MODE: kwargs["vram_mode"] = VRAM_MODE if DEVICE_MAP: kwargs["device_map"] = DEVICE_MAP engine = SenseNovaU1T2I(**kwargs) lora_merged = False if LORA_PATH: if not os.path.exists(LORA_PATH): print(f"WARNING: SENSENOVA_LORA={LORA_PATH} does not exist; running full-step.", flush=True) else: print(f"merging LoRA {LORA_PATH}", flush=True) engine.model = load_and_merge_lora_weight_from_safetensors(engine.model, LORA_PATH) lora_merged = True _state.update(engine=engine, lora_merged=lora_merged) print(f"SenseNova-U1.5 loaded in {time.time()-t0:.1f}s | lora_merged={lora_merged}", flush=True) class ImageRequest(BaseModel): prompt: str = Field(..., description="Text prompt. Direct natural language works for simple asks; for complex layouts the vendor recommends prompt enhancement upstream and spelling out what must stay fixed.") n: int = Field(1, description="Number of images (vendor batch_size).") size: str = Field("1:1", description="An aspect key from /v1/config -> supported_resolutions ('1:1' = 2048x2048, '16:9' = 2720x1536, up to '3:1' = 3456x1152), or an explicit WxH. Off-bucket sizes are accepted but the vendor warns quality degrades.") num_inference_steps: Optional[int] = Field(None, description="Sampler steps. Defaults to 8 when the official 8-step LoRA is merged, else 50. Setting this explicitly overrides either.") guidance_scale: Optional[float] = Field(None, description="CFG scale (vendor cfg_scale). Defaults to 1.0 with the 8-step LoRA merged, else 4.0. The card notes oversaturation/over-detail is usually fixed by LOWERING this.") cfg_norm: str = Field("none", description="Vendor cfg_norm strategy.") timestep_shift: float = Field(3.0, description="Flow timestep shift. Vendor default 3.0.") think_mode: bool = Field(False, description="Run the model's own reasoning pass before generating; the text is returned alongside the image. Slower, and intended for complex or heavily constrained prompts.") seed: Optional[int] = Field(None, description="Random seed. null = random per call.") response_format: str = Field("b64_json", description="Only b64_json supported.") def _params_doc(model): out = {} for name, info in model.model_fields.items(): d = info.default out[name] = {"default": None if d is ... else d, "description": info.description} return out def _resolve_size(size: str): if size in SUPPORTED_RESOLUTIONS: return SUPPORTED_RESOLUTIONS[size] try: w, h = (int(x) for x in size.lower().split("x")) except ValueError: raise HTTPException(400, f"size must be an aspect key {list(SUPPORTED_RESOLUTIONS)} or WxH; got {size}") return w, h @app.get("/health") def health(): ok = bool(_state) return {"status": "ok" if ok else "degraded", "model": SERVED_MODEL_ID, "loaded": ok, "lora_merged": _state.get("lora_merged", False)} @app.get("/v1/models") def models(): return {"object": "list", "data": [{"id": SERVED_MODEL_ID, "object": "model"}]} @app.get("/v1/config") def config(): return {"model": SERVED_MODEL_ID, "params": _params_doc(ImageRequest), "supported_resolutions": {k: f"{w}x{h}" for k, (w, h) in SUPPORTED_RESOLUTIONS.items()}, "lora_merged": _state.get("lora_merged", False), "active_defaults": _defaults(), "notes": [ "One checkpoint covers t2i, editing and VQA; this server exposes t2i " "and editing.", "Pixel-space generator — there is no separate VAE.", "Apache-2.0, so unlike qwen-image-2.1 and tinydit-256 this one is not " "restricted to non-commercial use.", "Known rough edges per the card: oversaturation (lower guidance_scale), " "dense/small mixed-script text, exact counts in constrained layouts, " "small faces and hands, and drift on broad multi-reference edits.", ]} @app.post("/v1/images/generations") def generate_images(req: ImageRequest): if not _state: raise HTTPException(503, "model not loaded") w, h = _resolve_size(req.size) d = _defaults() steps = req.num_inference_steps if req.num_inference_steps is not None else d["num_inference_steps"] cfg = req.guidance_scale if req.guidance_scale is not None else d["guidance_scale"] seed = req.seed if req.seed is not None else int(time.time() * 1000) % (2**31) t0 = time.time() # One engine, one CUDA context; FastAPI runs `def` endpoints in a threadpool, so # concurrent calls would interleave inside the sampler. Serialize and report the wait. with _lock: queued = time.time() - t0 images = _state["engine"].generate( req.prompt, image_size=(w, h), cfg_scale=cfg, cfg_norm=req.cfg_norm, timestep_shift=req.timestep_shift, num_steps=steps, batch_size=req.n, seed=seed, think_mode=req.think_mode) think = _state["engine"].last_think_text if req.think_mode else "" data = [] for im in images: buf = io.BytesIO(); im.save(buf, format="PNG") data.append({"b64_json": base64.b64encode(buf.getvalue()).decode()}) print(f"Generated {len(data)} image(s) at {w}x{h} in {time.time()-t0:.1f}s " f"(steps={steps} cfg={cfg})", flush=True) body = {"created": int(time.time()), "data": data, "params_used": {"n": req.n, "size": f"{w}x{h}", "num_inference_steps": steps, "guidance_scale": cfg, "cfg_norm": req.cfg_norm, "timestep_shift": req.timestep_shift, "think_mode": req.think_mode, "seed": seed, "lora_merged": _state.get("lora_merged", False), "queued_s": round(queued, 3)}} if think: body["think"] = think return JSONResponse(body) if __name__ == "__main__": print(f"SenseNova-U1.5 server on :{PORT}", flush=True) _load() uvicorn.run(app, host="0.0.0.0", port=PORT)