Persist audio download buttons as native OpenWebUI tool embeds
This commit is contained in:
1 parent
0c6037868b
commit
da9084ce90
3 files changed
+21
-14
No files matched your search
@@ -15,8 +15,10 @@ Audio **als Datei in OpenWebUI hochladen**, dann zum Beispiel:
|
||||
- „Bereinige die Sprachaufnahme.“ → `Speech.wav` und `Background.wav`
|
||||
|
||||
Die Dateieinträge erscheinen im Chat. Anklicken öffnet OpenWebUIs Audio-Vorschau
|
||||
mit Player. Zusätzlich schreibt das Plugin für jede Spur einen direkten
|
||||
Downloadlink in den Chat, unabhängig von der Modellantwort. Die Dateien bleiben
|
||||
mit Player. Zusätzlich liefert das Plugin für jede Spur einen direkten
|
||||
Downloadbutton als native Rich-UI-Werkzeugausgabe, unabhängig von der Modellantwort.
|
||||
Zusätzliche Textnachrichten können beim Abschluss der Antwort überschrieben werden.
|
||||
Die Dateien bleiben
|
||||
nach einem Browser-Refresh verfügbar. Keine zusätzliche Workspace-Kopie nötig.
|
||||
Gitarre/Piano sind experimentelle Demucs-6s-Spuren, keine perfekte Isolation.
|
||||
Unterstützt: Gesang, Schlagzeug, Bass, Gitarre, Piano, übrige Instrumente und Sprache.
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
title: Athena Audio-Trennung
|
||||
author: Athena Deck
|
||||
description: Trennt hochgeladene Audiodateien auf Athena und liefert einzelne WAV-Spuren mit Vorschau und Download.
|
||||
version: 1.0.1
|
||||
version: 1.0.2
|
||||
"""
|
||||
import asyncio
|
||||
import tempfile
|
||||
import time
|
||||
import wave
|
||||
from html import escape
|
||||
from starlette.responses import HTMLResponse
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
@@ -36,14 +38,14 @@ class Tools:
|
||||
self.valves = self.Valves()
|
||||
|
||||
@staticmethod
|
||||
async def _publish_results(attachments, emitter):
|
||||
links = ['[' + item['name'] + ' herunterladen](' + item['url'] + '?attachment=true)' for item in attachments]
|
||||
content = '\n\n**Audio herunterladen:**\n\n' + '\n'.join('- ' + link for link in links) + '\n\n'
|
||||
async def _publish_results(attachments, emitter, base_url=''):
|
||||
if emitter:
|
||||
await emitter({'type': 'chat:message:files', 'data': {'files': attachments}})
|
||||
# Emit ourselves: the model may omit download links from its summary.
|
||||
await emitter({'type': 'message', 'data': {'content': content}})
|
||||
return content
|
||||
# Native Rich UI is retained with the tool result. A 'message' event can
|
||||
# be overwritten when the following model response replaces chat content.
|
||||
buttons = ''.join('<a target="_blank" rel="noopener" download href="' + escape(base_url.rstrip('/') + item['url'] + '?attachment=true', quote=True) + '">' + escape(item['name']) + ' herunterladen</a>' for item in attachments)
|
||||
html = '<!doctype html><html><head><meta charset="utf-8"><style>body{font:14px system-ui;margin:8px;color:#aaa}nav{display:flex;gap:10px;flex-wrap:wrap}a{display:block;padding:10px 14px;border:1px solid #888;border-radius:10px;color:inherit;text-decoration:none}a:hover{background:#8883}</style></head><body><nav>' + buttons + '</nav></body></html>'
|
||||
return (HTMLResponse(html, headers={'Content-Disposition': 'inline'}), 'Audio-Trennung fertig. WAV-Spuren sind angehängt; Downloadbuttons werden im Werkzeugergebnis angezeigt.')
|
||||
|
||||
@staticmethod
|
||||
def _attachment(files, filename):
|
||||
@@ -195,9 +197,9 @@ class Tools:
|
||||
'size': size, 'content_type': 'audio/wav', 'file': data})
|
||||
if not attachments:
|
||||
raise ValueError('Keine passende Ergebnisspur vorhanden.')
|
||||
downloads = await self._publish_results(attachments, __event_emitter__)
|
||||
downloads = await self._publish_results(attachments, __event_emitter__, str(__request__.base_url))
|
||||
await status('Audio-Trennung fertig', done=True)
|
||||
return 'Fertig. Die WAV-Dateien sind im Chat angehängt. Downloadlinks wurden direkt im Chat angezeigt; nicht nochmals wiederholen. Zum Anhören einen Dateieintrag öffnen.' if __event_emitter__ else 'Fertig.' + downloads
|
||||
return downloads
|
||||
except asyncio.CancelledError:
|
||||
# Deck owns the job independently. Do not cancel a potentially shared/finished GPU job.
|
||||
raise
|
||||
|
||||
@@ -18,6 +18,7 @@ from starlette.datastructures import UploadFile, Headers
|
||||
from open_webui.models.users import Users
|
||||
from open_webui.routers.files import upload_file_handler
|
||||
from open_webui.utils.auth import create_token
|
||||
from open_webui.utils.middleware import process_tool_result
|
||||
from athena_audio import Tools
|
||||
|
||||
|
||||
@@ -49,13 +50,15 @@ async def main():
|
||||
file_ids = [uploaded.id]
|
||||
try:
|
||||
result = await tool.separate_audio(__files__=[{'id': uploaded.id, 'name': uploaded.filename}], __request__=request, __user__={'id': user.id}, __event_emitter__=emitter)
|
||||
assert not result.startswith('Audio-Trennung fehlgeschlagen'), result
|
||||
assert isinstance(result, tuple), result
|
||||
context, _, embeds = await process_tool_result(request, 'separate_audio', result, 'tool', user=user)
|
||||
assert len(embeds) == 1 and 'Audio-Trennung fertig' in context
|
||||
files = [f for e in events if e['type'] == 'chat:message:files' for f in e['data']['files']]
|
||||
file_ids.extend(f['id'] for f in files)
|
||||
assert sorted(f['name'] for f in files) == ['Instrumental.wav', 'Vocals.wav'], result
|
||||
downloads = ''.join(e['data']['content'] for e in events if e['type'] == 'message')
|
||||
for file in files:
|
||||
assert '[' + file['name'] + ' herunterladen](' + file['url'] + '?attachment=true)' in downloads
|
||||
assert file['name'] + ' herunterladen' in embeds[0]
|
||||
assert file['url'] + '?attachment=true' in embeds[0]
|
||||
async with aiohttp.ClientSession(headers={'Authorization': 'Bearer ' + token}) as session:
|
||||
for file in files:
|
||||
async with session.get('http://127.0.0.1:8080' + file['url']) as response:
|
||||
|
||||
Reference in new issue
Block a user