Files
Athena-Deck/deploy/import_acestep.py
T

52 lines
3.6 KiB
Python

"""Import the former router's ACE-Step files with verified, restorable Hub origins.
Copies only recipe files; does not change profiles, endpoints or running processes.
"""
import hashlib,json,os,shutil,time,urllib.request
from pathlib import Path
from acestep_models import ACE_REPO,ACE_BASE_REPO,ACE_BASE_REVISION,ACE_FILES,ACE_BASE_FILES
def run(old,library,plan):
old=Path(old);library=Path(library);result={};sources={}
files=[(ACE_REPO,'acestep-v15-xl-sft',name) for name in ['model-00001-of-00004.safetensors',*ACE_FILES]]+[(ACE_BASE_REPO,'',name) for name in ACE_BASE_FILES]
for repo,folder,name in files:
base=old/folder;source=base/name;meta=base/'.cache/huggingface/download'/(name+'.metadata')
revision=meta.read_text().splitlines()[0]
if repo==ACE_BASE_REPO and revision!=ACE_BASE_REVISION:raise ValueError('Unexpected base package revision')
key=(repo,revision)
if key not in sources:
with urllib.request.urlopen('https://huggingface.co/api/models/'+repo+'/revision/'+revision+'?blobs=true',timeout=30) as response:data=json.load(response)
if data['sha']!=revision:raise ValueError('Revision verification failed')
sources[key]={f['rfilename']:f for f in data['siblings']}
upstream=sources[key][name]
size=upstream.get('size',upstream.get('lfs',{}).get('size'))
if name.endswith('.py') and source.stat().st_size!=size:
# Old ACE auto-sync modified its local model code. Import the original
# small code file from the pinned Hub revision, leaving the old tree alone.
cache=Path(plan).parent/'acestep-source-files'/revision/name;cache.parent.mkdir(parents=True,exist_ok=True)
with urllib.request.urlopen('https://huggingface.co/'+repo+'/resolve/'+revision+'/'+name,timeout=30) as response:cache.write_bytes(response.read(2*1024**2))
source=cache
if source.stat().st_size!=size:raise ValueError('Source size mismatch: '+name)
digest=hashlib.sha256();git_digest=hashlib.sha1();git_digest.update(('blob '+str(source.stat().st_size)+'\0').encode())
with source.open('rb') as stream:
while chunk:=stream.read(8*1024**2):digest.update(chunk);git_digest.update(chunk)
sha=digest.hexdigest()
if upstream.get('lfs'):
if sha!=upstream['lfs']['sha256']:raise ValueError('Source SHA-256 mismatch: '+name)
elif git_digest.hexdigest()!=upstream['blobId']:raise ValueError('Source Git hash mismatch: '+name)
ident=hashlib.sha256((repo+revision+name).encode()).hexdigest();target=library/ident;target.mkdir(parents=True,exist_ok=True)
dest=target/('model'+Path(name).suffix)
if not dest.exists():
partial=dest.with_suffix(dest.suffix+'.partial');shutil.copyfile(source,partial);partial.replace(dest)
if dest.stat().st_size!=source.stat().st_size:raise ValueError('Imported size mismatch')
check=hashlib.sha256()
with dest.open('rb') as stream:
while chunk:=stream.read(8*1024**2):check.update(chunk)
if check.hexdigest()!=sha:raise ValueError('Imported checksum mismatch')
entry=dict(repo=repo,revision=revision,file=name,size=dest.stat().st_size,sha256=sha,upstream_hash_verified=True,kind='music',downloaded_at=time.time(),state='downloaded',runtime_ready=False,runtime_metadata=dict(pipeline_tag='text-to-audio',library_name='diffusers',architecture='acestep',tags=['license:mit']))
(target/'entry.tmp').write_text(json.dumps(entry));(target/'entry.tmp').replace(target/'entry.json')
for item in (dest,target/'entry.json',target):os.chown(item,65534,65534)
result[repo+'/'+name]=ident;print('Verified:',repo+'/'+name,flush=True)
Path(plan).write_text(json.dumps(result));print('Complete:',len(result),'verified files',flush=True)
if __name__=='__main__':
import sys;run(*sys.argv[1:])