Spaces:
Running on Zero
Running on Zero
Share the existing Base and Next4 sampling graph with depth control
Browse filesFiles changed (4)
- README.md+1-1
- app.py+15-21
- composition.py-46
- settings.py+31-5
29 unmodified lines3031323334353629 unmodified lines### 构图参考本地工具选择「构图参考」并载入参考图。使用现有 MOSS-Image Base 和 Next4(强度 `1.0`),再加载构图控制适配器。MoGe 3 输出的深度图或轮廓图约束空间结构,从噪声开始按提示词生成。默认使用深度、控制强度 `1.0`、控制结束比例 `0.60`、25 步、CFG 3。改变人物轮廓时可缩短控制时间,或切换到「轮廓」。输出比例与参考图不同时会居中裁剪。可用「按参考图比例」对齐取景。Kaggle 匿名调用 `/generate_from_composition`,每张图提交一个私有 Notebook 版本。PNG 保存有效采样参数、参考图校验值和控制参数。本地工具可从 PNG 恢复设置并复用本机的参考图副本。29 unmodified lines3031323334353629 unmodified lines### 构图参考本地工具选择「构图参考」并载入参考图。三个模式共享当前的 Base 模型、LoRA、采样、跳层与提示词设置,默认采用现有 Next4 工作流。构图参考另外加载控制适配器,由 MoGe 3 输出深度图或轮廓图来约束空间结构。默认参考方式为深度,控制强度为 `1.0`,控制结束比例为 `0.60`。输出比例与参考图不同时会居中裁剪。可用「按参考图比例」对齐取景。Kaggle 匿名调用 `/generate_from_composition`,每张图提交一个私有 Notebook 版本。PNG 保存有效采样参数、参考图校验值和控制参数。本地工具可从 PNG 恢复设置并复用本机的参考图副本。
11 unmodified lines1213141516171819202122232483 unmodified lines10810911011111211311411511611711811927 unmodified lines14714814915015115215315415515615715866 unmodified lines22522622722822923023123223323423523623723823924024155 unmodified lines29729829930030130230330430530630730830931031111 unmodified linesimport spacesimport gradio as grfrom composition import (CONTROL_ADAPTER, CONTROL_CFG, CONTROL_DEFAULTS, CONTROL_STEPS,CONTROL_TYPES, GEOMETRY_MODEL, GEOMETRY_REV, composition_graph, control_settings)from huggingface_hub import hf_hub_downloadfrom png_metadata import save_replay_metadatafrom reference_image import DEFAULT_DENOISE, decode_reference, normalize_denoise, reference_infofrom settings import (COMPOSITION_FIXED, NEGATIVE_MODES, NO_LORA, SAMPLERS, SCHEDULERS,apply_reference, apply_settings, defaults, normalize)ROOT = Path(__file__).resolve().parent83 unmodified linesawait nodes.init_public_apis()modules = ["nodes_flux", "nodes_model_advanced", "nodes_slg"]if graph["8"]["class_type"] == "SamplerCustom":modules.extend(("nodes_moss_image", "nodes_moge", "nodes_eps", "nodes_custom_sampler"))for module in modules:path = COMFY / "comfy_extras" / f"{module}.py"await nodes.load_custom_node(str(path), module_parent="comfy_extras")missing = {node["class_type"] for node in graph.values()} - nodes.NODE_CLASS_MAPPINGS.keys()if missing:raise RuntimeError(f"Missing ComfyUI nodes: {sorted(missing)}")27 unmodified lines@spaces.GPU(duration=composition_gpu_seconds)def run_composition_workflow(prompt, values, filename):if values["negative_mode"] == "自定义":negative = values["negative_text"]else:negative_id = GRAPH["8"]["inputs"]["negative"][0]negative = GRAPH[negative_id]["inputs"]["text"]graph = composition_graph(prompt, negative, values, filename, BASE_LOADER, adapters[values["lora"]], ENCODER, VAE)return execute_workflow(graph, prompt, values)66 unmodified linesyield path.relative_to(INPUT).as_posix()def generate_from_composition(prompt, width, height, seed, steps, cfg, negative_mode, negative_text,control_type, control_strength, control_end, reference_image):values = locals().copy()if not values.pop("prompt").strip():raise gr.Error("请输入正向提示词")values.pop("reference_image")if COMPOSITION_FIXED["lora"] not in adapters:raise gr.Error("Space 暂时无法加载构图参考所用的 Next4 LoRA,请检查模型挂载。")try:values.update(control_settings(values))values = normalize({**DEFAULT, **values, **COMPOSITION_FIXED}, adapters)except ValueError as error:raise gr.Error(str(error)) from Nonewith reference_input(reference_image, values, "composition") as filename:55 unmodified linesreference_image = gr.Textbox(visible=False)gr.Button(visible=False).click(generate_from_image, [*settings_inputs, denoise, reference_image],[image, used_seed], api_name="generate_from_image", concurrency_id="generation")composition_steps = gr.Slider(1, 60, value=CONTROL_STEPS, step=1, visible=False)composition_cfg = gr.Slider(0, 30, value=CONTROL_CFG, step=0.1, visible=False)control_type = gr.Dropdown(list(CONTROL_TYPES), value=CONTROL_DEFAULTS["control_type"], visible=False)control_strength = gr.Slider(0.05, 2, value=CONTROL_DEFAULTS["control_strength"], step=0.05, visible=False)control_end = gr.Slider(0.05, 1, value=CONTROL_DEFAULTS["control_end"], step=0.05, visible=False)gr.Button(visible=False).click(generate_from_composition,[prompt, width, height, seed, composition_steps, composition_cfg, neg_mode, neg_text,control_type, control_strength, control_end, reference_image],[image, used_seed], api_name="generate_from_composition", concurrency_id="generation",)11 unmodified lines1213141516171819202122232483 unmodified lines10810911011111211311411511611711811912027 unmodified lines14814915015115215315415566 unmodified lines22222322422522622722822923023123223323423523623723855 unmodified lines29429529629729829930030130230330430511 unmodified linesimport spacesimport gradio as grfrom composition import (CONTROL_ADAPTER, CONTROL_DEFAULTS, CONTROL_TYPES, GEOMETRY_MODEL, GEOMETRY_REV,control_settings)from huggingface_hub import hf_hub_downloadfrom png_metadata import save_replay_metadatafrom reference_image import DEFAULT_DENOISE, decode_reference, normalize_denoise, reference_infofrom settings import (NEGATIVE_MODES, NO_LORA, SAMPLERS, SCHEDULERS, apply_composition, apply_reference,apply_settings, defaults, normalize)ROOT = Path(__file__).resolve().parent83 unmodified linesawait nodes.init_public_apis()modules = ["nodes_flux", "nodes_model_advanced", "nodes_slg"]node_types = {node["class_type"] for node in graph.values()}if "MOSSImageControl" in node_types:modules.extend(("nodes_moss_image", "nodes_moge"))for module in modules:path = COMFY / "comfy_extras" / f"{module}.py"await nodes.load_custom_node(str(path), module_parent="comfy_extras")missing = node_types - nodes.NODE_CLASS_MAPPINGS.keys()if missing:raise RuntimeError(f"Missing ComfyUI nodes: {sorted(missing)}")27 unmodified lines@spaces.GPU(duration=composition_gpu_seconds)def run_composition_workflow(prompt, values, filename):graph = apply_settings(GRAPH, prompt, values, adapters)apply_composition(graph, values, filename, CONTROL_ADAPTER, GEOMETRY_MODEL)return execute_workflow(graph, prompt, values)66 unmodified linesyield path.relative_to(INPUT).as_posix()def generate_from_composition(prompt, lora, strength, width, height, seed, steps, cfg,sampler, scheduler, shift, rescale, single_layers, double_layers,slg_start, slg_end, negative_mode, negative_text,control_type, control_strength, control_end, reference_image):values = locals().copy()if not values.pop("prompt").strip():raise gr.Error("请输入正向提示词")values.pop("reference_image")try:values.update(control_settings(values))values = normalize(values, adapters)except ValueError as error:raise gr.Error(str(error)) from Nonewith reference_input(reference_image, values, "composition") as filename:55 unmodified linesreference_image = gr.Textbox(visible=False)gr.Button(visible=False).click(generate_from_image, [*settings_inputs, denoise, reference_image],[image, used_seed], api_name="generate_from_image", concurrency_id="generation")control_type = gr.Dropdown(list(CONTROL_TYPES), value=CONTROL_DEFAULTS["control_type"], visible=False)control_strength = gr.Slider(0.05, 2, value=CONTROL_DEFAULTS["control_strength"], step=0.05, visible=False)control_end = gr.Slider(0.05, 1, value=CONTROL_DEFAULTS["control_end"], step=0.05, visible=False)gr.Button(visible=False).click(generate_from_composition,[*settings_inputs, control_type, control_strength, control_end, reference_image],[image, used_seed], api_name="generate_from_composition", concurrency_id="generation",)
5 unmodified lines67891011121312 unmodified lines26272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071725 unmodified linesGEOMETRY_REV = "1484985258ba7ef45c314144fcf0e90924ffbf44"CONTROL_TYPES = {"depth": "深度", "mask": "轮廓"}CONTROL_DEFAULTS = {"control_type": "depth", "control_strength": 1.0, "control_end": 0.6}CONTROL_STEPS = 25CONTROL_CFG = 3def control_settings(settings):12 unmodified linesraise ValueError(f"{label}须在 {minimum} 至 {maximum} 之间。")values[key] = valuereturn valuesdef composition_graph(prompt, negative, values, filename, base_loader, style_lora, encoder, vae):size = {"width": values["width"], "height": values["height"]}nodes = {"1": ("UNETLoader", dict(base_loader)),"2": ("CLIPLoader", {"clip_name": encoder, "type": "moss_image"}),"3": ("ModelSamplingAuraFlow", {"model": ["16", 0], "shift": 5, "sampling": "flow"}),"4": ("CLIPTextEncode", {"clip": ["2", 0], "text": prompt}),"5": ("CLIPTextEncode", {"clip": ["2", 0], "text": negative}),"7": ("EmptyFlux2LatentImage", {**size, "batch_size": 1}),"8": ("SamplerCustom", {"model": ["12", 0], "positive": ["19", 0], "negative": ["19", 1],"latent_image": ["7", 0], "sampler": ["14", 0], "sigmas": ["13", 0],"noise_seed": int(values["seed"]), "add_noise": True, "cfg": values["cfg"],}),"9": ("VAELoader", {"vae_name": vae}),"10": ("VAEDecode", {"samples": ["8", 0], "vae": ["9", 0]}),"11": ("SaveImage", {"images": ["10", 0], "filename_prefix": "moss-composition"}),"12": ("SkipLayerGuidanceDiTSimple", {"model": ["3", 0], "single_layers": "24", "double_layers": "","start_percent": 0, "end_percent": 1,}),"13": ("BasicScheduler", {"model": ["12", 0], "scheduler": "simple", "steps": values["steps"], "denoise": 1,}),"14": ("SamplerER_SDE", {"solver_type": "ER-SDE", "max_stage": 3, "eta": 1, "s_noise": 1.05}),"15": ("LoraLoaderModelOnly", {"model": ["23", 0], "lora_name": CONTROL_ADAPTER, "strength_model": 1}),"16": ("TemporalScoreRescaling", {"model": ["15", 0], "tsr_k": 0.95, "tsr_sigma": 1}),"17": ("LoadImage", {"image": filename}),"18": ("ImageScale", {"image": ["17", 0], "upscale_method": "lanczos", "crop": "center", **size}),"19": ("MOSSImageControl", {"positive": ["4", 0], "negative": ["5", 0], "vae": ["9", 0], "depth_map": ["22", 0],"strength": values["control_strength"], "start_percent": 0, "end_percent": values["control_end"],}),"20": ("LoadMoGeModel", {"model_name": GEOMETRY_MODEL}),"21": ("MoGeInference", {"moge_model": ["20", 0], "image": ["18", 0], "resolution_level": 9, "fov_x_degrees": 0,"batch_size": 1, "force_projection": True, "apply_mask": True, "refine_steps": 3,}),"22": ("MoGeRender", {"moge_geometry": ["21", 0], "output": values["control_type"]}),"23": ("LoraLoaderModelOnly", {"model": ["1", 0], "lora_name": style_lora, "strength_model": values["strength"]}),}return {key: {"class_type": name, "inputs": inputs} for key, (name, inputs) in nodes.items()}5 unmodified lines6789101112 unmodified lines2425265 unmodified linesGEOMETRY_REV = "1484985258ba7ef45c314144fcf0e90924ffbf44"CONTROL_TYPES = {"depth": "深度", "mask": "轮廓"}CONTROL_DEFAULTS = {"control_type": "depth", "control_strength": 1.0, "control_end": 0.6}def control_settings(settings):12 unmodified linesraise ValueError(f"{label}须在 {minimum} 至 {maximum} 之间。")values[key] = valuereturn values
7 unmodified lines89101112131415161768 unmodified lines868788899091929394959697982 unmodified lines1011021037 unmodified linesSAMPLERS = ["er_sde", "euler", "euler_ancestral", "dpmpp_2m", "dpmpp_2m_sde", "heun"]SCHEDULERS = ["sgm_uniform", "normal", "simple", "karras", "exponential", "beta"]NEGATIVE_MODES = ["沿用工作流", "自定义"]COMPOSITION_FIXED = {"lora": "Next4", "strength": 1, "sampler": "er_sde", "scheduler": "simple", "shift": 5,"rescale": 0, "single_layers": "24", "double_layers": "", "slg_start": 0, "slg_end": 1,}def defaults(graph, adapters):68 unmodified linesreturn graphdef apply_reference(graph, values, filename):size = graph["7"]["inputs"]graph["17"] = {"class_type": "LoadImage", "inputs": {"image": filename}}graph["18"] = {"class_type": "ImageScale", "inputs": {"image": ["17", 0], "upscale_method": "lanczos", "crop": "center","width": size["width"], "height": size["height"],}}graph["7"] = {"class_type": "VAEEncode", "inputs": {"pixels": ["18", 0], "vae": ["9", 0]}}sample = graph["8"]["inputs"]sample["seed"] = sample.pop("noise_seed")2 unmodified linessample["denoise"] = values["denoise"]graph["8"]["class_type"] = "KSampler"return graph7 unmodified lines891011121368 unmodified lines82838485868788899091929394959697982 unmodified lines1011021031041051061071081091101111121131141151161171181191201211221231241251261271281297 unmodified linesSAMPLERS = ["er_sde", "euler", "euler_ancestral", "dpmpp_2m", "dpmpp_2m_sde", "heun"]SCHEDULERS = ["sgm_uniform", "normal", "simple", "karras", "exponential", "beta"]NEGATIVE_MODES = ["沿用工作流", "自定义"]def defaults(graph, adapters):68 unmodified linesreturn graphdef add_reference_image(graph, filename):size = graph["7"]["inputs"]graph["17"] = {"class_type": "LoadImage", "inputs": {"image": filename}}graph["18"] = {"class_type": "ImageScale", "inputs": {"image": ["17", 0], "upscale_method": "lanczos", "crop": "center","width": size["width"], "height": size["height"],}}def apply_reference(graph, values, filename):add_reference_image(graph, filename)graph["7"] = {"class_type": "VAEEncode", "inputs": {"pixels": ["18", 0], "vae": ["9", 0]}}sample = graph["8"]["inputs"]sample["seed"] = sample.pop("noise_seed")2 unmodified linessample["denoise"] = values["denoise"]graph["8"]["class_type"] = "KSampler"return graphdef apply_composition(graph, values, filename, control_adapter, geometry_model):add_reference_image(graph, filename)sampling_model = graph["3"]["inputs"]graph["23"] = {"class_type": "LoraLoaderModelOnly", "inputs": {"model": sampling_model["model"], "lora_name": control_adapter, "strength_model": 1,}}sampling_model["model"] = ["23", 0]sample = graph["8"]["inputs"]graph["19"] = {"class_type": "MOSSImageControl", "inputs": {"positive": sample["positive"], "negative": sample["negative"], "vae": ["9", 0],"depth_map": ["22", 0], "strength": values["control_strength"],"start_percent": 0, "end_percent": values["control_end"],}}sample.update(positive=["19", 0], negative=["19", 1])graph["20"] = {"class_type": "LoadMoGeModel", "inputs": {"model_name": geometry_model}}graph["21"] = {"class_type": "MoGeInference", "inputs": {"moge_model": ["20", 0], "image": ["18", 0], "resolution_level": 9, "fov_x_degrees": 0,"batch_size": 1, "force_projection": True, "apply_mask": True, "refine_steps": 3,}}graph["22"] = {"class_type": "MoGeRender", "inputs": {"moge_geometry": ["21", 0], "output": values["control_type"],}}return graph
README.md+1-1
app.py+15-21
composition.py-46
settings.py+31-5