import hashlib,json,os,subprocess,sys,time,urllib.request from pathlib import Path root=Path(__file__).resolve().parent os.environ.update(CUDA_VISIBLE_DEVICES='',OMP_NUM_THREADS='2',HF_HOME=str(root/'hf-cache'),HF_HUB_DISABLE_TELEMETRY='1',PIP_DISABLE_PIP_VERSION_CHECK='1') def status(state,phase): p=root/'prepare-status.tmp';p.write_text(json.dumps(dict(state=state,phase=phase,time=time.time())));p.replace(root/'prepare-status.json') def run(args): with (root/'prepare.log').open('ab') as f:subprocess.run([str(x) for x in args],stdout=f,stderr=f,check=True) try: status('running','Isolierte Python-Laufzeit erstellen') run([sys.executable,'-m','venv',root/'python']);python=root/'python/bin/python' status('running','Offizielle CUDA-Torch-Wheels installieren; GPU unsichtbar') run([python,'-m','pip','install','--no-cache-dir','torch==2.9.1','torchaudio==2.9.1','--index-url','https://download.pytorch.org/whl/cu128']) status('running','Hersteller-Abhängigkeiten installieren') req=(root/'requirements.lock').read_text() if (root/'requirements.lock').exists() else (root/'upstream-requirements.txt').read_text();(root/'requirements.txt').write_text('\n'.join(l for l in req.splitlines() if not l.startswith(('torch==','torchaudio==','chatterbox-tts==')))) run([python,'-m','pip','install','--no-cache-dir','-r',root/'requirements.txt']) run([python,'-m','pip','install','--no-cache-dir','--no-deps','chatterbox-tts==0.1.7']) status('running','Öffentliche Modellgewichte mit Prüfsummen laden') manifest=json.loads((root/'assets.json').read_text());model=root/'model' for a in manifest['files']: target=model/a['file'];target.parent.mkdir(parents=True,exist_ok=True) h=hashlib.sha256();g=hashlib.sha1(('blob '+str(a['size'])+'\0').encode()) with urllib.request.urlopen('https://huggingface.co/'+manifest['repo']+'/resolve/'+manifest['revision']+'/'+a['file'],timeout=120) as r,target.open('wb') as f: while chunk:=r.read(4*1024**2):f.write(chunk);h.update(chunk);g.update(chunk) if target.stat().st_size!=a['size'] or (h.hexdigest()!=a['sha256'] if a['sha256'] else g.hexdigest()!=a['git_sha1']):raise ValueError('Gewichtsprüfung fehlgeschlagen: '+a['file']) a['verified_sha256']=h.hexdigest() (root/'verified-assets.json').write_text(json.dumps(manifest,indent=2)) os.environ.update(HF_HUB_OFFLINE='1',TRANSFORMERS_OFFLINE='1') status('running','CPU-Import und vollständige Modelle prüfen; keine GPU-Nutzung') run([python,'-c',"import sys,torch;from pathlib import Path;sys.path.insert(0,str(Path(sys.argv[1])/'model'));import infer;assert not torch.cuda.is_available();m=infer.load_converter(Path(sys.argv[1])/'model',torch.device('cpu'));e=infer.load_ears(Path(sys.argv[1])/'model/conditioning/ears-medium',torch.device('cpu'));print('CPU_IMPORT_OK')",root]) with (root/'requirements.lock').open('wb') as f:subprocess.run([str(python),'-m','pip','freeze'],stdout=f,check=True) (root/'ready').write_text(manifest['revision']);status('complete','Gewichte geprüft und Laufzeit CPU-seitig bereit; noch kein Profil aktiviert') except Exception as e:status('failed',type(e).__name__+': '+str(e)[:350]);raise