xstard's picture
Implement encoder-only bilingual decision API and demo
b6ae5c2 verified
Raw History Blame Contribute Delete
1.24 kB
import hashlib
import json
from pathlib import Path
import re
def sha256(path):
digest = hashlib.sha256()
with Path(path).open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def verify_bundle(path):
path = Path(path)
manifest = json.loads((path / "manifest.json").read_text(encoding="utf-8"))
required = {"head.safetensors", "calibration.json", "metrics.json"}
if manifest.get('encoder_delta'):
required.add('encoder_delta.safetensors')
if manifest.get("format_version") != 1 or set(manifest.get("files", {})) != required:
raise ValueError("Неповний manifest артефактів.")
if not re.fullmatch(r"[a-f0-9]{40}", manifest.get("encoder_revision", "")):
raise ValueError("Не зафіксовано ревізію encoder.")
if not isinstance(manifest.get("hidden_size"), int) or manifest["hidden_size"] <= 0:
raise ValueError("Некоректний hidden_size.")
for name, digest in manifest["files"].items():
if sha256(path / name) != digest:
raise ValueError(f"SHA256 не збігається: {name}")
return manifest