ai-chatbot/services/smart-edit/prepare_separation.py

19 lines
904 B
Python

"""Install only the checksum-pinned official HTDemucs checkpoint, outside request handling."""
import hashlib,json,os,urllib.request
from pathlib import Path
from separate import CHECKSUM
root=Path(os.environ.get('SMART_EDIT_SEPARATION_MODEL_DIR','.smart-edit-models/demucs')).resolve()
root.mkdir(parents=True,exist_ok=True)
file=root/'955717e8-8726e21a.th'
url='https://dl.fbaipublicfiles.com/demucs/hybrid_transformer/955717e8-8726e21a.th'
if not file.exists() or hashlib.sha256(file.read_bytes()).hexdigest()!=CHECKSUM:
temp=file.with_suffix('.part')
try:
urllib.request.urlretrieve(url,temp)
if hashlib.sha256(temp.read_bytes()).hexdigest()!=CHECKSUM:
raise RuntimeError('HTDemucs 权重校验失败')
temp.replace(file)
finally:
temp.unlink(missing_ok=True)
print(json.dumps({'SMART_EDIT_SEPARATION_MODEL_DIR':str(root)},ensure_ascii=False))