Files

34 lines
2.2 KiB
Python

"""Download pinned CPU runtime/model only. Never start an inference service."""
import argparse,hashlib,json,os,pathlib,tarfile,urllib.request
p=argparse.ArgumentParser();p.add_argument('--root',required=True);args=p.parse_args()
root=pathlib.Path(args.root);root.mkdir(parents=True,exist_ok=True)
files=[dict(path='runtime.tar.gz',url='https://github.com/mudler/parakeet.cpp/releases/download/v0.6.1/parakeet-v0.6.1-bin-linux-cpu-x64.tar.gz',sha256='cce60d122ab72e1068cd0d164e54a21655a0b83f1b9c21befc20124f5a972c10'),dict(path='redux-packed.gguf',url='https://huggingface.co/mudler/parakeet-cpp-gguf/resolve/741158ae71e64ef5c89385862c18f777d07a97a1/redux-packed.gguf',sha256='574614b9a4d9f72ab202877a7ad6a1f4bf819dd1d27b9d42dfe8cd429fcebdd5')]
def digest(path):
h=hashlib.sha256()
with path.open('rb') as f:
for block in iter(lambda:f.read(8*1024*1024),b''):h.update(block)
return h.hexdigest()
for row in files:
target=root/row['path']
if not target.exists():
part=target.with_suffix(target.suffix+'.part')
with urllib.request.urlopen(row['url'],timeout=120) as r,part.open('wb') as f:
while True:
block=r.read(8*1024*1024)
if not block:break
f.write(block)
if digest(part)!=row['sha256']:raise RuntimeError('Download checksum mismatch')
part.replace(target)
if digest(target)!=row['sha256']:raise RuntimeError('Existing artifact checksum mismatch')
print(json.dumps({'verified':row['path'],'bytes':target.stat().st_size}),flush=True)
with tarfile.open(root/'runtime.tar.gz') as t:
# Reject traversal, links and devices rather than trusting an archive layout.
for member in t.getmembers():
path=pathlib.PurePosixPath(member.name)
if path.is_absolute() or '..' in path.parts or member.issym() or member.islnk() or member.isdev():raise RuntimeError('Unsafe archive member')
t.extractall(root/'runtime',filter='data')
for binary in (root/'runtime').rglob('parakeet-*'):
if binary.is_file() and binary.name in ('parakeet-cli','parakeet-server'):binary.chmod(0o755)
(root/'manifest.json').write_text(json.dumps({'version':'v0.6.1','model_repo':'mudler/parakeet-cpp-gguf','model_revision':'741158ae71e64ef5c89385862c18f777d07a97a1','files':files},indent=2))
print(json.dumps({'prepared':True,'inference_started':False}))