Files
Athena-Deck/server.py
T
Mikei386 c0bc5d3a1b Integrate Basalt telemetry and shared GPU speech runtimes
Publish the reviewed accumulated runtime, scheduler, profile, UI, native recipes and historical maintenance evidence. Includes Qwen GPU ASR correction and shared Anna/atelier ownership. Validated with 784 local tests, Python/JSON parsing and eight JavaScript syntax checks. Live GPU acceptance and full fresh-host restore limits remain documented.
2026-11-03 21:16:41 +01:00

1202 lines
93 KiB
Python

#!/usr/bin/env python3
"""Athena Deck prototype: loopback API, owned model processes, read-only telemetry."""
from video import Video
from execution_setup import assess as execution_assess
import argparse
import os
import hashlib
import secrets
from http.cookies import SimpleCookie, CookieError
from auth import CredentialStore, verify_password
from catalog import Catalog
from voxcpm_runtime import VoxCPMRuntime
from omnivoice_runtime import OmniVoiceRuntime
from tts_runtime import TTSRuntime
from stt import STT,read_upload
from audio_policy import AudioPolicy
from tts_test import TTSTests
from voxcpm_test import VoxCPMTests
from profiles import Profiles
from capacity import assess, overview
from runtime import Runtime
from image_runtime import ImageRuntime
from separator_runtime import SeparatorRuntime
from separator import SeparatorTests
from image_test import ImageTests
from prompt_enhancer import PromptEnhancer
from docker_support import DockerSupport
from inference import LlamaWorker,Scheduler,InferenceError
from strata_runtime import StrataRuntime
from strata_worker import StrataWorker,ChatWorkerRouter
from basalt_runtime import BasaltRuntime
from basalt_worker import BasaltWorker
from endpoint import Endpoint
from chat_test import ChatTests
from auto_test import AutoTests
from urllib.parse import urlsplit, parse_qs, unquote
from network.client import NetworkClient
from network.config import parse_config, ConfigError
from network.hosts import validate_hosts
import json
import signal
import socket
import subprocess
import sys
import threading
import time
import urllib.request
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
ROOT = Path(__file__).resolve().parent
class HardwareProvider:
def __init__(self):
self.lock = threading.Lock()
self.cached = None
self.checked = 0
def snapshot(self):
if os.environ.get('DECK_LOCAL_HARDWARE', '1') == '1':
from collect_hardware import collect
with self.lock:
if self.cached is None or time.monotonic()-self.checked >= 1:
self.cached = dict(collect(), available=True)
self.checked = time.monotonic()
return self.cached
with self.lock:
if time.monotonic() - self.checked < 1 and self.cached is not None:
return self.cached
try:
result = subprocess.run(['ssh', '-i', str(Path.home()/'.ssh/athena_key'), '-o', 'BatchMode=yes', '-o', 'ConnectTimeout=4', '-o', 'StrictHostKeyChecking=yes', 'root@192.168.1.212', 'python3 -'], input=(ROOT/'collect_hardware.py').read_text(), capture_output=True, text=True, timeout=10, check=True)
self.cached = json.loads(result.stdout)
self.cached['available'] = True
except (OSError, subprocess.SubprocessError, ValueError):
self.cached = dict(available=False, sampled_at=None, cpu={}, ram={}, gpus=[], errors=['Athena per SSH nicht erreichbar oder Messung fehlgeschlagen.'])
self.checked = time.monotonic()
return self.cached
class Server(ThreadingHTTPServer):
daemon_threads = True
def __init__(self, port, state_dir=None):
super().__init__((os.environ.get('DECK_BIND_HOST','127.0.0.1'), port), Handler)
state_path=Path(state_dir or os.environ.get('DECK_STATE_DIR', ROOT/'.state'))
for cache in ('cuda','torchinductor'):(state_path/'cache'/cache).mkdir(parents=True,exist_ok=True,mode=0o700)
self.catalog = Catalog(Path(state_dir or os.environ.get("DECK_STATE_DIR", ROOT/".state"))/"models")
self.runtime = Runtime(Path(state_dir or os.environ.get("DECK_STATE_DIR", ROOT/".state"))/"runtime")
self.profiles = Profiles(self.catalog.root.parent/"profiles.json",self.catalog)
self.runtime.catalog=self.catalog
self.runtime.profiles=self.profiles
self.image_tests = ImageTests(self.catalog.root.parent/"image-tests",self.profiles)
self.image_runtime = ImageRuntime(self.catalog.root.parent/"image-runtime")
self.image_tests.runtime = self.image_runtime
self.prompt_enhancer=PromptEnhancer(self.catalog.root.parent/'prompt-enhancers',self.catalog,self.profiles,self.image_runtime)
self.image_tests.enhancer=self.prompt_enhancer
self.profiles.prompt_enhancer_ready=lambda task:bool(self.prompt_enhancer.installed(task))
self.profiles.image_runtime_ready=lambda:self.image_tests.status()["runtime_installed"]
self.voxcpm_runtime=VoxCPMRuntime(self.catalog.root.parent/'voxcpm-runtime',self.image_runtime)
self.omnivoice_runtime=OmniVoiceRuntime(self.catalog.root.parent/'omnivoice-runtime',self.image_runtime)
self.profiles.omnivoice_runtime_ready=lambda:self.omnivoice_runtime.status()['installed']
self.profiles.voxcpm_runtime_ready=lambda:self.voxcpm_runtime.status()['installed']
from applio_runtime import ApplioRuntime
self.applio_runtime=ApplioRuntime(self.catalog.root.parent/'applio-runtime',self.image_runtime)
from xvc_runtime import XVCRuntime
self.xvc_runtime=XVCRuntime(self.catalog.root.parent/'xvc-runtime',self.image_runtime)
from soulx_singer_runtime import SoulXSingerRuntime
self.soulx_singer_runtime=SoulXSingerRuntime(self.catalog.root.parent/'soulx_singer-runtime',self.image_runtime)
self.tts_runtime=TTSRuntime(self.catalog.root.parent/'tts-runtime')
self.tts_tests=TTSTests(self.catalog.root.parent/'tts-tests',self.profiles,self.tts_runtime)
self.profiles.tts_blockers=self.tts_tests.blockers
from pocket_runtime import PocketRuntime,supported as pocket_model
from pocket import Pocket
self.pocket_runtime=PocketRuntime(self.catalog.root.parent/'pocket-runtime')
self.pocket=Pocket(self.catalog.root.parent/'pocket-jobs',self.profiles,self.pocket_runtime)
from qwen_native_runtime import QwenNativeRuntime,supported as native_qwen_model
from qwen_native import QwenNative
self.qwen_native_runtime=QwenNativeRuntime(self.catalog.root.parent/'qwen-native-runtime')
self.qwen_native=QwenNative(self.catalog.root.parent/'qwen-native-jobs',self.profiles,self.qwen_native_runtime)
from articulino_runtime import ArticulinoRuntime,supported as articulino_model
from articulino import Articulino
self.articulino_runtime=ArticulinoRuntime(self.catalog.root.parent/'articulino-runtime')
self.articulino=Articulino(self.catalog.root.parent/'articulino-jobs',self.profiles,self.articulino_runtime)
from kikiri_runtime import KikiriRuntime,supported as kikiri_model
from kikiri import Kikiri
self.kikiri_runtime=KikiriRuntime(self.catalog.root.parent/'kikiri-runtime')
self.kikiri=Kikiri(self.catalog.root.parent/'kikiri-jobs',self.profiles,self.kikiri_runtime)
self.profiles.kikiri_runtime_ready=lambda:self.kikiri_runtime.status()['installed']
self.profiles.articulino_runtime_ready=lambda:self.articulino_runtime.status()['installed']
self.profiles.qwen_native_runtime_ready=lambda:self.qwen_native_runtime.status()['installed']
self.profiles.pocket_runtime_ready=lambda:self.pocket_runtime.status()['installed']
self.profiles.tts_blockers=lambda p:self.kikiri.blockers(p) if kikiri_model(p.get('model')) else self.articulino.blockers(p) if articulino_model(p.get('model')) else self.qwen_native.blockers(p) if native_qwen_model(p.get('model')) else self.pocket.blockers(p) if pocket_model(p.get('model')) else self.tts_tests.blockers(p)
self.docker = DockerSupport()
self.hardware = HardwareProvider()
self.started = time.time()
self.network = NetworkClient()
if os.environ.get('DECK_AUTH_RPC') == '1':
from network.rpc import request
self.credentials = CredentialStore(rpc=request)
else:
self.credentials = CredentialStore(Path(state_dir or os.environ.get('DECK_STATE_DIR', ROOT/'.state'))/'auth.json')
if os.environ.get('DECK_REQUIRE_SETUP') == '1' and self.credentials.read() is None:
self.server_close()
raise RuntimeError('Serverzugang muss vor dem Start eingerichtet werden.')
self.strata_runtime=StrataRuntime(self.catalog.root.parent/'strata-runtime',self.catalog)
self.basalt_runtime=BasaltRuntime(self.catalog.root.parent/'basalt-runtime',self.catalog)
self.worker=ChatWorkerRouter(LlamaWorker(self.catalog.root.parent/'llama-worker',self.catalog,self.runtime),StrataWorker(self.catalog.root.parent/'strata-worker',self.catalog,self.strata_runtime),BasaltWorker(self.catalog.root.parent/'basalt-worker',self.catalog,self.basalt_runtime))
self.scheduler=Scheduler(self.worker)
self.runtime.deletion_guard=lambda:"Modellwechsel läuft" if self.scheduler.transition else None
self.voxcpm_tests=VoxCPMTests(self.catalog.root.parent/'voice-tests',self.profiles,self.voxcpm_runtime)
self.voxcpm_tests.omni_runtime=self.omnivoice_runtime
self.voxcpm_tests.qwen_runtime=self.tts_runtime
self.profiles.qwen_clone_blockers=self.voxcpm_tests.blockers
self.profiles.qwen_clone_ready=lambda:self.tts_runtime.status()["installed"]
clone_marker=self.tts_runtime.root/"voice-profile-created"
if self.tts_runtime.status()["installed"] and not clone_marker.exists():
from profiles import qwen_clone_model,SCHEMAS
model_id=self.tts_runtime.ensure_voice_library()
if not any(p["kind"]=="voice" and qwen_clone_model(p.get("model")) for p in self.profiles.status()["profiles"]):
self.profiles.save(dict(id=None,revision=0,name="Qwen1.7B Stimmklonen",kind="voice",model_id=model_id,parameters={k:v[2] for k,v in SCHEMAS["voice"].items()}))
clone_marker.write_text("Qwen1.7B Base voice profile registered\n")
self.scheduler.evict_voice=self.voxcpm_tests.unload_idle
self.voxcpm_tests.acquire=lambda wait=False:self.scheduler.image_reservation(wait,kind='voice')
self.separator_runtime=SeparatorRuntime(self.catalog.root.parent/'separator-runtime')
self.separator_tests=SeparatorTests(self.catalog.root.parent/'separator-tests',self.separator_runtime,self.scheduler)
from embeddings import Embeddings
import tempfile
self.embeddings=Embeddings(tempfile.mkdtemp(prefix='deck-embeddings-'),self.catalog,self.worker)
self.profiles.embedding_blockers=self.embeddings.blockers
self.profiles.chat_blockers=self.worker.blockers
self.image_tests.acquire=self.scheduler.image_reservation
self.prompt_enhancer.acquire=self.scheduler.image_reservation
self.tts_tests.acquire=self.scheduler.tts_reservation
self.voxcpm_tests.shared_tts=self.tts_tests
self.scheduler.evict_tts=self.tts_tests.unload_idle
self.stt=STT(self.profiles,self.catalog,self.runtime)
self.stt.acquire=self.scheduler.stt_reservation
self.scheduler.evict_stt=self.stt.unload_gpu_idle
self.profiles.stt_blockers=self.stt.blockers
if self.stt.parakeet.status()['installed']:
marker=self.stt.parakeet.root/'profile-created'
if not marker.exists():
from parakeet_runtime import supported as redux_supported
from profiles import SCHEMAS
model_id=self.stt.parakeet.register(self.catalog)
if not any(p['kind']=='stt' and redux_supported(p.get('model')) for p in self.profiles.status()['profiles']):
self.profiles.save(dict(id=None,revision=0,name='Parakeet Redux CPU',kind='stt',model_id=model_id,parameters={k:v[2] for k,v in SCHEMAS['stt'].items()}))
marker.write_text('Redux STT profile registered\n')
self.voxcpm_tests.reference_transcriber=self.stt.transcribe_reference
self.profiles.stt_blockers=self.stt.blockers
self.endpoint=Endpoint(self.catalog.root.parent,self.profiles,self.worker,self.scheduler,self.image_tests,self.credentials,self.server_port)
self.endpoint.embeddings=self.embeddings
self.endpoint.tts=self.tts_tests
self.tts_tests.clone_owner_profile=lambda:next((p['id'] for p in self.endpoint.rows() if p['kind']=='audio' and p['enabled'] and not self.tts_tests.blockers(p)),None)
self.endpoint.pocket=self.pocket
self.endpoint.kikiri=self.kikiri
self.endpoint.articulino=self.articulino
self.endpoint.qwen_native=self.qwen_native
self.endpoint.voxcpm=self.voxcpm_tests
from humaneness_runtime import HumanenessRuntime,supported as humaneness_model
from humaneness import Humaneness
self.humaneness_runtime=HumanenessRuntime(self.catalog.root.parent/'humaneness-runtime')
self.humaneness=Humaneness(self.catalog.root.parent/'humaneness-jobs',self.humaneness_runtime,self.profiles,self.scheduler)
self.profiles.humaneness_blockers=self.humaneness.blockers;self.endpoint.humaneness=self.humaneness
model_id=self.humaneness_runtime.register(self.catalog)
marker=self.humaneness_runtime.root/'profile-created'
if model_id and not marker.exists():
from profiles import SCHEMAS
if not any(humaneness_model(p.get('model')) for p in self.profiles.status()['profiles']):
self.profiles.save(dict(id=None,revision=0,name='Humaneness Voice Conversion',api_id='humaneness-vc',kind='voice',model_id=model_id,parameters={k:v[2] for k,v in SCHEMAS['voice'].items()}))
marker.write_text('Humaneness profile registered; endpoint publication requires GUI\n')
from seedvc_runtime import SeedVCRuntime,supported as seedvc_model
from seedvc import SeedVC
self.seedvc_runtime=SeedVCRuntime(self.catalog.root.parent/'seedvc-runtime')
self.seedvc=SeedVC(self.catalog.root.parent/'seedvc-jobs',self.seedvc_runtime,self.profiles,self.scheduler)
self.seedvc.voice_path=self.qwen_native.voice_path
self.profiles.seedvc_blockers=self.seedvc.blockers;self.endpoint.seedvc=self.seedvc
model_id=self.seedvc_runtime.register(self.catalog)
marker=self.seedvc_runtime.root/'profile-created'
if model_id and not marker.exists():
from profiles import SCHEMAS
if not any(seedvc_model(p.get('model')) for p in self.profiles.status()['profiles']):
self.profiles.save(dict(id=None,revision=0,name='Seed-VC GPU',api_id='seed-vc',kind='voice',model_id=model_id,parameters={}))
marker.write_text('Seed-VC profile registered; endpoint publication requires GUI\n')
self.endpoint.stt=self.stt
from applio import Applio
self.applio=Applio(self.catalog.root.parent/'applio-data',self.applio_runtime,self.scheduler,self.profiles,lambda:self.endpoint.config['enabled_profiles'])
self.profiles.applio_blockers=self.applio.blockers
self.endpoint.applio=self.applio
from xvc import XVC
self.xvc=XVC(self.catalog.root.parent/'xvc-data',self.xvc_runtime,self.scheduler,self.profiles,lambda:self.endpoint.config['enabled_profiles'])
self.profiles.xvc_blockers=self.xvc.blockers;self.endpoint.xvc=self.xvc
self.xvc.ensure()
from soulx_singer import SoulXSinger
self.soulx_singer=SoulXSinger(self.catalog.root.parent/'soulx_singer-data',self.soulx_singer_runtime,self.scheduler,self.profiles,lambda:self.endpoint.config['enabled_profiles'])
self.profiles.soulx_singer_blockers=self.soulx_singer.blockers
self.endpoint.soulx_singer=self.soulx_singer
self.soulx_singer.ensure()
def evict_voice():
self.voxcpm_tests.unload_idle();self.applio.unload_idle();self.xvc.unload_idle();self.soulx_singer.unload_idle()
self.scheduler.evict_voice=evict_voice
self.applio.ensure()
self.chat_tests=ChatTests(self.profiles,self.worker,self.scheduler)
self.auto_tests=AutoTests(self.catalog.root.parent/'auto-tests.json',self.profiles,self.worker,self.scheduler)
def stop_gpu_work():
self.seedvc.stop();self.humaneness.stop();self.kikiri.stop();self.articulino.stop();self.qwen_native.stop();self.pocket.stop();self.strata_runtime.stop();self.remix.stop();self.voxcpm_tests.stop();self.separator_tests.stop();self.music.close();self.auto_tests.stop();self.chat_tests.stop();self.image_tests.stop();self.prompt_enhancer.stop_preview();self.tts_tests.stop();self.worker.stop()
from video_original import VideoRuntimes
from ltx_original_runtime import LTXOriginalRuntime
from audio_cpp_runtime import AudioCppRuntime
self.audio_cpp_runtime=AudioCppRuntime(self.catalog.root.parent/'audio-cpp-runtime')
self.profiles.music_runtime_ready=lambda:self.audio_cpp_runtime.status()['installed']
from music import Music
from acestep_runtime import AceStepRuntime
from acestep import AceStep,MusicRouter
self.acestep_runtime=AceStepRuntime(self.catalog.root.parent/'acestep-runtime')
self.profiles.acestep_runtime_ready=lambda:self.acestep_runtime.status()['installed']
self.music=MusicRouter(Music(self.catalog.root.parent/'music-jobs',self.profiles,self.catalog,self.audio_cpp_runtime,self.scheduler),AceStep(self.catalog.root.parent/'acestep-jobs',self.profiles,self.catalog,self.acestep_runtime,self.scheduler))
self.profiles.music_blockers=self.music.blockers
self.endpoint.music=self.music
from remix_runtime import RemixRuntime
from remix import Remix
self.remix_runtime=RemixRuntime(self.catalog.root.parent/'music-remix-runtime',self.separator_runtime)
self.remix=Remix(self.catalog.root.parent/'music-remix-jobs',self.remix_runtime,self.music,self.scheduler)
from profiles import audio_package_recipe
self.profiles.runtime_recipe=lambda model:audio_package_recipe(model,self.audio_cpp_runtime.paths()[1])
self.ltx_original_runtime=LTXOriginalRuntime(self.catalog.root.parent/'ltx-original-runtime',self.image_runtime)
self.video=VideoRuntimes(self.scheduler,stop_gpu_work,self.docker,self.catalog.root.parent/'video',self.catalog,self.profiles,self.image_runtime,original_runtime=self.ltx_original_runtime)
self.endpoint.video=self.video
self.video.separator_status=lambda:dict(self.separator_runtime.status(),worker=self.separator_tests.status())
self.video.voice_profiles=lambda:[dict(id=p['id'],name=p['name']) for p in self.endpoint.rows() if p['kind']=='voice' and p['enabled'] and p['runnable']]
self.video.music_profiles=lambda:[dict(id=p['id'],name=p['name'],family='ACE-Step' if p.get('model',{}).get('repo')=='ACE-Step/acestep-v15-xl-sft' else 'YuE2') for p in self.endpoint.rows() if p['kind']=='music' and p['enabled'] and p['runnable']]
self.profiles.video_runtime_ready=lambda:self.image_runtime.status().get("installed",False)
from dashboard_data import Dashboard
self.dashboard=Dashboard(self,self.catalog.root.parent)
self.endpoint.speed_telemetry=self.dashboard.speed_snapshot
if self.endpoint.config['autostart']:
try:self.endpoint.start()
except ValueError as exc:self.endpoint.error=str(exc)
self.audio_policy=AudioPolicy(self.catalog.root.parent/'audio-policy.json',self.profiles,self.tts_tests,self.stt,self.scheduler,self.endpoint,pocket=self.pocket,qwen_native=self.qwen_native,articulino=self.articulino,kikiri=self.kikiri)
self.sessions = {}
self.login_attempts = []
self.auth_lock = threading.Lock()
def video_browser_auth(headers):
from http.cookies import SimpleCookie
try:
cookies=SimpleCookie();cookies.load(headers.get('Cookie',''));token=cookies['deck_session'].value
except (KeyError,ValueError):return False
record=self.credentials.read()
with self.auth_lock:
session=self.sessions.get(token)
return bool(record and session and session['expires']>time.monotonic() and secrets.compare_digest(session['revision'],record['password']['hash']))
self.endpoint.video_browser_auth=video_browser_auth
from backup import Backup
self.backup=Backup(self)
# Explicit one-time request created during an authorized SoulX activation.
activate=self.catalog.root.parent/'soulx_singer-activate.json'
if activate.is_file():
request=json.loads(activate.read_text());activate.unlink()
def activate_soulx():
result={}
try:
row=next(p for p in self.profiles.status()['profiles'] if p['id']==request['profile_id'])
from soulx_singer import soulx_singer_model
if not soulx_singer_model(row.get('model')):raise ValueError('Kein SoulX-Profil.')
deadline=time.monotonic()+45
while True:
try:
self.endpoint.enable({'id':row['id'],'enabled':True});break
except ValueError as exc:
if 'Worker konnte nicht entladen' not in str(exc) or time.monotonic()>deadline:raise
time.sleep(.5)
self.video.switch('voice')
deadline=time.monotonic()+90
while self.scheduler.gpu_mode=='switching' and time.monotonic()<deadline:time.sleep(.2)
if self.scheduler.gpu_mode!='voice':raise ValueError('Stimmwerkzeuge konnten nicht gestartet werden.')
import urllib.request
deadline=time.monotonic()+45
while True:
try:
health=urllib.request.Request('http://127.0.0.1:8139/health',headers={'X-SoulX-Singer-Bridge':self.soulx_singer.key})
with urllib.request.urlopen(health,timeout=2):break
except OSError:
if time.monotonic()>deadline:raise ValueError('SoulX-Oberfläche nicht bereit.')
time.sleep(.5)
req=urllib.request.Request('http://127.0.0.1:8139/load',data=b'{}',headers={'X-SoulX-Singer-Bridge':self.soulx_singer.key,'Content-Type':'application/json'})
with urllib.request.urlopen(req,timeout=120) as response:result=json.load(response)
except Exception as exc:result={'error':str(exc)};self.endpoint.error='SoulX-Aktivierung fehlgeschlagen: '+str(exc)
(self.soulx_singer.root/'activation.json').write_text(json.dumps(result))
threading.Thread(target=activate_soulx,daemon=True).start()
# A one-time, local maintenance request restores music after an authorized
# update. No credentials or song prompts are stored in this request.
preload=self.catalog.root.parent/'music-preload.json'
if preload.is_file():
request=json.loads(preload.read_text());preload.unlink()
def preload_music():
try:
self.video.switch('music')
deadline=time.monotonic()+90
while self.scheduler.gpu_mode=='switching' and time.monotonic()<deadline:time.sleep(.2)
self.music.load(request['profile_id'],wait=True)
except (ValueError,KeyError) as exc:self.endpoint.error='Musik vorladen fehlgeschlagen: '+str(exc)
threading.Thread(target=preload_music,daemon=True).start()
def server_close(self):
if getattr(self,'basalt_runtime',None):self.basalt_runtime.stop()
if getattr(self,'seedvc',None):self.seedvc.stop()
if getattr(self,'humaneness',None):self.humaneness.stop()
if getattr(self,'soulx_singer',None):self.soulx_singer.stop()
if getattr(self,'xvc',None):self.xvc.stop()
if getattr(self,'applio',None):self.applio.stop()
super().server_close()
class Handler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def respond(self, data, status=200, mime='application/json'):
body = json.dumps(data).encode() if mime == 'application/json' else data
self.send_response(status)
self.send_header('Content-Type', mime)
self.send_header('Content-Length', str(len(body)))
self.send_header('Cache-Control', 'no-store')
self.send_header('X-Content-Type-Options', 'nosniff')
policy="default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'none'"
if self.path.startswith('/stimmatelier/'):policy+="; media-src 'self' blob:"
self.send_header('Content-Security-Policy',policy)
self.end_headers()
self.wfile.write(body)
def ingress(self):
token = os.environ.get('DECK_PROXY_TOKEN', '')
if os.environ.get('DECK_PROXY_TOKEN_FILE'):
try:token=Path(os.environ['DECK_PROXY_TOKEN_FILE']).read_text().strip()
except OSError:pass
if token and secrets.compare_digest(self.headers.get('X-Deck-Proxy', ''), token):
return self.headers.get('X-Deck-Ingress', 'management')
return 'management'
def api_token_authenticated(self):
header = self.headers.get('Authorization', '')
if not header.startswith('Bearer ') or len(header)>300:
return False
record = self.server.credentials.read()
if not record or not record['api_token_hash']:
return False
actual = hashlib.sha256(header[7:].encode()).hexdigest()
return secrets.compare_digest(actual,record['api_token_hash'])
def token_route(self):
return (self.command == 'GET' and (self.path in ('/api/v1/status','/api/v1/hardware','/api/v1/separator-runtime','/api/v1/separator-tests','/api/v1/voice','/api/v1/voice/profiles','/api/v1/music-remix') or urlsplit(self.path).path in ('/api/v1/separator-tests/audio','/api/v1/voice/audio'))) or (self.command=='POST' and self.path in ('/api/v1/separator-jobs/start','/api/v1/separator-chat/start','/api/v1/separator-tests/cancel','/api/v1/voice/start','/api/v1/voice/cancel','/api/v1/voice/select','/api/v1/music-remix/transcribe'))
def session_token(self):
try:
return SimpleCookie(self.headers.get('Cookie',''))['deck_session'].value
except (KeyError, ValueError, CookieError):
return None
def authenticated(self, session_only=False):
if self.headers.get('Authorization'):
return not session_only and self.token_route() and self.api_token_authenticated()
record = self.server.credentials.read()
if not record:
return False
with self.server.auth_lock:
session = self.server.sessions.get(self.session_token())
return bool(session and session['expires']>time.monotonic() and secrets.compare_digest(session['revision'],record['password']['hash']))
def allow_password_attempt(self):
with self.server.auth_lock:
now = time.monotonic()
self.server.login_attempts = [t for t in self.server.login_attempts if now-t<60]
if len(self.server.login_attempts)>=10:
self.respond({'error':'Zu viele Versuche. Bitte eine Minute warten.'},429)
return False
self.server.login_attempts.append(now)
return True
def read_json(self):
self.connection.settimeout(10)
if self.headers.get('Transfer-Encoding'):
raise ValueError('Chunked Uploads werden nicht unterstützt.')
length = int(self.headers.get('Content-Length', '0'))
if not 0 < length <= 32768 or self.headers.get('Content-Type') != 'application/json':
raise ValueError('JSON-Anfrage erwartet, maximal 32 KiB.')
raw = self.rfile.read(length)
try:
value = json.loads(raw)
except (ValueError, UnicodeDecodeError):
raise ValueError('Ungültige JSON-Anfrage.') from None
if not isinstance(value, dict):
raise ValueError('JSON-Objekt erwartet.')
return value
def login(self):
if not self.allow_password_attempt():
return
try:
data = self.read_json()
with self.server.credentials.lock:
record = self.server.credentials.read()
if not record or not verify_password(data.get('password'),record['password']):
return self.respond({'error':'Anmeldung fehlgeschlagen.'},401)
token = secrets.token_urlsafe(32)
now = time.monotonic()
with self.server.auth_lock:
self.server.sessions = {k:v for k,v in self.server.sessions.items() if v['expires']>now}
if len(self.server.sessions)>=64:
self.server.sessions.pop(next(iter(self.server.sessions)))
self.server.sessions[token] = dict(expires=now+28800,revision=record['password']['hash'])
body = b'{"authenticated":true}'
self.send_response(200)
self.send_header('Content-Type','application/json')
self.send_header('Cache-Control','no-store')
self.send_header('Content-Length',str(len(body)))
self.send_header('Set-Cookie',f'deck_session={token}; HttpOnly; SameSite=Strict; Path=/; Max-Age=28800')
self.end_headers()
self.wfile.write(body)
except (ValueError, OSError):
self.respond({'error':'Ungültige Anmeldung.'},400)
def access_status(self):
record = self.server.credentials.read()
signed_in = self.authenticated(session_only=True)
value = dict(initialized=record is not None, authenticated=signed_in)
if signed_in:
value.update(api_token_configured=bool(record['api_token_hash']), password_changed_at=record['password_changed_at'], token_changed_at=record['token_changed_at'], account='Administrator', location=os.environ.get('DECK_LOCATION', 'Athena · Debian-Server'))
return self.respond(value)
def change_credentials(self, kind):
if not self.allow_password_attempt():
return
try:
data = self.read_json()
key = 'new_password' if kind == 'password' else 'new_token'
if set(data) != {'current_password',key}:
raise ValueError('Ungültige Zugangsdatenanfrage.')
self.server.credentials.change(kind,data['current_password'],data[key])
if kind == 'password':
with self.server.auth_lock:
self.server.sessions.clear()
return self.respond({'changed':True,'login_required':kind=='password'})
except ValueError as exc:
return self.respond({'error':str(exc)},400)
except OSError:
return self.respond({'error':'Zugangsdaten konnten nicht gespeichert werden.'},503)
def valid_host(self):
if os.environ.get('DECK_PROXY_TOKEN'):
return self.ingress() in ('lan','tunnel')
if os.environ.get('DECK_PROXY_TOKEN_FILE') and self.ingress() in ('lan','tunnel'):
return True
allowed = {f'127.0.0.1:{self.server.server_port}', f'localhost:{self.server.server_port}'}
allowed.update(filter(None,os.environ.get('DECK_ALLOWED_HOSTS','').split(',')))
return self.headers.get('Host') in allowed
def execution_runtime_snapshot(self):
# Installation checks need no video model/profile scan or worker status.
chat = bool(self.server.runtime.status().get('active'))
video_runtime = (self.server.video.original_runtime
if self.server.video.runtime_kind() == 'original'
else self.server.video.runtime)
return {'embeddings':chat,'stt':chat,'chat':chat,'basalt':self.server.basalt_runtime.status()['installed'],
'image':self.server.image_runtime.status().get('installed',False),
'seedvc':self.server.seedvc_runtime.status()['installed'],'humaneness':self.server.humaneness_runtime.status()['installed'],'kikiri':self.server.kikiri_runtime.status()['installed'],'articulino':self.server.articulino_runtime.status()['installed'],'qwen_native':self.server.qwen_native_runtime.status()['installed'],'pocket':self.server.pocket_runtime.status()['installed'],'audio':self.server.tts_runtime.status().get('installed',False),'parakeet':self.server.stt.parakeet.status()['installed'],
'video':video_runtime.status().get('installed',False),
'acestep':self.server.acestep_runtime.status()['installed'],
'audio_cpp':self.server.audio_cpp_runtime.status()['installed'],
'soulx_singer':self.server.soulx_singer_runtime.status()['installed'],'xvc':self.server.xvc_runtime.status()['installed'],'applio':self.server.applio_runtime.status()['installed'],'omnivoice':self.server.omnivoice_runtime.status()['installed'],'voxcpm':self.server.voxcpm_runtime.status()['installed'],'music_worker':True,'separator':self.server.separator_runtime.status()['installed']}
def execution_setup(self, model, profile=None, installed=None):
return execution_assess(model, self.execution_runtime_snapshot() if installed is None else installed, profile)
def do_GET(self):
if not self.valid_host():
return self.respond({'error': 'Host rejected'}, 403)
if self.path == '/api/v1/auth/status':
return self.access_status()
if self.path == '/login.js':
return self.respond((ROOT/'login.js').read_bytes(), mime='text/javascript')
if not self.authenticated() and self.path in ('/', '/stimmatelier/'):
# Serve the normal login at the studio entry too. Its successful
# reload returns to the same page; APIs/assets remain protected.
return self.respond((ROOT/'login.html').read_bytes(), mime='text/html; charset=utf-8')
if not self.authenticated() and self.path not in ('/', '/style.css'):
return self.respond({'error':'Anmeldung erforderlich.'},401)
if self.path.startswith('/stimmatelier/'):
try:
from stimmatelier_proxy import asset
body,mime=asset(self.path,self.server.catalog.root.parent)
return self.respond(body,mime=mime)
except (OSError,ValueError):return self.respond({'error':'Stimmatelier nicht erreichbar. Den Dienst unter Weitere Dienste starten.'},503)
routes = {'/strata-ui.js':('strata-ui.js','text/javascript'),'/soulx-singer-ui.js':('soulx-singer-ui.js','text/javascript'),'/xvc-ui.js':('xvc-ui.js','text/javascript'),'/applio-ui.js':('applio-ui.js','text/javascript'),'/acestep-ui.js':('acestep-ui.js','text/javascript'),'/voice-ui.js':('voice-ui.js','text/javascript'),'/voxcpm-ui.js':('voxcpm-ui.js','text/javascript'),'/separator-ui.js':('separator-ui.js','text/javascript'),'/music-ui.js':('music-ui.js','text/javascript'),'/audio-cpp-ui.js':('audio-cpp-ui.js','text/javascript'),'/ltx-original-ui.js':('ltx-original-ui.js','text/javascript'),'/backup-ui.js':('backup-ui.js','text/javascript'),'/dashboard-ui.js':('dashboard-ui.js','text/javascript'),'/dashboard.css':('dashboard.css','text/css'),'/themes.css':('themes.css','text/css'),'/video-ui.js':('video-ui.js','text/javascript'),'/stt-ui.js':('stt-ui.js','text/javascript'),'/tts-ui.js':('tts-ui.js','text/javascript'),'/auto-test-ui.js':('auto-test-ui.js','text/javascript'),'/chat-test-ui.js':('chat-test-ui.js','text/javascript'),'/endpoint-ui.js':('endpoint-ui.js','text/javascript'),'/docker-ui.js': ('docker-ui.js','text/javascript'), '/': ('index.html', 'text/html; charset=utf-8'), '/app.js': ('app.js', 'text/javascript'), '/style.css': ('style.css', 'text/css'), '/network-ui.js': ('network-ui.js', 'text/javascript'), '/access-ui.js': ('access-ui.js', 'text/javascript'), '/studio.js': ('studio.js', 'text/javascript'), '/catalog-ui.js': ('catalog-ui.js','text/javascript'), '/runtime-ui.js': ('runtime-ui.js','text/javascript'), '/profiles-ui.js': ('profiles-ui.js','text/javascript'), '/image-test-ui.js': ('image-test-ui.js','text/javascript')}
if self.path in routes:
name, mime = routes[self.path]
return self.respond((ROOT/name).read_bytes(), mime=mime)
if self.path == '/api/v1/status':
endpoint=self.server.endpoint.status()
public_endpoint={key:endpoint[key] for key in ('state','port','counts','active_requests')}
return self.respond(dict(name='Athena Deck', version='0.7.0', state='ready', uptime_seconds=round(time.time()-self.server.started), mode='isolated', location=os.environ.get('DECK_LOCATION', 'Athena · Debian-Server'), endpoint=public_endpoint))
if self.path == '/api/v1/auto-tests':return self.respond(self.server.auto_tests.status())
if self.path == '/api/v1/backup':return self.respond(self.server.backup.status())
if self.path == '/api/v1/chat-tests':return self.respond(self.server.chat_tests.status())
if self.path == '/api/v1/endpoint':return self.respond(self.server.endpoint.status())
if self.path == '/api/v1/docker':return self.respond(self.server.docker.status())
if self.path == '/api/v1/stt':return self.respond(self.server.stt.status())
if self.path == '/api/v1/voice/profiles':return self.respond({'profiles':[dict(id=p['id'],name=p['name'],repo=p['model']['repo'],enabled=p['enabled'],runnable=p['runnable']) for p in self.server.endpoint.rows() if p['kind']=='voice' and p.get('model')]})
if self.path == '/api/v1/seed-vc':return self.respond(self.server.seedvc.status())
if urlsplit(self.path).path == '/api/v1/seed-vc/audio':
try:return self.respond(self.server.seedvc.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except (OSError,ValueError) as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/conversion':return self.respond(self.server.humaneness.status())
if urlsplit(self.path).path == '/api/v1/conversion/audio':
try:return self.respond(self.server.humaneness.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except (OSError,ValueError) as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/voice':return self.respond(dict(self.server.voxcpm_tests.status(),mode=self.server.scheduler.gpu_mode))
if urlsplit(self.path).path == '/api/v1/voice/audio':
try:return self.respond(self.server.voxcpm_tests.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/tts':return self.respond(self.server.tts_tests.status())
if self.path == '/api/v1/pocket':return self.respond(self.server.pocket.status())
if self.path == '/api/v1/kikiri':return self.respond(self.server.kikiri.status())
if urlsplit(self.path).path == '/api/v1/kikiri/audio':
try:return self.respond(self.server.kikiri.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/articulino':return self.respond(self.server.articulino.status())
if urlsplit(self.path).path == '/api/v1/articulino/audio':
try:return self.respond(self.server.articulino.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/qwen-native':return self.respond(self.server.qwen_native.status())
if urlsplit(self.path).path == '/api/v1/qwen-native/audio':
try:return self.respond(self.server.qwen_native.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except (ValueError,IndexError) as exc:return self.respond({'error':str(exc)},400)
if urlsplit(self.path).path == '/api/v1/pocket/audio':
try:return self.respond(self.server.pocket.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except ValueError as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/audio-policy':return self.respond(self.server.audio_policy.status())
if urlsplit(self.path).path == '/api/v1/tts/audio':
try:return self.respond(self.server.tts_tests.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except (OSError,ValueError):return self.respond({'error':'Audio nicht verfügbar.'},404)
if self.path == '/api/v1/music-remix':return self.respond(self.server.remix.status())
if self.path == '/api/v1/music':return self.respond(self.server.music.status())
if urlsplit(self.path).path == '/api/v1/music/audio':
try:return self.respond(self.server.music.audio(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='audio/wav')
except (OSError,ValueError):return self.respond({'error':'Musik nicht verfügbar.'},404)
if self.path == '/api/v1/xvc':return self.respond(self.server.xvc.status())
if self.path == '/api/v1/xvc-runtime':return self.respond(self.server.xvc_runtime.status())
if self.path == '/api/v1/xvc-runtime/log':return self.respond(self.server.xvc_runtime.log())
if self.path == '/api/v1/soulx-singer':return self.respond(self.server.soulx_singer.status())
if self.path == '/api/v1/soulx-singer-runtime':return self.respond(self.server.soulx_singer_runtime.status())
if self.path == '/api/v1/soulx-singer-runtime/log':return self.respond(self.server.soulx_singer_runtime.log())
if self.path == '/api/v1/applio':return self.respond(self.server.applio.status())
if self.path == '/api/v1/applio-runtime':return self.respond(self.server.applio_runtime.status())
if self.path == '/api/v1/applio-runtime/log':return self.respond(self.server.applio_runtime.log())
if self.path == '/api/v1/omnivoice-runtime':return self.respond(self.server.omnivoice_runtime.status())
if self.path == '/api/v1/omnivoice-runtime/log':return self.respond(self.server.omnivoice_runtime.log())
if self.path == '/api/v1/voxcpm-runtime':return self.respond(self.server.voxcpm_runtime.status())
if self.path == '/api/v1/voxcpm-runtime/log':return self.respond(self.server.voxcpm_runtime.log())
if self.path == '/api/v1/separator-runtime':return self.respond(self.server.separator_runtime.status())
if self.path == '/api/v1/separator-runtime/log':return self.respond(self.server.separator_runtime.log())
if self.path == '/api/v1/separator-tests':return self.respond(self.server.separator_tests.status())
if urlsplit(self.path).path == '/api/v1/separator-tests/audio':
try:
query=parse_qs(urlsplit(self.path).query);return self.respond(self.server.separator_tests.audio(query.get('id',[''])[0],int(query.get('index',['-1'])[0])),mime='audio/wav')
except (ValueError,OSError):return self.respond({'error':'Spur nicht verfügbar.'},404)
if self.path == '/api/v1/acestep-runtime':return self.respond(self.server.acestep_runtime.status())
if self.path == '/api/v1/acestep-runtime/log':return self.respond(self.server.acestep_runtime.log())
if self.path == '/api/v1/audio-cpp-runtime':return self.respond(self.server.audio_cpp_runtime.status())
if self.path == '/api/v1/audio-cpp-runtime/log':return self.respond(self.server.audio_cpp_runtime.log())
if self.path == '/api/v1/ltx-original-runtime':return self.respond(self.server.ltx_original_runtime.status())
if self.path == '/api/v1/ltx-original-runtime/log':return self.respond(self.server.ltx_original_runtime.log())
if self.path == '/api/v1/video':return self.respond(self.server.video.status())
if self.path == '/api/v1/image-runtime':return self.respond(self.server.image_runtime.status())
if self.path == '/api/v1/image-tests':return self.respond(self.server.image_tests.status())
if self.path == '/api/v1/prompt-enhancers':return self.respond(self.server.prompt_enhancer.status())
if urlsplit(self.path).path == '/api/v1/image-tests/image':
try:return self.respond(self.server.image_tests.image(parse_qs(urlsplit(self.path).query).get('id',[''])[0]),mime='image/png')
except (OSError,ValueError):return self.respond({'error':'Bild nicht verfügbar.'},404)
if urlsplit(self.path).path == '/api/v1/profiles/components':
try:return self.respond(self.server.profiles.components(parse_qs(urlsplit(self.path).query).get('model_id',[''])[0],parse_qs(urlsplit(self.path).query).get('repo',[None])[0]))
except ValueError as exc:return self.respond({'error':str(exc)},400)
except Exception:return self.respond({'error':'Komponentenquelle nicht erreichbar. Bitte erneut versuchen.'},503)
if self.path == '/api/v1/profiles':
data=self.server.profiles.status()
installed=self.execution_runtime_snapshot()
for p in data['profiles']:p['execution']=self.execution_setup(p.get('model') or {},p,installed)
return self.respond(data)
if self.path == '/api/v1/huggingface':return self.respond(self.server.catalog.hub_auth.status())
if self.path == '/api/v1/hardware':
return self.respond(self.server.hardware.snapshot())
if self.path == '/api/v1/dashboard':return self.respond(self.server.dashboard.snapshot())
if urlsplit(self.path).path == '/api/v1/dashboard/history':
period=parse_qs(urlsplit(self.path).query).get('range',['24h'])[0]
return self.respond(self.server.dashboard.history.query(period))
if self.path == '/api/v1/dashboard/backups':return self.respond(self.server.dashboard.backups())
if self.path.startswith('/api/v1/dashboard/backups/download/'):
try:path=self.server.dashboard.backup_file(unquote(self.path.rsplit('/',1)[-1]))
except ValueError:return self.respond({'error':'Backup nicht verfügbar.'},404)
self.send_response(200)
self.send_header('Content-Type','application/octet-stream')
self.send_header('Content-Length',str(path.stat().st_size))
self.send_header('Content-Disposition','attachment; filename="'+path.name+'"')
self.send_header('Cache-Control','no-store')
self.send_header('X-Content-Type-Options','nosniff')
self.end_headers()
with path.open('rb') as stream:
while chunk:=stream.read(1024*1024):self.wfile.write(chunk)
return
if self.path in ('/api/v1/basalt-runtime','/api/v1/basalt-runtime/log'):
return self.respond(self.server.basalt_runtime.log() if self.path.endswith('/log') else self.server.basalt_runtime.status())
if self.path in ('/api/v1/strata-runtime','/api/v1/strata-runtime/log'):
return self.respond(self.server.strata_runtime.log() if self.path.endswith('/log') else self.server.strata_runtime.status())
if self.path.startswith('/api/v1/runtime'):
actions={'/api/v1/runtime':self.server.runtime.status,'/api/v1/runtime/prerequisites':self.server.runtime.prerequisites,'/api/v1/runtime/releases':self.server.runtime.releases,'/api/v1/runtime/log':self.server.runtime.log,'/api/v1/runtime/references':self.server.runtime.references}
if self.path in actions:
try:return self.respond(actions[self.path]())
except Exception:return self.respond({'error':'Laufzeitabfrage fehlgeschlagen; Verbindung oder Werkzeuge prüfen.'},503)
if self.path.startswith('/api/v1/catalog'):
try:
path=urlsplit(self.path);q=parse_qs(path.query)
if path.path=='/api/v1/catalog/search':
base=q.get('base_only',['false'])[0]
if base not in ('true','false'):raise ValueError('Ungültiger Basismodellfilter.')
return self.respond(self.server.catalog.search(q.get('q',[''])[0],q.get('kind',['chat'])[0],q.get('sort',['downloads'])[0],base=='true',q.get('purpose',['model'])[0]))
if path.path=='/api/v1/catalog/assessment':
data=self.server.catalog.files(q.get('repo',[''])[0])
return self.respond(overview(data['files'],self.server.hardware.snapshot()))
if path.path=='/api/v1/catalog/files':
data=dict(self.server.catalog.files(q.get('repo',[''])[0]));hardware=self.server.hardware.snapshot()
installed=self.execution_runtime_snapshot()
data['files']=[dict(f,assessment=assess(f['size'],hardware,f['name']),execution=self.execution_setup(dict(repo=data['repo'],revision=data['revision'],file=f['name'],kind=q.get('kind',['chat'])[0],runtime_metadata=data.get('runtime_metadata',{})),installed=installed)) for f in data['files']]
from profiles import component_recipe
from catalog import file_role
for f in data['files']:
model=dict(repo=data['repo'],file=f['name'],kind=q.get('kind',['chat'])[0])
recipe=component_recipe(model) or self.server.profiles.runtime_recipe(model)
if recipe:f['execution']['components']=[r['label'] for r in recipe.values()]
elif f['execution']['state']!='component':f['execution']['suggested_components']=[x['name'] for x in data['files'] if file_role(x['name'])['role'] in ('text_encoder','vae','vision_projector','audio_projector')][:12]
return self.respond(data)
if path.path=='/api/v1/catalog':
data=self.server.catalog.status()
installed=self.execution_runtime_snapshot()
for entry in data['entries']:
entry['execution']=self.execution_setup(entry,installed=installed)
entry['used_by_profiles']=self.server.profiles.references(entry['id'])
return self.respond(data)
except Exception as exc:
return self.respond({'error':str(exc) if isinstance(exc,ValueError) else 'Katalog nicht erreichbar.'},400)
if self.path == '/api/v1/network':
return self.respond(self.server.network.status(self.ingress()))
return self.respond({'error': 'Not found'}, 404)
def do_POST(self):
origin = self.headers.get('Origin')
bearer = self.token_route() and self.api_token_authenticated()
# The authenticated native GUI proxy validates Host before forwarding.
# TLS may terminate at an upstream reverse proxy; never trust arbitrary
# X-Forwarded-Proto headers to decide which browser origins are allowed.
origins = {'http://' + self.headers.get('Host', '')}
if self.ingress() in ('lan', 'tunnel'):
origins.add('https://' + self.headers.get('Host', ''))
if not self.valid_host() or (origin and origin not in origins) or (not bearer and self.headers.get('X-Athena-Deck') != '1'):
return self.respond({'error':'Same-origin control required'},403)
if self.path == '/api/v1/auth/setup':
try:
data = self.read_json()
if set(data) != {'password','api_token'}:
raise ValueError('Kennwort und API-Token werden benötigt.')
# Container installs always arrive provisioned. Never offer remote claim-on-first-use.
if os.environ.get('DECK_AUTH_RPC') or os.environ.get('DECK_REQUIRE_SETUP') == '1':
return self.respond({'error':'Serverzugang wird bei der Installation eingerichtet.'},403)
self.server.credentials.setup(data['password'],data['api_token'])
return self.respond({'initialized':True})
except ValueError as exc:
return self.respond({'error':str(exc)},400)
except OSError:
return self.respond({'error':'Einrichtung konnte nicht gespeichert werden.'},503)
if self.path == '/api/v1/login':
return self.login()
if not self.authenticated():
return self.respond({'error':'Anmeldung oder gültiger API-Token erforderlich.'},401)
if self.server.backup.busy() and not self.path.startswith('/api/v1/backup/') and self.path != '/api/v1/logout':
return self.respond({'error':'Wiederherstellung läuft. Änderungen und Tests sind vorübergehend gesperrt.'},409)
if self.path.startswith('/api/v1/backup/'):
try:
if not self.authenticated(session_only=True):return self.respond({'error':'Administratorsitzung erforderlich.'},401)
action=self.path.rsplit('/',1)[1]
if action=='export':
data=self.read_json()
if set(data)!={'password','theme','history'} or type(data['history']) is not bool:raise ValueError('Backup-Kennwort, Theme und History-Auswahl erforderlich.')
body=self.server.backup.export(data['password'],data['theme'],data['history'])
self.send_response(200);self.send_header('Content-Type','application/octet-stream');self.send_header('Content-Length',str(len(body)));self.send_header('Content-Disposition','attachment; filename="athena-deck-'+time.strftime('%Y%m%d-%H%M%S')+'.adbackup"');self.send_header('Cache-Control','no-store');self.end_headers();self.wfile.write(body);return
if action=='inspect':
from backup_codec import MAX
self.connection.settimeout(60)
length=int(self.headers.get('Content-Length','0'))
if self.headers.get('Transfer-Encoding') or self.headers.get('Content-Type')!='application/octet-stream' or not 0<length<=MAX:raise ValueError('Backup-Datei erwartet; maximal 64 MiB.')
raw=self.rfile.read(length)
if len(raw)!=length:raise ValueError('Backup-Upload unvollständig.')
return self.respond(self.server.backup.inspect(raw,__import__('base64').b64decode(self.headers.get('X-Backup-Passphrase',''),validate=True).decode('utf-8')))
if action=='checkpoint':
data=self.read_json()
if set(data)!={'name'}:raise ValueError('Rückfallstand erforderlich.')
return self.respond(self.server.backup.inspect_checkpoint(data['name']))
if action=='restore':return self.respond(self.server.backup.start(self.read_json()),202)
if action=='cancel':
if self.read_json():raise ValueError('Keine Parameter erwartet.')
return self.respond(self.server.backup.stop())
raise ValueError('Unbekannte Backup-Aktion.')
except ValueError as exc:return self.respond({'error':str(exc)},400)
except Exception:return self.respond({'error':'Backup-Aktion fehlgeschlagen. Dateiformat, freien Speicher und Systemhelfer prüfen.'},503)
if self.path == '/api/v1/audio-policy':
try:return self.respond(self.server.audio_policy.configure(self.read_json()))
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Audio-Einstellung konnte nicht gespeichert werden.'},503)
if self.path == '/api/v1/huggingface':
try:
data=self.read_json()
if set(data)!={'token'}:raise ValueError('Ungültige Hugging-Face-Einstellung.')
return self.respond(self.server.catalog.hub_credentials(data['token']))
except ValueError as exc:return self.respond({'error':str(exc)},400)
except Exception:return self.respond({'error':'Hugging-Face-Zugang konnte nicht gespeichert werden.'},503)
if self.path in ('/api/v1/auth/password','/api/v1/auth/token'):
return self.change_credentials('password' if self.path.endswith('/password') else 'token')
if self.path == '/api/v1/logout':
with self.server.auth_lock:
self.server.sessions.pop(self.session_token(),None)
return self.respond({'logged_out':True})
if self.path in ('/api/v1/docker/start','/api/v1/docker/stop','/api/v1/docker/delete'):
try:
data=self.read_json();action=self.path.rsplit('/',1)[1]
expected={'id','confirm'} if action=='delete' else {'id'}
if set(data)!=expected or (action=='delete' and data['confirm'] is not True):raise ValueError('Ungültige oder unbestätigte Containeraktion.')
return self.respond(self.server.docker.container_action(action,data['id']))
except (OSError,ValueError) as exc:return self.respond({'error':str(exc) if isinstance(exc,ValueError) else 'Docker-Systemhelfer nicht erreichbar.'},400)
if self.path == '/api/v1/docker/install':
try:
if self.read_json()!={'confirm':True}:raise ValueError('Die Docker-Erstinstallation muss ausdrücklich bestätigt werden.')
return self.respond(self.server.docker.install())
except (OSError,ValueError) as exc:return self.respond({'error':str(exc) if isinstance(exc,ValueError) else 'Docker-Systemhelfer nicht erreichbar.'},400)
if self.path in ('/api/v1/basalt-runtime/install','/api/v1/basalt-runtime/cancel','/api/v1/basalt-runtime/model'):
try:
if self.read_json()!={}:raise ValueError('Keine zusätzlichen Basalt-Parameter erwartet.')
action=self.path.rsplit('/',1)[1]
return self.respond(self.server.basalt_runtime.download_model() if action=='model' else self.server.basalt_runtime.stop() if action=='cancel' else self.server.basalt_runtime.start())
except (OSError,ValueError) as exc:return self.respond({'error':str(exc)},400)
if self.path.startswith('/api/v1/strata-runtime/'):
try:
data=self.read_json();action=self.path.rsplit('/',1)[1]
if action=='install' and not data:return self.respond(self.server.strata_runtime.start())
if action=='vision' and not data:return self.respond(self.server.strata_runtime.prepare_vision())
if action=='prepare' and set(data)=={'model_id'}:return self.respond(self.server.strata_runtime.prepare(data['model_id']))
if action=='cancel' and not data:return self.respond(self.server.strata_runtime.stop())
raise ValueError('Ungültige Strata-Aktion.')
except (OSError,ValueError) as exc:return self.respond({'error':str(exc)},400)
if self.path.startswith('/api/v1/runtime/'):
try:
data=self.read_json();action=self.path.rsplit('/',1)[1]
if action=='build' and set(data)=={'revision','backend','jobs'}:return self.respond(self.server.runtime.start(**data))
if action=='fit' and set(data)=={'profile','context','slots'}:return self.respond(self.server.runtime.fit(**data))
if action=='cancel' and not data:return self.respond(self.server.runtime.stop())
if action=='activate' and set(data)=={'build_id'}:return self.respond(self.server.runtime.activate(**data))
if action=='rollback' and not data:return self.respond(self.server.runtime.rollback())
if action=='delete' and set(data)=={'build_id','confirm_previous'}:return self.respond(self.server.runtime.delete(**data))
raise ValueError('Ungültige Laufzeitaktion.')
except ValueError as exc:return self.respond({'error':str(exc)},400)
except (OSError, subprocess.SubprocessError):return self.respond({'error':'Laufzeitaktion fehlgeschlagen; Speicher und Werkzeuge prüfen.'},503)
if self.path in ('/api/v1/auto-tests/start','/api/v1/auto-tests/cancel','/api/v1/auto-tests/save'):
try:
data=self.read_json()
if self.path.endswith('/start'):return self.respond(self.server.auto_tests.start(data))
if self.path.endswith('/save'):return self.respond(self.server.auto_tests.save(data))
if data:raise ValueError('Keine Parameter erwartet.')
return self.respond(self.server.auto_tests.stop())
except ValueError as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/chat-tests/start','/api/v1/chat-tests/cancel','/api/v1/chat-tests/unload'):
try:
data=self.read_json()
if self.path.endswith('/start'):return self.respond(self.server.chat_tests.start(data))
if data:raise ValueError('Keine Parameter erwartet.')
return self.respond(self.server.chat_tests.stop() if self.path.endswith('/cancel') else self.server.chat_tests.unload())
except ValueError as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/separator-profiles/save':
try:return self.respond(self.server.separator_tests.configure(self.read_json()))
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/xvc-runtime/install','/api/v1/xvc-runtime/cancel','/api/v1/xvc/configure'):
try:
data=self.read_json()
if self.path.endswith('/configure'):return self.respond(self.server.xvc.configure(data))
if data:raise ValueError('Keine Installationsparameter erwartet.')
return self.respond(self.server.xvc_runtime.stop() if self.path.endswith('/cancel') else self.server.xvc_runtime.start())
except ValueError as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/soulx-singer-runtime/install','/api/v1/soulx-singer-runtime/cancel','/api/v1/soulx-singer/configure'):
try:
data=self.read_json()
if self.path.endswith('/configure'):return self.respond(self.server.soulx_singer.configure(data))
if data:raise ValueError('Keine zusätzlichen Installationsfelder.')
return self.respond(self.server.soulx_singer_runtime.stop() if self.path.endswith('/cancel') else self.server.soulx_singer_runtime.start())
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/applio-runtime/install','/api/v1/applio-runtime/cancel','/api/v1/applio/configure'):
try:
data=self.read_json()
if self.path.endswith('/configure'):return self.respond(self.server.applio.configure(data))
if data:raise ValueError('Keine Installationsparameter erwartet.')
return self.respond(self.server.applio_runtime.stop() if self.path.endswith('/cancel') else self.server.applio_runtime.start())
except ValueError as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/omnivoice-runtime/install','/api/v1/omnivoice-runtime/cancel'):
try:
if self.read_json():raise ValueError('Keine Installationsparameter erwartet.')
return self.respond(self.server.omnivoice_runtime.stop() if self.path.endswith('/cancel') else self.server.omnivoice_runtime.start())
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/voxcpm-runtime/install','/api/v1/voxcpm-runtime/cancel'):
try:
if self.read_json():raise ValueError('Keine Parameter erwartet.')
return self.respond(self.server.voxcpm_runtime.stop() if self.path.endswith('/cancel') else self.server.voxcpm_runtime.start())
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/separator-runtime/install','/api/v1/separator-runtime/cancel','/api/v1/separator-runtime/download'):
try:
data=self.read_json()
if self.path.endswith('/download'):
if set(data)!={'model'}:raise ValueError('Nur Modell-ID erwartet.')
return self.respond(self.server.separator_runtime.download(data['model']))
if data:raise ValueError('Keine Parameter erwartet.')
return self.respond(self.server.separator_runtime.stop() if self.path.endswith('/cancel') else self.server.separator_runtime.start())
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/separator-tests/start','/api/v1/separator-jobs/start','/api/v1/separator-chat/start','/api/v1/separator-tests/cancel'):
try:
if self.path.endswith('/cancel'):
if self.read_json():raise ValueError('Keine Parameter erwartet.')
return self.respond(self.server.separator_tests.stop())
# Reuse bounded multipart parsing; separation permits stereo WAV.
fields,audio=read_upload(self,validate=False,max_audio=256*1024**2 if self.path in ('/api/v1/separator-jobs/start','/api/v1/separator-chat/start') else 8*1024**2)
if not {'model'}<=set(fields) or set(fields)-{'model','target'}:raise ValueError('Nur Modell, Zielspur und WAV-Datei erwartet.')
return self.respond(self.server.separator_tests.start(fields['model'],audio,target=fields.get('target'),full=self.path in ('/api/v1/separator-jobs/start','/api/v1/separator-chat/start'),automatic=self.path=='/api/v1/separator-chat/start'))
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/acestep-runtime/install','/api/v1/acestep-runtime/cancel'):
try:
if self.read_json():raise ValueError('Keine Installationsparameter erwartet.')
return self.respond(self.server.acestep_runtime.stop() if self.path.endswith('/cancel') else self.server.acestep_runtime.start())
except (OSError,ValueError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/audio-cpp-runtime/install','/api/v1/audio-cpp-runtime/cancel'):
try:
if self.read_json():raise ValueError('Keine Installationsparameter erwartet.')
return self.respond(self.server.audio_cpp_runtime.stop() if self.path.endswith('/cancel') else self.server.audio_cpp_runtime.start())
except (OSError,ValueError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/ltx-original-runtime/install','/api/v1/ltx-original-runtime/cancel'):
try:
if self.read_json():raise ValueError('Keine Installationsparameter erwartet.')
if self.path.endswith('/cancel'):return self.respond(self.server.ltx_original_runtime.stop())
return self.respond(self.server.ltx_original_runtime.start())
except (OSError,ValueError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/video/mode','/api/v1/video/service','/api/v1/video/runtime'):
try:
data=self.read_json()
if self.path.endswith('/runtime') and set(data)=={'runtime'}:return self.respond(self.server.video.select_runtime(data['runtime']))
if self.path.endswith('/service') and set(data)=={'id'}:return self.respond(self.server.video.select(data['id']))
if set(data)!={'mode'}:raise ValueError('Nur Modus llm, video, music oder separator erwartet.')
return self.respond(self.server.video.switch(data['mode']))
except (OSError,ValueError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/endpoint/start','/api/v1/endpoint/stop','/api/v1/endpoint/config','/api/v1/endpoint/profile'):
try:
data=self.read_json();ep=self.server.endpoint
if self.path.endswith('/config'):return self.respond(ep.configure(data))
if self.path.endswith('/profile'):return self.respond(ep.enable(data))
if data:raise ValueError('Keine Parameter erwartet.')
return self.respond(ep.start() if self.path.endswith('/start') else ep.stop())
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Endpunkt konnte nicht konfiguriert werden.'},503)
if self.path in ('/api/v1/stt/start','/api/v1/stt/setup','/api/v1/stt/assign','/api/v1/stt/cancel'):
try:
if self.path.endswith('/start'):
fields,audio=read_upload(self)
if 'model' in fields or not fields.get('profile_id'):raise ValueError('Profil-ID erforderlich.')
return self.respond(self.server.stt.start(fields['profile_id'],audio,fields.get('language','de')))
data=self.read_json()
if self.path.endswith('/assign') and set(data)=={'id','revision'}:return self.respond(self.server.stt.assign(data['id'],data['revision']))
if data:raise ValueError('Keine Parameter erwartet.')
return self.respond(self.server.stt.setup() if self.path.endswith('/setup') else self.server.stt.stop())
except ValueError as exc:return self.respond({'error':str(exc)},400)
except Exception:return self.respond({'error':'STT-Aktion fehlgeschlagen.'},503)
if self.path in ('/api/v1/music-remix/install','/api/v1/music-remix/cancel','/api/v1/music-remix/transcribe'):
try:
if self.path.endswith('/transcribe'):
fields,audio=read_upload(self,validate=False,allowed_fields=('melody_only',),max_audio=256*1024**2)
if fields.get('melody_only','true') not in ('true','false'):raise ValueError('Ungültiger Score-Modus.')
return self.respond(self.server.remix.transcribe(audio,fields.get('melody_only','true')=='true'))
if self.read_json():raise ValueError('Keine Installationsparameter erwartet.')
if self.path.endswith('/cancel'):self.server.remix.stop();return self.respond(self.server.remix_runtime.stop())
return self.respond(self.server.remix_runtime.start())
except ValueError as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/music/start','/api/v1/music/load','/api/v1/music/cancel'):
try:
data=self.read_json()
if self.path.endswith('/cancel') and not data:return self.respond(self.server.music.stop())
if self.path.endswith('/load'):
if set(data)!={'profile_id'}:raise ValueError('Nur Profil-ID zum Vorladen erwartet.')
return self.respond(self.server.music.load(data['profile_id']))
if set(data)-{'profile_id','lyrics','style','seed','max_tokens','steps','duration_seconds','guidance','thinking','instrumental','bpm','language'} or not {'profile_id','lyrics','style'}<=set(data):raise ValueError('Ungültige Musikfelder.')
return self.respond(self.server.music.start(**data))
except ValueError as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/voice/start','/api/v1/voice/cancel','/api/v1/voice/select'):
try:
if self.path.endswith('/select'):
data=self.read_json()
if set(data)!={'profile_id'}:raise ValueError('Nur Profil-ID erwartet.')
if self.api_token_authenticated() and not any(p['id']==data['profile_id'] and p['kind']=='voice' and p['enabled'] for p in self.server.endpoint.rows()):raise ValueError('Dieses Stimmprofil ist nicht aktiviert.')
return self.respond(self.server.voxcpm_tests.select_shared(data['profile_id']))
if self.path.endswith('/cancel'):
if self.read_json():raise ValueError('Keine Abbruchparameter erwartet.')
return self.respond(self.server.voxcpm_tests.stop())
if self.headers.get('Content-Type','').startswith('multipart/form-data'):
fields,audio=read_upload(self,validate=False,allowed_fields=('profile_id','text','device','cfg','steps','seed','reference_text','language','instruct','speed','duration','denoise','preprocess_prompt','postprocess_output'),max_field=65536,max_audio=512*1024**2)
fields['reference']=audio
for key in ('steps','seed'):
if key in fields:fields[key]=int(fields[key])
for key in ('cfg','speed','duration'):
if key in fields:fields[key]=float(fields[key])
for key in ('denoise','preprocess_prompt','postprocess_output'):
if key in fields:
if fields[key] not in ('true','false'):raise ValueError('Ungültige Audio-Option.')
fields[key]=fields[key]=='true'
else:fields=self.read_json()
if set(fields)-{'profile_id','text','device','cfg','steps','seed','reference_text','language','instruct','speed','duration','denoise','preprocess_prompt','postprocess_output','reference'} or not {'profile_id','text'}<=set(fields):raise ValueError('Ungültige VoxCPM2-Anfrage.')
if self.api_token_authenticated():
selected=next((p for p in self.server.endpoint.rows() if p['id']==fields['profile_id'] and p['kind']=='voice' and p['enabled']),None)
if not selected:raise ValueError('Dieses Stimmprofil ist nicht aktiviert. In Deck das gewünschte Profil freigeben.')
return self.respond(self.server.voxcpm_tests.start(**fields))
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/seed-vc/start':
try:
from humaneness import read_upload as read_conversion_upload
ident,source,reference,options=read_conversion_upload(self,validator=lambda a:a,reference_required=False,auto_default=True)
return self.respond(self.server.seedvc.start(ident,source,reference,**options))
except (OSError,ValueError,InferenceError) as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/seed-vc/cancel':return self.respond(self.server.seedvc.stop() or {'ok':True})
if self.path == '/api/v1/conversion/start':
try:
from humaneness import read_upload as read_conversion_upload
ident,source,reference,options=read_conversion_upload(self)
return self.respond(self.server.humaneness.start(ident,source,reference,**options))
except (OSError,ValueError,InferenceError) as exc:return self.respond({'error':str(exc)},400)
if self.path == '/api/v1/conversion/cancel':return self.respond(self.server.humaneness.stop() or {'ok':True})
if self.path in ('/api/v1/qwen-native/install','/api/v1/qwen-native/install-cancel','/api/v1/qwen-native/start','/api/v1/qwen-native/cancel'):
try:
if self.path.endswith('/start') and self.headers.get('Content-Type','').startswith('multipart/form-data'):
fields,audio=read_upload(self,validate=False,allowed_fields=('profile_id','text','speaker','language'),max_field=4096,max_audio=16*1024**2)
if not {'profile_id','text'}<=set(fields):raise ValueError('Profil und Text erforderlich.')
return self.respond(self.server.qwen_native.start(**fields,reference=audio))
data=self.read_json()
if self.path.endswith('/install') and not data:return self.respond(self.server.qwen_native_runtime.start())
if self.path.endswith('/install-cancel') and not data:return self.respond(self.server.qwen_native_runtime.stop())
if self.path.endswith('/start') and set(data)=={'profile_id','text','speaker','language'}:
if self.api_token_authenticated() and not any(p['id']==data['profile_id'] and p['kind']=='audio' and p['enabled'] for p in self.server.endpoint.rows()):raise ValueError('Qwen-CPU-Profil ist am Endpunkt nicht freigegeben.')
return self.respond(self.server.qwen_native.start(**data))
if self.path.endswith('/cancel') and not data:return self.respond(self.server.qwen_native.stop())
raise ValueError('Ungültige Qwen-CPU-Aktion.')
except (ValueError,TypeError,KeyError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/kikiri/install','/api/v1/kikiri/install-cancel','/api/v1/kikiri/start','/api/v1/kikiri/cancel'):
try:
data=self.read_json()
if self.path.endswith('/install') and not data:return self.respond(self.server.kikiri_runtime.start())
if self.path.endswith('/install-cancel') and not data:return self.respond(self.server.kikiri_runtime.stop())
if self.path.endswith('/start') and set(data)=={'profile_id','text','speaker','language'}:
if self.api_token_authenticated() and not any(p['id']==data['profile_id'] and p['kind']=='audio' and p['enabled'] for p in self.server.endpoint.rows()):raise ValueError('Kikiri-Profil ist am Endpunkt nicht freigegeben.')
return self.respond(self.server.kikiri.start(**data))
if self.path.endswith('/cancel') and not data:return self.respond(self.server.kikiri.stop())
raise ValueError('Ungültige Kikiri-Aktion.')
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/articulino/install','/api/v1/articulino/install-cancel','/api/v1/articulino/start','/api/v1/articulino/cancel'):
try:
data=self.read_json()
if self.path.endswith('/install') and not data:return self.respond(self.server.articulino_runtime.start())
if self.path.endswith('/install-cancel') and not data:return self.respond(self.server.articulino_runtime.stop())
if self.path.endswith('/start') and set(data)=={'profile_id','text','speaker','language'}:
return self.respond(self.server.articulino.start(**data))
if self.path.endswith('/cancel') and not data:return self.respond(self.server.articulino.stop())
raise ValueError('Ungültige Articulino-Aktion.')
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/pocket/install','/api/v1/pocket/install-cancel','/api/v1/pocket/start','/api/v1/pocket/cancel'):
try:
data=self.read_json()
if self.path.endswith('/install') and not data:return self.respond(self.server.pocket_runtime.start())
if self.path.endswith('/install-cancel') and not data:return self.respond(self.server.pocket_runtime.stop())
if self.path.endswith('/start') and set(data)=={'profile_id','text','speaker','language'}:
if self.api_token_authenticated() and not any(p['id']==data['profile_id'] and p['kind']=='audio' and p['enabled'] for p in self.server.endpoint.rows()):raise ValueError('Pocket-Profil ist am Endpunkt nicht freigegeben.')
return self.respond(self.server.pocket.start(**data))
if self.path.endswith('/cancel') and not data:return self.respond(self.server.pocket.stop())
raise ValueError('Ungültige Pocket-Aktion.')
except (ValueError,OSError) as exc:return self.respond({'error':str(exc)},400)
if self.path in ('/api/v1/tts/install','/api/v1/tts/install-cancel','/api/v1/tts/assign','/api/v1/tts/start','/api/v1/tts/cancel'):
try:
data=self.read_json();t=self.server.tts_tests
if self.path.endswith('/install') and not data:return self.respond(self.server.tts_runtime.start())
if self.path.endswith('/install-cancel') and not data:return self.respond(self.server.tts_runtime.stop())
if self.path.endswith('/assign') and set(data)=={'id','revision'}:return self.respond(t.assign(data['id'],data['revision']))
if self.path.endswith('/start') and set(data)=={'profile_id','text','speaker','language'}:return self.respond(t.start(**data))
if self.path.endswith('/cancel') and not data:return self.respond(t.stop())
raise ValueError('Ungültige TTS-Anfrage.')
except ValueError as exc:return self.respond({'error':str(exc)},400)
except Exception:return self.respond({'error':'TTS-Aktion fehlgeschlagen.'},503)
if self.path in ('/api/v1/image-runtime/install','/api/v1/image-runtime/cancel'):
try:
if self.read_json():raise ValueError('Keine Parameter erwartet.')
with self.server.image_tests.lock:
if self.path.endswith('/cancel'):return self.respond(self.server.image_runtime.stop())
job=self.server.image_tests.job
if job and job.get('state')=='running':raise ValueError('Zuerst den laufenden Bildauftrag beenden.')
return self.respond(self.server.image_runtime.start())
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Installation konnte nicht vorbereitet werden.'},503)
if self.path in ('/api/v1/image-tests/start','/api/v1/image-tests/cancel'):
try:
if self.path.endswith('/start') and self.headers.get('Content-Type','').startswith('multipart/form-data;'):
from image_upload import read_image_upload
fields,images=read_image_upload(self,'profile_id')
if set(fields)-{'profile_id','prompt','skip_enhancer'} or not {'profile_id','prompt'}<=set(fields) or fields.get('skip_enhancer','0') not in ('0','1'):raise ValueError('Ungültige Bildtest-Anfrage.')
return self.respond(self.server.image_tests.start(fields['profile_id'],fields['prompt'],reference_images=images,skip_enhancer=fields.get('skip_enhancer')=='1'))
data=self.read_json()
if self.path.endswith('/cancel') and not data:return self.respond(self.server.image_tests.stop())
if self.path.endswith('/start') and set(data)=={'profile_id','prompt'}:return self.respond(self.server.image_tests.start(**data))
raise ValueError('Ungültige Bildtest-Anfrage.')
except ValueError as exc:return self.respond({'error':str(exc)},400)
except Exception:return self.respond({'error':'Bildtest konnte nicht vorbereitet werden; Ressourcen prüfen.'},503)
if self.path == '/api/v1/profiles/prompt-enhancer':
try:return self.respond(self.server.profiles.configure_prompt_enhancer(self.read_json()))
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Prompt-Aufwerter-Einstellung konnte nicht gespeichert werden.'},503)
if self.path in ('/api/v1/prompt-enhancers/install','/api/v1/prompt-enhancers/cancel','/api/v1/prompt-enhancers/preview'):
try:
enhancer=self.server.prompt_enhancer
if self.path.endswith('/preview') and self.headers.get('Content-Type','').startswith('multipart/form-data;'):
from image_upload import read_image_upload
fields,images=read_image_upload(self,'profile_id')
if set(fields)!={'profile_id','prompt'}:raise ValueError('Profil und Prompt erforderlich.')
return self.respond(enhancer.preview_start(fields['profile_id'],fields['prompt'],images))
data=self.read_json()
if self.path.endswith('/install') and set(data)=={'task'}:return self.respond(enhancer.install(data['task']))
if self.path.endswith('/cancel') and not data:return self.respond(enhancer.stop())
if self.path.endswith('/preview') and set(data)=={'profile_id','prompt'}:return self.respond(enhancer.preview_start(data['profile_id'],data['prompt'],[]))
raise ValueError('Ungültige Prompt-Aufwerter-Anfrage.')
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Prompt-Aufwerter nicht erreichbar.'},503)
if self.path == '/api/v1/profiles/components':
try:return self.respond(self.server.profiles.assign(self.read_json()))
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Zuordnung konnte nicht gespeichert werden.'},503)
if self.path == '/api/v1/profiles/delete':
try:
data=self.read_json()
with self.server.image_tests.lock, self.server.tts_tests.lock, self.server.stt.lock, self.server.voxcpm_tests.lock, self.server.pocket.lock, self.server.qwen_native.lock, self.server.articulino.lock, self.server.kikiri.lock:
kikiri_job=self.server.kikiri.job
if kikiri_job and kikiri_job.get('state')=='running' and kikiri_job.get('profile_id')==data.get('id'):raise ValueError('Kikiri-Profil wird gerade ausgeführt. Zuerst Auftrag beenden.')
art_job=self.server.articulino.job
if art_job and art_job.get('state')=='running' and art_job.get('profile_id')==data.get('id'):raise ValueError('Articulino-Profil wird gerade ausgeführt. Zuerst Auftrag beenden.')
native_job=self.server.qwen_native.job
if native_job and native_job.get('state')=='running' and native_job.get('profile_id')==data.get('id'):raise ValueError('Qwen-CPU-Profil wird gerade ausgeführt. Zuerst Auftrag beenden.')
pocket_job=self.server.pocket.job
if pocket_job and pocket_job.get('state')=='running' and pocket_job.get('profile_id')==data.get('id'):raise ValueError('Pocket-Profil wird gerade ausgeführt. Zuerst Auftrag beenden.')
voice_job=self.server.voxcpm_tests.job
if voice_job and voice_job.get('state')=='running' and voice_job.get('profile_id')==data.get('id'):raise ValueError('Stimmprofil wird gerade ausgeführt. Zuerst Auftrag abbrechen.')
stt_job=self.server.stt.job
if stt_job and stt_job.get("state")=="running" and stt_job.get("profile_id")==data.get("id"):raise ValueError("Dieses STT-Profil wird gerade ausgeführt. Zuerst den Auftrag beenden.")
tts_job=self.server.tts_tests.job
if tts_job and tts_job.get("state")=="running" and tts_job.get("profile_id")==data.get("id"):raise ValueError("Dieses TTS-Profil wird gerade ausgeführt. Zuerst den Auftrag beenden.")
job=self.server.image_tests.job
if job and job.get('state')=='running' and job.get('profile_id')==data.get('id'):raise ValueError('Dieses Profil wird gerade ausgeführt. Zuerst den Auftrag beenden.')
result=self.server.profiles.delete(data)
if self.server.kikiri.profile_id==data.get('id'):self.server.kikiri.stop()
if self.server.articulino.profile_id==data.get('id'):self.server.articulino.stop()
if self.server.pocket.profile_id==data.get('id'):self.server.pocket.stop()
if self.server.qwen_native.profile_id==data.get('id'):self.server.qwen_native.stop()
if stt_job and stt_job.get('profile_id')==data.get('id'):self.server.stt.stop()
if tts_job and tts_job.get('profile_id')==data.get('id'):self.server.tts_tests.stop()
return self.respond(result)
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Profil konnte nicht gelöscht werden.'},503)
if self.path == '/api/v1/profiles/preset':
try:return self.respond(self.server.profiles.preset(self.read_json()))
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Preset konnte nicht gespeichert werden.'},503)
if self.path == '/api/v1/reasoning-budget':
try:return self.respond(self.server.profiles.save_reasoning_budget(self.read_json()))
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Globale Denkgrenzen konnten nicht gespeichert werden.'},503)
if self.path == '/api/v1/profiles/save':
try:return self.respond(self.server.profiles.save(self.read_json()))
except ValueError as exc:return self.respond({'error':str(exc)},400)
except OSError:return self.respond({'error':'Profil konnte nicht gespeichert werden.'},503)
if self.path in ('/api/v1/catalog/download','/api/v1/catalog/cancel','/api/v1/catalog/dismiss','/api/v1/catalog/delete'):
try:
data=self.read_json()
if self.path.endswith('/delete'):
if set(data)!={'id'}:raise ValueError('Modelldatei-ID erwartet.')
with self.server.worker.lock:
active=self.server.worker.profile['model_id'] if self.server.worker.profile else None
video_rows=self.server.video.models()
selected=next((x for x in video_rows if x['model']['id']==self.server.video.selection.get('model_id')),None)
if selected and data['id'] in [selected['model']['id'],*selected['components'].values()]:raise ValueError('Datei wird vom aktiven Videomodell verwendet. Zuerst eine andere Video-Auswahl treffen.')
return self.respond(self.server.catalog.delete(data['id'],self.server.profiles,active))
if self.path.endswith('/dismiss') and set(data)=={'id'}:return self.respond(self.server.catalog.dismiss(data['id']))
if self.path.endswith('/cancel') and not data:return self.respond(self.server.catalog.stop())
if set(data)!={'repo','filename','revision','kind'}:raise ValueError('Ungültige Downloadparameter.')
return self.respond(self.server.catalog.start(**data))
except Exception as exc:
return self.respond({'error':str(exc) if isinstance(exc,ValueError) else 'Download konnte nicht vorbereitet werden.'},400)
if self.path.startswith('/api/v1/network/'):
action = self.path.rsplit('/',1)[1]
if action not in ('install','import','connect','disconnect','mode','confirm','cancel','delete','hosts'):
return self.respond({'error':'Not found'},404)
try:
data = self.read_json()
if action == 'install':
result = self.server.network.install(data.get('password'),data.get('api_token'))
else:
fields = {'import': {'config'}, 'mode': {'mode'}, 'confirm': {'trial_id'}, 'hosts': {'allowed_hosts'}}.get(action,set())
if set(data) != fields:
raise ValueError('Ungültige Netzwerkparameter.')
if action == 'import':
parse_config(data.get('config'),native=self.server.network.native)
if action == 'hosts':
data['allowed_hosts']=validate_hosts(data.get('allowed_hosts'))
result = self.server.network.call(action, data, self.ingress())
self.server.network.cached = None
return self.respond(result)
except (ValueError, OSError, subprocess.SubprocessError) as exc:
message = str(exc) if isinstance(exc, ValueError) else 'Netzwerkmodul nicht erreichbar.'
return self.respond({'error':message},400)
return self.respond({'error':'Not found'},404)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, default=8108)
args = parser.parse_args()
server = Server(args.port) # Binding fails rather than taking over an occupied port.
def shutdown(*_):
threading.Thread(target=server.shutdown, daemon=True).start()
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
print(f'Athena Deck: http://127.0.0.1:{server.server_port}', flush=True)
try:
server.serve_forever()
finally:
server.backup.stop()
server.dashboard.close()
server.auto_tests.stop()
server.chat_tests.stop()
server.video.close()
server.endpoint.close()
server.stt.stop()
server.soulx_singer.stop();server.soulx_singer_runtime.stop();server.applio.stop();server.applio_runtime.stop();server.voxcpm_tests.stop();server.tts_tests.stop();server.kikiri.stop();server.kikiri_runtime.stop();server.articulino.stop();server.articulino_runtime.stop();server.qwen_native.stop();server.qwen_native_runtime.stop();server.pocket.stop();server.pocket_runtime.stop()
server.tts_runtime.stop();server.voxcpm_runtime.stop();server.omnivoice_runtime.stop()
server.image_runtime.stop()
server.ltx_original_runtime.stop()
server.remix.stop();server.remix_runtime.stop();server.separator_tests.stop();server.separator_runtime.stop();server.music.close();server.acestep_runtime.stop();server.audio_cpp_runtime.stop()
server.prompt_enhancer.stop()
server.image_tests.stop()
server.runtime.stop()
server.catalog.stop(shutdown=True)
server.server_close()
if __name__ == '__main__':
main()