Files
xiaobailing-turn-detection/experiments/turnsense-1.0/setup_assets.py
T

29 lines
1.6 KiB
Python
Raw Normal View History

"""Restore fixed official assets; no speech or labels are transmitted."""
import hashlib,json,tarfile,urllib.request,io
from pathlib import Path
HERE=Path(__file__).resolve().parent
m=json.loads((HERE/'model-manifest.json').read_text())
def download(url):
with urllib.request.urlopen(url,timeout=120) as r: return r.read()
def main():
vendor=HERE/'vendor';vendor.mkdir(exist_ok=True)
revision=m['code_revision'];archive=download(f'https://codeload.github.com/{m["code_repo"]}/tar.gz/{revision}')
# Extract only the official inference code, frontend sources and license.
with tarfile.open(fileobj=io.BytesIO(archive),mode='r:gz') as tar:
for member in tar.getmembers():
rel=Path(*Path(member.name).parts[1:])
if not member.isfile() or rel.is_absolute() or '..' in rel.parts: continue
if str(rel) not in ('infer.py','LICENSE') and not (str(rel).startswith('frontend/') and rel.suffix=='.py'): continue
target=vendor/rel;target.parent.mkdir(parents=True,exist_ok=True);target.write_bytes(tar.extractfile(member).read())
models=HERE/'models';models.mkdir(exist_ok=True)
for name,sha in m['files'].items():
target=models/name
if target.exists() and hashlib.sha256(target.read_bytes()).hexdigest()==sha: continue
data=download(f'https://huggingface.co/{m["repo"]}/resolve/{m["revision"]}/TurnSense1.0/{name}')
if hashlib.sha256(data).hexdigest()!=sha: raise ValueError('Checksum mismatch')
target.write_bytes(data)
print('Official code and weight checksums restored')
if __name__=='__main__': main()