74 lines
3.5 KiB
Python
74 lines
3.5 KiB
Python
"""Download only the pinned Kokoro files and verify canonical SHA256 before publishing."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import time
|
|
import urllib.request
|
|
|
|
ALLOWED = {'config.json', 'kokoro-v1_0.pth', 'voices/zf_xiaobei.pt', 'voices/zm_yunxi.pt'}
|
|
|
|
|
|
def verified(path, entry):
|
|
if path.is_symlink() or not path.is_file() or path.stat().st_size != entry['size']:
|
|
return False
|
|
digest = hashlib.sha256()
|
|
with path.open('rb') as handle:
|
|
for chunk in iter(lambda: handle.read(4 * 1024 * 1024), b''):
|
|
digest.update(chunk)
|
|
return digest.hexdigest() == entry['sha256']
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--manifest', type=Path, required=True)
|
|
parser.add_argument('--destination', type=Path, default=Path('/workspace/models/kokoro-82m'))
|
|
args = parser.parse_args()
|
|
manifest = json.loads(args.manifest.read_text(encoding='utf-8'))
|
|
if manifest['repo'] != 'hexgrad/Kokoro-82M' or {x['rfilename'] for x in manifest['files']} != ALLOWED:
|
|
raise ValueError('Unexpected model manifest')
|
|
destination = args.destination.resolve()
|
|
if not destination.is_relative_to(Path('/workspace').resolve()):
|
|
raise ValueError('Model destination must be inside the isolated workspace')
|
|
destination.mkdir(parents=True, exist_ok=True)
|
|
for entry in manifest['files']:
|
|
path = destination / entry['rfilename']
|
|
if path.is_symlink() or not path.resolve().is_relative_to(destination):
|
|
raise ValueError('Invalid model file path')
|
|
if verified(path, entry):
|
|
print('Verified existing', entry['rfilename'], flush=True)
|
|
continue
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
temporary = path.with_name(path.name + '.part')
|
|
if temporary.is_symlink():
|
|
raise ValueError('Temporary model path is a symlink')
|
|
if shutil.which('aria2c'):
|
|
subprocess.run(['aria2c', '--file-allocation=none', '--continue=true',
|
|
'--max-connection-per-server=4', '--split=4', '--max-tries=4',
|
|
'--retry-wait=3', '--console-log-level=warn', '--summary-interval=0',
|
|
'--checksum=sha-256=' + entry['sha256'],
|
|
'--dir=' + str(path.parent), '--out=' + temporary.name,
|
|
entry['download_url']], check=True)
|
|
else:
|
|
request = urllib.request.Request(entry['download_url'], headers={'User-Agent': 'CMVR-Visual/1.0'})
|
|
with urllib.request.urlopen(request, timeout=90) as response, temporary.open('wb') as handle:
|
|
written = 0
|
|
while chunk := response.read(1024 * 1024):
|
|
written += len(chunk)
|
|
if written > entry['size']:
|
|
raise ValueError('Model download exceeds expected size')
|
|
handle.write(chunk)
|
|
if not verified(temporary, entry):
|
|
raise ValueError('Canonical model checksum mismatch: ' + entry['rfilename'])
|
|
temporary.replace(path)
|
|
print('Downloaded and verified', entry['rfilename'], entry['size'], flush=True)
|
|
(destination / '.deployment-manifest.json').write_text(
|
|
json.dumps({**manifest, 'verified_at': time.time()}, indent=2) + '\n', encoding='utf-8')
|
|
print('Kokoro model and both Chinese voices fully verified', flush=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|