Files
Athena-Deck/deploy/import_pocket.py

29 lines
2.3 KiB
Python

"""Import verified public Pocket files from an existing HF cache, no downloads.
Run as the Deck service user. Does not edit profiles, endpoint settings or keys.
Optional private exported voice is copied into the existing Anna voice folder.
"""
import argparse,hashlib,json,shutil,time
from pathlib import Path
FILES=[('kyutai/pocket-tts','3e82814a68665eec246ff649b14c71331f955c06','languages/german/model.safetensors','3b6702b36f4387735031226cc012f7b90a7a13e6bee4d3b0ce3929736e5e150a'),('kyutai/pocket-tts-without-voice-cloning','4e1e0a3e611c51c0b4ed8174fc10f32a54644303','languages/german/tokenizer.json','2d77849811e3d1b6ae80e5b11b203f57e1847901104d8a0573b34f46f8dddb53')]
def main():
p=argparse.ArgumentParser();p.add_argument('--state',required=True,type=Path);p.add_argument('--cache',required=True,type=Path);p.add_argument('--anna',type=Path);a=p.parse_args();ids=[]
for repo,revision,name,expected in FILES:
source=a.cache/('models--'+repo.replace('/','--'))/'snapshots'/revision/name
if hashlib.sha256(source.read_bytes()).hexdigest()!=expected:raise ValueError('Öffentliche Pocket-Datei hat unerwartete SHA256.')
ident=hashlib.sha256((repo+revision+name).encode()).hexdigest();target=a.state/'models'/ident;target.mkdir(parents=True,exist_ok=True,mode=0o700)
dest=target/('model'+Path(name).suffix)
if not dest.exists():
temp=target/'import.tmp';shutil.copyfile(source,temp);temp.chmod(0o600);temp.replace(dest)
if hashlib.sha256(dest.read_bytes()).hexdigest()!=expected:raise ValueError('Vorhandene Bibliotheksdatei weicht ab; nicht überschrieben.')
entry=dict(repo=repo,revision=revision,file=name,size=dest.stat().st_size,sha256=expected,upstream_hash_verified=True,kind='audio',downloaded_at=time.time(),state='downloaded',runtime_ready=True)
temp=target/'entry.tmp';temp.write_text(json.dumps(entry));temp.chmod(0o600);temp.replace(target/'entry.json');ids.append(ident)
if a.anna:
voice=a.state/'speech-voices/Anna';voice.mkdir(parents=True,exist_ok=True,mode=0o700)
dest=voice/'pocket-german.safetensors'
if dest.exists() and dest.read_bytes()!=a.anna.read_bytes():raise ValueError('Anderes Anna-Pocket-Profil vorhanden; nicht überschrieben.')
if not dest.exists():shutil.copyfile(a.anna,dest)
dest.chmod(0o600)
print(json.dumps(dict(model_id=ids[0],tokenizer_id=ids[1],private_anna_imported=bool(a.anna))))
if __name__=='__main__':main()