- 新增 comfyui/ui_convert.py:离线确定性转换(reroute/bypass/mute 透传、 子图展开为 packed:inner 限定 id、Set/Get 变量解析、控件三种映射来源, 无法可靠映射的文件进入需人工确认,绝不静默猜测) - 新增 convert / import-batch / import-scan 端点:dry-run 逐文件 ok/warn/fail 报告 + 建议元数据;导入端冲突跳过、重复执行幂等 - workflows 表加列 category/model_family/media_io/tags/favorite/ last_used_at/source/source_path(纯加列迁移,旧行为零影响) - GET /api/workflows?view=meta 轻量列表不含全量 JSON;旧接口行为不变 - 元数据建议:分类/模型家族(主加载器加权+目录回退,规避 qwen_image_vae 误标)/输入输出媒介/MarkdownNote 描述素材 - enqueue 时记录 last_used_at;PATCH 支持 favorite/tags 等字段 - 测试 23 项新增(转换语义单测+146 文件语料+对拍+端点/迁移/幂等) Co-authored-by: multica-agent <github@multica.ai>
161 lines
6.5 KiB
Python
161 lines
6.5 KiB
Python
"""Suggested workflow metadata for imports (HEL-681 / S2 data layer).
|
|
|
|
Everything here derives from two honest sources: the repo directory the file
|
|
came from (category) and the converted API graph itself (usage, media in/out,
|
|
model family, note text). Per HEL-675's inventory, clean display names and
|
|
tags stay human work — the suggestion is a starting point, never a guess that
|
|
hides uncertainty.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import Any
|
|
|
|
from calliope.comfyui.parser import (
|
|
classify_usage,
|
|
parse_dynamic_inputs,
|
|
parse_dynamic_outputs,
|
|
)
|
|
|
|
# Repo top-level directory → (category key, display label). Directory names
|
|
# are mapping sources only — they never surface to users verbatim.
|
|
CATEGORY_DIRS: dict[str, tuple[str, str]] = {
|
|
"0-基础处理:图像合并-音频分离-视频截取合并-图像视频放大": ("basic", "基础处理"),
|
|
"1-语音生成": ("audio_tts", "语音生成"),
|
|
"2-音乐生成": ("music", "音乐生成"),
|
|
"3-图像生成": ("image", "图像生成"),
|
|
"4-视频生成": ("video", "视频生成"),
|
|
"5-数字人": ("digital_human", "数字人"),
|
|
"6-动作迁移": ("motion_transfer", "动作迁移"),
|
|
}
|
|
|
|
# Evidence patterns → model family. Shared VAE/text-encoder files must NOT
|
|
# count (HEL-675: Anima/Z-Image graphs reference qwen_image_vae and get
|
|
# mislabeled Qwen — `qwen.?image(?!_vae)` is deliberate).
|
|
_FAMILY_PATTERNS: tuple[tuple[str, str], ...] = (
|
|
(r"minimax[_\-]?(music|_audio)|MiniMaxMusic", "MiniMax-Music"),
|
|
(r"minimax.?h3|minimax_h3|h3[-_]realism|fl2v[aa]?", "MiniMax-H3"),
|
|
(r"ltx[-_.]?2", "LTX-2.5"),
|
|
(r"ltx", "LTX"),
|
|
(r"krea", "Krea2"),
|
|
(r"qwen.?image(?!_vae)|qwen_image_2|qwen2vl|qwen3vl|Qwen3?[-.]?ASR|qwen3?[-_.]?tts", "Qwen"),
|
|
(r"z[._-]?image", "Z-Image"),
|
|
(r"flux", "Flux"),
|
|
(r"\bwan", "Wan"),
|
|
(r"ace[_-]?step", "ACE-Step"),
|
|
(r"index[_-]?tts", "IndexTTS2"),
|
|
(r"anima[-_.]?(turbo|klein)", "Anima"),
|
|
(r"sdpose", "SDPose"),
|
|
(r"gemma", "Gemma"),
|
|
(r"instantmesh|instant_mesh", "InstantMesh"),
|
|
(r"hidream", "HiDream"),
|
|
)
|
|
_FAMILY_RE = [(re.compile(pat, re.IGNORECASE), fam) for pat, fam in _FAMILY_PATTERNS]
|
|
|
|
# Main-model loaders dominate the vote: a UNET/checkpoint reference says far
|
|
# more about the workflow's family than a shared VAE or text encoder does.
|
|
_PRIMARY_LOADER_RE = re.compile(r"UNETLoader|CheckpointLoader|unet|diffusion_model", re.IGNORECASE)
|
|
|
|
# Directory names that carry family evidence the files' own widget values
|
|
# lack (HEL-675: 14 krea2 files reference no "krea" string at all).
|
|
_FAMILY_DIRS = (
|
|
(re.compile(r"krea2", re.IGNORECASE), "Krea2"),
|
|
(re.compile(r"Minimax-h3|uncensored", re.IGNORECASE), "MiniMax-H3"),
|
|
(re.compile(r"Z-Image", re.IGNORECASE), "Z-Image"),
|
|
(re.compile(r"Qwen-Image", re.IGNORECASE), "Qwen"),
|
|
(re.compile(r"^Anima$", re.IGNORECASE), "Anima"),
|
|
(re.compile(r"Flux", re.IGNORECASE), "Flux"),
|
|
)
|
|
|
|
_NOTE_TYPES = frozenset({"Note", "MarkdownNote"})
|
|
|
|
|
|
def category_from_source_path(source_path: str | None) -> tuple[str | None, str | None]:
|
|
"""(category key, label) derived from the file's repo-relative path."""
|
|
if not source_path:
|
|
return None, None
|
|
head = source_path.replace("\\", "/").split("/", 1)[0]
|
|
return CATEGORY_DIRS.get(head, (None, None))
|
|
|
|
|
|
def model_family_from(workflow: dict[str, Any], source_path: str | None = None) -> str | None:
|
|
"""The graph's dominant model family: main-model loader references weigh
|
|
triple, other node evidence single, and the repo sub-directory adds a
|
|
strong vote for families the widget values don't spell out."""
|
|
votes: dict[str, int] = {}
|
|
for node in workflow.values():
|
|
if not isinstance(node, dict):
|
|
continue
|
|
class_type = str(node.get("class_type") or "")
|
|
blob = " ".join(
|
|
[class_type]
|
|
+ [str(v) for v in (node.get("inputs") or {}).values() if isinstance(v, str)]
|
|
)
|
|
weight = 3 if _PRIMARY_LOADER_RE.search(blob) else 1
|
|
for pattern, family in _FAMILY_RE:
|
|
if pattern.search(blob):
|
|
votes[family] = votes.get(family, 0) + weight
|
|
break # one vote per node: strongest family that node shows
|
|
if source_path:
|
|
parts = source_path.replace("\\", "/").split("/")
|
|
for part in parts[1:2]:
|
|
for pattern, family in _FAMILY_DIRS:
|
|
if pattern.search(part):
|
|
votes[family] = votes.get(family, 0) + 2
|
|
if not votes:
|
|
return None
|
|
return max(votes.items(), key=lambda kv: kv[1])[0]
|
|
|
|
|
|
def note_description(ui_workflow: dict[str, Any], limit: int = 160) -> str | None:
|
|
"""First non-trivial author note as description material (never final copy)."""
|
|
for node in ui_workflow.get("nodes") or []:
|
|
if not isinstance(node, dict) or str(node.get("type")) not in _NOTE_TYPES:
|
|
continue
|
|
values = node.get("widgets_values")
|
|
text = values[0] if isinstance(values, list) and values else None
|
|
if isinstance(values, dict):
|
|
text = values.get("text") or next(iter(values.values()), None)
|
|
if isinstance(text, str):
|
|
text = text.strip()
|
|
if len(text) >= 8:
|
|
return text[:limit].strip()
|
|
return None
|
|
|
|
|
|
def suggested_metadata(
|
|
converted: dict[str, Any],
|
|
source_path: str | None = None,
|
|
ui_workflow: dict[str, Any] | None = None,
|
|
) -> dict[str, Any]:
|
|
"""The import suggestion bundle for one converted workflow."""
|
|
judgment = classify_usage(converted)
|
|
media_kinds = ("image", "image_list", "video", "video_list", "audio", "audio_list")
|
|
media_in = sorted(
|
|
{
|
|
entry["kind"].replace("_list", "")
|
|
for entry in parse_dynamic_inputs(converted)
|
|
if entry.get("kind") in media_kinds
|
|
}
|
|
)
|
|
media_out = sorted(
|
|
{
|
|
str(entry.get("kind"))
|
|
for entry in parse_dynamic_outputs(converted)
|
|
if entry.get("kind") in ("image", "video", "audio")
|
|
}
|
|
)
|
|
category, category_label = category_from_source_path(source_path)
|
|
return {
|
|
"kind": "video" if "video" in media_out and "image" not in media_out else "image",
|
|
"usage": judgment["usage"],
|
|
"usage_ambiguous": judgment["ambiguous"],
|
|
"category": category,
|
|
"category_label": category_label,
|
|
"model_family": model_family_from(converted, source_path),
|
|
"media_in": media_in,
|
|
"media_out": media_out,
|
|
"description": note_description(ui_workflow or {}),
|
|
}
|