Files
aigc-home/docs/hel-892/flux2-views-document-evidence/bind_sources.py
T

140 lines
5.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Bind exact text to the three-view graph; never execute upstream code."""
import hashlib
import json
import os
import subprocess
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
REF = ROOT / "docs/hel-892/flux2-views-reference"
def main():
rows = []
specs = [
("flux2-reference", "core", None),
("flux2-reference", "frontend", None),
("flux2-reference", "tt", None),
("flux2-img-reference", "lora", None),
("flux2-lineart-reference", "llama", None),
("flux2-lineart-reference", "cr", None),
("flux2-lineart-reference", "showtext", None),
]
for origin, key, allowed in specs:
parent = REF.parent / origin
for old in json.loads((parent / "sources.json").read_bytes())["sources"]:
if old["id"] != key or allowed is not None and old["file"] not in allowed:
continue
e = dict(old)
path = (parent / e["local_file"]).resolve()
assert hashlib.sha256(path.read_bytes()).hexdigest() == e["sha256"]
e["local_file"] = os.path.relpath(path, REF)
# Reused source entries may already point to another identity directory.
identity = parent
if key in ["cr", "showtext"]:
identity = REF.parent / ("tts-reference" if key == "cr" else "llama-reference")
e["identity_root"] = os.path.relpath(identity, REF)
e["identity_key"] = key
if "tree" not in e:
commit_text = (identity / (key + "-commit.txt")).read_text()
e["tree"] = commit_text.splitlines()[0].removeprefix("tree ")
license_path = parent / old["license_file"]
if license_path.is_file():
e["license_file"] = os.path.relpath(license_path, REF)
else:
candidate = identity / (key + "__LICENSE")
e["license_file"] = (
os.path.relpath(candidate, REF)
if candidate.is_file()
else "准确树无许可文本;不推定许可"
)
e["applicability"] = (
"仅本图独立绑定:core0.8.2/旁路LoRA0.21.1/frontend1.36.14;"
"TT/llama/CR/Show完整提交分别适用;目标安装与UE/tab恢复未知"
)
rows.append(e)
# Only the exact core's framework list conditions, not a newer executor.
e = dict(next(e for e in rows if e["id"] == "core"))
file = "execution.py"
path = REF / "core__execution.py"
url = f"https://raw.githubusercontent.com/{e['repo']}/{e['commit']}/{file}"
if not path.exists():
with urllib.request.urlopen(url, timeout=30) as response:
path.write_bytes(response.read())
raw = path.read_bytes()
blob = hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
tree_data = json.loads((REF / e["identity_root"] / "core-tree.json").read_bytes())
assert next(t["sha"] for t in tree_data["tree"] if t["path"] == file) == blob
e.update(
file=file,
local_file=path.name,
url=url,
git_blob_sha1=blob,
sha256=hashlib.sha256(raw).hexdigest(),
)
rows.append(e)
# This Easy commit differs from the preceding graph; acquire its own Git objects.
key, repo, rev = ("easy", "yolain/ComfyUI-Easy-Use", "81c510c06e18dffd4f43518644fc35964c9168ca")
cache = ROOT / ".local-evidence/flux2-views-easy-git"
subprocess.run(["git", "init", "--bare", str(cache)], check=True, stdout=subprocess.DEVNULL)
subprocess.run(
[
"git",
"-C",
str(cache),
"fetch",
"--filter=blob:none",
"--depth=1",
"https://github.com/" + repo + ".git",
rev,
],
check=True,
)
def git(*args):
return subprocess.check_output(["git", "-C", str(cache), *args])
assert git("rev-parse", "FETCH_HEAD").decode().strip() == rev
tree = git("rev-parse", rev + "^{tree}").decode().strip()
(REF / "easy-commit.txt").write_bytes(git("cat-file", "commit", rev))
entries = []
for line in git("ls-tree", "-r", rev).decode().splitlines():
head, path = line.split("\t", 1)
mode, kind, sha = head.split()
entries.append(dict(path=path, mode=mode, type=kind, sha=sha))
(REF / "easy-tree.json").write_text(
json.dumps(dict(sha=tree, truncated=False, tree=entries), indent=2) + "\n"
)
for file in ["__init__.py", "py/nodes/prompt.py", "pyproject.toml", "LICENSE"]:
raw = git("show", rev + ":" + file)
name = key + "__" + file.replace("/", "__")
(REF / name).write_bytes(raw)
rows.append(
dict(
id=key,
repo=repo,
tag=rev,
commit=rev,
tree=tree,
file=file,
local_file=name,
url=f"https://raw.githubusercontent.com/{repo}/{rev}/{file}",
sha256=hashlib.sha256(raw).hexdigest(),
git_blob_sha1=next(e["sha"] for e in entries if e["path"] == file),
license_file="easy__LICENSE",
identity_root=".",
identity_key="easy",
applicability="本图97独立完整提交;静态参考,未执行列表合并或推理",
)
)
(REF / "sources.json").write_text(
json.dumps(dict(sources=rows), ensure_ascii=False, indent=2) + "\n"
)
print("Bound", len(rows), "exact necessary texts")
if __name__ == "__main__":
main()