""" title: Smart Image Generator (ComfyUI) author: ai-stack version: 0.2.0 description: Generate images via ComfyUI with automatic SDXL checkpoint routing. The LLM picks (or auto-detects) the right model — photoreal, Pony score-tag, NoobAI/Illustrious furry, etc. — based on the user's request. Each style ships with the creator-recommended sampler, scheduler, CFG, steps, CLIP skip, prompt-prefix dialect, and negatives. required_open_webui_version: 0.5.0 """ import asyncio import base64 import re import time import uuid from typing import Awaitable, Callable, Optional import aiohttp from pydantic import BaseModel, Field # ───────────────────────────────────────────────────────────────────────────── # Per-style settings — sampler/scheduler/cfg/steps/clip_skip/prefix/negatives # come from each model's creator page on Civitai. Three prefix dialects in # play: photoreal (no prefix, natural language), Pony score chain (REQUIRED # for any Pony-derived checkpoint), and Booru quality tags (NoobAI / # Illustrious lineage). Never cross-contaminate. # ───────────────────────────────────────────────────────────────────────────── STYLES = { "photo": { "ckpt": "CyberRealisticXLPlay_V8.0_FP16.safetensors", "sampler": "dpmpp_2m_sde", "scheduler": "karras", "cfg": 4.0, "steps": 28, "clip_skip": 1, "prefix": "", # natural language only — no quality tags "negative": ( "cartoon, drawing, illustration, anime, manga, painting, sketch, " "render, 3d, cgi, watercolor, plastic skin, doll-like, oversaturated, " "lowres, blurry, jpeg artifacts, noisy, grainy, low quality, worst quality, " "bad anatomy, deformed, mutated, extra limbs, extra fingers, missing fingers, " "fused fingers, malformed hands, asymmetric face, " "watermark, signature, text, logo, label, username" ), }, "juggernaut": { "ckpt": "Juggernaut-XL_v9_RunDiffusionPhoto_v2.safetensors", "sampler": "dpmpp_2m_sde", "scheduler": "karras", "cfg": 4.5, "steps": 35, "clip_skip": 1, "prefix": "", # natural language only "negative": ( "cartoon, drawing, illustration, anime, manga, painting, sketch, " "render, 3d, cgi, plastic skin, washed out, oversaturated, " "lowres, blurry, jpeg artifacts, low quality, worst quality, " "bad anatomy, deformed, mutated, extra limbs, extra fingers, missing fingers, " "fused fingers, malformed hands, " "watermark, signature, text, logo, username" ), }, "pony": { "ckpt": "ponyDiffusionV6XL_v6StartWithThisOne.safetensors", "sampler": "euler_ancestral", "scheduler": "normal", "cfg": 7.5, "steps": 25, "clip_skip": 2, # REQUIRED — the full chain. Just `score_9` alone is much weaker. "prefix": "score_9, score_8_up, score_7_up, score_6_up, score_5_up, score_4_up, ", # Pony's creator notes negatives are usually unnecessary; conservative # baseline only. Source-toggle tags (source_pony/furry/anime/cartoon) # are intentionally omitted — they exclude entire content domains. "negative": ( "score_6, score_5, score_4, " "worst quality, low quality, lowres, blurry, jpeg artifacts, noisy, " "bad anatomy, bad proportions, bad hands, extra digit, fewer digits, " "fused fingers, malformed limbs, deformed, ugly, " "censored, monochrome, " "watermark, signature, text, logo, artist name, patreon username, twitter username" ), }, "general": { "ckpt": "talmendoxlSDXL_v11Beta.safetensors", "sampler": "dpmpp_2m", "scheduler": "karras", "cfg": 8.0, # Talmendo wants notably higher CFG than the others "steps": 30, "clip_skip": 2, "prefix": "", # creator says don't push "masterpiece" — fights the amateur aesthetic "negative": ( "lowres, blurry, jpeg artifacts, noisy, grainy, low quality, worst quality, " "bad anatomy, deformed, mutated, extra limbs, missing fingers, fused fingers, " "malformed hands, ugly, " "watermark, signature, text, logo" ), }, "furry-nai": { "ckpt": "reedFURRYMixSDXL_v23nai.safetensors", "sampler": "euler_ancestral", "scheduler": "normal", "cfg": 5.0, "steps": 30, "clip_skip": 2, "prefix": ( "masterpiece, best quality, high quality, good quality, " "detailed eyes, highres, absurdres, furry, " ), "negative": ( "human, realistic, photorealistic, 3d, cgi, " "worst quality, bad_quality, normal quality, lowres, " "anatomical nonsense, bad anatomy, interlocked fingers, extra fingers, " "bad_feet, bad_hands, deformed anatomy, bad proportions, " "censored, simple background, transparent, face backlighting, " "watermark, signature, text, logo, username, jpeg artifacts" ), }, "furry-noob": { "ckpt": "indigoVoidFurryFusedXL_noobaiV32.safetensors", "sampler": "euler_ancestral", # creator: other samplers won't work "scheduler": "normal", "cfg": 4.5, "steps": 20, "clip_skip": 2, "prefix": ( "masterpiece, best quality, perfect quality, absurdres, newest, " "very aesthetic, vibrant colors, " ), "negative": ( "human, realistic, photorealistic, 3d, cgi, " "shiny skin, shiny clothing, " "worst quality, low quality, lowres, blurry, jpeg artifacts, noisy, " "bad anatomy, bad hands, mutated hands, bad proportions, " "extra digit, fewer digits, fused fingers, malformed limbs, deformed, ugly, " "watermark, signature, text, logo, username, artist signature" ), }, "furry-il": { "ckpt": "novaFurryXL_ilV170.safetensors", "sampler": "euler_ancestral", "scheduler": "normal", "cfg": 4.0, "steps": 30, "clip_skip": 2, # Illustrious wants `newest` in positive and `old`/`oldest` in negative # — these are year-bucket tags from the training set. `furry` and # `anthro` are universally helpful here. "prefix": ( "masterpiece, best quality, amazing quality, very aesthetic, " "high resolution, ultra-detailed, absurdres, newest, furry, anthro, " ), "negative": ( "human, multiple tails, modern, recent, old, oldest, " "graphic, cartoon, painting, crayon, graphite, abstract, glitch, " "deformed, mutated, ugly, disfigured, long body, conjoined, " "lowres, bad anatomy, bad hands, missing fingers, extra digits, fewer digits, " "cropped, very displeasing, worst quality, bad quality, sketch, " "jpeg artifacts, signature, watermark, username, text, simple background, " "bad ai-generated" ), }, } DEFAULT_STYLE = "general" # First-match-wins keyword router used when the caller didn't pick a style. # Order matters — narrower patterns above broader ones. ROUTING_RULES = [ # Pony score chain is the single strongest signal — Pony only (re.compile(r"\bscore_\d", re.I), "pony"), (re.compile(r"\bpony\b", re.I), "pony"), # NoobAI / Illustrious explicit mentions (re.compile(r"\b(noobai|noob)\b", re.I), "furry-noob"), (re.compile(r"\b(illustrious|ilxl)\b", re.I), "furry-il"), # Generic furry — defaults to NovaFurry (Illustrious lineage, current sweet spot) (re.compile(r"\b(furry|anthro|feral|kemono|fursona|species)\b", re.I), "furry-il"), # Photo / photoreal (re.compile(r"\b(juggernaut)\b", re.I), "juggernaut"), (re.compile(r"\b(photo|photograph|realistic|portrait|selfie|cinematic)\b", re.I), "photo"), # Generic anime / illustration → Pony covers anime well (re.compile(r"\b(anime|manga|2d|illustration)\b", re.I), "pony"), ] def _route_style(prompt: str) -> str: for pattern, style in ROUTING_RULES: if pattern.search(prompt): return style return DEFAULT_STYLE def _build_workflow(positive: str, negative: str, settings: dict, width: int, height: int, seed: int) -> dict: """ Construct an SDXL txt2img workflow. CLIP skip is implemented via a CLIPSetLastLayer node so the same graph works for skip 1 (-1) and skip 2 (-2). """ return { "3": {"class_type": "KSampler", "inputs": { "seed": seed if seed > 0 else int(time.time() * 1000) % (2**31), "steps": settings["steps"], "cfg": settings["cfg"], "sampler_name": settings["sampler"], "scheduler": settings["scheduler"], "denoise": 1.0, "model": ["4", 0], "positive": ["6", 0], "negative": ["7", 0], "latent_image": ["5", 0], }}, "4": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": settings["ckpt"]}}, "5": {"class_type": "EmptyLatentImage", "inputs": {"width": width, "height": height, "batch_size": 1}}, "6": {"class_type": "CLIPTextEncode", "inputs": {"text": positive, "clip": ["10", 0]}}, "7": {"class_type": "CLIPTextEncode", "inputs": {"text": negative, "clip": ["10", 0]}}, "8": {"class_type": "VAEDecode", "inputs": {"samples": ["3", 0], "vae": ["4", 2]}}, "9": {"class_type": "SaveImage", "inputs": {"filename_prefix": "smartgen", "images": ["8", 0]}}, "10": {"class_type": "CLIPSetLastLayer", "inputs": {"stop_at_clip_layer": -settings["clip_skip"], "clip": ["4", 1]}}, } class Tools: class Valves(BaseModel): COMFYUI_BASE_URL: str = Field( default="http://comfyui:8188", description="ComfyUI server URL reachable from the open-webui container.", ) TIMEOUT_SECONDS: int = Field( default=240, description="Maximum wait for a single generation to complete.", ) def __init__(self): self.valves = self.Valves() async def generate_image( self, prompt: str, style: Optional[str] = None, negative_prompt: Optional[str] = None, width: int = 1024, height: int = 1024, seed: int = 0, __event_emitter__: Optional[Callable[[dict], Awaitable[None]]] = None, ) -> str: """ Generate an image with the right SDXL checkpoint and creator- recommended sampler/CFG/steps/prompt-format for the request. Pick `style` based on what the user wants: - "photo": photorealistic photographs, portraits, cinematic shots. Uses CyberRealisticXL — natural-language prompts, no quality tags. - "juggernaut": versatile photoreal alternative — sharper, more saturated. Uses Juggernaut-XL — natural-language prompts, no quality tags. - "pony": anime / illustration with Pony's score-tag prompt format. Uses Pony Diffusion V6 XL — score_9..score_4_up chain auto-prepended. Best for anime, cartoon, and stylised art. - "general": amateur-photo aesthetic, catch-all SDXL. Uses TalmendoXL — natural-language prompts, higher CFG. - "furry-nai": anthropomorphic characters, NAI-trained mix. Uses reedFURRYMix — booru quality tags auto-prepended. - "furry-noob": anthropomorphic characters, NoobAI base. Uses IndigoVoid FurryFused — booru quality tags auto-prepended. - "furry-il": anthropomorphic characters, Illustrious base. Uses NovaFurryXL — booru quality + year tags auto-prepended. Default for unspecified furry/anthro requests. If `style` is omitted, the tool auto-detects from `prompt` keywords. Each style has its own creator-recommended sampler, CFG, steps, and CLIP skip — you don't need to override any of these. :param prompt: The image description. Style-appropriate quality tags (Pony score chain, Booru masterpiece chain, etc.) are prepended automatically — don't include them in `prompt`. :param style: One of the keys above. Omit to auto-route. :param negative_prompt: Extra negatives appended to the per-style baseline. Usually unneeded — each style ships with a tuned negative. :param width: Output width in pixels (default 1024 — SDXL native). Use 832 for portraits with height 1216, or 1216 with height 832 for landscapes. :param height: Output height in pixels (default 1024). :param seed: Specific seed, or 0 to randomize. :return: Markdown embedding the generated image. """ chosen = style or _route_style(prompt) settings = STYLES.get(chosen) if not settings: return ( f"Unknown style '{chosen}'. " f"Available: {', '.join(STYLES.keys())}" ) async def emit(msg: str, done: bool = False): if __event_emitter__: await __event_emitter__({ "type": "status", "data": {"description": msg, "done": done}, }) await emit(f"Routing to {chosen} ({settings['ckpt']})") positive = f"{settings['prefix']}{prompt}" negative = settings["negative"] if negative_prompt: negative = f"{negative}, {negative_prompt}" workflow = _build_workflow( positive=positive, negative=negative, settings=settings, width=width, height=height, seed=seed, ) client_id = str(uuid.uuid4()) base = self.valves.COMFYUI_BASE_URL.rstrip("/") async with aiohttp.ClientSession() as session: async with session.post( f"{base}/prompt", json={"prompt": workflow, "client_id": client_id}, ) as resp: if resp.status != 200: return f"ComfyUI rejected the prompt: {resp.status} {await resp.text()}" submit = await resp.json() prompt_id = submit.get("prompt_id") if not prompt_id: return f"ComfyUI didn't return a prompt_id: {submit}" await emit( f"Sampling — {settings['sampler']}/{settings['scheduler']}, " f"CFG {settings['cfg']}, {settings['steps']} steps" ) deadline = time.time() + self.valves.TIMEOUT_SECONDS output_images = [] while time.time() < deadline: await asyncio.sleep(1.5) async with session.get(f"{base}/history/{prompt_id}") as resp: if resp.status != 200: continue history = await resp.json() if prompt_id in history: for node_out in history[prompt_id].get("outputs", {}).values(): output_images.extend(node_out.get("images", [])) if output_images: break if not output_images: return f"Timed out after {self.valves.TIMEOUT_SECONDS}s waiting for image." await emit("Fetching result…") img = output_images[0] params = { "filename": img["filename"], "subfolder": img.get("subfolder", ""), "type": img.get("type", "output"), } async with session.get(f"{base}/view", params=params) as resp: if resp.status != 200: return f"Failed to fetch image: {resp.status}" raw = await resp.read() b64 = base64.b64encode(raw).decode("ascii") await emit(f"Done — {chosen}", done=True) return f"![{chosen}](data:image/png;base64,{b64})"