Add songforge as independent repo with path layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
swl
2026-10-09 19:18:33 +08:00
co-authored by Cursor
commit f39deb1848
52 changed files with 10289 additions and 0 deletions
View File
+173
View File
@@ -0,0 +1,173 @@
# -*- coding: utf-8 -*-
"""全局索引 catalog.jsonl:只存元数据,可从 runs 重建。"""
import json
import os
import re
from . import paths as pmod
def catalog_path(cfg):
p = cfg["paths"].get("catalog_file") or "songforge/index/catalog.jsonl"
return pmod.under_workflows(p)
def day_dir(runs_root, day=None):
"""runs_root/YYYY-MM-DD/"""
import datetime
if not day:
day = datetime.date.today().isoformat()
return os.path.join(runs_root, day)
def allocate_run_dir(runs_root, run_id, day=None):
"""占位 data/runs/YYYY-MM-DD/<run_id>/,返回绝对路径。"""
d = day_dir(runs_root, day)
pmod.ensure_dir(d)
path = os.path.join(d, run_id)
os.makedirs(path, exist_ok=False)
return path
def find_run_dir(runs_root, run_id):
"""在按日目录、_migrated、扁平根下查找 run 目录。"""
rid = (run_id or "").strip().rstrip("/\\")
if not rid:
return None
if os.path.isdir(rid) and os.path.isfile(os.path.join(rid, "manifest.json")):
return os.path.abspath(rid)
base = os.path.basename(rid.replace("/", os.sep))
if not os.path.isdir(runs_root):
return None
# 直接子目录(兼容旧扁平)
cand = os.path.join(runs_root, base)
if os.path.isfile(os.path.join(cand, "manifest.json")):
return cand
# 按日
for day in os.listdir(runs_root):
day_path = os.path.join(runs_root, day)
if not os.path.isdir(day_path):
continue
cand = os.path.join(day_path, base)
if os.path.isfile(os.path.join(cand, "manifest.json")):
return cand
# _migrated 下也可能套一层
if day in ("_migrated",):
for sub in os.listdir(day_path):
cand = os.path.join(day_path, sub)
if os.path.basename(cand) == base and \
os.path.isfile(os.path.join(cand, "manifest.json")):
return cand
# 全树兜底
for root, dirs, files in os.walk(runs_root):
if os.path.basename(root) == base and "manifest.json" in files:
return root
return None
def _entry_from_manifest(rundir, man):
results = man.get("results") or []
seeds = [r.get("seed") for r in results if r.get("seed") is not None]
files = []
for r in results:
for fn in r.get("files") or []:
files.append(fn)
day = None
m = re.search(r"(\d{4}-\d{2}-\d{2})", rundir.replace("\\", "/"))
if m:
day = m.group(1)
elif man.get("created"):
day = str(man["created"])[:10]
return {
"run_id": man.get("run_id") or os.path.basename(rundir),
"date": day,
"title": man.get("title"),
"style": man.get("style"),
"bpm": man.get("bpm"),
"key": man.get("key"),
"duration": man.get("duration"),
"seeds": seeds,
"qc_verdict": man.get("qc_verdict"),
"audio_files": files,
"path": rundir.replace("\\", "/"),
}
def append_entry(cfg, entry):
path = catalog_path(cfg)
pmod.ensure_dir(os.path.dirname(path))
with open(path, "a", encoding="utf-8") as f:
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
return path
def update_qc_verdict(cfg, run_id, verdict):
"""重写 catalog:更新匹配 run_id 的 qc_verdict。"""
path = catalog_path(cfg)
if not os.path.isfile(path):
return
rows = []
with open(path, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
row = json.loads(line)
except ValueError:
continue
if row.get("run_id") == run_id:
row["qc_verdict"] = verdict
rows.append(row)
with open(path, "w", encoding="utf-8") as f:
for row in rows:
f.write(json.dumps(row, ensure_ascii=False) + "\n")
def rebuild(cfg):
"""扫描 runs_dir,重写 catalog.jsonl。"""
runs_root = pmod.under_workflows(cfg["paths"]["runs_dir"])
entries = []
if os.path.isdir(runs_root):
for root, dirs, files in os.walk(runs_root):
if "manifest.json" not in files:
continue
mp = os.path.join(root, "manifest.json")
try:
with open(mp, encoding="utf-8") as f:
man = json.load(f)
except (ValueError, OSError):
continue
entries.append(_entry_from_manifest(root, man))
entries.sort(key=lambda e: (e.get("date") or "", e.get("run_id") or ""))
path = catalog_path(cfg)
pmod.ensure_dir(os.path.dirname(path))
with open(path, "w", encoding="utf-8") as f:
for e in entries:
f.write(json.dumps(e, ensure_ascii=False) + "\n")
return path, len(entries)
def list_entries(cfg, day=None, limit=50):
path = catalog_path(cfg)
if not os.path.isfile(path):
return []
out = []
with open(path, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
row = json.loads(line)
except ValueError:
continue
if day and row.get("date") != day:
continue
out.append(row)
out.reverse()
return out[:limit]
def entry_from_gen(rundir, manifest):
return _entry_from_manifest(rundir, manifest)
+120
View File
@@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
"""ComfyUI 执行引擎:提交、轮询、重试、结果回收。只用标准库。"""
import json
import time
import urllib.error
import urllib.request
from .errors import JobFailed, ServerError
class Engine(object):
def __init__(self, server, timeout=3600, poll_interval=10,
submit_retries=3, logger=None):
self.server = server.rstrip("/")
self.timeout = timeout
self.poll_interval = poll_interval
self.submit_retries = submit_retries
self.log = logger
def _log(self, level, msg, **kw):
if self.log:
self.log.log(level, msg, **kw)
def _get(self, path, timeout=15):
url = self.server + path
try:
with urllib.request.urlopen(url, timeout=timeout) as r:
return json.loads(r.read().decode("utf-8"))
except urllib.error.HTTPError as e:
raise ServerError("GET %s 返回 %s" % (path, e.code), e.read().decode()[:400])
except Exception as e:
raise ServerError("GET %s 失败: %s" % (path, e))
def _post(self, path, payload, timeout=60):
url = self.server + path
req = urllib.request.Request(
url, data=payload.encode("utf-8"),
headers={"Content-Type": "application/json"})
try:
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.loads(r.read().decode("utf-8"))
except urllib.error.HTTPError as e:
raise ServerError("POST %s 返回 %s" % (path, e.code),
e.read().decode()[:600])
except Exception as e:
raise ServerError("POST %s 失败: %s" % (path, e))
def health(self):
"""返回 (ok, info)。不抛异常,供 doctor 使用。"""
try:
stats = self._get("/system_stats")
except ServerError as e:
return False, str(e)
dev = (stats.get("devices") or [{}])[0]
info = {
"device": dev.get("name", "?"),
"vram_total_gb": round(dev.get("vram_total", 0) / 1024 ** 3, 1),
"comfyui_version": stats.get("system", {}).get("comfyui_version", "?"),
}
q = self._get("/queue")
info["queue_running"] = len(q.get("queue_running") or [])
info["queue_pending"] = len(q.get("queue_pending") or [])
return True, info
def queue(self):
return self._get("/queue")
def submit(self, prompt_json):
"""prompt_json 可以是 dict(API prompt)或 JSON 字符串。
最终 POST /prompt 的 body 是 {"prompt": <prompt对象>}。"""
if isinstance(prompt_json, str):
prompt_json = json.loads(prompt_json)
payload = self._post("/prompt", json.dumps({"prompt": prompt_json}, ensure_ascii=False))
pid = payload.get("prompt_id")
if not pid:
raise ServerError("提交成功但未返回 prompt_id",
json.dumps(payload, ensure_ascii=False)[:400])
return pid
def submit_retry(self, prompt_json):
last = None
for attempt in range(1, self.submit_retries + 1):
try:
return self.submit(prompt_json)
except ServerError as e:
last = e
self._log("warn", "提交失败(%d/%d): %s"
% (attempt, self.submit_retries, e))
if attempt < self.submit_retries:
time.sleep(3 * attempt)
raise last
def wait(self, pid, on_progress=None):
"""轮询直到任务结束。返回 (outputs, errors)。"""
t0 = time.time()
tick = 0
while time.time() - t0 < self.timeout:
time.sleep(self.poll_interval)
tick += 1
h = self._get("/history/" + pid)
if pid not in h:
if on_progress:
on_progress(tick, int(time.time() - t0))
continue
rec = h[pid]
errs = [m for m in rec.get("status", {}).get("messages", [])
if m[0] == "execution_error"]
outs = [a.get("filename") for o in rec.get("outputs", {}).values()
for a in o.get("audio", [])]
if errs:
raise JobFailed("任务执行失败: %s" % pid,
json.dumps(errs, ensure_ascii=False)[:600])
return outs, rec
raise JobFailed("等待超时(%ds): %s" % (self.timeout, pid))
def object_info(self, class_type):
try:
return self._get("/object_info/" + class_type)
except ServerError:
return {}
+38
View File
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
"""内核异常。cli_driver 只捕获这些,不吞系统异常。"""
class SongForgeError(Exception):
"""所有内核错误的基类。"""
class ConfigError(SongForgeError):
"""配置非法,或试图使用被禁用的模型。"""
class RuleViolation(SongForgeError):
"""违反生歌铁律。"""
class LyricDensityError(RuleViolation):
"""歌词字数与目标时长不匹配(死心塌地时长对应关系)。"""
class LexiconError(RuleViolation):
"""歌词命中身份禁用词。"""
class ServerError(SongForgeError):
"""ComfyUI 不可达或返回异常。"""
def __init__(self, msg, detail=""):
super().__init__(msg)
self.detail = detail
class JobFailed(SongForgeError):
"""任务在 ComfyUI 侧执行失败。"""
def __init__(self, msg, detail=""):
super().__init__(msg)
self.detail = detail
+119
View File
@@ -0,0 +1,119 @@
# -*- coding: utf-8 -*-
"""路径解析。ComfyUI 根优先配置/环境变量,再自动探测。"""
import datetime
import os
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
_MARKERS = (
os.path.join("models", "checkpoints"),
os.path.join("custom_nodes"),
os.path.join("user", "default"),
)
def comfy_root(explicit=None, cfg=None):
"""返回 ComfyUI 根目录绝对路径。"""
if explicit:
return os.path.abspath(explicit)
if cfg:
cr = (cfg.get("paths") or {}).get("comfy_root")
if cr:
return os.path.abspath(cr)
env = os.environ.get("SONGFORGE_COMFY_ROOT")
if env:
return os.path.abspath(env)
cur = ROOT
for _ in range(6):
hits = sum(1 for m in _MARKERS if os.path.isdir(os.path.join(cur, m)))
if hits >= 2:
return cur
parent = os.path.dirname(cur)
if parent == cur:
break
cur = parent
return os.path.dirname(os.path.dirname(os.path.dirname(ROOT)))
def model_path(root, *parts):
return os.path.join(root, "models", *parts)
def user_default(root):
return os.path.join(root, "user", "default")
def output_dir(root, prefix="MyMusic"):
return os.path.join(root, "output", *prefix.split("/"))
def music_dir(cfg=None, root=None):
"""音频成品目录:paths.music_dir 或 ComfyUI/output/<output_prefix>。"""
if cfg:
md = (cfg.get("paths") or {}).get("music_dir")
if md:
return os.path.abspath(md)
root = root or comfy_root(cfg=cfg)
return output_dir(root, cfg["paths"].get("output_prefix", "MyMusic"))
root = root or comfy_root()
return output_dir(root, "MyMusic")
def under_workflows(rel, root=None):
if not rel:
raise ValueError("路径为空")
if os.path.isabs(rel):
return os.path.normpath(rel)
base = root if root is not None else ROOT
return os.path.normpath(os.path.join(base, rel))
def ensure_dir(path):
os.makedirs(path, exist_ok=True)
return path
def today_iso():
return datetime.date.today().isoformat()
def day_subdir(base, day=None):
"""base/YYYY-MM-DD/"""
return os.path.join(base, day or today_iso())
def tmp_today(cfg, day=None):
"""paths.tmp_dir/YYYY-MM-DD/,确保存在。"""
base = under_workflows(cfg["paths"]["tmp_dir"])
return ensure_dir(day_subdir(base, day))
def path_layout(cfg):
"""从配置展开本包目录布局(绝对路径)。"""
p = cfg["paths"]
cro = comfy_root(cfg=cfg)
return {
"workflows": ROOT,
"comfy_root": cro,
"runs": under_workflows(p["runs_dir"]),
"tmp": under_workflows(p["tmp_dir"]),
"tmp_today": day_subdir(under_workflows(p["tmp_dir"])),
"lyrics": under_workflows(p["lyrics_dir"]),
"qc_cache": under_workflows(p["qc_cache_dir"]),
"style_cache": under_workflows(p["style_cache_dir"]),
"index": under_workflows(p.get("index_dir") or "songforge/index"),
"catalog": under_workflows(
p.get("catalog_file") or "songforge/index/catalog.jsonl"),
"baseline": under_workflows(
p.get("baseline_dir") or "songforge/config/baseline"),
"audio_out": music_dir(cfg, cro),
"pkg": under_workflows("songforge"),
}
# 根目录不应出现的过程文件(doctor WARN)
ROOT_JUNK_NAMES = ("qc.json", "run.log", "manifest.json")
+208
View File
@@ -0,0 +1,208 @@
# -*- coding: utf-8 -*-
"""songforge 核心包 - 工作流构建器"""
import copy
import json
import os
import re
from .errors import ConfigError
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# UI widgets_values 顺序 -> API inputs 键名。按节点类型分别声明,避免混用。
WIDGET_MAP = {
"CheckpointLoaderSimple": ["ckpt_name"],
"LoraLoader": ["lora_name", "strength_model", "strength_clip"],
"ModelSamplingAuraFlow": ["shift", "sampling"],
"EmptyAceStep1.5LatentAudio": ["seconds", "batch_size"],
"TextEncodeAceStepAudio1.5": [
"tags", "lyrics", "seed", "bpm", "duration", "timesignature",
"language", "keyscale", "generate_audio_codes", "cfg_scale",
"temperature", "top_p", "top_k", "min_p",
],
"KSampler": ["seed", "control_after_generate", "steps", "cfg",
"sampler_name", "scheduler", "denoise"],
"SaveAudio": ["filename_prefix"],
"SaveAudioAdvanced": ["filename_prefix", "format"],
"VAEDecodeAudio": [],
}
def load_template(path_or_name):
"""path_or_name 可以是 workflows 下的文件名,也可以是绝对路径。"""
p = path_or_name
if not os.path.isabs(p):
p = os.path.join(ROOT, p)
if not os.path.exists(p):
raise ConfigError("workflow 模板不存在: %s" % p)
with open(p, encoding="utf-8") as f:
return json.load(f)
def convert(ui_wf):
"""UI 格式 workflow dict -> API prompt dict。"""
links = ui_wf["links"]
# 兼容两种格式:ComfyUI 对象数组 (dict) 或 元组列表 [id, origin, target, ...]
if links and isinstance(links[0], dict):
link_src = {l["id"]: [l["origin_id"], l["origin_slot"]] for l in links}
else:
link_src = {l[0]: [l[1], l[2]] for l in links}
api = {}
for n in ui_wf["nodes"]:
cls = n["type"]
keys = WIDGET_MAP.get(cls)
inputs = {}
if keys:
wv = n.get("widgets_values", [])
if len(wv) < len(keys):
raise ConfigError(
"节点 %s (%s) 的 widgets_values 只有 %d 项,期望 %d 项;"
"模板可能被手改过"
% (n.get("id"), cls, len(wv), len(keys)))
for k, v in zip(keys, wv):
inputs[k] = v
for inp in n.get("inputs", []):
lid = inp.get("link")
if lid is not None and lid in link_src:
src = link_src[lid]
inputs[inp["name"]] = [str(src[0]), src[1]]
api[str(n["id"])] = {"class_type": cls, "inputs": inputs}
return api
def find_node(ui_wf, class_type):
for n in ui_wf["nodes"]:
if n["type"] == class_type:
return n
return None
def render(wf_result):
"""build_workflow 返回的 dict -> API prompt。"""
if isinstance(wf_result, dict) and "workflow" in wf_result and "class_type" not in wf_result:
return convert(wf_result["workflow"])
return convert(wf_result)
def build_workflow(template, *, title, tags, lyrics, seed, bpm, duration,
keyscale, sampler, encode, model, output_prefix,
output_format, language="zh", timesignature="4"):
"""按节点类型定位并写入参数。不依赖固定节点 ID。
model["lora"] 为空 / None 时直接跳过 LoraLoader:
我们用的 ace_step_1.5_turbo_swl_fused.safetensors 本身就是
"""
from ..config.loader import load
from ..rules.ironlaw import build_tags as _build_tags, build_lyrics, validate
from ..rules.ironlaw import build_chill_lyrics
# 1. 解析歌词章节
sections = []
lines = lyrics.split('\n')
current_tag = None
current_content = []
for line in lines:
line = line.strip()
if not line:
continue
tag_match = re.match(r'^\[(\w+(?:[-]\w+)?)\]$', line)
if tag_match:
if current_tag is not None and current_content:
sections.append((current_tag, '\n'.join(current_content)))
current_tag = tag_match.group(1)
current_content = []
else:
if current_tag is not None:
current_content.append(line)
if current_tag is not None and current_content:
sections.append((current_tag, '\n'.join(current_content)))
has_intro = lyrics.lstrip().lower().startswith('[intro]')
# 5. 组合工作流
workflow = copy.deepcopy(template)
# 从配置文件加载默认参数(single source of truth)
config = load()
model_cfg = config.get("model", {})
sampler_cfg = config.get("sampler", {})
encode_cfg = config.get("encode", {})
paths_cfg = config.get("paths", {})
# 合并参数来源: CLI/函数传入 > config > 硬coded 安全兜底
ckpt_name = model.get("checkpoint") or model_cfg.get("checkpoint", "ace_step_1.5_turbo_swl_fused.safetensors")
lora_name = model.get("lora") or model_cfg.get("lora", "")
lora_model_strength = model.get("lora_model_strength", model_cfg.get("lora_model_strength", 0.5))
lora_clip_strength = model.get("lora_clip_strength", model_cfg.get("lora_clip_strength", 0.5))
shift = sampler.get("shift", sampler_cfg.get("shift", 5.5))
sampling = sampler.get("sampling", sampler_cfg.get("sampling", "flow"))
steps = sampler.get("steps", sampler_cfg.get("steps", 28))
cfg = sampler.get("cfg", sampler_cfg.get("cfg", 2.0))
sampler_name = sampler.get("sampler_name", sampler_cfg.get("sampler_name", "euler"))
scheduler = sampler.get("scheduler", sampler_cfg.get("scheduler", "simple"))
denoise = sampler.get("denoise", sampler_cfg.get("denoise", 1.0))
batch_size = sampler.get("batch_size", sampler_cfg.get("batch_size", 1))
cfg_scale = encode.get("cfg_scale", encode_cfg.get("cfg_scale", 3.0))
temperature = encode.get("temperature", encode_cfg.get("temperature", 0.68))
top_p = encode.get("top_p", encode_cfg.get("top_p", 0.88))
top_k = encode.get("top_k", encode_cfg.get("top_k", 50))
min_p = encode.get("min_p", encode_cfg.get("min_p", 0.02))
generate_audio_codes = encode.get("generate_audio_codes", encode_cfg.get("generate_audio_codes", True))
output_format = output_format or paths_cfg.get("output_format", "flac")
# 遍历节点设置参数
for n in workflow["nodes"]:
if n["type"] == "CheckpointLoaderSimple":
n["widgets_values"] = [ckpt_name]
elif n["type"] == "LoraLoader":
# 设置 LoRA 加载器参数
if "widgets_values" in n:
n["widgets_values"] = [lora_name, lora_model_strength, lora_clip_strength]
elif n["type"] == "ModelSamplingAuraFlow":
if "widgets_values" in n:
n["widgets_values"] = [shift, sampling]
elif n["type"] == "EmptyAceStep1.5LatentAudio":
if "widgets_values" in n:
n["widgets_values"] = [duration, batch_size]
elif n["type"] == "TextEncodeAceStepAudio1.5":
if "widgets_values" in n:
# 构建 widgets_values: tags, lyrics, seed, bpm, duration, timesignature,
# language, keyscale, generate_audio_codes, cfg_scale, temperature, top_p, top_k, min_p
n["widgets_values"] = [
tags,
lyrics,
seed,
bpm,
duration,
timesignature,
language,
keyscale,
generate_audio_codes,
cfg_scale,
temperature,
top_p,
top_k,
min_p,
]
elif n["type"] == "KSampler":
if "widgets_values" in n:
n["widgets_values"] = [seed, "fixed", steps, cfg,
sampler_name, scheduler, denoise]
elif n["type"] == "SaveAudioAdvanced":
if "widgets_values" in n:
n["widgets_values"] = [output_prefix, output_format]
elif n["type"] == "SaveAudio":
if "widgets_values" in n:
n["widgets_values"] = [output_prefix]
return {
"success": True,
"workflow": workflow,
"lyrics": lyrics,
"tags": tags,
"has_intro": has_intro,
}