Files
Athena-Deck/integrations/openwebui/install_speech.py

64 lines
3.4 KiB
Python

"""Connect OpenWebUI audio to the existing Athena API without exporting keys."""
import asyncio
from datetime import timedelta
import json
import aiohttp
from open_webui.models.config import Config
from open_webui.models.users import Users
from open_webui.utils.auth import create_token
async def main():
urls = await Config.get('openai.api_base_urls') or []
keys = await Config.get('openai.api_keys') or []
configs = await Config.get('openai.api_configs') or {}
candidates = [(i, url) for i, url in enumerate(urls) if '192.168.1.212:8120' in url]
if len(candidates) != 1:
raise RuntimeError('Athena connection is not uniquely identified')
index, url = candidates[0]
connection = configs.get(str(index)) or configs.get(url) or {}
key = connection.get('key') or keys[index]
admins = [u for u in (await Users.get_users())['users'] if u.role == 'admin']
if len(admins) != 1:
raise RuntimeError('Expected one administrator')
token = create_token({'id': admins[0].id}, timedelta(minutes=5))
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30)) as session:
for route, expected in [('speech', 'qwen-tts'), ('transcriptions', 'qwen3-asr-stt')]:
async with session.get(url.rstrip('/') + '/audio/' + route + '/models',
headers={'Authorization': 'Bearer ' + key}) as response:
response.raise_for_status()
listing = await response.json()
if expected not in [m['id'] for m in listing['data']]:
raise RuntimeError('Required speech profile is not exposed')
headers = {'Authorization': 'Bearer ' + token}
base = 'http://127.0.0.1:8080/api/v1/audio'
async with session.get(base + '/config', headers=headers) as response:
response.raise_for_status()
config = await response.json()
config['tts'].update(ENGINE='openai', OPENAI_API_BASE_URL=url.rstrip('/'),
OPENAI_API_KEY=key, MODEL='qwen-tts', VOICE='Ryan', SPLIT_ON='punctuation')
config['tts']['OPENAI_PARAMS'] = {**(config['tts'].get('OPENAI_PARAMS') or {}),
'response_format': 'wav', 'voice': 'Ryan'}
config['stt'].update(ENGINE='openai', OPENAI_API_BASE_URL=url.rstrip('/'),
OPENAI_API_KEY=key, MODEL='qwen3-asr-stt', OPENAI_API_REQUEST_FORMAT='multipart')
async with session.post(base + '/config/update', headers=headers, json=config) as response:
response.raise_for_status()
await response.json()
async with session.get(base + '/config', headers=headers) as response:
response.raise_for_status()
saved = await response.json()
for section in ('tts', 'stt'):
for field in ('ENGINE', 'MODEL', 'OPENAI_API_BASE_URL', 'OPENAI_API_KEY'):
assert saved[section][field] == config[section][field]
assert saved['tts']['OPENAI_PARAMS']['response_format'] == 'wav'
assert saved['tts']['OPENAI_PARAMS']['voice'] == 'Ryan'
assert saved['tts']['VOICE'] == 'Ryan'
assert saved['tts']['SPLIT_ON'] == 'punctuation'
print(json.dumps({'tts': saved['tts']['MODEL'], 'stt': saved['stt']['MODEL'],
'format': 'wav', 'voice': 'Ryan', 'verified': True}))
if __name__ == '__main__':
asyncio.run(main())