anima-collection-mcp / tests /metadata_ui_demo.py
yamiuchi's picture
Name PNG downloads and scroll to generation output on click
ecb572c verified
Raw History Blame Contribute Delete
3.7 kB
"""CPU fixture exercising the real UI and public API wrappers, no model/GPU."""
import ast
from pathlib import Path
import sys
import gradio as gr
import numpy as np
from PIL import Image as PILImage
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT))
from generation_metadata import generation_record,image_with_metadata,image_download_path
ILLUSTRIOUS_EMBEDDING_FILES=[("negativeXL_D.safetensors","","SDXL")]
def serialized_generate(*args, **kwargs):
model,prompt,negative,width,height,seed,steps,cfg,sampler,scheduler=args[:10]
family="anima" if model.startswith("anima") else "illustrious"
loras=[]
for index in range(4):
enabled,name,strength=args[10+index*3:13+index*3]
if enabled and name!="---":
loras.append(dict(slot=index+1,name=name,strength_model=strength,strength_clip=0.0))
embeddings=args[22] or []
actual_negative=(", ".join("embedding:"+n for n in embeddings)+", " if embeddings else "")+negative
record=generation_record(family=family,model=model,input_prompt=prompt,input_negative_prompt=negative,
prompt=prompt.replace("<replace:red hair>","red hair"),negative_prompt=actual_negative,
width=64,height=64,seed=9223372036854775806,steps=steps,cfg=cfg,sampler=sampler,scheduler=scheduler,
vae="qwen_image_vae.safetensors" if family=="anima" else "Checkpoint内蔵VAE",
vae_source="external" if family=="anima" else "checkpoint",loras=loras,embeddings=embeddings,
comfyui_commit="cpu-fixture",int8=family=="anima")
pixels=np.zeros((64,64,3),dtype=np.uint8); pixels[:,:,1]=128
return image_with_metadata(pixels,record),"CPU metadata fixture",record["prompt"],record["negative_prompt"]
source=ast.parse((ROOT/"app.py").read_text(encoding="utf-8"))
wrappers=[n for n in source.body if isinstance(n,ast.FunctionDef) and n.name in ("generate_image_ui","generate_image")]
exec(compile(ast.Module(body=wrappers,type_ignores=[]),"app.py","exec"))
with gr.Blocks(analytics_enabled=False) as demo:
for family in ("anima","illustrious"):
with gr.Tab(family):
common=[family+"_INT8.safetensors" if family=="anima" else "wai.safetensors",
"<replace:red hair>, 日本語\nblue eyes","bad quality",64,64,-1,
8 if family=="anima" else 30,1.0 if family=="anima" else 5.0,
"er_sde" if family=="anima" else "euler_ancestral","simple" if family=="anima" else "normal",
True,"details.safetensors",.7,False,"---",1,False,"---",1,False,"---",1]
output=gr.Image(format="png",streaming=True,label=family+" PNG")
info=gr.Markdown()
# Bind family via default, avoiding a late-bound loop value.
def make_ui(values,family):
def run():
return generate_image_ui(values[0],None,*values[1:],embedding_enable_1=family=="illustrious")
return run
gr.Button("Generate fixture "+family).click(make_ui(common,family),None,[output,info],api_name="ui_"+family)
api_inputs=[(gr.Checkbox(value=value,visible=False) if isinstance(value,bool) else
gr.Textbox(value=value,visible=False) if isinstance(value,str) else
gr.Number(value=value,visible=False)) for value in common]
# Same external 22 generation inputs + default Progress as production.
api_image=gr.Image(format="png",visible=False)
api_info=gr.Markdown(visible=False)
gr.Button("API fixture",visible=False).click(generate_image,api_inputs,[api_image,api_info],api_name="generate_image")
if __name__=="__main__":
demo.launch(server_name="127.0.0.1",server_port=7866)