Files
myfileagent-pipeline/reviewer/prepare_runtime.py
T

62 lines
2.3 KiB
Python
Raw Normal View History

"""Download and checksum the pinned public GGUF model and llama.cpp CPU archive."""
import argparse
import hashlib
import json
from pathlib import Path
import shutil
import tarfile
import urllib.request
LOCK = Path(__file__).with_name("model-lock.json")
def digest(path):
value = hashlib.sha256()
with Path(path).open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
value.update(chunk)
return value.hexdigest()
def fetch(url, target, expected):
if target.is_file() and digest(target) == expected:
return
part = target.with_name(target.name + ".part")
part.unlink(missing_ok=True)
try:
with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "ssw590-member1"}), timeout=60) as source, part.open("wb") as dest:
shutil.copyfileobj(source, dest, 1024 * 1024)
if digest(part) != expected:
raise ValueError(f"checksum mismatch: {target.name}")
part.replace(target)
finally:
part.unlink(missing_ok=True)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--dest", type=Path, required=True, help="directory outside Git for model/runtime files")
args = parser.parse_args()
lock = json.loads(LOCK.read_text())
args.dest.mkdir(parents=True, exist_ok=True)
model = args.dest / lock["quantized_filename"]
archive = args.dest / lock["runtime_archive"]
fetch(lock["quantized_url"], model, lock["quantized_sha256"])
fetch(lock["runtime_url"], archive, lock["runtime_archive_sha256"])
with tarfile.open(archive) as bundle:
members = bundle.getmembers()
root_name = "llama-" + lock["runtime_tag"]
if any(member.name != root_name and not member.name.startswith(root_name + "/") for member in members):
raise ValueError("unexpected runtime archive layout")
bundle.extractall(args.dest, filter="data")
server = args.dest / f"llama-{lock['runtime_tag']}" / lock["entrypoint"]
if not server.is_file():
raise FileNotFoundError("runtime entrypoint missing from verified archive")
print(f"REVIEWER_MODEL_PATH={model}")
print(f"REVIEWER_LLAMA_COMPLETION={server}")
print("Install libgomp1 in the execution image and ensure its directory is on the loader path.")
if __name__ == "__main__":
main()