Files
Athena-Deck/integrations/openwebui/install_audio.py
T

60 lines
2.8 KiB
Python

"""Run inside OpenWebUI: install our tool via its API, preserving other tools/settings.
Usage: python install_audio.py SOURCE CONNECTION_JSON
The connection JSON contains an existing Deck token. It is never printed.
"""
import asyncio
from datetime import timedelta
import json
from pathlib import Path
import sys
import aiohttp
from open_webui.models.users import Users
from open_webui.models.tools import Tools
from open_webui.models.models import Models
from open_webui.utils.auth import create_token
TOOL_ID = 'athena_audio'
async def main():
users = (await Users.get_users())['users']
admins = [u for u in users if u.role == 'admin']
if len(admins) != 1:
raise RuntimeError('Installation benötigt einen eindeutig bestimmten Administrator.')
token = create_token({'id': admins[0].id}, timedelta(minutes=5))
connection = json.loads(Path(sys.argv[2]).read_text())
async with aiohttp.ClientSession(headers={'Authorization': 'Bearer ' + token}) as session:
async def post(path, data):
async with session.post('http://127.0.0.1:8080/api/v1/' + path, json=data) as response:
if response.status != 200:
raise RuntimeError('OpenWebUI-Konfiguration fehlgeschlagen: HTTP ' + str(response.status))
return await response.json()
existing = await Tools.get_tool_by_id(TOOL_ID)
form = {'id': TOOL_ID, 'name': 'Athena Audio-Trennung',
'content': Path(sys.argv[1]).read_text(),
'meta': {'description': 'Gesang, Instrumente oder Sprache aus hochgeladenem Audio trennen. Einzelne WAV-Spuren mit Vorschau und Download.'},
'access_grants': [g.model_dump() for g in existing.access_grants] if existing else []}
installed = await post('tools/id/' + TOOL_ID + '/update' if existing else 'tools/create', form)
await post('tools/id/' + TOOL_ID + '/valves/update',
{'DECK_URL': connection.get('url', 'http://192.168.1.212:8108'), 'DECK_API_TOKEN': connection['token']})
# Only Athena chat model wrappers. Preserve all other metadata and parameters.
changed = []
for model in await Models.get_all_models():
if not any(term in model.name.lower() for term in ('qwen', 'gemma', 'holo')):
continue
form = model.model_dump()
meta = form['meta']
ids = meta.get('toolIds') or []
if TOOL_ID not in ids:
meta['toolIds'] = [*ids, TOOL_ID]
await post('models/model/update', form)
changed.append(model.name)
print(json.dumps({'installed': installed['id'], 'functions': [s['name'] for s in (await Tools.get_tool_by_id(TOOL_ID)).specs], 'enabled_models': changed}, ensure_ascii=False))
if __name__ == '__main__':
asyncio.run(main())