Rise
The Vieneo Province
Loading...
Searching...
No Matches
Restore Model.py
Go to the documentation of this file.
1"""Restore the exact base model recorded in model-manifest.json; no audio is uploaded."""
2import hashlib
3import json
4import os
5import shutil
6import urllib.request
7from pathlib import Path
8
9root = Path(__file__).resolve().parent
10manifest = json.loads((root/'model-manifest.json').read_text(encoding='utf-8'))
11target = root/'models/nano'
12target.mkdir(parents=True, exist_ok=True)
13
14def matches(path, entry):
15 if not path.is_file() or path.stat().st_size != entry['bytes']:
16 return False
17 with path.open('rb') as stream:
18 return hashlib.file_digest(stream, 'sha256').hexdigest() == entry['sha256']
19
20for name, entry in manifest['files'].items():
21 if Path(name).name != name:
22 raise ValueError('Invalid filename in model manifest.')
23 output = target/name
24 if matches(output, entry):
25 print(f'Already verified: {name}', flush=True)
26 continue
27 if shutil.disk_usage(target).free < entry['bytes'] + 64*1024*1024:
28 raise RuntimeError(f'Insufficient free disk space to restore {name}.')
29 url = f"https://huggingface.co/{manifest['repository']}/resolve/{manifest['revision']}/{name}"
30 temporary = output.with_name(output.name+'.download')
31 print(f'Downloading {name}...', flush=True)
32 with urllib.request.urlopen(url, timeout=120) as source, temporary.open('wb') as sink:
33 shutil.copyfileobj(source, sink, 1024*1024)
34 if not matches(temporary, entry):
35 raise RuntimeError(f'Integrity check failed: {temporary}')
36 os.replace(temporary, output)
37print('All model files are verified.', flush=True)
matches(path, entry)