"""Install checksum-pinned official Paraformer/VAD/punctuation artifacts.""" import hashlib,json,os,urllib.request from pathlib import Path from concurrent.futures import ThreadPoolExecutor root=Path(os.environ.get('AUDIO_MODEL_ROOT','/opt/audio-processing/models')) manifest=json.loads(Path(__file__).with_name('models.json').read_text()) def checksum(p): h=hashlib.sha256() with p.open('rb') as f: for b in iter(lambda:f.read(1024*1024),b''):h.update(b) return h.hexdigest() def install(item): p=root/item['file'];p.parent.mkdir(parents=True,exist_ok=True) if p.exists() and p.stat().st_size==item['bytes'] and checksum(p)==item['sha256']:return temp=p.with_suffix(p.suffix+'.part');size=0 try: with urllib.request.urlopen(item['url'],timeout=60) as r,temp.open('wb') as f: while b:=r.read(1024*1024): size+=len(b) if size>item['bytes']:raise RuntimeError('Model size mismatch: '+item['file']) f.write(b) if size!=item['bytes'] or checksum(temp)!=item['sha256']:raise RuntimeError('Model checksum mismatch: '+item['file']) temp.replace(p);print('verified '+item['file'],flush=True) finally:temp.unlink(missing_ok=True) with ThreadPoolExecutor(max_workers=4) as pool:list(pool.map(install,manifest)) print('ALL_SPEECH_MODELS_VERIFIED',flush=True)