Files

28 lines
1.6 KiB
Python

"""Import ONLY the selected private +20 model/voice/config, no profile publication."""
import argparse,hashlib,json,os,shutil,sys,tempfile
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[2]))
from kikiri_runtime import KikiriRuntime,ASSETS,verify_assets
def import_voice(state,model,voice,config):
state=Path(state);target=state/'kikiri-data/anna-plus20';os.umask(0o077)
sources={'model.pth':Path(model),'voice.pt':Path(voice),'config.json':Path(config)}
for name,source in sources.items():
if source.is_symlink() or not source.is_file():raise ValueError('Normale lokale Datei erforderlich: '+name)
with source.open('rb') as f:digest=hashlib.file_digest(f,'sha256').hexdigest()
if digest!=ASSETS[name][1]:raise ValueError('Nicht der ausgewählte Anna-Stand: '+name)
if target.exists():verify_assets(target)
else:
target.parent.mkdir(parents=True,exist_ok=True,mode=0o700)
with tempfile.TemporaryDirectory(dir=target.parent,prefix='.import-') as work:
work=Path(work)
for name,source in sources.items():shutil.copyfile(source,work/name);(work/name).chmod(0o600)
verify_assets(work);work.rename(target)
ident=KikiriRuntime(state/'kikiri-runtime').ensure_library()
return dict(imported=True,model_id=ident,private_assets='kikiri-data/anna-plus20',profile_published=False)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('--state',required=True,type=Path)
for name in ('model','voice','config'):p.add_argument('--'+name,required=True,type=Path)
a=p.parse_args();print(json.dumps(import_voice(a.state,a.model,a.voice,a.config)))