"""UI-only bridge: all GPU execution and profile selection remain in Athena Deck.""" import io,json,os,time,urllib.request,urllib.error,wave from pathlib import Path CONFIG=Path('/data/deck-connection.json') BASE='http://127.0.0.1:8108' def request(path,data=None,ctype='application/json'): token=json.loads(CONFIG.read_text()).get('token','') if CONFIG.exists() else '' if not token:raise ValueError('Deck-Verbindung zuerst einrichten.') req=urllib.request.Request(BASE+path,data=data,headers={'Authorization':'Bearer '+token,'Content-Type':ctype}) try: with urllib.request.urlopen(req,timeout=90) as r:return r.read() except urllib.error.HTTPError as exc: try:error=json.loads(exc.read()).get('error','Deck-Anfrage abgelehnt') except Exception:error='Deck-Anfrage abgelehnt (HTTP '+str(exc.code)+')' raise ValueError(error if isinstance(error,str) else error.get('message','Deck-Anfrage abgelehnt')) from None def connect(token): if not token:raise ValueError('API-Token eingeben.') # Check before persisting; keep token out of output and logs. req=urllib.request.Request(BASE+'/api/v1/voice/profiles',headers={'Authorization':'Bearer '+token}) with urllib.request.urlopen(req,timeout=10) as r: if r.status!=200:raise ValueError('Deck-Verbindung nicht bestätigt.') CONFIG.parent.mkdir(parents=True,exist_ok=True);tmp=CONFIG.with_suffix('.tmp');tmp.write_text(json.dumps({'token':token}));tmp.chmod(0o600);tmp.replace(CONFIG) return 'Deck verbunden. In Deck das gewünschte Stimmprofil aktivieren und Stimmwerkzeuge starten.' def generate(family,text,device='3060',reference=None,**options): rows=json.loads(request('/api/v1/voice/profiles'))['profiles'] p=next((p for p in rows if p['repo']==family and p['enabled'] and p['runnable']),None) if not p:raise ValueError('Dieses Stimmprofil zuerst in Deck aktivieren. Es darf nur ein Stimmprofil aktiv sein.') data=dict(profile_id=p['id'],text=text,device=device,**options) ctype='application/json' if reference: import soundfile as sf arr,rate=sf.read(reference);buf=io.BytesIO();sf.write(buf,arr,rate,format='WAV',subtype='PCM_16');audio=buf.getvalue() boundary='athena-voice-'+os.urandom(12).hex();parts=[] for k,v in data.items(): value=str(v).lower() if isinstance(v,bool) else str(v) parts.append(('--'+boundary+'\r\nContent-Disposition: form-data; name="'+k+'"\r\n\r\n'+value+'\r\n').encode()) parts.extend([('--'+boundary+'\r\nContent-Disposition: form-data; name="file"; filename="reference.wav"\r\nContent-Type: audio/wav\r\n\r\n').encode(),audio,('\r\n--'+boundary+'--\r\n').encode()]);body=b''.join(parts);ctype='multipart/form-data; boundary='+boundary else:body=json.dumps(data).encode() job=json.loads(request('/api/v1/voice/start',body,ctype));deadline=time.monotonic()+650 while time.monotonic()