next-gpt-lite / tools /validate.py
setariaa's picture
Code + report (attempt 1)
0b0e1c1 verified
Raw History Blame Contribute Delete
2.86 kB
"""Validate saved evidence and the deployed app against recorded artifacts.
Checks: all Python parses; every checkpoint referenced by results exists; eval.json numbers are
reproducible from the stored predictions; source/revision manifest is complete; the Gradio app
can be imported and answers one prompt with the saved projections.
"""
from __future__ import annotations
import ast
import hashlib
import json
import pathlib
import sys
ROOT = pathlib.Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
checks: dict = {}
def main() -> None:
for p in ROOT.rglob("*.py"):
ast.parse(p.read_text(encoding="utf-8"))
checks["python_syntax"] = "passed"
sources = json.loads((ROOT / "data/sources.json").read_text(encoding="utf-8"))
checks["sources_entries"] = len(sources)
cached = [s for s in sources if (ROOT / ".cache" / s["repo"].replace("/", "--") / s["file"]).exists()]
checks["sources_present_locally"] = len(cached)
ckpt = ROOT / "checkpoints"
for name in ("stage1_image.pt", "stage1_audio.pt", "stage2_image.pt", "stage2_audio.pt",
"audio_decoder.pt"):
assert (ckpt / name).exists(), name
assert (ckpt / "stage3/projections.pt").exists()
assert (ckpt / "stage3/adapter/adapter_config.json").exists()
checks["checkpoints_present"] = True
metrics = json.loads((ROOT / "results/stage_metrics.json").read_text(encoding="utf-8"))
log = [json.loads(line) for line in (ROOT / "results/training_log.jsonl").read_text(encoding="utf-8").splitlines() if line.strip()]
checks["training_log_records"] = len(log)
assert any(r["stage"] == 1 for r in log) and any(r["stage"] == 3 for r in log)
ev = json.loads((ROOT / "results/eval.json").read_text(encoding="utf-8"))
sys.path.insert(0, str(ROOT))
from nextgpt_lite.eval import bleu
recomputed = bleu(ev["caption_image"]["preds"], ev["caption_image"]["refs"])
assert abs(recomputed - ev["caption_image"]["bleu4"]) < 1e-9
checks["bleu_recomputable"] = recomputed
for f in (ROOT / "results/samples").glob("gen_image_test_*.png"):
assert f.stat().st_size > 1000
checks["generated_image_samples"] = len(list((ROOT / "results/samples").glob("gen_image_test_*.png")))
from nextgpt_lite.app import get_model
model = get_model()
res = model.chat([{"role": "user", "content": "请用一句话介绍你自己。"}], max_new_tokens=24)
checks["app_smoke_text"] = res.text[:80]
res2 = model.chat([{"role": "user", "content": "请生成一张海边的图片"}], max_new_tokens=16)
checks["app_smoke_modalities"] = res2.modalities
(ROOT / "results/validation.json").write_text(json.dumps(checks, indent=2, ensure_ascii=False), encoding="utf-8")
print(json.dumps(checks, indent=2, ensure_ascii=False))
if __name__ == "__main__":
main()