2Btainment commited on
Commit
45d92cc
·
verified ·
1 Parent(s): 356a13c

2B-Studio Phase 6: privater Space-Test (Shell + Worker-Routen)

Browse files
.gitignore ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ .venv/
4
+ tests/
5
+ *.wav
6
+ *.mp3
7
+ *.flac
8
+ *.ogg
9
+ .pytest_cache/
10
+ .ruff_cache/
README.md CHANGED
@@ -1,13 +1,32 @@
1
  ---
2
- title: 2b Studio
3
- emoji: 📚
4
- colorFrom: pink
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 6.26.0
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: 2B-Studio
3
+ emoji: 🎛️
4
+ colorFrom: yellow
5
+ colorTo: gray
6
  sdk: gradio
7
+ sdk_version: "6.25.0"
 
8
  app_file: app.py
9
  pinned: false
10
+ short_description: 2B-Studio – Musik generieren & mastern
11
  ---
12
 
13
+ # 2B-Studio
14
+
15
+ Gradio-Shell für **Generieren → Hören → Rüberschalten → Mastern**.
16
+
17
+ - **MiniMax Music 3** und **2B-Audio** laufen über öffentliche Worker (HF-Space).
18
+ - Freie Modelle (Stable Audio Open, ACE-Step, AudioLDM 2) sind als Mock markiert.
19
+ - **ONUS-Mastering ist nur lokal verfügbar** und im Space deaktiviert.
20
+
21
+ ## Secrets
22
+
23
+ | Name | Wert |
24
+ |---|---|
25
+ | `HF_TOKEN` | Hugging-Face-Token (für private/gated Worker) |
26
+
27
+ ## Lokal starten
28
+
29
+ ```bash
30
+ uv sync --extra dev
31
+ STUDIO_PORT=7861 uv run python app.py
32
+ ```
app.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+
5
+ from studio.ui import CSS, build_app
6
+
7
+ demo = build_app()
8
+
9
+
10
+ if __name__ == "__main__":
11
+ demo.launch(
12
+ server_name=os.environ.get("STUDIO_HOST", "0.0.0.0"),
13
+ server_port=int(os.environ.get("STUDIO_PORT", os.environ.get("PORT", "7860"))),
14
+ share=False,
15
+ show_error=True,
16
+ css=CSS,
17
+ )
prompts/producer.md ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Du bist der strukturierte Musikproducer-Agent von 2B-Studio.
2
+
3
+ Antworte ausschließlich als JSON-Objekt für `fill_create_fields`. Ändere nie Felder direkt und
4
+ starte niemals selbst eine Generierung. Spiegele die Sprache des Users.
5
+
6
+ Pflichtfelder im Payload:
7
+ - model_id
8
+ - genre_style_instruments
9
+ - lyrics (`[Instrumental]` bei Instrumentals)
10
+ - negative
11
+ - duration_s (5–180)
12
+ - idea
13
+ - studio: global_metadata, vocal_details, arrangement
14
+ - renderings: musicgen_prompt, short_caption
15
+ - rationale
16
+ - preview
17
+ - ask_user (null oder genau eine Rückfrage)
18
+ - warnings (Liste)
19
+
20
+ Modelle:
21
+ - minimax-music3: Vocals und strukturierte Lyrics, derzeit noch kein echter Worker
22
+ - 2b-musicgen-small: live, instrumental bevorzugt, maximal 20 Sekunden
23
+ - 2b-musicgen-medium: live, instrumental bevorzugt, maximal 30 Sekunden
24
+ - stable-audio-open: derzeit Mock, instrumental, maximal 47 Sekunden
25
+
26
+ Regeln:
27
+ - Instrumental bleibt instrumental.
28
+ - Keine widersprüchlichen Tempo-/Mood-Angaben.
29
+ - Negativ-Prompt beschreibt konkrete Klangfehler.
30
+ - Lyrics-Tags stehen allein in ihrer Zeile.
31
+ - Bei fehlender entscheidender Information höchstens eine Rückfrage.
32
+ - Audio wird niemals an dich gesendet; du erhältst nur Text und aktuelle Felder.
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=6.0,<7.0
2
+ pydantic>=2.9,<3.0
3
+ pyyaml>=6.0,<7.0
4
+ gradio_client>=2.0,<3.0
schemas/model-registry.yaml ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2B-Studio model registry — Entwurf, nicht produktiv verdrahtet.
2
+ # UI: EINE flache Modelauswahl. group ist nur intern (welcher Worker).
3
+
4
+ version: 1
5
+ updated: 2026-08-24
6
+
7
+ ui:
8
+ control: single_dropdown
9
+ label: Modelauswahl
10
+ note: MiniMax steht in derselben Liste wie die 2baudio-Modelle.
11
+
12
+ groups:
13
+ minimax:
14
+ worker: minimax-adapter
15
+ 2b-audio:
16
+ worker: 2baudio-generate-api
17
+ free:
18
+ worker: per-model-adapter
19
+
20
+ models:
21
+ - id: minimax-music3
22
+ group: minimax
23
+ label: MiniMax Music 3
24
+ weights: MiniMaxAI/MiniMax-Music3
25
+ space_ref: MiniMaxAI/MiniMax-Music3
26
+ adapter: minimax_music3
27
+ host: hf-zerogpu-a10g
28
+ capabilities:
29
+ vocals: true
30
+ lyrics: structured
31
+ max_duration_s: 180
32
+ streaming: true
33
+ instrumental: true
34
+ inputs: [idea, lyrics, duration_s, studio.global_metadata, studio.vocal_details, studio.arrangement]
35
+ license_note: Weights- und Space-Lizenz vor kommerziellem Einsatz prüfen. Attribution Pflicht.
36
+ mvp: true
37
+
38
+ - id: 2b-musicgen-small
39
+ group: 2b-audio
40
+ label: 2B Audio Small
41
+ weights: facebook/musicgen-small
42
+ space_ref: 2Btainment/2baudio
43
+ adapter: musicgen_2baudio
44
+ host: hf-zerogpu-or-cpu
45
+ profile: profile-4-low-vram
46
+ capabilities:
47
+ vocals: weak
48
+ lyrics: cue-only
49
+ max_duration_s: 20
50
+ streaming: false
51
+ instrumental: true
52
+ inputs: [renderings.musicgen_prompt, negative, duration_s]
53
+ license_note: MusicGen typischerweise CC-BY-NC. Nicht still kommerziell auf der .com verkaufen.
54
+ blockers: [FORCE_CPU, dummy_wav_fallback]
55
+ mvp: false
56
+
57
+ - id: 2b-musicgen-medium
58
+ group: 2b-audio
59
+ label: 2B Audio
60
+ weights: facebook/musicgen-medium
61
+ space_ref: 2Btainment/2baudio
62
+ adapter: musicgen_2baudio
63
+ host: hf-zerogpu
64
+ profile: profile-5-balanced
65
+ capabilities:
66
+ vocals: weak
67
+ lyrics: cue-only
68
+ max_duration_s: 30
69
+ streaming: false
70
+ instrumental: true
71
+ inputs: [renderings.musicgen_prompt, negative, duration_s]
72
+ license_note: wie 2b-musicgen-small
73
+ blockers: [FORCE_CPU, dummy_wav_fallback]
74
+ mvp: phase-2
75
+
76
+ - id: stable-audio-open
77
+ group: free
78
+ label: Stable Audio Open
79
+ weights: stabilityai/stable-audio-open-1.0
80
+ adapter: stable_audio_open
81
+ host: hf-zerogpu
82
+ capabilities:
83
+ vocals: false
84
+ lyrics: false
85
+ max_duration_s: 47
86
+ streaming: false
87
+ instrumental: true
88
+ inputs: [renderings.short_caption, negative, duration_s]
89
+ license_note: Stability Community License / kommerzielle Schwelle prüfen.
90
+ mvp: phase-2
91
+
92
+ - id: ace-step
93
+ group: free
94
+ label: ACE-Step
95
+ adapter: ace_step
96
+ host: hf-zerogpu
97
+ capabilities:
98
+ vocals: true
99
+ lyrics: true
100
+ max_duration_s: 120
101
+ streaming: false
102
+ instrumental: true
103
+ inputs: [idea, lyrics, duration_s]
104
+ license_note: Upstream-LICENSE lesen.
105
+ mvp: later
106
+
107
+ - id: audioldm2
108
+ group: free
109
+ label: AudioLDM 2
110
+ weights: cvssp/audioldm2
111
+ adapter: audioldm2
112
+ host: cpu-or-gpu
113
+ capabilities:
114
+ vocals: false
115
+ lyrics: false
116
+ max_duration_s: 20
117
+ streaming: false
118
+ instrumental: true
119
+ inputs: [renderings.short_caption, duration_s]
120
+ mvp: later
121
+
122
+ routing:
123
+ prefer_minimax_if:
124
+ - vocals_requested
125
+ - duration_s > 45
126
+ - has_structured_lyrics
127
+ prefer_2b_audio_if:
128
+ - user_wants_fast
129
+ - minimax_unhealthy
130
+ - duration_s <= 30 and not vocals_requested
131
+ never_silently_downgrade_vocals: true
132
+
133
+ onus:
134
+ adapter: onus_master
135
+ host: vps-or-local
136
+ default_profile: streaming_safe
137
+ default_purity: soft_keep_air
138
+ default_keep_air_db: -20
139
+ default_margin: 2.0
140
+ strict_requires_confirm: true
141
+ profiles:
142
+ - streaming_safe
143
+ - streaming_loud
144
+ - club
145
+ - dynamic_hifi
146
+ - premium_clarity
147
+ - audiophile_preserve
schemas/session.schema.json ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://2b-entertainment.com/schemas/2b-studio-session.json",
4
+ "title": "2BStudioSession",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": ["id", "created_at", "locale", "create", "master", "tracks", "chat"],
8
+ "properties": {
9
+ "id": { "type": "string", "minLength": 8 },
10
+ "created_at": { "type": "string", "format": "date-time" },
11
+ "locale": { "type": "string", "enum": ["de", "en"] },
12
+ "create": {
13
+ "type": "object",
14
+ "additionalProperties": false,
15
+ "required": ["genre_style_instruments", "lyrics", "negative", "duration_s", "model_id"],
16
+ "properties": {
17
+ "genre_style_instruments": { "type": "string", "maxLength": 4000 },
18
+ "lyrics": { "type": "string", "maxLength": 8000 },
19
+ "negative": { "type": "string", "maxLength": 1500 },
20
+ "idea": {
21
+ "type": "string",
22
+ "maxLength": 2000,
23
+ "description": "Rohidee aus dem Chat, nicht als eigenes UI-Feld. Speist die drei Promptboxen."
24
+ },
25
+ "duration_s": { "type": "integer", "minimum": 5, "maximum": 300 },
26
+ "model_id": { "type": "string" },
27
+ "mode": { "type": "string", "enum": ["simple", "studio"] },
28
+ "negative_terms": {
29
+ "type": "array",
30
+ "items": { "type": "string" },
31
+ "maxItems": 24
32
+ },
33
+ "studio": {
34
+ "type": "object",
35
+ "additionalProperties": false,
36
+ "properties": {
37
+ "global_metadata": { "type": "string" },
38
+ "vocal_details": { "type": "string" },
39
+ "arrangement": { "type": "string" }
40
+ }
41
+ },
42
+ "renderings": {
43
+ "type": "object",
44
+ "additionalProperties": false,
45
+ "properties": {
46
+ "musicgen_prompt": { "type": "string", "maxLength": 1500 },
47
+ "short_caption": { "type": "string", "maxLength": 800 }
48
+ }
49
+ },
50
+ "last_job_id": { "type": ["string", "null"] }
51
+ }
52
+ },
53
+ "master": {
54
+ "type": "object",
55
+ "additionalProperties": false,
56
+ "required": ["profile", "purity_mode"],
57
+ "properties": {
58
+ "source_track_id": { "type": ["string", "null"] },
59
+ "profile": {
60
+ "type": "string",
61
+ "enum": [
62
+ "streaming_safe",
63
+ "streaming_loud",
64
+ "club",
65
+ "dynamic_hifi",
66
+ "premium_clarity",
67
+ "audiophile_preserve"
68
+ ]
69
+ },
70
+ "purity_mode": { "type": "string", "enum": ["soft_keep_air", "strict", "off"] },
71
+ "keep_air_db": { "type": "number", "minimum": -48, "maximum": 0 },
72
+ "margin": { "type": "number", "minimum": 0.5, "maximum": 2.9 },
73
+ "intensity": { "type": "integer", "minimum": 0, "maximum": 100 },
74
+ "last_report_id": { "type": ["string", "null"] },
75
+ "human_approval": { "type": "string", "enum": ["pending", "approved", "rejected", "n/a"] }
76
+ }
77
+ },
78
+ "tracks": {
79
+ "type": "array",
80
+ "items": { "$ref": "#/$defs/track" }
81
+ },
82
+ "chat": {
83
+ "type": "array",
84
+ "items": { "$ref": "#/$defs/chat_turn" }
85
+ }
86
+ },
87
+ "$defs": {
88
+ "track": {
89
+ "type": "object",
90
+ "additionalProperties": false,
91
+ "required": ["id", "role", "path"],
92
+ "properties": {
93
+ "id": { "type": "string" },
94
+ "role": { "type": "string", "enum": ["draft", "clean", "residual", "master", "upload"] },
95
+ "path": { "type": "string" },
96
+ "engine": { "type": "string" },
97
+ "duration_s": { "type": "number" },
98
+ "sample_rate": { "type": "integer" },
99
+ "parent_id": { "type": ["string", "null"] },
100
+ "metrics": { "type": "object" }
101
+ }
102
+ },
103
+ "chat_turn": {
104
+ "type": "object",
105
+ "additionalProperties": false,
106
+ "required": ["role", "text"],
107
+ "properties": {
108
+ "role": { "type": "string", "enum": ["user", "prompt_agent", "master_agent", "system"] },
109
+ "text": { "type": "string" },
110
+ "actions": {
111
+ "type": "array",
112
+ "items": { "$ref": "#/$defs/action" }
113
+ }
114
+ }
115
+ },
116
+ "action": {
117
+ "type": "object",
118
+ "additionalProperties": false,
119
+ "required": ["type"],
120
+ "properties": {
121
+ "type": {
122
+ "type": "string",
123
+ "enum": [
124
+ "fill_create_fields",
125
+ "fill_master_params",
126
+ "propose_model",
127
+ "start_job",
128
+ "explain_metrics",
129
+ "ask_user"
130
+ ]
131
+ },
132
+ "payload": { "type": "object" },
133
+ "preview": { "type": "string" },
134
+ "rationale": { "type": "string" }
135
+ }
136
+ }
137
+ }
138
+ }
studio/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ """2B-Studio application package."""
2
+
3
+ from studio.state import StudioSession, new_session
4
+
5
+ __all__ = ["StudioSession", "new_session"]
studio/adapters/__init__.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Music-engine adapters."""
2
+
3
+ from studio.adapters.base import (
4
+ AdapterError,
5
+ DummyAudioError,
6
+ GenerateRequest,
7
+ InvalidAudioError,
8
+ MusicAdapter,
9
+ WorkerTimeoutError,
10
+ WorkerUnavailableError,
11
+ )
12
+ from studio.adapters.minimax import MiniMaxAdapter
13
+ from studio.adapters.mock import MockAdapter
14
+ from studio.adapters.onus import OnusAdapter, OnusRequest, OnusResult
15
+ from studio.adapters.router import (
16
+ AdapterRouter,
17
+ RoutingDecision,
18
+ real_2b_enabled,
19
+ real_minimax_enabled,
20
+ route_id_for_model,
21
+ )
22
+ from studio.adapters.two_b_audio import TwoBAudioAdapter
23
+
24
+ __all__ = [
25
+ "AdapterRouter",
26
+ "AdapterError",
27
+ "DummyAudioError",
28
+ "GenerateRequest",
29
+ "InvalidAudioError",
30
+ "MiniMaxAdapter",
31
+ "MockAdapter",
32
+ "MusicAdapter",
33
+ "OnusAdapter",
34
+ "OnusRequest",
35
+ "OnusResult",
36
+ "RoutingDecision",
37
+ "TwoBAudioAdapter",
38
+ "WorkerTimeoutError",
39
+ "WorkerUnavailableError",
40
+ "real_2b_enabled",
41
+ "real_minimax_enabled",
42
+ "route_id_for_model",
43
+ ]
studio/adapters/base.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Protocol
4
+
5
+ from pydantic import BaseModel, Field
6
+
7
+ from studio.state import Track
8
+
9
+
10
+ class AdapterError(RuntimeError):
11
+ """Base class for visible engine failures."""
12
+
13
+
14
+ class WorkerUnavailableError(AdapterError):
15
+ pass
16
+
17
+
18
+ class WorkerTimeoutError(AdapterError):
19
+ pass
20
+
21
+
22
+ class DummyAudioError(AdapterError):
23
+ pass
24
+
25
+
26
+ class InvalidAudioError(AdapterError):
27
+ pass
28
+
29
+
30
+ class GenerateRequest(BaseModel):
31
+ model_id: str
32
+ genre_style_instruments: str
33
+ lyrics: str = ""
34
+ negative: str = ""
35
+ duration_s: int = Field(ge=1, le=300)
36
+ advanced: dict[str, Any] = Field(default_factory=dict)
37
+
38
+
39
+ class MusicAdapter(Protocol):
40
+ @property
41
+ def model_ids(self) -> tuple[str, ...]: ...
42
+
43
+ def health(self) -> bool: ...
44
+
45
+ def generate(self, request: GenerateRequest) -> Track: ...
studio/adapters/minimax.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import time
6
+ import urllib.request
7
+ from collections.abc import Callable
8
+ from pathlib import Path
9
+ from typing import Any
10
+ from uuid import uuid4
11
+
12
+ from gradio_client import Client
13
+
14
+ from studio.adapters.base import (
15
+ GenerateRequest,
16
+ InvalidAudioError,
17
+ WorkerTimeoutError,
18
+ WorkerUnavailableError,
19
+ )
20
+ from studio.audio_analysis import analyze_wav
21
+ from studio.audio_io import ensure_browser_wav, resolve_audio_path
22
+ from studio.registry import ModelEntry
23
+ from studio.state import Track
24
+
25
+
26
+ def huggingface_token() -> str | None:
27
+ env_token = os.environ.get("HF_TOKEN", "").strip()
28
+ if env_token:
29
+ return env_token
30
+ token_file = Path.home() / ".cache" / "huggingface" / "token"
31
+ if token_file.is_file():
32
+ value = token_file.read_text(encoding="utf-8").strip()
33
+ return value or None
34
+ return None
35
+
36
+
37
+ class MiniMaxAdapter:
38
+ """Gradio client for MiniMax Music 3 via a configured Space worker."""
39
+
40
+ def __init__(
41
+ self,
42
+ output_dir: Path,
43
+ models: tuple[ModelEntry, ...],
44
+ *,
45
+ space_id: str | None = None,
46
+ timeout_s: float | None = None,
47
+ client_factory: Callable[[], Any] | None = None,
48
+ healthcheck: Callable[[], bool] | None = None,
49
+ ) -> None:
50
+ self.output_dir = output_dir
51
+ self.output_dir.mkdir(parents=True, exist_ok=True)
52
+ self.download_dir = self.output_dir / ".minimax-downloads"
53
+ self.download_dir.mkdir(exist_ok=True)
54
+ self.space_id = space_id or os.environ.get(
55
+ "MINIMAX_SPACE_ID", "MiniMaxAI/MiniMax-Music3"
56
+ )
57
+ self.timeout_s = timeout_s or float(os.environ.get("MINIMAX_TIMEOUT_S", "900"))
58
+ self._models = {model.id: model for model in models}
59
+ self._client_factory = client_factory
60
+ self._healthcheck = healthcheck
61
+ self._client: Any | None = None
62
+ self._health_cache: tuple[float, bool] | None = None
63
+
64
+ @property
65
+ def model_ids(self) -> tuple[str, ...]:
66
+ return tuple(self._models)
67
+
68
+ def _get_client(self) -> Any:
69
+ if self._client is None:
70
+ if self._client_factory is not None:
71
+ self._client = self._client_factory()
72
+ else:
73
+ token = huggingface_token()
74
+ if not token:
75
+ raise WorkerUnavailableError(
76
+ "MiniMax braucht ein Hugging-Face-Token (HF_TOKEN oder ~/.cache/huggingface/token)."
77
+ )
78
+ self._client = Client(
79
+ self.space_id,
80
+ token=token,
81
+ verbose=False,
82
+ download_files=self.download_dir,
83
+ httpx_kwargs={"timeout": min(self.timeout_s, 90.0)},
84
+ )
85
+ return self._client
86
+
87
+ def health(self) -> bool:
88
+ if self._healthcheck is not None:
89
+ return self._healthcheck()
90
+ now = time.monotonic()
91
+ if self._health_cache and now - self._health_cache[0] < 30:
92
+ return self._health_cache[1]
93
+ if not huggingface_token():
94
+ self._health_cache = (now, False)
95
+ return False
96
+ try:
97
+ url = f"https://huggingface.co/api/spaces/{self.space_id}"
98
+ request = urllib.request.Request(url, headers={"User-Agent": "2B-Studio/0.1"})
99
+ with urllib.request.urlopen(request, timeout=10) as response:
100
+ payload = json.loads(response.read().decode("utf-8"))
101
+ healthy = payload.get("runtime", {}).get("stage") == "RUNNING"
102
+ except Exception:
103
+ healthy = False
104
+ self._health_cache = (now, healthy)
105
+ return healthy
106
+
107
+ @staticmethod
108
+ def _studio_state(request: GenerateRequest) -> dict[str, Any]:
109
+ instrumental = request.lyrics.strip().casefold() in {"", "[instrumental]"}
110
+ studio = request.advanced.get("studio") if isinstance(request.advanced, dict) else {}
111
+ studio = studio if isinstance(studio, dict) else {}
112
+ lyrics = "[Instrumental]" if instrumental else request.lyrics.strip()
113
+ style = request.genre_style_instruments.strip() or "modern electronic production"
114
+ return {
115
+ "mode": "studio",
116
+ "description": style,
117
+ "instrumental": instrumental,
118
+ "title": str(request.advanced.get("project_name", "2B-Studio")),
119
+ "lyrics": lyrics,
120
+ "global_meta": studio.get("global_metadata")
121
+ or (
122
+ f"Basic Attributes: {style}. Global Emotional Progression: focused opening, "
123
+ "developed middle, resolved ending. Sonics & Production Profile: clean low end, "
124
+ "defined drums, present lead, controlled stereo width."
125
+ ),
126
+ "vocals": studio.get("vocal_details")
127
+ or (
128
+ "Vocal Direction: instrumental, no lead vocals."
129
+ if instrumental
130
+ else "Vocal Gender & Timbre: expressive lead vocal with clear diction."
131
+ ),
132
+ "arrangement": studio.get("arrangement")
133
+ or f"Instrument Lifecycle Description: {style}. Avoid: {request.negative or 'mud, clipping, haze'}.",
134
+ }
135
+
136
+ def generate(self, request: GenerateRequest) -> Track:
137
+ if request.model_id not in self._models:
138
+ raise ValueError(f"MiniMaxAdapter unterstützt {request.model_id!r} nicht.")
139
+ if not self.health():
140
+ raise WorkerUnavailableError(
141
+ f"MiniMax-Worker {self.space_id} ist nicht bereit oder nicht authentifiziert."
142
+ )
143
+
144
+ seed = int(request.advanced.get("seed", 0))
145
+ randomize = seed == 0
146
+ steps = int(request.advanced.get("steps", os.environ.get("MINIMAX_STEPS", "24")))
147
+ guidance = float(request.advanced.get("guidance", 1.7))
148
+ duration = max(5, min(180, int(request.duration_s)))
149
+ state = self._studio_state(request)
150
+ client = self._get_client()
151
+ started = time.monotonic()
152
+ job = client.submit(
153
+ state,
154
+ duration,
155
+ seed,
156
+ randomize,
157
+ 0,
158
+ steps,
159
+ guidance,
160
+ api_name="/studio_generate",
161
+ )
162
+ wav_value: Any = None
163
+ used_seed: Any = seed
164
+ try:
165
+ for output in job.outputs():
166
+ if isinstance(output, (tuple, list)) and len(output) >= 3 and output[2]:
167
+ wav_value = output[2]
168
+ if len(output) >= 4:
169
+ used_seed = output[3]
170
+ result = job.result(timeout=self.timeout_s)
171
+ except TimeoutError as error:
172
+ job.cancel()
173
+ self._client = None
174
+ raise WorkerTimeoutError(
175
+ f"MiniMax hat nach {self.timeout_s:.0f} Sekunden nicht geantwortet."
176
+ ) from error
177
+ except Exception as error:
178
+ job.cancel()
179
+ self._client = None
180
+ raise WorkerUnavailableError(f"MiniMax-Job abgebrochen: {error}") from error
181
+
182
+ if wav_value is None and isinstance(result, (tuple, list)) and len(result) >= 3:
183
+ wav_value = result[2]
184
+ if len(result) >= 4:
185
+ used_seed = result[3]
186
+ if wav_value is None:
187
+ raise InvalidAudioError("MiniMax lieferte keine WAV-Datei.")
188
+
189
+ source_path = resolve_audio_path(wav_value)
190
+ destination = self.output_dir / f"draft-minimax-{uuid4().hex[:12]}.wav"
191
+ try:
192
+ ensure_browser_wav(source_path, destination)
193
+ analysis = analyze_wav(destination)
194
+ except Exception as error:
195
+ destination.unlink(missing_ok=True)
196
+ raise InvalidAudioError(f"Ungültiges MiniMax-Audio: {error}") from error
197
+ if analysis.duration_s < 3:
198
+ destination.unlink(missing_ok=True)
199
+ raise InvalidAudioError("MiniMax lieferte eine verdächtig kurze WAV.")
200
+
201
+ return Track(
202
+ id=uuid4().hex,
203
+ role="draft",
204
+ path=str(destination),
205
+ engine=f"minimax-real:{request.model_id}",
206
+ duration_s=analysis.duration_s,
207
+ sample_rate=analysis.sample_rate,
208
+ metrics={
209
+ "phase": 3,
210
+ "duration_ms": round((time.monotonic() - started) * 1000),
211
+ "analysis": analysis.as_dict(),
212
+ "seed": used_seed,
213
+ },
214
+ metadata={
215
+ "model_id": request.model_id,
216
+ "adapter_id": "minimax-real",
217
+ "genre_style_instruments": request.genre_style_instruments,
218
+ "lyrics": request.lyrics,
219
+ "negative": request.negative,
220
+ "advanced": request.advanced,
221
+ "studio_state": state,
222
+ "used_seed": used_seed,
223
+ },
224
+ )
studio/adapters/mock.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ import math
6
+ import sys
7
+ import wave
8
+ from array import array
9
+ from pathlib import Path
10
+ from uuid import uuid4
11
+
12
+ from studio.adapters.base import GenerateRequest
13
+ from studio.state import Track
14
+
15
+
16
+ class MockAdapter:
17
+ """Create a deterministic, lightweight chord preview without a model worker."""
18
+
19
+ def __init__(
20
+ self,
21
+ output_dir: Path,
22
+ model_ids: tuple[str, ...],
23
+ adapter_id: str = "generic-mock",
24
+ ) -> None:
25
+ self.output_dir = output_dir
26
+ self.output_dir.mkdir(parents=True, exist_ok=True)
27
+ self._model_ids = model_ids
28
+ self.adapter_id = adapter_id
29
+
30
+ @property
31
+ def model_ids(self) -> tuple[str, ...]:
32
+ return self._model_ids
33
+
34
+ def health(self) -> bool:
35
+ return self.output_dir.is_dir() and self.output_dir.exists()
36
+
37
+ def generate(self, request: GenerateRequest) -> Track:
38
+ if request.model_id not in self._model_ids:
39
+ raise ValueError(f"MockAdapter unterstützt {request.model_id!r} nicht.")
40
+
41
+ signature = json.dumps(request.model_dump(), sort_keys=True, ensure_ascii=False)
42
+ digest = hashlib.sha256(signature.encode("utf-8")).hexdigest()
43
+ output_path = self.output_dir / f"draft-{digest[:12]}.wav"
44
+ sample_rate = 24_000
45
+ self._write_preview(output_path, request.duration_s, sample_rate, digest)
46
+
47
+ return Track(
48
+ id=uuid4().hex,
49
+ role="draft",
50
+ path=str(output_path),
51
+ engine=f"{self.adapter_id}:{request.model_id}",
52
+ duration_s=float(request.duration_s),
53
+ sample_rate=sample_rate,
54
+ metrics={"deterministic": True, "phase": 2, "adapter_id": self.adapter_id},
55
+ metadata={
56
+ "model_id": request.model_id,
57
+ "adapter_id": self.adapter_id,
58
+ "genre_style_instruments": request.genre_style_instruments,
59
+ "lyrics": request.lyrics,
60
+ "negative": request.negative,
61
+ "advanced": request.advanced,
62
+ },
63
+ )
64
+
65
+ @staticmethod
66
+ def _write_preview(path: Path, duration_s: int, sample_rate: int, digest: str) -> None:
67
+ frequencies = [
68
+ 110 + int(digest[0:2], 16),
69
+ 220 + int(digest[2:4], 16),
70
+ 330 + int(digest[4:6], 16),
71
+ ]
72
+ frame_count = duration_s * sample_rate
73
+ fade_frames = max(1, int(sample_rate * 0.12))
74
+ pcm = array("h")
75
+
76
+ for index in range(frame_count):
77
+ time_s = index / sample_rate
78
+ fade_in = min(1.0, index / fade_frames)
79
+ fade_out = min(1.0, (frame_count - index - 1) / fade_frames)
80
+ envelope = min(fade_in, fade_out)
81
+ pulse = 0.70 + (0.30 * max(0.0, math.sin(2 * math.pi * 2 * time_s)))
82
+ signal = sum(
83
+ math.sin(2 * math.pi * frequency * time_s) / (position + 1)
84
+ for position, frequency in enumerate(frequencies)
85
+ )
86
+ sample = int(max(-1.0, min(1.0, signal * 0.22 * envelope * pulse)) * 32767)
87
+ pcm.extend((sample, sample))
88
+
89
+ if sys.byteorder != "little":
90
+ pcm.byteswap()
91
+ with wave.open(str(path), "wb") as wav_file:
92
+ wav_file.setnchannels(2)
93
+ wav_file.setsampwidth(2)
94
+ wav_file.setframerate(sample_rate)
95
+ wav_file.writeframes(pcm.tobytes())
studio/adapters/onus.py ADDED
@@ -0,0 +1,275 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ import os
6
+ import subprocess
7
+ from collections.abc import Callable
8
+ from pathlib import Path
9
+ from typing import Any, Literal
10
+ from uuid import uuid4
11
+
12
+ from pydantic import BaseModel, Field
13
+
14
+ from studio.adapters.base import AdapterError, InvalidAudioError, WorkerTimeoutError
15
+ from studio.audio_analysis import analyze_wav
16
+ from studio.audio_io import ensure_browser_wav
17
+ from studio.state import Track
18
+
19
+ MasterProfile = Literal[
20
+ "streaming_safe",
21
+ "streaming_loud",
22
+ "club",
23
+ "dynamic_hifi",
24
+ "premium_clarity",
25
+ "audiophile_preserve",
26
+ ]
27
+ PurityMode = Literal["soft_keep_air", "strict", "off"]
28
+
29
+
30
+ class OnusRequest(BaseModel):
31
+ profile: MasterProfile = "streaming_safe"
32
+ purity_mode: PurityMode = "soft_keep_air"
33
+ keep_air_db: float = Field(default=-20.0, ge=-48, le=0)
34
+ margin: float = Field(default=2.0, ge=0.5, le=2.9)
35
+ intensity: int = Field(default=50, ge=0, le=100)
36
+ strict_approved: bool = False
37
+ analyze_only: bool = False
38
+
39
+
40
+ class OnusResult(BaseModel):
41
+ session_dir: str
42
+ analysis: dict[str, Any]
43
+ purity: dict[str, Any] | None = None
44
+ master: dict[str, Any] | None = None
45
+ report_markdown: str = ""
46
+ tracks: list[Track] = Field(default_factory=list)
47
+ original_unchanged: bool
48
+ human_approval: Literal["pending"] = "pending"
49
+
50
+
51
+ CommandRunner = Callable[[list[str], float], subprocess.CompletedProcess[str]]
52
+
53
+
54
+ def _default_base_dir() -> Path:
55
+ configured = os.environ.get("ONUS_BASE_DIR")
56
+ candidates = [
57
+ Path(configured).expanduser() if configured else None,
58
+ Path.home() / "Musik" / "ADAMAS_Kundenpaket_v2.0.0" / "studio",
59
+ Path.home() / "Schreibtisch" / "EXTension" / "Music-Agent" / "ONUS_Studio",
60
+ ]
61
+ for candidate in candidates:
62
+ if candidate and candidate.is_dir():
63
+ return candidate.resolve()
64
+ raise AdapterError("Keine lokale ONUS-Installation gefunden. ONUS_BASE_DIR setzen.")
65
+
66
+
67
+ class OnusAdapter:
68
+ def __init__(
69
+ self,
70
+ runtime_dir: Path,
71
+ *,
72
+ base_dir: Path | None = None,
73
+ timeout_s: float | None = None,
74
+ command_runner: CommandRunner | None = None,
75
+ ) -> None:
76
+ self.runtime_dir = runtime_dir.resolve()
77
+ self.runtime_dir.mkdir(parents=True, exist_ok=True)
78
+ self.base_dir = (base_dir or _default_base_dir()).resolve()
79
+ self.cli = self.base_dir / ".venv" / "bin" / "onus"
80
+ self.python = self.base_dir / ".venv" / "bin" / "python"
81
+ self.timeout_s = timeout_s or float(os.environ.get("ONUS_TIMEOUT_S", "600"))
82
+ self.command_runner = command_runner or self._run_subprocess
83
+
84
+ def health(self) -> bool:
85
+ return self.cli.is_file() and os.access(self.cli, os.X_OK) and self.python.is_file()
86
+
87
+ @staticmethod
88
+ def _sha256(path: Path) -> str:
89
+ with path.open("rb") as handle:
90
+ return hashlib.file_digest(handle, "sha256").hexdigest()
91
+
92
+ @staticmethod
93
+ def _run_subprocess(command: list[str], timeout_s: float) -> subprocess.CompletedProcess[str]:
94
+ try:
95
+ return subprocess.run(
96
+ command,
97
+ check=False,
98
+ capture_output=True,
99
+ text=True,
100
+ timeout=timeout_s,
101
+ )
102
+ except subprocess.TimeoutExpired as error:
103
+ raise WorkerTimeoutError(
104
+ f"ONUS hat nach {timeout_s:.0f} Sekunden nicht geantwortet."
105
+ ) from error
106
+
107
+ def _execute(self, command: list[str]) -> None:
108
+ result = self.command_runner(command, self.timeout_s)
109
+ if result.returncode != 0:
110
+ detail = (result.stderr or result.stdout or "unbekannter Fehler")[-2000:]
111
+ raise AdapterError(f"ONUS fehlgeschlagen: {detail}")
112
+
113
+ @staticmethod
114
+ def _read_json(path: Path) -> dict[str, Any]:
115
+ if not path.is_file():
116
+ raise AdapterError(f"ONUS-Ergebnis fehlt: {path.name}")
117
+ payload = json.loads(path.read_text(encoding="utf-8"))
118
+ if not isinstance(payload, dict):
119
+ raise AdapterError(f"Ungültiges ONUS-Ergebnis: {path.name}")
120
+ return payload
121
+
122
+ def _track_from_path(
123
+ self,
124
+ path: Path,
125
+ *,
126
+ role: Literal["clean", "residual", "master"],
127
+ parent_id: str,
128
+ metadata: dict[str, Any],
129
+ ) -> Track:
130
+ if not path.is_file():
131
+ raise InvalidAudioError(f"ONUS-Ausgabe fehlt: {path.name}")
132
+ playback = self.runtime_dir / f"{role}-{uuid4().hex[:10]}.wav"
133
+ ensure_browser_wav(path, playback)
134
+ analysis = analyze_wav(playback)
135
+ return Track(
136
+ id=uuid4().hex,
137
+ role=role,
138
+ path=str(playback),
139
+ engine="onus-local",
140
+ duration_s=analysis.duration_s,
141
+ sample_rate=analysis.sample_rate,
142
+ parent_id=parent_id,
143
+ metrics={"analysis": analysis.as_dict()},
144
+ metadata=metadata,
145
+ )
146
+
147
+ def process(self, source: Track, request: OnusRequest) -> OnusResult:
148
+ if not self.health():
149
+ raise AdapterError("ONUS CLI oder ONUS-Python ist nicht ausführbar.")
150
+ if request.purity_mode == "strict" and not request.strict_approved:
151
+ raise AdapterError("Strict Purity benötigt eine ausdrückliche menschliche Bestätigung.")
152
+
153
+ source_path = Path(source.path).resolve()
154
+ if not source_path.is_file():
155
+ raise InvalidAudioError("Der Master-Quelltrack fehlt.")
156
+ original_hash = self._sha256(source_path)
157
+ onus_session = self.runtime_dir / f"onus-{uuid4().hex[:12]}"
158
+ onus_session.mkdir(parents=True, exist_ok=False)
159
+
160
+ self._execute(
161
+ [str(self.cli), "analyze", str(source_path), "--session", str(onus_session)]
162
+ )
163
+ analysis = self._read_json(onus_session / "analysis_before.json")
164
+ if request.analyze_only:
165
+ unchanged = self._sha256(source_path) == original_hash
166
+ return OnusResult(
167
+ session_dir=str(onus_session),
168
+ analysis=analysis,
169
+ original_unchanged=unchanged,
170
+ )
171
+
172
+ master_input = source_path
173
+ purity_metrics: dict[str, Any] | None = None
174
+ tracks: list[Track] = []
175
+ common_metadata = {
176
+ "onus_session": str(onus_session),
177
+ "profile": request.profile,
178
+ "purity_mode": request.purity_mode,
179
+ "keep_air_db": request.keep_air_db,
180
+ "margin": request.margin,
181
+ "human_approval": "pending",
182
+ }
183
+
184
+ if request.purity_mode == "soft_keep_air":
185
+ self._execute(
186
+ [
187
+ str(self.cli),
188
+ "purity",
189
+ str(source_path),
190
+ "--keep-air-db",
191
+ str(request.keep_air_db),
192
+ "--margin",
193
+ str(request.margin),
194
+ "--session",
195
+ str(onus_session),
196
+ ]
197
+ )
198
+ elif request.purity_mode == "strict":
199
+ strict_script = (
200
+ "import json,sys; from pathlib import Path; "
201
+ "from onus.purity.engine import run_purity; "
202
+ "print(json.dumps(run_purity(Path(sys.argv[1]), Path(sys.argv[2]), "
203
+ "strict=True, keep_air_db=None, margin=float(sys.argv[3]))))"
204
+ )
205
+ self._execute(
206
+ [
207
+ str(self.python),
208
+ "-c",
209
+ strict_script,
210
+ str(source_path),
211
+ str(onus_session),
212
+ str(request.margin),
213
+ ]
214
+ )
215
+
216
+ if request.purity_mode != "off":
217
+ purity_metrics = self._read_json(onus_session / "metrics.json")
218
+ outputs = purity_metrics.get("outputs", {})
219
+ clean_path = Path(str(outputs.get("clean_final", "")))
220
+ residual_path = Path(str(outputs.get("residual", "")))
221
+ clean_track = self._track_from_path(
222
+ clean_path,
223
+ role="clean",
224
+ parent_id=source.id,
225
+ metadata=common_metadata,
226
+ )
227
+ residual_track = self._track_from_path(
228
+ residual_path,
229
+ role="residual",
230
+ parent_id=source.id,
231
+ metadata=common_metadata,
232
+ )
233
+ tracks.extend([clean_track, residual_track])
234
+ master_input = clean_path
235
+
236
+ self._execute(
237
+ [
238
+ str(self.cli),
239
+ "master",
240
+ str(master_input),
241
+ "--profile",
242
+ request.profile,
243
+ "--compressed-format",
244
+ "opus",
245
+ "--session",
246
+ str(onus_session),
247
+ ]
248
+ )
249
+ master_metrics = self._read_json(onus_session / "master_result.json")
250
+ master_path = Path(str(master_metrics.get("output", "")))
251
+ master_parent = tracks[0].id if tracks else source.id
252
+ tracks.append(
253
+ self._track_from_path(
254
+ master_path,
255
+ role="master",
256
+ parent_id=master_parent,
257
+ metadata=common_metadata,
258
+ )
259
+ )
260
+
261
+ self._execute([str(self.cli), "report", str(onus_session)])
262
+ report_path = onus_session / "report.md"
263
+ report_markdown = report_path.read_text(encoding="utf-8") if report_path.is_file() else ""
264
+ unchanged = self._sha256(source_path) == original_hash
265
+ if not unchanged:
266
+ raise AdapterError("Sicherheitsabbruch: ONUS hat den Originaltrack verändert.")
267
+ return OnusResult(
268
+ session_dir=str(onus_session),
269
+ analysis=analysis,
270
+ purity=purity_metrics,
271
+ master=master_metrics,
272
+ report_markdown=report_markdown,
273
+ tracks=tracks,
274
+ original_unchanged=True,
275
+ )
studio/adapters/router.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from dataclasses import dataclass
5
+ from pathlib import Path
6
+
7
+ from studio.adapters.base import GenerateRequest, MusicAdapter, WorkerUnavailableError
8
+ from studio.adapters.minimax import MiniMaxAdapter, huggingface_token
9
+ from studio.adapters.mock import MockAdapter
10
+ from studio.adapters.two_b_audio import TwoBAudioAdapter
11
+ from studio.registry import ModelEntry, ModelRegistry
12
+ from studio.state import Track
13
+
14
+
15
+ @dataclass(frozen=True, slots=True)
16
+ class RoutingDecision:
17
+ model: ModelEntry
18
+ route_id: str
19
+ adapter: MusicAdapter
20
+ status: str = "MOCK_READY"
21
+ real_worker_available: bool = False
22
+
23
+
24
+ def real_2b_enabled() -> bool:
25
+ return os.environ.get("TWO_B_AUDIO_MODE", "real").strip().casefold() == "real"
26
+
27
+
28
+ def real_minimax_enabled() -> bool:
29
+ mode = os.environ.get("MINIMAX_MODE", "auto").strip().casefold()
30
+ if mode == "mock":
31
+ return False
32
+ if mode == "real":
33
+ return True
34
+ return bool(huggingface_token())
35
+
36
+
37
+ def route_id_for_model(
38
+ model: ModelEntry,
39
+ real_2b: bool | None = None,
40
+ real_minimax: bool | None = None,
41
+ ) -> str:
42
+ use_real_2b = real_2b_enabled() if real_2b is None else real_2b
43
+ use_real_minimax = real_minimax_enabled() if real_minimax is None else real_minimax
44
+ if model.group == "minimax":
45
+ return "minimax-real" if use_real_minimax else "minimax-mock"
46
+ if model.group == "2b-audio":
47
+ return "2baudio-real" if use_real_2b else "2baudio-mock"
48
+ return f"free-{model.adapter}-mock"
49
+
50
+
51
+ class AdapterRouter:
52
+ """Resolve registry models to real Phase-3 or explicit mock adapters."""
53
+
54
+ def __init__(
55
+ self,
56
+ registry: ModelRegistry,
57
+ output_dir: Path,
58
+ *,
59
+ real_2b: bool | None = None,
60
+ real_minimax: bool | None = None,
61
+ ) -> None:
62
+ self.registry = registry
63
+ self.real_2b = real_2b_enabled() if real_2b is None else real_2b
64
+ self.real_minimax = real_minimax_enabled() if real_minimax is None else real_minimax
65
+ grouped_ids: dict[str, list[str]] = {}
66
+ for model in registry.models:
67
+ grouped_ids.setdefault(
68
+ route_id_for_model(model, self.real_2b, self.real_minimax), []
69
+ ).append(model.id)
70
+ self._adapters: dict[str, MusicAdapter] = {
71
+ route_id: MockAdapter(output_dir, tuple(model_ids), adapter_id=route_id)
72
+ for route_id, model_ids in grouped_ids.items()
73
+ if route_id not in {"2baudio-real", "minimax-real"}
74
+ }
75
+ real_2b_models = tuple(model for model in registry.models if model.group == "2b-audio")
76
+ if self.real_2b and real_2b_models:
77
+ self._adapters["2baudio-real"] = TwoBAudioAdapter(output_dir, real_2b_models)
78
+ real_minimax_models = tuple(model for model in registry.models if model.group == "minimax")
79
+ if self.real_minimax and real_minimax_models:
80
+ self._adapters["minimax-real"] = MiniMaxAdapter(output_dir, real_minimax_models)
81
+
82
+ def route(self, model_id: str) -> RoutingDecision:
83
+ model = self.registry.get(model_id)
84
+ route_id = route_id_for_model(model, self.real_2b, self.real_minimax)
85
+ is_real = route_id.endswith("-real")
86
+ return RoutingDecision(
87
+ model=model,
88
+ route_id=route_id,
89
+ adapter=self._adapters[route_id],
90
+ status="REAL_CONFIGURED" if is_real else "MOCK_READY",
91
+ real_worker_available=is_real,
92
+ )
93
+
94
+ def generate(self, request: GenerateRequest) -> Track:
95
+ decision = self.route(request.model_id)
96
+ if not decision.adapter.health():
97
+ raise WorkerUnavailableError(f"Adapter {decision.route_id} ist nicht bereit.")
98
+ track = decision.adapter.generate(request)
99
+ track.metadata.update(
100
+ {
101
+ "route_id": decision.route_id,
102
+ "registry_adapter": decision.model.adapter,
103
+ "model_group": decision.model.group,
104
+ "routing_status": decision.status,
105
+ "real_worker_available": decision.real_worker_available,
106
+ }
107
+ )
108
+ track.metrics["route_id"] = decision.route_id
109
+ return track
110
+
111
+ def health(self) -> dict[str, bool]:
112
+ return {route_id: adapter.health() for route_id, adapter in self._adapters.items()}
studio/adapters/two_b_audio.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import time
6
+ import urllib.request
7
+ from collections.abc import Callable
8
+ from pathlib import Path
9
+ from typing import Any
10
+ from uuid import uuid4
11
+
12
+ from gradio_client import Client
13
+
14
+ from studio.adapters.base import (
15
+ DummyAudioError,
16
+ GenerateRequest,
17
+ InvalidAudioError,
18
+ WorkerTimeoutError,
19
+ WorkerUnavailableError,
20
+ )
21
+ from studio.audio_analysis import analyze_wav
22
+ from studio.audio_io import ensure_browser_wav, resolve_audio_path
23
+ from studio.registry import ModelEntry
24
+ from studio.state import Track
25
+
26
+
27
+ class TwoBAudioAdapter:
28
+ """Synchronous Gradio client for the public 2Btainment/2baudio worker."""
29
+
30
+ def __init__(
31
+ self,
32
+ output_dir: Path,
33
+ models: tuple[ModelEntry, ...],
34
+ *,
35
+ space_id: str | None = None,
36
+ timeout_s: float | None = None,
37
+ client_factory: Callable[[], Any] | None = None,
38
+ healthcheck: Callable[[], bool] | None = None,
39
+ ) -> None:
40
+ self.output_dir = output_dir
41
+ self.output_dir.mkdir(parents=True, exist_ok=True)
42
+ self.download_dir = self.output_dir / ".2baudio-downloads"
43
+ self.download_dir.mkdir(exist_ok=True)
44
+ self.space_id = space_id or os.environ.get(
45
+ "TWO_B_AUDIO_SPACE_ID", "2Btainment/2baudio"
46
+ )
47
+ self.timeout_s = timeout_s or float(os.environ.get("TWO_B_AUDIO_TIMEOUT_S", "600"))
48
+ self._models = {model.id: model for model in models}
49
+ self._client_factory = client_factory
50
+ self._healthcheck = healthcheck
51
+ self._client: Any | None = None
52
+ self._health_cache: tuple[float, bool] | None = None
53
+
54
+ @property
55
+ def model_ids(self) -> tuple[str, ...]:
56
+ return tuple(self._models)
57
+
58
+ def _get_client(self) -> Any:
59
+ if self._client is None:
60
+ if self._client_factory is not None:
61
+ self._client = self._client_factory()
62
+ else:
63
+ token = os.environ.get("HF_TOKEN") or None
64
+ self._client = Client(
65
+ self.space_id,
66
+ token=token,
67
+ verbose=False,
68
+ download_files=self.download_dir,
69
+ httpx_kwargs={"timeout": min(self.timeout_s, 60.0)},
70
+ )
71
+ return self._client
72
+
73
+ def health(self) -> bool:
74
+ if self._healthcheck is not None:
75
+ return self._healthcheck()
76
+ now = time.monotonic()
77
+ if self._health_cache and now - self._health_cache[0] < 30:
78
+ return self._health_cache[1]
79
+ try:
80
+ url = f"https://huggingface.co/api/spaces/{self.space_id}"
81
+ request = urllib.request.Request(url, headers={"User-Agent": "2B-Studio/0.1"})
82
+ with urllib.request.urlopen(request, timeout=10) as response:
83
+ payload = json.loads(response.read().decode("utf-8"))
84
+ healthy = payload.get("runtime", {}).get("stage") == "RUNNING"
85
+ except Exception:
86
+ healthy = False
87
+ self._health_cache = (now, healthy)
88
+ return healthy
89
+
90
+ def generate(self, request: GenerateRequest) -> Track:
91
+ model = self._models.get(request.model_id)
92
+ if model is None:
93
+ raise ValueError(f"TwoBAudioAdapter unterstützt {request.model_id!r} nicht.")
94
+ if not self.health():
95
+ raise WorkerUnavailableError(
96
+ f"Der Worker {self.space_id} ist momentan nicht erreichbar."
97
+ )
98
+
99
+ seed = int(request.advanced.get("seed", 12345))
100
+ job_id = f"2bstudio-{uuid4().hex[:16]}"
101
+ profile = model.profile or "profile-4-low-vram"
102
+ model_hint = model.weights or request.model_id
103
+ parameters = {
104
+ "seed": seed,
105
+ "negative_prompt": request.negative,
106
+ "max_new_tokens": max(128, min(1503, request.duration_s * 50)),
107
+ "run_n_segments": 1 if request.duration_s <= 15 else 2,
108
+ "prompt_filter_level": "balanced",
109
+ }
110
+ client = self._get_client()
111
+ started = time.monotonic()
112
+ job = client.submit(
113
+ job_id,
114
+ str(request.advanced.get("project_name", "2B-Studio")),
115
+ "manual",
116
+ model_hint,
117
+ profile,
118
+ request.genre_style_instruments,
119
+ request.lyrics,
120
+ json.dumps(parameters, ensure_ascii=False),
121
+ api_name="/generate",
122
+ )
123
+ try:
124
+ result = job.result(timeout=self.timeout_s)
125
+ except TimeoutError as error:
126
+ job.cancel()
127
+ self._client = None
128
+ raise WorkerTimeoutError(
129
+ f"2B-Audio hat nach {self.timeout_s:.0f} Sekunden nicht geantwortet."
130
+ ) from error
131
+ except Exception as error:
132
+ job.cancel()
133
+ self._client = None
134
+ raise WorkerUnavailableError(f"2B-Audio-Job abgebrochen: {error}") from error
135
+
136
+ if not isinstance(result, (tuple, list)) or len(result) != 3:
137
+ raise InvalidAudioError("2B-Audio lieferte ein unerwartetes Antwortformat.")
138
+ audio_value, log_value, settings_value = result
139
+ settings = settings_value if isinstance(settings_value, dict) else {}
140
+ generation_mode = str(settings.get("generation_mode", "unknown"))
141
+ if generation_mode != "model":
142
+ reason = str(settings.get("generation_error") or "unbekannter Workerfehler")
143
+ raise DummyAudioError(
144
+ f"2B-Audio lieferte nur Dummy-Audio ({generation_mode}): {reason}"
145
+ )
146
+
147
+ source_path = resolve_audio_path(audio_value)
148
+ if not source_path.is_file():
149
+ raise InvalidAudioError("Die vom Worker gemeldete WAV-Datei fehlt.")
150
+ destination = self.output_dir / f"draft-{job_id}.wav"
151
+ ensure_browser_wav(source_path, destination)
152
+ try:
153
+ analysis = analyze_wav(destination)
154
+ except Exception as error:
155
+ destination.unlink(missing_ok=True)
156
+ raise InvalidAudioError(f"Ungültiges Worker-Audio: {error}") from error
157
+ if analysis.duration_s <= 2.6:
158
+ destination.unlink(missing_ok=True)
159
+ raise DummyAudioError(
160
+ "2B-Audio lieferte eine verdächtig kurze WAV (Dummy-Schwelle 2,6 s)."
161
+ )
162
+
163
+ elapsed_ms = round((time.monotonic() - started) * 1000)
164
+ return Track(
165
+ id=uuid4().hex,
166
+ role="draft",
167
+ path=str(destination),
168
+ engine=f"2baudio-real:{request.model_id}",
169
+ duration_s=analysis.duration_s,
170
+ sample_rate=analysis.sample_rate,
171
+ metrics={
172
+ "phase": 3,
173
+ "duration_ms": elapsed_ms,
174
+ "analysis": analysis.as_dict(),
175
+ "generation_mode": generation_mode,
176
+ },
177
+ metadata={
178
+ "model_id": request.model_id,
179
+ "adapter_id": "2baudio-real",
180
+ "genre_style_instruments": request.genre_style_instruments,
181
+ "lyrics": request.lyrics,
182
+ "negative": request.negative,
183
+ "advanced": request.advanced,
184
+ "worker_settings": settings,
185
+ "worker_log": str(log_value)[-4000:],
186
+ "job_id": job_id,
187
+ },
188
+ )
studio/agent.py ADDED
@@ -0,0 +1,323 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import re
6
+ from pathlib import Path
7
+ from typing import Any, Literal, Protocol
8
+
9
+ from huggingface_hub import InferenceClient
10
+ from pydantic import BaseModel, Field
11
+
12
+ from studio.adapters.router import route_id_for_model
13
+ from studio.registry import ModelRegistry, load_registry
14
+
15
+ PROMPT_PATH = Path(__file__).resolve().parents[1] / "prompts" / "producer.md"
16
+
17
+
18
+ class FieldProposal(BaseModel):
19
+ model_id: str | None = None
20
+ genre_style_instruments: str
21
+ lyrics: str
22
+ negative: str
23
+ rationale: str
24
+ duration_s: int = Field(default=20, ge=5, le=180)
25
+ idea: str = ""
26
+ studio: dict[str, str] = Field(default_factory=dict)
27
+ renderings: dict[str, str] = Field(default_factory=dict)
28
+ preview: str = ""
29
+ ask_user: str | None = None
30
+ warnings: list[str] = Field(default_factory=list)
31
+ source: Literal["hf-inference", "fallback"] = "fallback"
32
+
33
+
34
+ class ProducerAction(BaseModel):
35
+ type: Literal["fill_create_fields"] = "fill_create_fields"
36
+ payload: FieldProposal
37
+ preview: str
38
+ ask_user: str | None = None
39
+
40
+
41
+ class MasterProposal(BaseModel):
42
+ profile: Literal[
43
+ "streaming_safe",
44
+ "streaming_loud",
45
+ "club",
46
+ "dynamic_hifi",
47
+ "premium_clarity",
48
+ "audiophile_preserve",
49
+ ] = "streaming_safe"
50
+ purity_mode: Literal["soft_keep_air", "strict", "off"] = "soft_keep_air"
51
+ keep_air_db: float = Field(default=-20.0, ge=-48, le=0)
52
+ margin: float = Field(default=2.0, ge=0.5, le=2.9)
53
+ intensity: int = Field(default=50, ge=0, le=100)
54
+ analyze_only: bool = False
55
+ rationale: str
56
+ preview: str
57
+ requires_strict_approval: bool = False
58
+
59
+
60
+ class MasterAction(BaseModel):
61
+ type: Literal["fill_master_params"] = "fill_master_params"
62
+ payload: MasterProposal
63
+ preview: str
64
+
65
+
66
+ class ProducerProvider(Protocol):
67
+ def propose(self, message: str, current_fields: dict[str, Any]) -> dict[str, Any]: ...
68
+
69
+
70
+ class HFInferenceProducerProvider:
71
+ def __init__(self) -> None:
72
+ token = os.environ.get("HF_TOKEN", "")
73
+ if not token:
74
+ raise ValueError("HF_TOKEN ist nicht gesetzt.")
75
+ self.model_id = os.environ.get(
76
+ "AGENT_MODEL_ID", "meta-llama/Llama-3.2-3B-Instruct"
77
+ )
78
+ self.client = InferenceClient(token=token)
79
+ self.system_prompt = PROMPT_PATH.read_text(encoding="utf-8")
80
+
81
+ def propose(self, message: str, current_fields: dict[str, Any]) -> dict[str, Any]:
82
+ response = self.client.chat.completions.create(
83
+ model=self.model_id,
84
+ messages=[
85
+ {"role": "system", "content": self.system_prompt},
86
+ {
87
+ "role": "user",
88
+ "content": json.dumps(
89
+ {"idea": message, "current_fields": current_fields},
90
+ ensure_ascii=False,
91
+ ),
92
+ },
93
+ ],
94
+ temperature=0.2,
95
+ max_tokens=1400,
96
+ )
97
+ content = response.choices[0].message.content or "{}"
98
+ cleaned = re.sub(r"^```(?:json)?\s*|\s*```$", "", content.strip())
99
+ parsed = json.loads(cleaned)
100
+ if not isinstance(parsed, dict):
101
+ raise ValueError("Der Agent lieferte kein JSON-Objekt.")
102
+ payload = parsed.get("payload", parsed)
103
+ if not isinstance(payload, dict):
104
+ raise ValueError("Der Agent lieferte kein gültiges Payload-Objekt.")
105
+ return payload
106
+
107
+
108
+ def _duration_from_message(message: str) -> int:
109
+ match = re.search(
110
+ r"\b(?P<value>\d{1,3})\s*(?P<unit>sekunden?|seconds?|secs?|s|minuten?|minutes?|min)\b",
111
+ message.casefold(),
112
+ )
113
+ if not match:
114
+ return 20
115
+ value = int(match.group("value"))
116
+ if match.group("unit").startswith(("min", "minute")):
117
+ value *= 60
118
+ return max(5, min(180, value))
119
+
120
+
121
+ def _fallback_proposal(message: str) -> FieldProposal:
122
+ normalized = message.casefold()
123
+ duration_s = _duration_from_message(message)
124
+ vocals_requested = any(
125
+ term in normalized
126
+ for term in (
127
+ "vocal",
128
+ "gesang",
129
+ "sänger",
130
+ "sängerin",
131
+ "männlich",
132
+ "weiblich",
133
+ "lyrics",
134
+ "stimme",
135
+ )
136
+ ) and "instrumental" not in normalized
137
+
138
+ if any(term in normalized for term in ("riddim", "reggae", "dub")):
139
+ style = (
140
+ "dark roots riddim, 140 BPM, analog sub bass, tight live drums, "
141
+ "warm organ skanks, evolving dub transitions"
142
+ )
143
+ elif any(term in normalized for term in ("techno", "club", "rave")):
144
+ style = (
145
+ "hypnotic techno, 132 BPM, rolling sub bass, crisp percussion, "
146
+ "warehouse depth, tension-building arrangement"
147
+ )
148
+ elif any(term in normalized for term in ("lofi", "chill", "entspannt")):
149
+ style = (
150
+ "lo-fi soul, 82 BPM, dusty drums, mellow Rhodes piano, warm tape texture, "
151
+ "human timing, intimate dynamics"
152
+ )
153
+ elif any(term in normalized for term in ("film", "cinematic", "episch")):
154
+ style = (
155
+ "cinematic electronica, 96 BPM, evolving strings, deep percussion, "
156
+ "wide synth layers, clear emotional arc"
157
+ )
158
+ elif any(term in normalized for term in ("rock", "gitarre", "guitar")):
159
+ style = (
160
+ "modern alternative rock, 118 BPM, expressive electric guitars, live drums, "
161
+ "driving bass, dynamic verse-to-chorus lift"
162
+ )
163
+ else:
164
+ style = (
165
+ "modern electronic soul, 108 BPM, punchy drums, warm bass, memorable hook, "
166
+ "intentional transitions, polished stereo depth"
167
+ )
168
+
169
+ if vocals_requested:
170
+ style += ", expressive lead vocal with clear diction"
171
+ lyrics = (
172
+ "[Verse]\nWe turn the quiet into light\nEvery step is drawing near\n\n"
173
+ "[Chorus]\nWe hold the line tonight\nMake the hidden signal clear"
174
+ )
175
+ model_id = "minimax-music3"
176
+ else:
177
+ lyrics = "[Instrumental]"
178
+ model_id = "2b-musicgen-medium" if duration_s > 20 else "2b-musicgen-small"
179
+
180
+ short_caption = style[:800]
181
+ return FieldProposal(
182
+ model_id=model_id,
183
+ genre_style_instruments=style,
184
+ lyrics=lyrics,
185
+ negative=(
186
+ "muddy low end, harsh sibilance, uncontrolled clipping, phase cancellation, "
187
+ "static arrangement, generic AI artifacts"
188
+ ),
189
+ duration_s=duration_s,
190
+ idea=message.strip(),
191
+ studio={
192
+ "global_metadata": f"Basic Attributes: {style}",
193
+ "vocal_details": (
194
+ "Vocal Gender & Timbre: expressive lead, clear diction"
195
+ if vocals_requested
196
+ else "Vocal Direction: instrumental, no lead vocals"
197
+ ),
198
+ "arrangement": "Instrument Lifecycle: focused opening, developed middle, resolved ending",
199
+ },
200
+ renderings={
201
+ "musicgen_prompt": style,
202
+ "short_caption": short_caption,
203
+ },
204
+ preview=f"{duration_s}s · {'Vocal' if vocals_requested else 'Instrumental'} · {model_id}",
205
+ rationale=(
206
+ "MiniMax passt zur strukturierten Vocal-Anforderung."
207
+ if vocals_requested
208
+ else "Der live angebundene 2B-Audio-Worker passt zum instrumentalen Entwurf."
209
+ ),
210
+ source="fallback",
211
+ )
212
+
213
+
214
+ class ProducerAgent:
215
+ def __init__(
216
+ self,
217
+ registry: ModelRegistry,
218
+ provider: ProducerProvider | None = None,
219
+ ) -> None:
220
+ self.registry = registry
221
+ self.provider = provider
222
+ if self.provider is None and os.environ.get("HF_TOKEN"):
223
+ self.provider = HFInferenceProducerProvider()
224
+
225
+ def propose(
226
+ self,
227
+ message: str,
228
+ current_fields: dict[str, Any] | None = None,
229
+ ) -> ProducerAction:
230
+ warnings: list[str] = []
231
+ if self.provider is not None:
232
+ try:
233
+ raw = self.provider.propose(message, current_fields or {})
234
+ proposal = FieldProposal.model_validate({**raw, "source": "hf-inference"})
235
+ except Exception as error:
236
+ proposal = _fallback_proposal(message)
237
+ warnings.append(f"LLM-Fallback aktiv: {type(error).__name__}")
238
+ else:
239
+ proposal = _fallback_proposal(message)
240
+
241
+ try:
242
+ model = self.registry.get(proposal.model_id or "")
243
+ except KeyError:
244
+ model = self.registry.get("2b-musicgen-small")
245
+ proposal.model_id = model.id
246
+ warnings.append("Unbekanntes Agentenmodell wurde auf 2B Audio Small zurückgesetzt.")
247
+
248
+ if proposal.duration_s > model.max_duration_s:
249
+ warnings.append(
250
+ f"Dauer wurde an {model.label} angepasst: maximal {model.max_duration_s} s."
251
+ )
252
+ proposal.duration_s = model.max_duration_s
253
+ if proposal.lyrics.strip().casefold() != "[instrumental]" and not model.supports_vocals:
254
+ warnings.append(f"{model.label} unterstützt Gesang nur eingeschränkt.")
255
+ if not route_id_for_model(model).endswith("-real"):
256
+ warnings.append(
257
+ f"{model.label} läuft aktuell als Mock-Route; der echte Worker ist nicht verbunden."
258
+ )
259
+
260
+ proposal.warnings = [*proposal.warnings, *warnings]
261
+ if not proposal.preview:
262
+ proposal.preview = f"{proposal.duration_s}s · {model.label}"
263
+ return ProducerAction(
264
+ payload=proposal,
265
+ preview=proposal.preview,
266
+ ask_user=proposal.ask_user,
267
+ )
268
+
269
+
270
+ def propose_fields(message: str) -> FieldProposal:
271
+ """Compatibility helper using the validated local fallback/provider chain."""
272
+
273
+ return ProducerAgent(load_registry()).propose(message).payload
274
+
275
+
276
+ def propose_master(message: str) -> MasterAction:
277
+ normalized = message.casefold()
278
+ analyze_only = any(
279
+ term in normalized for term in ("nur messen", "nur analyse", "only analyze", "analyse only")
280
+ )
281
+ if any(term in normalized for term in ("club", "dj", "dancefloor")):
282
+ profile = "club"
283
+ intensity = 72
284
+ elif any(term in normalized for term in ("lauter", "loud", "druck")):
285
+ profile = "streaming_loud"
286
+ intensity = 65
287
+ elif any(term in normalized for term in ("hifi", "dynamisch", "audiophil")):
288
+ profile = "dynamic_hifi"
289
+ intensity = 42
290
+ else:
291
+ profile = "streaming_safe"
292
+ intensity = 50
293
+
294
+ strict = "strict" in normalized
295
+ purity_off = any(
296
+ term in normalized for term in ("ohne purity", "kein purity", "nur master", "skip purity")
297
+ )
298
+ purity_mode = "strict" if strict else "off" if purity_off else "soft_keep_air"
299
+ preserve_air = any(
300
+ term in normalized for term in ("luft", "air", "wärme", "hall", "nicht tot")
301
+ )
302
+ keep_air_db = -24.0 if preserve_air else -20.0
303
+ margin = 1.5 if preserve_air else 2.0
304
+ rationale = (
305
+ "Strict wurde nur als Vorschlag markiert und benötigt eine separate Bestätigung."
306
+ if strict
307
+ else "soft_keep_air schützt Air, Hall und Wärme; das Original bleibt unverändert."
308
+ )
309
+ if analyze_only:
310
+ rationale = "Es wird ausschließlich gemessen; keine Audiodatei wird verändert oder erzeugt."
311
+
312
+ proposal = MasterProposal(
313
+ profile=profile,
314
+ purity_mode=purity_mode,
315
+ keep_air_db=keep_air_db,
316
+ margin=margin,
317
+ intensity=intensity,
318
+ analyze_only=analyze_only,
319
+ rationale=rationale,
320
+ preview=f"{profile} · {purity_mode}{' · nur Analyse' if analyze_only else ''}",
321
+ requires_strict_approval=strict,
322
+ )
323
+ return MasterAction(payload=proposal, preview=proposal.preview)
studio/audio_analysis.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import math
5
+ import subprocess
6
+ import sys
7
+ import wave
8
+ from array import array
9
+ from dataclasses import asdict, dataclass
10
+ from pathlib import Path
11
+ from typing import Any
12
+
13
+
14
+ @dataclass(frozen=True, slots=True)
15
+ class AudioAnalysis:
16
+ duration_s: float
17
+ sample_rate: int
18
+ channels: int
19
+ peak_dbfs: float
20
+ rms_dbfs: float
21
+ crest_db: float
22
+ zero_crossing_rate: float
23
+ character: str
24
+
25
+ def as_dict(self) -> dict[str, Any]:
26
+ return asdict(self)
27
+
28
+
29
+ def _dbfs(amplitude: float) -> float:
30
+ if amplitude <= 0:
31
+ return -96.0
32
+ return max(-96.0, 20 * math.log10(amplitude / 32767.0))
33
+
34
+
35
+ def _describe_character(rms_dbfs: float, crest_db: float, zero_crossing_rate: float) -> str:
36
+ density = "dicht" if rms_dbfs > -12 else "ausgewogen" if rms_dbfs > -20 else "dynamisch"
37
+ tone = (
38
+ "brillant/perkussiv"
39
+ if zero_crossing_rate > 0.14
40
+ else "warm/dunkel"
41
+ if zero_crossing_rate < 0.055
42
+ else "tonal balanciert"
43
+ )
44
+ transients = "kompakt" if crest_db < 7 else "transientenreich" if crest_db > 13 else "offen"
45
+ return f"{density}, {tone}, {transients}"
46
+
47
+
48
+ def _read_audio_as_pcm16(audio_path: Path) -> tuple[int, int, int, bytes]:
49
+ try:
50
+ with wave.open(str(audio_path), "rb") as wav_file:
51
+ channels = wav_file.getnchannels()
52
+ sample_width = wav_file.getsampwidth()
53
+ sample_rate = wav_file.getframerate()
54
+ frame_count = wav_file.getnframes()
55
+ frames = wav_file.readframes(frame_count)
56
+ if sample_width == 2:
57
+ return channels, sample_rate, frame_count, frames
58
+ except wave.Error:
59
+ pass
60
+
61
+ probe = subprocess.run(
62
+ [
63
+ "ffprobe",
64
+ "-v",
65
+ "error",
66
+ "-select_streams",
67
+ "a:0",
68
+ "-show_entries",
69
+ "stream=sample_rate,channels",
70
+ "-of",
71
+ "json",
72
+ str(audio_path),
73
+ ],
74
+ check=False,
75
+ capture_output=True,
76
+ text=True,
77
+ timeout=30,
78
+ )
79
+ if probe.returncode != 0:
80
+ raise ValueError(f"FFprobe konnte die WAV-Datei nicht lesen: {probe.stderr[-500:]}")
81
+ payload = json.loads(probe.stdout)
82
+ streams = payload.get("streams", [])
83
+ if not streams:
84
+ raise ValueError("Die WAV-Datei enthält keinen Audiostream.")
85
+ channels = int(streams[0]["channels"])
86
+ sample_rate = int(streams[0]["sample_rate"])
87
+ decoded = subprocess.run(
88
+ [
89
+ "ffmpeg",
90
+ "-v",
91
+ "error",
92
+ "-nostdin",
93
+ "-i",
94
+ str(audio_path),
95
+ "-f",
96
+ "s16le",
97
+ "-acodec",
98
+ "pcm_s16le",
99
+ "-",
100
+ ],
101
+ check=False,
102
+ capture_output=True,
103
+ timeout=120,
104
+ )
105
+ if decoded.returncode != 0:
106
+ detail = decoded.stderr.decode("utf-8", errors="replace")[-500:]
107
+ raise ValueError(f"FFmpeg konnte die WAV-Datei nicht dekodieren: {detail}")
108
+ frame_count = len(decoded.stdout) // (2 * channels)
109
+ return channels, sample_rate, frame_count, decoded.stdout
110
+
111
+
112
+ def analyze_wav(path: str | Path) -> AudioAnalysis:
113
+ audio_path = Path(path)
114
+ channels, sample_rate, frame_count, frames = _read_audio_as_pcm16(audio_path)
115
+ if channels < 1 or sample_rate < 1 or frame_count < 1:
116
+ raise ValueError("Die WAV-Datei enthält keine analysierbaren Audiodaten.")
117
+
118
+ samples = array("h")
119
+ samples.frombytes(frames)
120
+ if sys.byteorder != "little":
121
+ samples.byteswap()
122
+
123
+ peak = max(abs(sample) for sample in samples)
124
+ square_sum = sum(sample * sample for sample in samples)
125
+ rms = math.sqrt(square_sum / len(samples))
126
+ mono_samples = samples[::channels]
127
+ crossings = sum(
128
+ 1
129
+ for previous, current in zip(mono_samples, mono_samples[1:], strict=False)
130
+ if (previous < 0 <= current) or (previous >= 0 > current)
131
+ )
132
+ zero_crossing_rate = crossings / max(1, len(mono_samples) - 1)
133
+ peak_dbfs = _dbfs(float(peak))
134
+ rms_dbfs = _dbfs(rms)
135
+ crest_db = max(0.0, peak_dbfs - rms_dbfs)
136
+
137
+ return AudioAnalysis(
138
+ duration_s=frame_count / sample_rate,
139
+ sample_rate=sample_rate,
140
+ channels=channels,
141
+ peak_dbfs=round(peak_dbfs, 2),
142
+ rms_dbfs=round(rms_dbfs, 2),
143
+ crest_db=round(crest_db, 2),
144
+ zero_crossing_rate=round(zero_crossing_rate, 4),
145
+ character=_describe_character(rms_dbfs, crest_db, zero_crossing_rate),
146
+ )
studio/audio_io.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import subprocess
4
+ import wave
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+
9
+ def resolve_audio_path(value: Any) -> Path:
10
+ if value is None:
11
+ raise ValueError("Es wurde keine Audiodatei übergeben.")
12
+ path_attr = getattr(value, "path", None)
13
+ if path_attr:
14
+ return Path(str(path_attr))
15
+ if isinstance(value, dict) and value.get("path"):
16
+ return Path(str(value["path"]))
17
+ return Path(str(value))
18
+
19
+
20
+ def is_browser_wav(path: Path) -> bool:
21
+ try:
22
+ with wave.open(str(path), "rb") as wav_file:
23
+ return wav_file.getsampwidth() == 2 and wav_file.getnchannels() in {1, 2}
24
+ except wave.Error:
25
+ return False
26
+
27
+
28
+ def ensure_browser_wav(source: str | Path, destination: str | Path) -> Path:
29
+ source_path = resolve_audio_path(source)
30
+ dest_path = Path(destination)
31
+ dest_path.parent.mkdir(parents=True, exist_ok=True)
32
+ if not source_path.is_file():
33
+ raise ValueError("Die Audiodatei wurde nicht gefunden.")
34
+ if source_path.resolve() == dest_path.resolve() and is_browser_wav(source_path):
35
+ return dest_path
36
+ if is_browser_wav(source_path) and source_path.suffix.casefold() == ".wav":
37
+ dest_path.write_bytes(source_path.read_bytes())
38
+ return dest_path
39
+
40
+ converted = subprocess.run(
41
+ [
42
+ "ffmpeg",
43
+ "-y",
44
+ "-hide_banner",
45
+ "-nostdin",
46
+ "-i",
47
+ str(source_path),
48
+ "-ac",
49
+ "2",
50
+ "-ar",
51
+ "48000",
52
+ "-c:a",
53
+ "pcm_s16le",
54
+ str(dest_path),
55
+ ],
56
+ check=False,
57
+ capture_output=True,
58
+ timeout=120,
59
+ )
60
+ if converted.returncode != 0 or not dest_path.is_file():
61
+ detail = converted.stderr.decode("utf-8", errors="replace")[-500:]
62
+ raise ValueError(f"WAV konnte nicht browserfähig konvertiert werden: {detail}")
63
+ return dest_path
studio/library.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import re
4
+ from pathlib import Path
5
+ from uuid import uuid4
6
+
7
+ from studio.audio_analysis import analyze_wav
8
+ from studio.audio_io import ensure_browser_wav, resolve_audio_path
9
+ from studio.state import StudioSession, Track
10
+
11
+ ROLE_LABELS = {
12
+ "draft": "Draft",
13
+ "upload": "Upload",
14
+ "clean": "Clean",
15
+ "residual": "Residual",
16
+ "master": "Master",
17
+ }
18
+
19
+
20
+ def add_track(session: StudioSession, track: Track) -> StudioSession:
21
+ session.tracks.append(track)
22
+ session.master.source_track_id = track.id
23
+ return session
24
+
25
+
26
+ def library_choices(session: StudioSession) -> list[tuple[str, str]]:
27
+ counters: dict[str, int] = {}
28
+ choices: list[tuple[str, str]] = []
29
+ for track in session.tracks:
30
+ counters[track.role] = counters.get(track.role, 0) + 1
31
+ role = ROLE_LABELS[track.role]
32
+ number = counters[track.role]
33
+ filename = Path(track.path).name
34
+ choices.append((f"▶ {role} {number:02d} · {filename}", track.id))
35
+ return choices
36
+
37
+
38
+ def select_track(session: StudioSession, track_id: str) -> Track:
39
+ track = session.track_by_id(track_id)
40
+ if track is None:
41
+ raise KeyError("Der gewählte Track ist nicht mehr in dieser Session verfügbar.")
42
+ session.master.source_track_id = track.id
43
+ return track
44
+
45
+
46
+ def delete_track(session: StudioSession, track_id: str) -> Track:
47
+ track = session.track_by_id(track_id)
48
+ if track is None:
49
+ raise KeyError("Der gewählte Track ist nicht mehr in dieser Session verfügbar.")
50
+
51
+ session.tracks = [item for item in session.tracks if item.id != track_id]
52
+ session.master.source_track_id = session.tracks[-1].id if session.tracks else None
53
+
54
+ track_path = Path(track.path).resolve()
55
+ runtime_dir = Path(session.runtime_dir).resolve()
56
+ path_is_still_used = any(Path(item.path).resolve() == track_path for item in session.tracks)
57
+ if track_path.is_relative_to(runtime_dir) and not path_is_still_used:
58
+ track_path.unlink(missing_ok=True)
59
+ return track
60
+
61
+
62
+ def import_wav(session: StudioSession, source_path: str | Path) -> Track:
63
+ source = resolve_audio_path(source_path)
64
+ if not source.is_file():
65
+ raise ValueError("Die ausgewählte Audiodatei wurde nicht gefunden.")
66
+ if source.suffix.casefold() != ".wav":
67
+ raise ValueError("Bitte eine WAV-Datei importieren.")
68
+ if source.stat().st_size > 250 * 1024 * 1024:
69
+ raise ValueError("Die WAV-Datei darf maximal 250 MB groß sein.")
70
+
71
+ safe_stem = re.sub(r"[^a-zA-Z0-9_-]+", "-", source.stem).strip("-") or "audio"
72
+ destination = Path(session.runtime_dir) / f"upload-{uuid4().hex[:8]}-{safe_stem}.wav"
73
+ ensure_browser_wav(source, destination)
74
+ try:
75
+ analysis = analyze_wav(destination)
76
+ except Exception:
77
+ destination.unlink(missing_ok=True)
78
+ raise
79
+
80
+ track = Track(
81
+ id=uuid4().hex,
82
+ role="upload",
83
+ path=str(destination),
84
+ engine="local-upload",
85
+ duration_s=analysis.duration_s,
86
+ sample_rate=analysis.sample_rate,
87
+ metrics={"analysis": analysis.as_dict()},
88
+ metadata={
89
+ "original_name": source.name,
90
+ "genre_style_instruments": "",
91
+ "lyrics": "[Instrumental]",
92
+ "negative": "",
93
+ "model_id": "minimax-music3",
94
+ },
95
+ )
96
+ add_track(session, track)
97
+ return track
studio/registry.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import Any
5
+
6
+ import yaml
7
+ from pydantic import BaseModel, Field
8
+
9
+ DEFAULT_REGISTRY_PATH = Path(__file__).resolve().parents[1] / "schemas" / "model-registry.yaml"
10
+
11
+
12
+ class ModelEntry(BaseModel):
13
+ id: str
14
+ group: str
15
+ label: str
16
+ adapter: str
17
+ host: str
18
+ weights: str | None = None
19
+ space_ref: str | None = None
20
+ profile: str | None = None
21
+ capabilities: dict[str, Any] = Field(default_factory=dict)
22
+ blockers: list[str] = Field(default_factory=list)
23
+ mvp: bool | str = False
24
+
25
+ @property
26
+ def mock_label(self) -> str:
27
+ return f"{self.label} · Mock"
28
+
29
+ @property
30
+ def max_duration_s(self) -> int:
31
+ return int(self.capabilities.get("max_duration_s", 30))
32
+
33
+ @property
34
+ def supports_vocals(self) -> bool:
35
+ return bool(self.capabilities.get("vocals", False))
36
+
37
+ @property
38
+ def supports_lyrics(self) -> bool:
39
+ return bool(self.capabilities.get("lyrics", False))
40
+
41
+ @property
42
+ def route_label(self) -> str:
43
+ if self.group == "minimax":
44
+ return "MiniMax Adapter"
45
+ if self.group == "2b-audio":
46
+ return "2B-Audio Adapter"
47
+ return f"Free Adapter · {self.adapter}"
48
+
49
+
50
+ class ModelRegistry(BaseModel):
51
+ version: int
52
+ models: list[ModelEntry]
53
+
54
+ def get(self, model_id: str) -> ModelEntry:
55
+ model = next((item for item in self.models if item.id == model_id), None)
56
+ if model is None:
57
+ raise KeyError(f"Unbekanntes Modell: {model_id}")
58
+ return model
59
+
60
+ def choices(self) -> list[tuple[str, str]]:
61
+ return [(model.mock_label, model.id) for model in self.models]
62
+
63
+
64
+ def load_registry(path: Path | None = None) -> ModelRegistry:
65
+ registry_path = path or DEFAULT_REGISTRY_PATH
66
+ with registry_path.open(encoding="utf-8") as handle:
67
+ raw = yaml.safe_load(handle)
68
+ if not isinstance(raw, dict):
69
+ raise ValueError("Die Model-Registry muss ein YAML-Objekt enthalten.")
70
+ return ModelRegistry.model_validate(raw)
studio/remix.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from studio.audio_analysis import AudioAnalysis
6
+ from studio.state import Track
7
+
8
+
9
+ class RemixPlan(BaseModel):
10
+ model_id: str
11
+ genre_style_instruments: str
12
+ lyrics: str
13
+ negative: str
14
+ duration_s: int
15
+ rationale: str
16
+
17
+
18
+ def _join_unique(*values: str) -> str:
19
+ parts: list[str] = []
20
+ seen: set[str] = set()
21
+ for value in values:
22
+ for part in (item.strip() for item in value.replace(" · ", ",").split(",")):
23
+ if part and part.casefold() not in seen:
24
+ parts.append(part)
25
+ seen.add(part.casefold())
26
+ return ", ".join(parts)
27
+
28
+
29
+ def build_remix_plan(track: Track, analysis: AudioAnalysis) -> RemixPlan:
30
+ metadata = track.metadata
31
+ source_prompt = str(metadata.get("genre_style_instruments", "")).strip()
32
+ source_lyrics = str(metadata.get("lyrics", "[Instrumental]")).strip() or "[Instrumental]"
33
+ source_negative = str(metadata.get("negative", "")).strip()
34
+ model_id = str(metadata.get("model_id", "minimax-music3"))
35
+
36
+ character_prompt = (
37
+ f"reference-aware remix, preserve the core melodic and rhythmic identity, "
38
+ f"{analysis.character}, refined arrangement, intentional transitions, "
39
+ "high-definition production, controlled depth, memorable focal hook"
40
+ )
41
+ corrective_terms = [
42
+ "uncontrolled clipping",
43
+ "phase cancellation",
44
+ "muddy low mids",
45
+ "generic AI artifacts",
46
+ "unfocused arrangement",
47
+ ]
48
+ if analysis.peak_dbfs > -1.0:
49
+ corrective_terms.append("over-limited transients")
50
+ if analysis.zero_crossing_rate > 0.14:
51
+ corrective_terms.append("brittle high-frequency noise")
52
+ if analysis.crest_db < 7:
53
+ corrective_terms.append("lifeless microdynamics")
54
+
55
+ return RemixPlan(
56
+ model_id=model_id,
57
+ genre_style_instruments=_join_unique(source_prompt, character_prompt),
58
+ lyrics=source_lyrics,
59
+ negative=_join_unique(source_negative, ", ".join(corrective_terms)),
60
+ duration_s=max(5, min(30, round(analysis.duration_s))),
61
+ rationale=(
62
+ f"Referenz analysiert: {analysis.character}; RMS {analysis.rms_dbfs:.1f} dBFS, "
63
+ f"Crest {analysis.crest_db:.1f} dB. Identität bleibt erhalten, technische "
64
+ "Schwächen werden als Negativregeln zurückgeführt."
65
+ ),
66
+ )
studio/state.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import tempfile
5
+ from datetime import UTC, datetime
6
+ from pathlib import Path
7
+ from typing import Any, Literal
8
+ from uuid import uuid4
9
+
10
+ from pydantic import BaseModel, ConfigDict, Field
11
+
12
+ TrackRole = Literal["draft", "clean", "residual", "master", "upload"]
13
+ ChatRole = Literal["user", "prompt_agent", "master_agent", "system"]
14
+
15
+
16
+ class CreateSettings(BaseModel):
17
+ genre_style_instruments: str = ""
18
+ lyrics: str = ""
19
+ negative: str = ""
20
+ idea: str = ""
21
+ duration_s: int = Field(default=12, ge=5, le=300)
22
+ model_id: str = "minimax-music3"
23
+ mode: Literal["simple", "studio"] = "simple"
24
+ negative_terms: list[str] = Field(default_factory=list)
25
+ studio: dict[str, str] = Field(default_factory=dict)
26
+ renderings: dict[str, str] = Field(default_factory=dict)
27
+ last_job_id: str | None = None
28
+
29
+
30
+ class MasterSettings(BaseModel):
31
+ source_track_id: str | None = None
32
+ profile: Literal[
33
+ "streaming_safe",
34
+ "streaming_loud",
35
+ "club",
36
+ "dynamic_hifi",
37
+ "premium_clarity",
38
+ "audiophile_preserve",
39
+ ] = "streaming_safe"
40
+ purity_mode: Literal["soft_keep_air", "strict", "off"] = "soft_keep_air"
41
+ keep_air_db: float = Field(default=-20, ge=-48, le=0)
42
+ margin: float = Field(default=2.0, ge=0.5, le=2.9)
43
+ intensity: int = Field(default=50, ge=0, le=100)
44
+ last_report_id: str | None = None
45
+ human_approval: Literal["pending", "approved", "rejected", "n/a"] = "n/a"
46
+
47
+
48
+ class Track(BaseModel):
49
+ id: str
50
+ role: TrackRole
51
+ path: str
52
+ engine: str = "mock"
53
+ duration_s: float
54
+ sample_rate: int
55
+ parent_id: str | None = None
56
+ metrics: dict[str, Any] = Field(default_factory=dict)
57
+ metadata: dict[str, Any] = Field(default_factory=dict, exclude=True)
58
+
59
+
60
+ class ChatTurn(BaseModel):
61
+ role: ChatRole
62
+ text: str
63
+ actions: list[dict[str, Any]] = Field(default_factory=list)
64
+
65
+
66
+ class StudioSession(BaseModel):
67
+ model_config = ConfigDict(arbitrary_types_allowed=True)
68
+
69
+ id: str
70
+ created_at: datetime
71
+ locale: Literal["de", "en"] = "de"
72
+ create: CreateSettings = Field(default_factory=CreateSettings)
73
+ master: MasterSettings = Field(default_factory=MasterSettings)
74
+ tracks: list[Track] = Field(default_factory=list)
75
+ chat: list[ChatTurn] = Field(default_factory=list)
76
+ runtime_dir: str = Field(exclude=True)
77
+
78
+ def track_by_id(self, track_id: str | None) -> Track | None:
79
+ if not track_id:
80
+ return None
81
+ return next((track for track in self.tracks if track.id == track_id), None)
82
+
83
+ def current_track(self) -> Track | None:
84
+ selected = self.track_by_id(self.master.source_track_id)
85
+ return selected or (self.tracks[-1] if self.tracks else None)
86
+
87
+
88
+ def new_session() -> StudioSession:
89
+ session_id = uuid4().hex
90
+ output_root = Path(
91
+ os.environ.get("STUDIO_OUTPUT_DIR", Path(tempfile.gettempdir()) / "2b-studio")
92
+ )
93
+ runtime_dir = output_root / session_id
94
+ runtime_dir.mkdir(parents=True, exist_ok=True)
95
+ return StudioSession(
96
+ id=session_id,
97
+ created_at=datetime.now(UTC),
98
+ runtime_dir=str(runtime_dir),
99
+ )
studio/ui.py ADDED
@@ -0,0 +1,1976 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from hashlib import sha256
5
+ from html import escape
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ import gradio as gr
10
+
11
+ from studio.adapters import (
12
+ AdapterError,
13
+ AdapterRouter,
14
+ GenerateRequest,
15
+ OnusAdapter,
16
+ OnusRequest,
17
+ route_id_for_model,
18
+ )
19
+ from studio.agent import FieldProposal, MasterProposal, ProducerAgent, propose_master
20
+ from studio.audio_analysis import AudioAnalysis, analyze_wav
21
+ from studio.library import add_track, delete_track, import_wav, library_choices, select_track
22
+ from studio.registry import ModelRegistry, load_registry
23
+ from studio.remix import build_remix_plan
24
+ from studio.state import ChatTurn, StudioSession, new_session
25
+
26
+ KEYWORD_LEXICON = {
27
+ "◉ Genres": ("genre", ("Dark Riddim", "Afro House", "Neo Soul", "Cinematic")),
28
+ "♬ Instrumente": (
29
+ "genre",
30
+ ("Analog Bass", "Rhodes Piano", "808 Drums", "Warm Strings"),
31
+ ),
32
+ "◐ Moods": ("genre", ("Hypnotic", "Euphoric", "Melancholic", "Raw & Intimate")),
33
+ "✦ Meta-Formulierungen": (
34
+ "genre",
35
+ ("radio-ready hook", "dynamic arrangement", "human timing", "wide stereo image"),
36
+ ),
37
+ "⊘ Negativ-Prompts": (
38
+ "negative",
39
+ ("muddy low end", "harsh sibilance", "clipping", "generic AI haze"),
40
+ ),
41
+ }
42
+
43
+ PROMPT_RECIPES = {
44
+ "🔥 Club-Druck": (
45
+ "club-ready low end, punchy transient design, energetic drop, controlled stereo width",
46
+ "weak kick, muddy sub bass, flat dynamics",
47
+ ),
48
+ "✧ Vocal-Klarheit": (
49
+ "intimate lead vocal, present center image, clear diction, supportive arrangement",
50
+ "harsh sibilance, buried vocals, boxy mids, excessive reverb",
51
+ ),
52
+ "◒ Analog-Wärme": (
53
+ "warm analog character, subtle tape saturation, human timing, organic dynamics",
54
+ "brittle highs, sterile digital tone, over-compression",
55
+ ),
56
+ "◈ Cinematic-Weite": (
57
+ "cinematic depth, evolving arrangement, wide orchestral layers, emotional arc",
58
+ "static arrangement, narrow image, synthetic strings, abrupt transitions",
59
+ ),
60
+ }
61
+
62
+ LOADER_HTML = """
63
+ <div class="studio-loader" role="status" aria-live="polite">
64
+ <div class="loader-orbit"><span></span><span></span><span></span></div>
65
+ <div><strong>2B formt deinen Take</strong><small>Mock-Waveform wird lokal gerendert</small></div>
66
+ </div>
67
+ """
68
+
69
+ CSS = """
70
+ @import url('https://fonts.googleapis.com/css2?family=Chakra+Petch:wght@500;600;700&family=Manrope:wght@400;500;600;700&display=swap');
71
+ :root {
72
+ --studio-bg: #080b10;
73
+ --studio-panel: rgba(18, 23, 31, .92);
74
+ --studio-line: rgba(255, 255, 255, .09);
75
+ --studio-muted: #8f98a7;
76
+ --studio-ink: #f5f7fa;
77
+ --studio-orange: #ff6b21;
78
+ --studio-orange-soft: rgba(255, 107, 33, .16);
79
+ }
80
+ .gradio-container, .gradio-container input, .gradio-container textarea,
81
+ .gradio-container button, .gradio-container select {
82
+ font-family: "Manrope", sans-serif !important;
83
+ }
84
+ .gradio-container {
85
+ width:100% !important;
86
+ max-width:none !important;
87
+ min-height:100vh !important;
88
+ margin: 0 auto !important;
89
+ background:
90
+ radial-gradient(circle at 50% -20%, rgba(255,107,33,.15), transparent 36%),
91
+ linear-gradient(155deg, #0d1118 0%, var(--studio-bg) 58%) !important;
92
+ color: var(--studio-ink);
93
+ }
94
+ .studio-shell {
95
+ width:min(100%, 1920px) !important; min-height:100vh; margin:0 auto !important;
96
+ padding:18px clamp(16px, 2.2vw, 44px) 48px;
97
+ }
98
+ .studio-header { display:flex; align-items:center; justify-content:space-between; margin-bottom:16px; }
99
+ .studio-brand { display:flex; align-items:center; gap:12px; }
100
+ .studio-mark {
101
+ display:grid; place-items:center; width:42px; height:42px; border-radius:13px;
102
+ color:white; font-family:"Chakra Petch",sans-serif; font-weight:700;
103
+ background:linear-gradient(145deg,#ff8a3d,#e34800);
104
+ box-shadow:0 12px 34px rgba(255,107,33,.25);
105
+ }
106
+ .studio-title { font-family:"Chakra Petch",sans-serif; font-size:21px; font-weight:700; letter-spacing:.02em; }
107
+ .studio-kicker { color:var(--studio-muted); font-size:12px; letter-spacing:.12em; text-transform:uppercase; }
108
+ .phase-pill { color:#ffad7d; border:1px solid rgba(255,107,33,.35); background:var(--studio-orange-soft); padding:7px 11px; border-radius:999px; font-size:12px; }
109
+ .player-stage {
110
+ width:min(100%, 1320px) !important; max-width:none; margin:0 auto 14px; padding:18px 22px 12px;
111
+ border:1px solid var(--studio-line);
112
+ border-radius:24px; background:linear-gradient(180deg,rgba(25,31,42,.9),rgba(12,16,23,.9));
113
+ box-shadow:0 24px 80px rgba(0,0,0,.34);
114
+ }
115
+ .wave-audio { border:0 !important; background:transparent !important; }
116
+ .transport { width:min(100%, 1320px) !important; max-width:none; margin:0 auto 12px; align-items:center; }
117
+ .transport button { min-width:48px !important; border-radius:12px !important; }
118
+ .master-arrow button { color:#fff !important; border-color:rgba(255,107,33,.45) !important; background:linear-gradient(145deg,#ff752c,#dc4700) !important; font-weight:750 !important; }
119
+ .model-wrap { width:min(100%, 940px) !important; max-width:none; margin:0 auto 20px; }
120
+ .model-controls { align-items:flex-end; gap:10px; }
121
+ .instrumental-switch {
122
+ min-width:190px !important; border:1px solid rgba(255,107,33,.32) !important;
123
+ background:var(--studio-orange-soft) !important; border-radius:14px !important; padding:10px 12px !important;
124
+ }
125
+ .instrumental-switch label { font-weight:700 !important; color:#ffd0b5 !important; }
126
+ .model-status-card {
127
+ margin-top:8px; padding:10px 12px; border:1px solid rgba(255,255,255,.08); border-radius:13px;
128
+ background:rgba(7,10,15,.45);
129
+ }
130
+ .model-status-card > div:first-child { display:flex; justify-content:space-between; gap:12px; align-items:center; }
131
+ .model-status-card strong { font-family:"Chakra Petch",sans-serif; color:#f3f5f8; font-size:12px; }
132
+ .model-status-card > div:first-child span { color:#8b95a4; font-size:10px; }
133
+ .model-status-pills { display:flex; flex-wrap:wrap; gap:5px; margin:7px 0; }
134
+ .model-status-pills b, .model-status-pills span {
135
+ border:1px solid rgba(255,255,255,.075); border-radius:999px; padding:3px 6px;
136
+ color:#aeb6c2; font-size:9px; font-weight:500;
137
+ }
138
+ .model-status-pills b { color:#ffbd96; border-color:rgba(255,107,33,.3); background:rgba(255,107,33,.1); }
139
+ .model-status-card small { color:#707a89; font-size:9px; line-height:1.45; }
140
+ .model-status-card code { color:#c8ced7; }
141
+ .studio-grid {
142
+ display:grid !important; width:100% !important;
143
+ grid-template-columns:minmax(280px,320px) minmax(680px,1fr) minmax(260px,300px);
144
+ gap:18px; align-items:start;
145
+ }
146
+ .panel-card {
147
+ border:1px solid var(--studio-line) !important; border-radius:18px !important;
148
+ background:var(--studio-panel) !important; padding:14px !important;
149
+ }
150
+ .prompt-workspace-head {
151
+ display:flex; align-items:end; justify-content:space-between; gap:20px; margin:0 2px 10px;
152
+ }
153
+ .prompt-workspace-head h2 {
154
+ margin:0; font-family:"Chakra Petch",sans-serif; font-size:18px; letter-spacing:.02em;
155
+ }
156
+ .prompt-workspace-head p { margin:3px 0 0; color:#848e9d; font-size:11px; }
157
+ .prompt-workspace-badge {
158
+ flex:none; color:#ffad7d; font-size:10px; letter-spacing:.08em; text-transform:uppercase;
159
+ border:1px solid rgba(255,107,33,.28); border-radius:999px; padding:5px 8px;
160
+ }
161
+ .prompt-stack {
162
+ display:flex !important; flex-direction:column !important; width:100% !important; gap:9px !important;
163
+ }
164
+ .prompt-card {
165
+ width:100% !important;
166
+ border:1px solid rgba(255,255,255,.1) !important; border-radius:16px !important;
167
+ overflow:hidden; background:linear-gradient(180deg,rgba(26,32,43,.96),rgba(14,18,25,.96)) !important;
168
+ box-shadow:0 12px 30px rgba(0,0,0,.16);
169
+ }
170
+ .prompt-card > button, .prompt-card summary {
171
+ min-height:50px !important; padding:0 16px !important;
172
+ font-family:"Chakra Petch",sans-serif !important; font-weight:600 !important;
173
+ letter-spacing:.035em; color:#f8cfb9 !important;
174
+ }
175
+ .prompt-card:hover { border-color:rgba(255,107,33,.27) !important; }
176
+ .prompt-box { border:0 !important; background:transparent !important; }
177
+ .prompt-box textarea {
178
+ min-height:190px !important; border:1px solid rgba(255,255,255,.075) !important;
179
+ border-radius:12px !important; background:rgba(5,8,12,.5) !important; line-height:1.55 !important;
180
+ font-size:14px !important; padding:14px !important;
181
+ }
182
+ .prompt-card-footer { align-items:center !important; justify-content:space-between !important; }
183
+ .prompt-helper { color:#747e8d !important; font-size:10px !important; margin:0 !important; }
184
+ .clear-button { justify-content:flex-end !important; flex:0 0 auto !important; min-width:38px !important; }
185
+ .clear-button button {
186
+ min-width:34px !important; width:34px !important; height:30px !important; padding:0 !important;
187
+ color:#aeb5c0 !important; border-color:rgba(255,255,255,.08) !important; background:transparent !important;
188
+ }
189
+ .clear-button button:hover { color:#ff8b51 !important; border-color:rgba(255,107,33,.4) !important; }
190
+ .smart-library { padding:14px !important; }
191
+ .library-intro { margin-bottom:10px; }
192
+ .library-intro strong { font-family:"Chakra Petch",sans-serif; color:#fff; font-size:16px; }
193
+ .library-intro span { display:block; margin-top:3px; color:#848e9d; font-size:10px; line-height:1.5; }
194
+ .lexicon-card { margin-top:7px; padding:0 !important; overflow:hidden; background:rgba(5,8,12,.25) !important; }
195
+ .lexicon-card > button, .lexicon-card summary {
196
+ min-height:39px !important; font-family:"Chakra Petch",sans-serif !important; font-size:12px !important;
197
+ }
198
+ .lexicon-category { margin:10px 0 5px !important; color:#929cab !important; font-size:11px !important; letter-spacing:.08em; text-transform:uppercase; }
199
+ .chip-row { gap:5px !important; flex-wrap:wrap !important; }
200
+ .tag-chip { flex:0 1 auto !important; min-width:0 !important; }
201
+ .tag-chip button {
202
+ min-width:0 !important; min-height:28px !important; padding:4px 8px !important;
203
+ border-radius:999px !important; border:1px solid rgba(255,255,255,.09) !important;
204
+ background:rgba(255,255,255,.035) !important; color:#bdc4cf !important; font-size:10px !important;
205
+ }
206
+ .tag-chip button:hover { border-color:rgba(255,107,33,.45) !important; color:#ffd1b9 !important; background:var(--studio-orange-soft) !important; }
207
+ .recipe-grid { display:grid !important; grid-template-columns:1fr 1fr; gap:6px !important; }
208
+ .recipe-button { width:100% !important; }
209
+ .recipe-button button {
210
+ width:100% !important; min-height:42px !important; padding:7px !important; text-align:left !important;
211
+ border:1px solid rgba(255,107,33,.18) !important; border-radius:11px !important;
212
+ background:linear-gradient(145deg,rgba(255,107,33,.09),rgba(255,255,255,.025)) !important;
213
+ color:#e8c8b6 !important; font-size:10px !important;
214
+ }
215
+ .recipe-button button:hover { border-color:rgba(255,107,33,.48) !important; transform:translateY(-1px); }
216
+ .agent-card { margin-top:12px; border-color:rgba(255,107,33,.22) !important; }
217
+ .agent-title { color:#ffad7d; font-size:12px; font-weight:750; letter-spacing:.08em; text-transform:uppercase; }
218
+ .agent-composer { align-items:center; gap:8px; }
219
+ .agent-preview { margin-top:9px; }
220
+ .proposal-shell {
221
+ border:1px solid rgba(255,107,33,.32); border-radius:15px; padding:12px;
222
+ background:linear-gradient(145deg,rgba(255,107,33,.11),rgba(20,24,32,.92));
223
+ }
224
+ .proposal-head { display:flex; justify-content:space-between; gap:10px; align-items:center; margin-bottom:10px; }
225
+ .proposal-head strong { font-family:"Chakra Petch",sans-serif; font-size:14px; color:#fff; }
226
+ .proposal-model { color:#ffad7d; font-size:11px; border:1px solid rgba(255,107,33,.28); padding:4px 7px; border-radius:999px; }
227
+ .proposal-grid { display:grid; grid-template-columns:1.2fr 1fr; gap:8px; }
228
+ .proposal-item { border:1px solid rgba(255,255,255,.075); border-radius:10px; padding:9px; background:rgba(3,6,10,.32); font-size:12px; color:#d8dde5; }
229
+ .proposal-item b { display:block; color:#858f9e; font-size:9px; letter-spacing:.1em; text-transform:uppercase; margin-bottom:4px; }
230
+ .proposal-wide { grid-column:1 / -1; }
231
+ .proposal-warnings { margin:9px 0 0; padding:8px 10px 8px 26px; border-radius:9px; background:rgba(255,178,74,.08); color:#e8bd83; font-size:10px; }
232
+ .proposal-actions { justify-content:flex-end; margin-top:8px; }
233
+ .library-radio label { border-radius:11px !important; }
234
+ .library-column { position:sticky; top:12px; }
235
+ .library-preview { margin-top:8px !important; border-radius:12px !important; overflow:hidden; }
236
+ .library-preview audio { min-height:42px !important; }
237
+ .library-upload { margin-top:8px !important; }
238
+ .library-upload button { min-height:34px !important; font-size:10px !important; }
239
+ .library-head strong { font-family:"Chakra Petch",sans-serif; font-size:16px; color:#fff; }
240
+ .library-head span { display:block; margin-top:3px; color:#808a99; font-size:10px; }
241
+ .library-empty {
242
+ border:1px dashed rgba(255,255,255,.1); border-radius:11px; padding:12px !important;
243
+ color:#737d8b !important; text-align:center; font-size:10px !important;
244
+ }
245
+ .track-analysis {
246
+ margin-top:8px; border:1px solid rgba(255,107,33,.2); border-radius:12px; padding:10px;
247
+ background:rgba(255,107,33,.055);
248
+ }
249
+ .track-analysis-head { display:flex; justify-content:space-between; gap:8px; margin-bottom:8px; }
250
+ .track-analysis-head strong { font-family:"Chakra Petch",sans-serif; font-size:11px; color:#ffd0b7; }
251
+ .track-analysis-head span { font-size:9px; color:#8d96a4; }
252
+ .analysis-grid { display:grid; grid-template-columns:1fr 1fr; gap:5px; }
253
+ .analysis-metric { border:1px solid rgba(255,255,255,.065); border-radius:8px; padding:6px; font-size:9px; color:#828c9b; }
254
+ .analysis-metric b { display:block; color:#e3e7ed; font-size:11px; margin-top:2px; }
255
+ .analysis-character { margin:7px 0 0; color:#c6ccd5; font-size:10px; line-height:1.45; }
256
+ .library-actions { display:grid !important; grid-template-columns:1fr 1fr; gap:6px !important; margin-top:8px; }
257
+ .library-actions button, .library-actions a { min-width:0 !important; min-height:34px !important; font-size:10px !important; }
258
+ .remix-action button {
259
+ color:#fff !important; border-color:rgba(255,107,33,.42) !important;
260
+ background:linear-gradient(145deg,#ff752c,#d94a05) !important; font-weight:700 !important;
261
+ }
262
+ .delete-action button { color:#d18b8b !important; }
263
+ .status-strip { width:min(100%,1320px) !important; max-width:none; margin:8px auto 0; color:var(--studio-muted); text-align:center; }
264
+ .studio-loader {
265
+ max-width:560px; margin:4px auto 14px; padding:10px 14px; display:flex; align-items:center;
266
+ justify-content:center; gap:12px; border:1px solid rgba(255,107,33,.28); border-radius:14px;
267
+ background:rgba(255,107,33,.08); color:#f6d4c2;
268
+ }
269
+ .studio-loader strong { display:block; font-family:"Chakra Petch",sans-serif; font-size:13px; }
270
+ .studio-loader small { display:block; color:#9ba3af; font-size:10px; margin-top:2px; }
271
+ .loader-orbit { width:30px; height:30px; position:relative; animation:loader-spin 1.3s linear infinite; }
272
+ .loader-orbit span { position:absolute; width:8px; height:8px; border-radius:50%; background:#ff6b21; box-shadow:0 0 14px rgba(255,107,33,.7); }
273
+ .loader-orbit span:nth-child(1) { left:11px; top:0; }
274
+ .loader-orbit span:nth-child(2) { right:1px; bottom:3px; opacity:.7; }
275
+ .loader-orbit span:nth-child(3) { left:1px; bottom:3px; opacity:.4; }
276
+ @keyframes loader-spin { to { transform:rotate(360deg); } }
277
+ .master-workspace { min-height:280px; display:flex; flex-direction:column; justify-content:center; }
278
+ .orange-note { color:#ffad7d; }
279
+ footer { display:none !important; }
280
+ @media (max-width: 1390px) and (min-width: 921px) {
281
+ .studio-grid { grid-template-columns:minmax(260px,290px) minmax(600px,1fr); }
282
+ .advanced-column { grid-column:1; grid-row:1; }
283
+ .center-column { grid-column:2; grid-row:1 / span 2; }
284
+ .library-column { grid-column:1; grid-row:2; position:static; }
285
+ }
286
+ @media (max-width: 920px) {
287
+ .studio-shell { padding:12px 8px 28px; }
288
+ .studio-grid { display:flex !important; flex-direction:column !important; }
289
+ .center-column { order:1; }
290
+ .advanced-column { order:2; }
291
+ .library-column { order:3; }
292
+ .library-column { position:static; }
293
+ .prompt-card { width:100%; }
294
+ .prompt-box textarea { min-height:155px !important; }
295
+ .prompt-workspace-head { align-items:start; }
296
+ .model-controls { flex-direction:column !important; align-items:stretch; }
297
+ .instrumental-switch { min-width:0 !important; }
298
+ .proposal-grid { grid-template-columns:1fr; }
299
+ .proposal-wide { grid-column:auto; }
300
+ .phase-pill { display:none; }
301
+ .transport { gap:5px; }
302
+ .transport button { min-width:42px !important; padding-left:8px !important; padding-right:8px !important; }
303
+ }
304
+ @media (max-width: 520px) {
305
+ .recipe-grid { grid-template-columns:1fr; }
306
+ .prompt-workspace-badge { display:none; }
307
+ }
308
+ """
309
+
310
+
311
+ def _ensure_session(session: StudioSession | None) -> StudioSession:
312
+ return session or new_session()
313
+
314
+
315
+ def _source_label(session: StudioSession) -> str:
316
+ track = session.current_track()
317
+ if track is None:
318
+ return "**Quelle:** Noch kein Track gewählt"
319
+ return f"**Quelle:** `{Path(track.path).name}` · {track.engine}"
320
+
321
+
322
+ def _analysis_for_track(track: Any) -> AudioAnalysis:
323
+ analysis = analyze_wav(track.path)
324
+ track.metrics["analysis"] = analysis.as_dict()
325
+ return analysis
326
+
327
+
328
+ def _analysis_html(track: Any, analysis: AudioAnalysis | None = None) -> str:
329
+ result = analysis or _analysis_for_track(track)
330
+ channel_label = "Stereo" if result.channels == 2 else f"{result.channels} Kanal"
331
+ return f"""
332
+ <section class="track-analysis">
333
+ <div class="track-analysis-head"><strong>Referenz-Analyse</strong>
334
+ <span>{escape(Path(track.path).name)}</span></div>
335
+ <div class="analysis-grid">
336
+ <div class="analysis-metric">Dauer<b>{result.duration_s:.1f} s</b></div>
337
+ <div class="analysis-metric">Format<b>{result.sample_rate // 1000} kHz · {channel_label}</b></div>
338
+ <div class="analysis-metric">RMS<b>{result.rms_dbfs:.1f} dBFS</b></div>
339
+ <div class="analysis-metric">Crest<b>{result.crest_db:.1f} dB</b></div>
340
+ </div>
341
+ <p class="analysis-character">{escape(result.character)}</p>
342
+ </section>
343
+ """
344
+
345
+
346
+ def _download_update(track: Any | None) -> Any:
347
+ if track is None:
348
+ return gr.DownloadButton(value=None, interactive=False)
349
+ return gr.DownloadButton(value=track.path, interactive=True)
350
+
351
+
352
+ def _model_status_html(model_id: str, registry: ModelRegistry) -> str:
353
+ model = registry.get(model_id)
354
+ route_id = route_id_for_model(model)
355
+ is_live = route_id.endswith("-real")
356
+ vocals = "Gesang" if model.supports_vocals else "Instrumental"
357
+ lyrics = "Lyrics" if model.supports_lyrics else "keine Lyrics"
358
+ blockers = (
359
+ f" · Worker-Blocker: {', '.join(model.blockers)}" if model.blockers else ""
360
+ )
361
+ return f"""
362
+ <section class="model-status-card">
363
+ <div><strong>{escape(model.label)}</strong><span>{escape(model.route_label)}</span></div>
364
+ <div class="model-status-pills">
365
+ <b>{"Live-Worker" if is_live else "Mock bereit"}</b><span>{vocals}</span><span>{lyrics}</span>
366
+ <span>max. {model.max_duration_s} s</span>
367
+ </div>
368
+ <small>Route <code>{escape(route_id)}</code> über <code>{escape(model.adapter)}</code>.
369
+ {"Dummy-Fallback wird strikt als Fehler abgelehnt" if is_live else "Echter Worker noch nicht konfiguriert"}
370
+ {escape(blockers)}.</small>
371
+ </section>
372
+ """
373
+
374
+
375
+ def _model_selection_update(
376
+ model_id: str,
377
+ current_duration: float,
378
+ registry: ModelRegistry,
379
+ ) -> tuple[Any, Any, str]:
380
+ model = registry.get(model_id)
381
+ preview_limit = max(5, min(180, model.max_duration_s))
382
+ duration = max(5, min(preview_limit, round(current_duration)))
383
+ return (
384
+ gr.HTML(value=_model_status_html(model_id, registry)),
385
+ gr.Slider(minimum=5, maximum=preview_limit, value=duration, step=1),
386
+ f"Routing: {model.label} → {route_id_for_model(model)}",
387
+ )
388
+
389
+
390
+ def _empty_library_update(has_tracks: bool) -> Any:
391
+ return gr.Markdown(
392
+ value="Keine Tracks in dieser Session." if not has_tracks else "",
393
+ visible=not has_tracks,
394
+ )
395
+
396
+
397
+ def _chat_messages(session: StudioSession, agent_role: str) -> list[dict[str, str]]:
398
+ allowed = {"user", agent_role}
399
+ messages: list[dict[str, str]] = []
400
+ for turn in session.chat:
401
+ if turn.role not in allowed:
402
+ continue
403
+ role = "user" if turn.role == "user" else "assistant"
404
+ messages.append({"role": role, "content": turn.text})
405
+ return messages
406
+
407
+
408
+ def _append_keyword(current_value: str, keyword: str) -> str:
409
+ current = current_value.strip()
410
+ if keyword.casefold() in current.casefold():
411
+ return current_value
412
+ if not current:
413
+ return keyword
414
+ return f"{current.rstrip(' ,')} · {keyword}"
415
+
416
+
417
+ def _apply_prompt_recipe(
418
+ genre_value: str, negative_value: str, recipe_name: str
419
+ ) -> tuple[str, str, str]:
420
+ genre_phrase, negative_phrase = PROMPT_RECIPES[recipe_name]
421
+ return (
422
+ _append_keyword(genre_value, genre_phrase),
423
+ _append_keyword(negative_value, negative_phrase),
424
+ f"✓ Rezept „{recipe_name}“ wurde auf Sound und Negativ-Prompt verteilt.",
425
+ )
426
+
427
+
428
+ def _instrumental_update(enabled: bool, current_lyrics: str) -> tuple[Any, str]:
429
+ if enabled:
430
+ return (
431
+ gr.Textbox(value="[Instrumental]", interactive=False),
432
+ "Instrumental aktiv · Lyrics sind deaktiviert.",
433
+ )
434
+ lyrics = "" if current_lyrics.strip().casefold() == "[instrumental]" else current_lyrics
435
+ return (
436
+ gr.Textbox(value=lyrics, interactive=True),
437
+ "Instrumental aus · Lyrics können wieder bearbeitet werden.",
438
+ )
439
+
440
+
441
+ def _clear_lyrics(instrumental: bool) -> Any:
442
+ return gr.Textbox(
443
+ value="[Instrumental]" if instrumental else "",
444
+ interactive=not instrumental,
445
+ )
446
+
447
+
448
+ def _proposal_html(proposal: FieldProposal) -> str:
449
+ lyrics = escape(proposal.lyrics).replace("\n", "<br>")
450
+ warnings = "".join(f"<li>{escape(item)}</li>" for item in proposal.warnings)
451
+ return f"""
452
+ <section class="proposal-shell">
453
+ <div class="proposal-head">
454
+ <strong>Agent-Vorschlag</strong>
455
+ <span class="proposal-model">{proposal.duration_s}s · {escape(proposal.model_id or "Aktuelles Modell")}</span>
456
+ </div>
457
+ <div class="proposal-grid">
458
+ <div class="proposal-item"><b>Sound & Instrumente</b>{escape(proposal.genre_style_instruments)}</div>
459
+ <div class="proposal-item"><b>Lyrics</b>{lyrics}</div>
460
+ <div class="proposal-item"><b>Negativ</b>{escape(proposal.negative)}</div>
461
+ <div class="proposal-item"><b>Routing</b>{escape(proposal.rationale)}</div>
462
+ <div class="proposal-item proposal-wide"><b>Agentenquelle</b>{escape(proposal.source)} · Generierung startet erst nach deiner Bestätigung.</div>
463
+ </div>
464
+ {f'<ul class="proposal-warnings">{warnings}</ul>' if warnings else ''}
465
+ </section>
466
+ """
467
+
468
+
469
+ def _show_loading() -> tuple[str, Any]:
470
+ return "Job eingereiht · Worker wird vorbereitet …", gr.HTML(value=LOADER_HTML, visible=True)
471
+
472
+
473
+ def _stop_loading() -> tuple[str, Any]:
474
+ return (
475
+ "Generierung gestoppt. Bereits erzeugtes Audio bleibt erhalten.",
476
+ gr.HTML(visible=False),
477
+ )
478
+
479
+
480
+ def _generate(
481
+ session: StudioSession | None,
482
+ registry: ModelRegistry,
483
+ model_id: str,
484
+ genre: str,
485
+ lyrics: str,
486
+ negative: str,
487
+ instrumental: bool,
488
+ duration_s: int,
489
+ seed: int,
490
+ ) -> tuple[Any, ...]:
491
+ current = _ensure_session(session)
492
+ registry.get(model_id)
493
+ effective_lyrics = "[Instrumental]" if instrumental else lyrics
494
+ current.create.model_id = model_id
495
+ current.create.genre_style_instruments = genre
496
+ current.create.lyrics = effective_lyrics
497
+ current.create.negative = negative
498
+ current.create.duration_s = int(duration_s)
499
+
500
+ router = AdapterRouter(registry, Path(current.runtime_dir))
501
+ request = GenerateRequest(
502
+ model_id=model_id,
503
+ genre_style_instruments=genre,
504
+ lyrics=effective_lyrics,
505
+ negative=negative,
506
+ duration_s=int(duration_s),
507
+ advanced={
508
+ "seed": int(seed),
509
+ "instrumental": instrumental,
510
+ "studio": current.create.studio,
511
+ "project_name": "2B-Studio",
512
+ },
513
+ )
514
+ try:
515
+ track = router.generate(request)
516
+ except (AdapterError, ValueError) as error:
517
+ return (
518
+ gr.skip(),
519
+ gr.skip(),
520
+ gr.skip(),
521
+ gr.skip(),
522
+ f"✕ Generierung fehlgeschlagen: {error}",
523
+ _source_label(current),
524
+ current,
525
+ gr.HTML(visible=False),
526
+ gr.skip(),
527
+ gr.skip(),
528
+ gr.skip(),
529
+ gr.skip(),
530
+ gr.skip(),
531
+ gr.skip(),
532
+ gr.skip(),
533
+ gr.skip(),
534
+ )
535
+ add_track(current, track)
536
+ analysis = _analysis_for_track(track)
537
+ analysis_markup = _analysis_html(track, analysis)
538
+ choices = library_choices(current)
539
+ model_label = registry.get(model_id).label
540
+ mode = "Instrumental" if instrumental else "Song"
541
+ route_id = str(track.metadata["route_id"])
542
+ render_mode = "Live-Track" if str(route_id).endswith("-real") else "Mock"
543
+ status = f"✓ {mode} als {render_mode} mit {model_label} · {track.duration_s:.1f} s · {route_id}"
544
+ download_update = gr.DownloadButton(value=track.path, interactive=True)
545
+ return (
546
+ track.path,
547
+ gr.Radio(choices=choices, value=track.id),
548
+ gr.Radio(choices=choices, value=track.id),
549
+ download_update,
550
+ status,
551
+ _source_label(current),
552
+ current,
553
+ gr.HTML(visible=False),
554
+ _download_update(track),
555
+ _download_update(track),
556
+ gr.HTML(value=analysis_markup, visible=True),
557
+ gr.HTML(value=analysis_markup, visible=True),
558
+ _empty_library_update(True),
559
+ _empty_library_update(True),
560
+ track.path,
561
+ track.path,
562
+ )
563
+
564
+
565
+ def _load_track(
566
+ track_id: str, session: StudioSession | None
567
+ ) -> tuple[str | None, str | None, Any, Any, str, str, StudioSession, Any]:
568
+ current = _ensure_session(session)
569
+ if not track_id:
570
+ return (
571
+ None,
572
+ None,
573
+ _download_update(None),
574
+ _download_update(None),
575
+ "Kein Track gewählt.",
576
+ _source_label(current),
577
+ current,
578
+ gr.HTML(value="", visible=False),
579
+ )
580
+ track = select_track(current, track_id)
581
+ return (
582
+ track.path,
583
+ track.path,
584
+ _download_update(track),
585
+ _download_update(track),
586
+ f"▶ {Path(track.path).name} ist im Player geladen.",
587
+ _source_label(current),
588
+ current,
589
+ gr.HTML(value=_analysis_html(track), visible=True),
590
+ )
591
+
592
+
593
+ def _delete_selected(track_id: str, session: StudioSession | None) -> tuple[Any, ...]:
594
+ current = _ensure_session(session)
595
+ if not track_id:
596
+ raise gr.Error("Bitte zuerst einen Track in der Bibliothek auswählen.")
597
+ deleted = delete_track(current, track_id)
598
+ fallback = current.current_track()
599
+ choices = library_choices(current)
600
+ fallback_id = fallback.id if fallback else None
601
+ analysis_markup = _analysis_html(fallback) if fallback else ""
602
+ analysis_update = gr.HTML(value=analysis_markup, visible=fallback is not None)
603
+ return (
604
+ fallback.path if fallback else None,
605
+ gr.Radio(choices=choices, value=fallback_id),
606
+ gr.Radio(choices=choices, value=fallback_id),
607
+ _download_update(fallback),
608
+ _download_update(fallback),
609
+ _download_update(fallback),
610
+ f"🗑 {Path(deleted.path).name} wurde aus der Session gelöscht.",
611
+ _source_label(current),
612
+ current,
613
+ analysis_update,
614
+ gr.HTML(value=analysis_markup, visible=fallback is not None),
615
+ _empty_library_update(bool(current.tracks)),
616
+ _empty_library_update(bool(current.tracks)),
617
+ fallback.path if fallback else None,
618
+ fallback.path if fallback else None,
619
+ )
620
+
621
+
622
+ def _show_remix_loading() -> tuple[str, Any]:
623
+ return (
624
+ "Smart Remix analysiert den Referenztrack und perfektioniert die Prompts …",
625
+ gr.HTML(value=LOADER_HTML, visible=True),
626
+ )
627
+
628
+
629
+ def _smart_remix(
630
+ track_id: str,
631
+ session: StudioSession | None,
632
+ registry: ModelRegistry,
633
+ ) -> tuple[Any, ...]:
634
+ current = _ensure_session(session)
635
+ source = current.track_by_id(track_id) if track_id else current.current_track()
636
+ if source is None:
637
+ raise gr.Error("Für Smart Remix muss zuerst ein Referenztrack ausgewählt werden.")
638
+
639
+ source_analysis = _analysis_for_track(source)
640
+ plan = build_remix_plan(source, source_analysis)
641
+ try:
642
+ registry.get(plan.model_id)
643
+ model_id = plan.model_id
644
+ except KeyError:
645
+ model_id = "minimax-music3"
646
+
647
+ reference_hash = sha256(Path(source.path).read_bytes()).hexdigest()
648
+ seed = int(reference_hash[:8], 16)
649
+ router = AdapterRouter(registry, Path(current.runtime_dir))
650
+ try:
651
+ remix = router.generate(
652
+ GenerateRequest(
653
+ model_id=model_id,
654
+ genre_style_instruments=plan.genre_style_instruments,
655
+ lyrics=plan.lyrics,
656
+ negative=plan.negative,
657
+ duration_s=plan.duration_s,
658
+ advanced={
659
+ "seed": seed,
660
+ "workflow": "smart_remix",
661
+ "reference_track_id": source.id,
662
+ "reference_sha256": reference_hash,
663
+ "reference_analysis": source_analysis.as_dict(),
664
+ },
665
+ )
666
+ )
667
+ except AdapterError as error:
668
+ instrumental = current.create.lyrics.strip().casefold() == "[instrumental]"
669
+ return (
670
+ gr.skip(),
671
+ gr.skip(),
672
+ gr.skip(),
673
+ gr.skip(),
674
+ gr.skip(),
675
+ gr.skip(),
676
+ f"✕ Smart Remix fehlgeschlagen: {error}",
677
+ _source_label(current),
678
+ current,
679
+ gr.HTML(visible=False),
680
+ gr.skip(),
681
+ gr.skip(),
682
+ gr.skip(),
683
+ gr.skip(),
684
+ current.create.model_id,
685
+ current.create.genre_style_instruments,
686
+ gr.Textbox(value=current.create.lyrics, interactive=not instrumental),
687
+ current.create.negative,
688
+ instrumental,
689
+ gr.skip(),
690
+ gr.skip(),
691
+ )
692
+ remix.parent_id = source.id
693
+ remix.metadata.update(
694
+ {
695
+ "workflow": "smart_remix",
696
+ "reference_track_id": source.id,
697
+ "reference_analysis": source_analysis.as_dict(),
698
+ "remix_rationale": plan.rationale,
699
+ }
700
+ )
701
+ remix.metrics["reference_analysis"] = source_analysis.as_dict()
702
+ add_track(current, remix)
703
+ current.create.model_id = model_id
704
+ current.create.genre_style_instruments = plan.genre_style_instruments
705
+ current.create.lyrics = plan.lyrics
706
+ current.create.negative = plan.negative
707
+ current.create.duration_s = plan.duration_s
708
+
709
+ remix_analysis = _analysis_for_track(remix)
710
+ analysis_markup = _analysis_html(remix, remix_analysis)
711
+ choices = library_choices(current)
712
+ instrumental = plan.lyrics.strip().casefold() == "[instrumental]"
713
+ return (
714
+ remix.path,
715
+ gr.Radio(choices=choices, value=remix.id),
716
+ gr.Radio(choices=choices, value=remix.id),
717
+ _download_update(remix),
718
+ _download_update(remix),
719
+ _download_update(remix),
720
+ f"✓ Smart Remix erzeugt · Referenz {Path(source.path).name} · {plan.rationale}",
721
+ _source_label(current),
722
+ current,
723
+ gr.HTML(visible=False),
724
+ gr.HTML(value=analysis_markup, visible=True),
725
+ gr.HTML(value=analysis_markup, visible=True),
726
+ _empty_library_update(True),
727
+ _empty_library_update(True),
728
+ model_id,
729
+ plan.genre_style_instruments,
730
+ gr.Textbox(value=plan.lyrics, interactive=not instrumental),
731
+ plan.negative,
732
+ instrumental,
733
+ remix.path,
734
+ remix.path,
735
+ )
736
+
737
+
738
+ def _master_selected(
739
+ track_id: str,
740
+ session: StudioSession | None,
741
+ ) -> tuple[Any, Any, str, str, StudioSession, str, Any, Any, Any, Any, str, str]:
742
+ current = _ensure_session(session)
743
+ track = current.track_by_id(track_id) if track_id else current.current_track()
744
+ if track is None:
745
+ raise gr.Error("Bitte zuerst einen Track für das Mastering auswählen.")
746
+ select_track(current, track.id)
747
+ return (
748
+ gr.Column(visible=False),
749
+ gr.Column(visible=True),
750
+ "Track als Master-Quelle gesetzt · lokales ONUS ist startbereit.",
751
+ _source_label(current),
752
+ current,
753
+ track.path,
754
+ gr.Radio(choices=library_choices(current), value=track.id),
755
+ _download_update(track),
756
+ _download_update(track),
757
+ gr.HTML(value=_analysis_html(track), visible=True),
758
+ track.path,
759
+ track.path,
760
+ )
761
+
762
+
763
+ def _import_library_wav(
764
+ source_path: str | None,
765
+ session: StudioSession | None,
766
+ ) -> tuple[Any, ...]:
767
+ current = _ensure_session(session)
768
+ if not source_path:
769
+ raise gr.Error("Bitte zuerst eine WAV-Datei auswählen.")
770
+ try:
771
+ track = import_wav(current, source_path)
772
+ except ValueError as error:
773
+ raise gr.Error(str(error)) from error
774
+
775
+ choices = library_choices(current)
776
+ analysis_markup = _analysis_html(track)
777
+ return (
778
+ track.path,
779
+ track.path,
780
+ track.path,
781
+ gr.Radio(choices=choices, value=track.id),
782
+ gr.Radio(choices=choices, value=track.id),
783
+ _download_update(track),
784
+ _download_update(track),
785
+ _download_update(track),
786
+ f"✓ {Path(source_path).name} wurde lokal importiert und analysiert.",
787
+ _source_label(current),
788
+ current,
789
+ gr.HTML(value=analysis_markup, visible=True),
790
+ gr.HTML(value=analysis_markup, visible=True),
791
+ _empty_library_update(True),
792
+ _empty_library_update(True),
793
+ )
794
+
795
+
796
+ def _producer_reply(
797
+ message: str,
798
+ session: StudioSession | None,
799
+ agent: ProducerAgent,
800
+ ) -> tuple[str, list[dict[str, str]], dict[str, Any] | None, Any, Any, Any, StudioSession]:
801
+ current = _ensure_session(session)
802
+ clean_message = message.strip()
803
+ if not clean_message:
804
+ return (
805
+ "",
806
+ _chat_messages(current, "prompt_agent"),
807
+ None,
808
+ gr.HTML(visible=False),
809
+ gr.Button(interactive=False),
810
+ gr.Button(visible=False),
811
+ current,
812
+ )
813
+
814
+ action = agent.propose(clean_message, current.create.model_dump())
815
+ proposal = action.payload
816
+ warning_note = f" {len(proposal.warnings)} Hinweis(e) stehen in der Vorschau." if proposal.warnings else ""
817
+ response = (
818
+ f"{action.preview}. Genre, Lyrics, Negativ-Prompt, Dauer und Modell sind vorbereitet."
819
+ f"{warning_note} Prüfe alles und klicke **Übernehmen**; ich starte keinen Job selbst."
820
+ )
821
+ current.create.idea = clean_message
822
+ current.chat.extend(
823
+ [
824
+ ChatTurn(role="user", text=clean_message),
825
+ ChatTurn(
826
+ role="prompt_agent",
827
+ text=response,
828
+ actions=[{"type": "fill_create_fields", "payload": proposal.model_dump()}],
829
+ ),
830
+ ]
831
+ )
832
+ return (
833
+ "",
834
+ _chat_messages(current, "prompt_agent"),
835
+ proposal.model_dump(),
836
+ gr.HTML(value=_proposal_html(proposal), visible=True),
837
+ gr.Button(interactive=True),
838
+ gr.Button(visible=True),
839
+ current,
840
+ )
841
+
842
+
843
+ def _apply_proposal(
844
+ raw_proposal: dict[str, Any] | None, session: StudioSession | None
845
+ ) -> tuple[str, str, Any, str, bool, int, str, StudioSession, Any]:
846
+ current = _ensure_session(session)
847
+ if not raw_proposal:
848
+ instrumental = current.create.lyrics.strip().casefold() == "[instrumental]"
849
+ return (
850
+ current.create.model_id,
851
+ current.create.genre_style_instruments,
852
+ gr.Textbox(value=current.create.lyrics, interactive=not instrumental),
853
+ current.create.negative,
854
+ instrumental,
855
+ current.create.duration_s,
856
+ "Der Agent hat noch keinen Vorschlag erstellt.",
857
+ current,
858
+ gr.Button(interactive=False),
859
+ )
860
+ proposal = FieldProposal.model_validate(raw_proposal)
861
+ model_id = proposal.model_id or current.create.model_id
862
+ instrumental = proposal.lyrics.strip().casefold() == "[instrumental]"
863
+ current.create.model_id = model_id
864
+ current.create.genre_style_instruments = proposal.genre_style_instruments
865
+ current.create.lyrics = proposal.lyrics
866
+ current.create.negative = proposal.negative
867
+ current.create.duration_s = proposal.duration_s
868
+ current.create.idea = proposal.idea
869
+ current.create.studio = proposal.studio
870
+ current.create.renderings = proposal.renderings
871
+ return (
872
+ model_id,
873
+ proposal.genre_style_instruments,
874
+ gr.Textbox(value=proposal.lyrics, interactive=not instrumental),
875
+ proposal.negative,
876
+ instrumental,
877
+ proposal.duration_s,
878
+ "✓ Agent-Vorschlag übernommen. Alle Felder bleiben editierbar.",
879
+ current,
880
+ gr.Button(interactive=False),
881
+ )
882
+
883
+
884
+ def _discard_proposal() -> tuple[None, Any, Any, Any, str]:
885
+ return (
886
+ None,
887
+ gr.HTML(value="", visible=False),
888
+ gr.Button(interactive=False),
889
+ gr.Button(visible=False),
890
+ "Agent-Vorschlag verworfen.",
891
+ )
892
+
893
+
894
+ def _master_reply(
895
+ message: str, session: StudioSession | None
896
+ ) -> tuple[str, list[dict[str, str]], dict[str, Any] | None, Any, Any, StudioSession]:
897
+ current = _ensure_session(session)
898
+ clean_message = message.strip()
899
+ if not clean_message:
900
+ return (
901
+ "",
902
+ _chat_messages(current, "master_agent"),
903
+ None,
904
+ gr.HTML(visible=False),
905
+ gr.Button(interactive=False),
906
+ current,
907
+ )
908
+ action = propose_master(clean_message)
909
+ proposal = action.payload
910
+ response = (
911
+ f"{action.preview}. {proposal.rationale} Prüfe den Vorschlag und übernimm ihn; "
912
+ "ONUS startet nicht automatisch."
913
+ )
914
+ current.chat.extend(
915
+ [ChatTurn(role="user", text=clean_message), ChatTurn(role="master_agent", text=response)]
916
+ )
917
+ approval = (
918
+ "<p class='master-warning'>Strict benötigt anschließend eine ausdrückliche Bestätigung.</p>"
919
+ if proposal.requires_strict_approval
920
+ else ""
921
+ )
922
+ preview = f"""
923
+ <section class="proposal-shell">
924
+ <div class="proposal-head"><strong>Master-Vorschlag</strong>
925
+ <span class="proposal-model">{escape(proposal.preview)}</span></div>
926
+ <div class="proposal-grid">
927
+ <div class="proposal-item"><b>Profil</b>{escape(proposal.profile)}</div>
928
+ <div class="proposal-item"><b>Purity</b>{escape(proposal.purity_mode)}</div>
929
+ <div class="proposal-item"><b>Air / Margin</b>{proposal.keep_air_db:g} dB · {proposal.margin:g}</div>
930
+ <div class="proposal-item"><b>Begründung</b>{escape(proposal.rationale)}</div>
931
+ </div>{approval}
932
+ </section>
933
+ """
934
+ return (
935
+ "",
936
+ _chat_messages(current, "master_agent"),
937
+ proposal.model_dump(),
938
+ gr.HTML(value=preview, visible=True),
939
+ gr.Button(interactive=True),
940
+ current,
941
+ )
942
+
943
+
944
+ def _apply_master_proposal(
945
+ raw_proposal: dict[str, Any] | None,
946
+ session: StudioSession | None,
947
+ ) -> tuple[str, str, float, float, int, bool, bool, str, StudioSession]:
948
+ current = _ensure_session(session)
949
+ if not raw_proposal:
950
+ return (
951
+ current.master.profile,
952
+ current.master.purity_mode,
953
+ current.master.keep_air_db,
954
+ current.master.margin,
955
+ current.master.intensity,
956
+ False,
957
+ False,
958
+ "Der Mastering-Agent hat noch keinen Vorschlag erstellt.",
959
+ current,
960
+ )
961
+ proposal = MasterProposal.model_validate(raw_proposal)
962
+ current.master.profile = proposal.profile
963
+ current.master.purity_mode = proposal.purity_mode
964
+ current.master.keep_air_db = proposal.keep_air_db
965
+ current.master.margin = proposal.margin
966
+ current.master.intensity = proposal.intensity
967
+ current.master.human_approval = "pending" if proposal.requires_strict_approval else "n/a"
968
+ return (
969
+ proposal.profile,
970
+ proposal.purity_mode,
971
+ proposal.keep_air_db,
972
+ proposal.margin,
973
+ proposal.intensity,
974
+ proposal.analyze_only,
975
+ False,
976
+ "✓ Master-Vorschlag übernommen. ONUS wartet auf deinen Start.",
977
+ current,
978
+ )
979
+
980
+
981
+ def _master_report_html(result: Any) -> str:
982
+ loudness = result.analysis.get("loudness", {})
983
+ master = result.master or {}
984
+ after = master.get("after", {}) if isinstance(master, dict) else {}
985
+ purity = result.purity or {}
986
+ return f"""
987
+ <section class="track-analysis master-report">
988
+ <div class="track-analysis-head"><strong>ONUS Report</strong>
989
+ <span>Human approval: pending</span></div>
990
+ <div class="analysis-grid">
991
+ <div class="analysis-metric">Input LUFS<b>{loudness.get('integrated_lufs', 'n/a')}</b></div>
992
+ <div class="analysis-metric">Master LUFS<b>{after.get('integrated_lufs', 'n/a')}</b></div>
993
+ <div class="analysis-metric">True Peak<b>{after.get('true_peak_dbtp', 'n/a')}</b></div>
994
+ <div class="analysis-metric">QA<b>{master.get('perceptual_verdict', 'Analyze only')}</b></div>
995
+ <div class="analysis-metric">Residual Ratio<b>{purity.get('residual_energy_ratio', 'off')}</b></div>
996
+ <div class="analysis-metric">Air erhalten<b>{purity.get('air_preserved_db', 'off')} dB</b></div>
997
+ </div>
998
+ <p class="analysis-character">Original unverändert: {result.original_unchanged}</p>
999
+ </section>
1000
+ """
1001
+
1002
+
1003
+ def _show_master_loading() -> tuple[str, Any]:
1004
+ return "ONUS analysiert lokal · Original bleibt unverändert …", gr.HTML(
1005
+ value=LOADER_HTML, visible=True
1006
+ )
1007
+
1008
+
1009
+ def _run_onus(
1010
+ track_id: str,
1011
+ session: StudioSession | None,
1012
+ profile: str,
1013
+ purity_mode: str,
1014
+ keep_air_db: float,
1015
+ margin: float,
1016
+ intensity: int,
1017
+ analyze_only: bool,
1018
+ strict_approved: bool,
1019
+ ) -> tuple[Any, ...]:
1020
+ current = _ensure_session(session)
1021
+ source = current.track_by_id(track_id) if track_id else current.current_track()
1022
+ if source is None:
1023
+ raise gr.Error("Bitte zuerst einen Master-Quelltrack auswählen.")
1024
+ if os.environ.get("STUDIO_SPACE_MODE") == "1":
1025
+ raise gr.Error(
1026
+ "ONUS-Mastering läuft nur lokal auf dem Studio-Rechner. "
1027
+ "Im HF-Space ist die Master-Ebene deaktiviert."
1028
+ )
1029
+ try:
1030
+ result = OnusAdapter(Path(current.runtime_dir)).process(
1031
+ source,
1032
+ OnusRequest(
1033
+ profile=profile,
1034
+ purity_mode=purity_mode,
1035
+ keep_air_db=keep_air_db,
1036
+ margin=margin,
1037
+ intensity=intensity,
1038
+ analyze_only=analyze_only,
1039
+ strict_approved=strict_approved,
1040
+ ),
1041
+ )
1042
+ except (AdapterError, ValueError) as error:
1043
+ return (
1044
+ gr.skip(),
1045
+ source.path,
1046
+ gr.skip(),
1047
+ gr.skip(),
1048
+ gr.skip(),
1049
+ gr.skip(),
1050
+ gr.skip(),
1051
+ gr.skip(),
1052
+ f"✕ ONUS fehlgeschlagen: {error}",
1053
+ _source_label(current),
1054
+ current,
1055
+ gr.HTML(visible=False),
1056
+ gr.HTML(value="", visible=False),
1057
+ gr.skip(),
1058
+ gr.skip(),
1059
+ gr.skip(),
1060
+ gr.skip(),
1061
+ gr.HTML(visible=False),
1062
+ gr.skip(),
1063
+ gr.skip(),
1064
+ )
1065
+
1066
+ report_html = gr.HTML(value=_master_report_html(result), visible=True)
1067
+ if result.tracks:
1068
+ current.tracks.extend(result.tracks)
1069
+ output = result.tracks[-1]
1070
+ current.master.source_track_id = output.id
1071
+ current.master.last_report_id = Path(result.session_dir).name
1072
+ choices = library_choices(current)
1073
+ analysis_markup = _analysis_html(output)
1074
+ status = f"✓ ONUS Master fertig · {profile} · Original unverändert"
1075
+ return (
1076
+ output.path,
1077
+ source.path,
1078
+ output.path,
1079
+ gr.Radio(choices=choices, value=output.id),
1080
+ gr.Radio(choices=choices, value=output.id),
1081
+ _download_update(output),
1082
+ _download_update(output),
1083
+ _download_update(output),
1084
+ status,
1085
+ _source_label(current),
1086
+ current,
1087
+ report_html,
1088
+ gr.HTML(value=analysis_markup, visible=True),
1089
+ gr.HTML(value=analysis_markup, visible=True),
1090
+ output.path,
1091
+ output.path,
1092
+ gr.HTML(visible=False),
1093
+ _empty_library_update(True),
1094
+ _empty_library_update(True),
1095
+ gr.HTML(visible=False),
1096
+ )
1097
+
1098
+ status = "✓ ONUS Analyse abgeschlossen · keine Audiodatei verändert"
1099
+ return (
1100
+ source.path,
1101
+ source.path,
1102
+ None,
1103
+ gr.skip(),
1104
+ gr.skip(),
1105
+ _download_update(source),
1106
+ _download_update(source),
1107
+ _download_update(source),
1108
+ status,
1109
+ _source_label(current),
1110
+ current,
1111
+ report_html,
1112
+ gr.skip(),
1113
+ gr.skip(),
1114
+ source.path,
1115
+ source.path,
1116
+ gr.HTML(visible=False),
1117
+ _empty_library_update(bool(current.tracks)),
1118
+ _empty_library_update(bool(current.tracks)),
1119
+ gr.HTML(visible=False),
1120
+ )
1121
+
1122
+
1123
+ def _open_master(
1124
+ session: StudioSession | None,
1125
+ ) -> tuple[Any, Any, str, str, StudioSession, str | None]:
1126
+ current = _ensure_session(session)
1127
+ track = current.current_track()
1128
+ if track is None:
1129
+ return (
1130
+ gr.Column(visible=True),
1131
+ gr.Column(visible=False),
1132
+ "Erzeuge oder wähle zuerst einen Track.",
1133
+ _source_label(current),
1134
+ current,
1135
+ None,
1136
+ )
1137
+ if os.environ.get("STUDIO_SPACE_MODE") == "1":
1138
+ return (
1139
+ gr.Column(visible=True),
1140
+ gr.Column(visible=False),
1141
+ "ONUS-Mastering ist im HF-Space deaktiviert (nur lokal verfügbar).",
1142
+ _source_label(current),
1143
+ current,
1144
+ None,
1145
+ )
1146
+ current.master.source_track_id = track.id
1147
+ return (
1148
+ gr.Column(visible=False),
1149
+ gr.Column(visible=True),
1150
+ "Track an die lokale ONUS-Master-Ebene übergeben.",
1151
+ _source_label(current),
1152
+ current,
1153
+ track.path,
1154
+ )
1155
+
1156
+
1157
+ def _open_create(session: StudioSession | None) -> tuple[Any, Any, str, StudioSession]:
1158
+ current = _ensure_session(session)
1159
+ return (
1160
+ gr.Column(visible=True),
1161
+ gr.Column(visible=False),
1162
+ "Create-Ebene aktiv.",
1163
+ current,
1164
+ )
1165
+
1166
+
1167
+ def build_app() -> gr.Blocks:
1168
+ registry = load_registry()
1169
+ producer_agent = ProducerAgent(registry)
1170
+ model_choices = [
1171
+ (
1172
+ f"{model.label} · "
1173
+ f"{'Live' if route_id_for_model(model).endswith('-real') else 'Mock'}",
1174
+ model.id,
1175
+ )
1176
+ for model in registry.models
1177
+ ]
1178
+ lexicon_bindings: list[tuple[gr.Button, str, str]] = []
1179
+ recipe_bindings: list[tuple[gr.Button, str]] = []
1180
+
1181
+ with gr.Blocks(title="2B-Studio") as app:
1182
+ session_state = gr.State(value=new_session)
1183
+ proposal_state = gr.State(value=None)
1184
+ master_proposal_state = gr.State(value=None)
1185
+
1186
+ with gr.Column(elem_classes="studio-shell"):
1187
+ gr.HTML(
1188
+ """
1189
+ <header class="studio-header">
1190
+ <div class="studio-brand">
1191
+ <div class="studio-mark">2B</div>
1192
+ <div><div class="studio-title">2B-Studio</div>
1193
+ <div class="studio-kicker">Create · Listen · Master</div></div>
1194
+ </div>
1195
+ <div class="phase-pill">LOCAL MOCK · PHASE 1</div>
1196
+ </header>
1197
+ """
1198
+ )
1199
+
1200
+ with gr.Column(elem_classes="player-stage"):
1201
+ audio = gr.Audio(
1202
+ label="Aktueller Track",
1203
+ type="filepath",
1204
+ interactive=False,
1205
+ elem_classes="wave-audio",
1206
+ )
1207
+
1208
+ with gr.Row(elem_classes="transport"):
1209
+ download = gr.DownloadButton("⬇", interactive=False, scale=0)
1210
+ stop = gr.Button("⏹", scale=0)
1211
+ generate = gr.Button("▶ Generieren", variant="primary", scale=1)
1212
+ pause = gr.Button("⏸", scale=0)
1213
+ gr.Button("−", interactive=False, scale=0)
1214
+ master_arrow = gr.Button("Master →", scale=1, elem_classes="master-arrow")
1215
+
1216
+ status = gr.Markdown(
1217
+ "Bereit · Prompt eingeben oder den Musikproducer-Agenten fragen.",
1218
+ elem_classes="status-strip",
1219
+ )
1220
+ loading = gr.HTML(value=LOADER_HTML, visible=False)
1221
+
1222
+ with gr.Column(visible=True) as create_panel:
1223
+ with (
1224
+ gr.Column(elem_classes="model-wrap"),
1225
+ gr.Row(elem_classes="model-controls"),
1226
+ ):
1227
+ model = gr.Dropdown(
1228
+ choices=model_choices,
1229
+ value="minimax-music3",
1230
+ label="Modelauswahl",
1231
+ info="Alle Phase-1-Modelle laufen lokal über den MockAdapter",
1232
+ scale=3,
1233
+ )
1234
+ instrumental = gr.Checkbox(
1235
+ label="Instrumental",
1236
+ value=False,
1237
+ info="Gesang und Lyrics ausschalten",
1238
+ elem_classes="instrumental-switch",
1239
+ scale=1,
1240
+ )
1241
+ model_status = gr.HTML(
1242
+ value=_model_status_html("minimax-music3", registry),
1243
+ elem_classes="model-wrap",
1244
+ )
1245
+
1246
+ with gr.Row(elem_classes="studio-grid"):
1247
+ with gr.Column(elem_classes=["panel-card", "advanced-column"]):
1248
+ gr.Markdown("### Feineinstellungen")
1249
+ duration = gr.Slider(5, 180, value=12, step=1, label="Dauer · Sekunden")
1250
+ seed = gr.Number(value=2, precision=0, label="Seed")
1251
+ gr.Markdown(
1252
+ "<span class='orange-note'>Lokal & deterministisch</span><br>"
1253
+ "Echte Worker werden erst in Phase 3 verbunden."
1254
+ )
1255
+ gr.HTML(
1256
+ """
1257
+ <div class="library-intro">
1258
+ <strong>Prompt-Bibliothek</strong>
1259
+ <span>Rezepte verteilen passende Begriffe automatisch. Einzelne
1260
+ Bausteine ergänzen das zugehörige Feld ohne Duplikate.</span>
1261
+ </div>
1262
+ """
1263
+ )
1264
+ with (
1265
+ gr.Accordion(
1266
+ "✦ Intelligente Schnellrezepte",
1267
+ open=True,
1268
+ elem_classes="lexicon-card",
1269
+ ),
1270
+ gr.Row(elem_classes="recipe-grid"),
1271
+ ):
1272
+ for recipe_name in PROMPT_RECIPES:
1273
+ recipe_button = gr.Button(
1274
+ recipe_name,
1275
+ size="sm",
1276
+ elem_classes="recipe-button",
1277
+ )
1278
+ recipe_bindings.append((recipe_button, recipe_name))
1279
+ for category, (target, keywords) in KEYWORD_LEXICON.items():
1280
+ with (
1281
+ gr.Accordion(
1282
+ category,
1283
+ open=False,
1284
+ elem_classes="lexicon-card",
1285
+ ),
1286
+ gr.Row(elem_classes="chip-row"),
1287
+ ):
1288
+ for keyword in keywords:
1289
+ chip = gr.Button(
1290
+ keyword,
1291
+ size="sm",
1292
+ elem_classes="tag-chip",
1293
+ )
1294
+ lexicon_bindings.append((chip, target, keyword))
1295
+
1296
+ with gr.Column(elem_classes="center-column"):
1297
+ gr.HTML(
1298
+ """
1299
+ <div class="prompt-workspace-head">
1300
+ <div><h2>Prompt Workspace</h2>
1301
+ <p>Jeden Bereich separat öffnen, fokussiert bearbeiten und wieder einklappen.</p></div>
1302
+ <span class="prompt-workspace-badge">3 modulare Editoren</span>
1303
+ </div>
1304
+ """
1305
+ )
1306
+ with gr.Column(elem_classes="prompt-stack"):
1307
+ with gr.Accordion(
1308
+ "01 · SOUND — Genre, Style & Instrumente",
1309
+ open=True,
1310
+ elem_classes="prompt-card",
1311
+ ):
1312
+ genre = gr.Textbox(
1313
+ placeholder="z. B. dark riddim, 140 BPM, analog bass …",
1314
+ lines=7,
1315
+ show_label=False,
1316
+ elem_classes="prompt-box",
1317
+ )
1318
+ with gr.Row(elem_classes="prompt-card-footer"):
1319
+ gr.Markdown(
1320
+ "Klangbild, Tempo, Arrangement und Instrumentierung",
1321
+ elem_classes="prompt-helper",
1322
+ )
1323
+ with gr.Row(elem_classes="clear-button"):
1324
+ clear_genre = gr.Button("🗑", size="sm", min_width=34)
1325
+ with gr.Accordion(
1326
+ "02 · LYRICS — Text & Songstruktur",
1327
+ open=False,
1328
+ elem_classes="prompt-card",
1329
+ ):
1330
+ lyrics = gr.Textbox(
1331
+ placeholder="[Verse]\n…",
1332
+ lines=7,
1333
+ show_label=False,
1334
+ elem_classes="prompt-box",
1335
+ )
1336
+ with gr.Row(elem_classes="prompt-card-footer"):
1337
+ gr.Markdown(
1338
+ "Verse, Chorus, Bridge und Vocal-Anweisungen",
1339
+ elem_classes="prompt-helper",
1340
+ )
1341
+ with gr.Row(elem_classes="clear-button"):
1342
+ clear_lyrics = gr.Button("🗑", size="sm", min_width=34)
1343
+ with gr.Accordion(
1344
+ "03 · AVOID — Negativ-Prompt",
1345
+ open=False,
1346
+ elem_classes="prompt-card",
1347
+ ):
1348
+ negative = gr.Textbox(
1349
+ placeholder="muddy low end, clipping …",
1350
+ lines=7,
1351
+ show_label=False,
1352
+ elem_classes="prompt-box",
1353
+ )
1354
+ with gr.Row(elem_classes="prompt-card-footer"):
1355
+ gr.Markdown(
1356
+ "Unerwünschte Artefakte, Klangfarben und Produktionsfehler",
1357
+ elem_classes="prompt-helper",
1358
+ )
1359
+ with gr.Row(elem_classes="clear-button"):
1360
+ clear_negative = gr.Button("🗑", size="sm", min_width=34)
1361
+
1362
+ with gr.Column(elem_classes=["panel-card", "agent-card"]):
1363
+ gr.HTML("<div class='agent-title'>KI-Musikproducer · Prompt-Spezialist</div>")
1364
+ producer_chat = gr.Chatbot(value=[], height=210, show_label=False)
1365
+ with gr.Row(elem_classes="agent-composer"):
1366
+ producer_input = gr.Textbox(
1367
+ placeholder="Beschreibe deinen Song …",
1368
+ show_label=False,
1369
+ scale=5,
1370
+ )
1371
+ producer_send = gr.Button("→", variant="primary", scale=0)
1372
+ producer_preview = gr.HTML(
1373
+ value="", visible=False, elem_classes="agent-preview"
1374
+ )
1375
+ with gr.Row(elem_classes="proposal-actions"):
1376
+ discard_proposal = gr.Button(
1377
+ "Verwerfen", size="sm", visible=False
1378
+ )
1379
+ apply_proposal = gr.Button(
1380
+ "Vorschlag übernehmen",
1381
+ variant="primary",
1382
+ interactive=False,
1383
+ )
1384
+
1385
+ with gr.Column(elem_classes=["panel-card", "library-column"]):
1386
+ gr.HTML(
1387
+ """
1388
+ <div class="library-head"><strong>Track-Bibliothek</strong>
1389
+ <span>Auswählen, vorhören, remixen oder an Master übergeben.</span></div>
1390
+ """
1391
+ )
1392
+ library_create = gr.Radio(
1393
+ choices=[],
1394
+ label="Session-Tracks",
1395
+ show_label=False,
1396
+ elem_classes="library-radio",
1397
+ )
1398
+ library_upload_create = gr.File(
1399
+ label="WAV als Referenz importieren",
1400
+ file_types=[".wav"],
1401
+ type="filepath",
1402
+ elem_classes="library-upload",
1403
+ )
1404
+ library_empty_create = gr.Markdown(
1405
+ "Keine Tracks in dieser Session.", elem_classes="library-empty"
1406
+ )
1407
+ library_preview_audio_create = gr.Audio(
1408
+ label="Direkte Vorschau",
1409
+ type="filepath",
1410
+ interactive=False,
1411
+ elem_classes="library-preview",
1412
+ )
1413
+ library_analysis_create = gr.HTML(value="", visible=False)
1414
+ with gr.Row(elem_classes="library-actions"):
1415
+ library_preview_create = gr.Button("▶ Vorschau", size="sm")
1416
+ library_download_create = gr.DownloadButton(
1417
+ "⬇ Download", interactive=False
1418
+ )
1419
+ with gr.Row(elem_classes="library-actions"):
1420
+ library_remix_create = gr.Button(
1421
+ "✦ Smart Remix",
1422
+ size="sm",
1423
+ elem_classes="remix-action",
1424
+ )
1425
+ library_master_create = gr.Button("Master →", size="sm")
1426
+ library_delete_create = gr.Button(
1427
+ "🗑 Track löschen",
1428
+ size="sm",
1429
+ elem_classes="delete-action",
1430
+ )
1431
+
1432
+ with gr.Column(visible=False) as master_panel:
1433
+ back_create = gr.Button("← Zurück zu Create", size="sm")
1434
+ with gr.Row(elem_classes="studio-grid"):
1435
+ with gr.Column(elem_classes=["panel-card", "advanced-column"]):
1436
+ gr.Markdown("### Master-Fein")
1437
+ master_profile = gr.Dropdown(
1438
+ [
1439
+ ("Streaming Safe", "streaming_safe"),
1440
+ ("Streaming Loud", "streaming_loud"),
1441
+ ("Club", "club"),
1442
+ ("Dynamic Hi-Fi", "dynamic_hifi"),
1443
+ ("Premium Clarity", "premium_clarity"),
1444
+ ("Audiophile Preserve", "audiophile_preserve"),
1445
+ ],
1446
+ value="streaming_safe",
1447
+ label="Profil",
1448
+ )
1449
+ master_purity = gr.Dropdown(
1450
+ [
1451
+ ("Soft · Air behalten", "soft_keep_air"),
1452
+ ("Purity aus", "off"),
1453
+ ("Strict · Bestätigung nötig", "strict"),
1454
+ ],
1455
+ value="soft_keep_air",
1456
+ label="Purity-Modus",
1457
+ )
1458
+ master_keep_air = gr.Slider(
1459
+ -48, 0, value=-20, step=1, label="Air erhalten · dB"
1460
+ )
1461
+ master_margin = gr.Slider(
1462
+ 0.5, 2.9, value=2.0, step=0.1, label="HPSS Margin"
1463
+ )
1464
+ master_intensity = gr.Slider(
1465
+ 0, 100, value=50, step=1, label="Intensität"
1466
+ )
1467
+ master_analyze_only = gr.Checkbox(label="Nur analysieren", value=False)
1468
+ master_strict_approval = gr.Checkbox(
1469
+ label="Strict ausdrücklich bestätigen",
1470
+ value=False,
1471
+ )
1472
+ gr.Markdown(
1473
+ "Originalschutz aktiv · `soft_keep_air` ist Standard · "
1474
+ "Human Approval bleibt `pending`."
1475
+ )
1476
+
1477
+ with gr.Column(elem_classes="center-column"):
1478
+ with gr.Column(elem_classes=["panel-card", "master-workspace"]):
1479
+ master_source = gr.Markdown("**Quelle:** Noch kein Track gewählt")
1480
+ gr.Markdown("## ONUS · lokales A/B-Mastering")
1481
+ with gr.Row():
1482
+ master_source_audio = gr.Audio(
1483
+ label="A · Original",
1484
+ type="filepath",
1485
+ interactive=False,
1486
+ )
1487
+ master_result_audio = gr.Audio(
1488
+ label="B · ONUS Master",
1489
+ type="filepath",
1490
+ interactive=False,
1491
+ )
1492
+ master_report = gr.HTML(value="", visible=False)
1493
+ master_run = gr.Button(
1494
+ "ONUS ANALYZE / MASTER",
1495
+ variant="primary",
1496
+ elem_classes="master-arrow",
1497
+ )
1498
+
1499
+ with gr.Column(elem_classes=["panel-card", "agent-card"]):
1500
+ gr.HTML("<div class='agent-title'>Mastering-Agent</div>")
1501
+ master_chat = gr.Chatbot(value=[], height=210, show_label=False)
1502
+ with gr.Row():
1503
+ master_input = gr.Textbox(
1504
+ placeholder="Mehr Druck, aber Dynamik behalten …",
1505
+ show_label=False,
1506
+ scale=5,
1507
+ )
1508
+ master_send = gr.Button("→", variant="primary", scale=0)
1509
+ master_preview = gr.HTML(value="", visible=False)
1510
+ apply_master_proposal = gr.Button(
1511
+ "Master-Vorschlag übernehmen",
1512
+ interactive=False,
1513
+ )
1514
+
1515
+ with gr.Column(elem_classes=["panel-card", "library-column"]):
1516
+ gr.HTML(
1517
+ """
1518
+ <div class="library-head"><strong>Track-Bibliothek</strong>
1519
+ <span>Master-Quelle und Varianten der aktuellen Session.</span></div>
1520
+ """
1521
+ )
1522
+ library_master = gr.Radio(
1523
+ choices=[],
1524
+ label="Session-Tracks",
1525
+ show_label=False,
1526
+ elem_classes="library-radio",
1527
+ )
1528
+ library_upload_master = gr.File(
1529
+ label="WAV als Referenz importieren",
1530
+ file_types=[".wav"],
1531
+ type="filepath",
1532
+ elem_classes="library-upload",
1533
+ )
1534
+ library_empty_master = gr.Markdown(
1535
+ "Keine Tracks in dieser Session.", elem_classes="library-empty"
1536
+ )
1537
+ library_preview_audio_master = gr.Audio(
1538
+ label="Direkte Vorschau",
1539
+ type="filepath",
1540
+ interactive=False,
1541
+ elem_classes="library-preview",
1542
+ )
1543
+ library_analysis_master = gr.HTML(value="", visible=False)
1544
+ with gr.Row(elem_classes="library-actions"):
1545
+ library_preview_master = gr.Button("▶ Vorschau", size="sm")
1546
+ library_download_master = gr.DownloadButton(
1547
+ "⬇ Download", interactive=False
1548
+ )
1549
+ with gr.Row(elem_classes="library-actions"):
1550
+ library_remix_master = gr.Button(
1551
+ "✦ Smart Remix",
1552
+ size="sm",
1553
+ elem_classes="remix-action",
1554
+ )
1555
+ library_delete_master = gr.Button(
1556
+ "🗑 Löschen",
1557
+ size="sm",
1558
+ elem_classes="delete-action",
1559
+ )
1560
+
1561
+ generation_start = generate.click(
1562
+ _show_loading,
1563
+ outputs=[status, loading],
1564
+ )
1565
+ generation_event = generation_start.then(
1566
+ fn=lambda session, model_id, genre_text, lyrics_text, negative_text, instrumental_enabled, seconds, seed_value: _generate(
1567
+ session,
1568
+ registry,
1569
+ model_id,
1570
+ genre_text,
1571
+ lyrics_text,
1572
+ negative_text,
1573
+ instrumental_enabled,
1574
+ seconds,
1575
+ seed_value,
1576
+ ),
1577
+ inputs=[
1578
+ session_state,
1579
+ model,
1580
+ genre,
1581
+ lyrics,
1582
+ negative,
1583
+ instrumental,
1584
+ duration,
1585
+ seed,
1586
+ ],
1587
+ outputs=[
1588
+ audio,
1589
+ library_create,
1590
+ library_master,
1591
+ download,
1592
+ status,
1593
+ master_source,
1594
+ session_state,
1595
+ loading,
1596
+ library_download_create,
1597
+ library_download_master,
1598
+ library_analysis_create,
1599
+ library_analysis_master,
1600
+ library_empty_create,
1601
+ library_empty_master,
1602
+ library_preview_audio_create,
1603
+ library_preview_audio_master,
1604
+ ],
1605
+ )
1606
+ stop.click(
1607
+ _stop_loading,
1608
+ outputs=[status, loading],
1609
+ cancels=[generation_event],
1610
+ )
1611
+ pause.click(
1612
+ lambda: "Pause/Play steuerst du direkt im Player oberhalb der Transportleiste.",
1613
+ outputs=status,
1614
+ )
1615
+ master_arrow.click(
1616
+ _open_master,
1617
+ inputs=session_state,
1618
+ outputs=[
1619
+ create_panel,
1620
+ master_panel,
1621
+ status,
1622
+ master_source,
1623
+ session_state,
1624
+ master_source_audio,
1625
+ ],
1626
+ )
1627
+ back_create.click(
1628
+ _open_create,
1629
+ inputs=session_state,
1630
+ outputs=[create_panel, master_panel, status, session_state],
1631
+ )
1632
+
1633
+ for trigger in (producer_send, producer_input):
1634
+ event_method = trigger.click if isinstance(trigger, gr.Button) else trigger.submit
1635
+ event_method(
1636
+ lambda message, session: _producer_reply(message, session, producer_agent),
1637
+ inputs=[producer_input, session_state],
1638
+ outputs=[
1639
+ producer_input,
1640
+ producer_chat,
1641
+ proposal_state,
1642
+ producer_preview,
1643
+ apply_proposal,
1644
+ discard_proposal,
1645
+ session_state,
1646
+ ],
1647
+ )
1648
+ apply_proposal.click(
1649
+ _apply_proposal,
1650
+ inputs=[proposal_state, session_state],
1651
+ outputs=[
1652
+ model,
1653
+ genre,
1654
+ lyrics,
1655
+ negative,
1656
+ instrumental,
1657
+ duration,
1658
+ status,
1659
+ session_state,
1660
+ apply_proposal,
1661
+ ],
1662
+ )
1663
+ discard_proposal.click(
1664
+ _discard_proposal,
1665
+ outputs=[
1666
+ proposal_state,
1667
+ producer_preview,
1668
+ apply_proposal,
1669
+ discard_proposal,
1670
+ status,
1671
+ ],
1672
+ )
1673
+ instrumental.input(
1674
+ _instrumental_update,
1675
+ inputs=[instrumental, lyrics],
1676
+ outputs=[lyrics, status],
1677
+ )
1678
+ model.change(
1679
+ lambda model_id, seconds: _model_selection_update(
1680
+ model_id, seconds, registry
1681
+ ),
1682
+ inputs=[model, duration],
1683
+ outputs=[model_status, duration, status],
1684
+ )
1685
+ clear_genre.click(lambda: "", outputs=genre)
1686
+ clear_lyrics.click(_clear_lyrics, inputs=instrumental, outputs=lyrics)
1687
+ clear_negative.click(lambda: "", outputs=negative)
1688
+ for chip, target, keyword in lexicon_bindings:
1689
+ target_component = genre if target == "genre" else negative
1690
+ chip.click(
1691
+ lambda current_value, word=keyword: _append_keyword(current_value, word),
1692
+ inputs=target_component,
1693
+ outputs=target_component,
1694
+ )
1695
+ for recipe_button, recipe_name in recipe_bindings:
1696
+ recipe_button.click(
1697
+ lambda genre_value, negative_value, name=recipe_name: _apply_prompt_recipe(
1698
+ genre_value, negative_value, name
1699
+ ),
1700
+ inputs=[genre, negative],
1701
+ outputs=[genre, negative, status],
1702
+ )
1703
+ master_send.click(
1704
+ _master_reply,
1705
+ inputs=[master_input, session_state],
1706
+ outputs=[
1707
+ master_input,
1708
+ master_chat,
1709
+ master_proposal_state,
1710
+ master_preview,
1711
+ apply_master_proposal,
1712
+ session_state,
1713
+ ],
1714
+ )
1715
+ master_input.submit(
1716
+ _master_reply,
1717
+ inputs=[master_input, session_state],
1718
+ outputs=[
1719
+ master_input,
1720
+ master_chat,
1721
+ master_proposal_state,
1722
+ master_preview,
1723
+ apply_master_proposal,
1724
+ session_state,
1725
+ ],
1726
+ )
1727
+ apply_master_proposal.click(
1728
+ _apply_master_proposal,
1729
+ inputs=[master_proposal_state, session_state],
1730
+ outputs=[
1731
+ master_profile,
1732
+ master_purity,
1733
+ master_keep_air,
1734
+ master_margin,
1735
+ master_intensity,
1736
+ master_analyze_only,
1737
+ master_strict_approval,
1738
+ status,
1739
+ session_state,
1740
+ ],
1741
+ )
1742
+ library_create.input(
1743
+ _load_track,
1744
+ inputs=[library_create, session_state],
1745
+ outputs=[
1746
+ audio,
1747
+ library_preview_audio_create,
1748
+ download,
1749
+ library_download_create,
1750
+ status,
1751
+ master_source,
1752
+ session_state,
1753
+ library_analysis_create,
1754
+ ],
1755
+ )
1756
+ library_master.input(
1757
+ _load_track,
1758
+ inputs=[library_master, session_state],
1759
+ outputs=[
1760
+ audio,
1761
+ library_preview_audio_master,
1762
+ download,
1763
+ library_download_master,
1764
+ status,
1765
+ master_source,
1766
+ session_state,
1767
+ library_analysis_master,
1768
+ ],
1769
+ )
1770
+ library_master.input(
1771
+ lambda track_id, session: (
1772
+ session.track_by_id(track_id).path
1773
+ if session and session.track_by_id(track_id)
1774
+ else None
1775
+ ),
1776
+ inputs=[library_master, session_state],
1777
+ outputs=master_source_audio,
1778
+ )
1779
+ library_preview_create.click(
1780
+ _load_track,
1781
+ inputs=[library_create, session_state],
1782
+ outputs=[
1783
+ audio,
1784
+ library_preview_audio_create,
1785
+ download,
1786
+ library_download_create,
1787
+ status,
1788
+ master_source,
1789
+ session_state,
1790
+ library_analysis_create,
1791
+ ],
1792
+ )
1793
+ library_preview_master.click(
1794
+ _load_track,
1795
+ inputs=[library_master, session_state],
1796
+ outputs=[
1797
+ audio,
1798
+ library_preview_audio_master,
1799
+ download,
1800
+ library_download_master,
1801
+ status,
1802
+ master_source,
1803
+ session_state,
1804
+ library_analysis_master,
1805
+ ],
1806
+ )
1807
+
1808
+ delete_outputs = [
1809
+ audio,
1810
+ library_create,
1811
+ library_master,
1812
+ download,
1813
+ library_download_create,
1814
+ library_download_master,
1815
+ status,
1816
+ master_source,
1817
+ session_state,
1818
+ library_analysis_create,
1819
+ library_analysis_master,
1820
+ library_empty_create,
1821
+ library_empty_master,
1822
+ library_preview_audio_create,
1823
+ library_preview_audio_master,
1824
+ ]
1825
+ library_delete_create.click(
1826
+ _delete_selected,
1827
+ inputs=[library_create, session_state],
1828
+ outputs=delete_outputs,
1829
+ )
1830
+ library_delete_master.click(
1831
+ _delete_selected,
1832
+ inputs=[library_master, session_state],
1833
+ outputs=delete_outputs,
1834
+ )
1835
+
1836
+ library_master_create.click(
1837
+ _master_selected,
1838
+ inputs=[library_create, session_state],
1839
+ outputs=[
1840
+ create_panel,
1841
+ master_panel,
1842
+ status,
1843
+ master_source,
1844
+ session_state,
1845
+ audio,
1846
+ library_master,
1847
+ download,
1848
+ library_download_master,
1849
+ library_analysis_master,
1850
+ library_preview_audio_master,
1851
+ master_source_audio,
1852
+ ],
1853
+ )
1854
+
1855
+ master_start = master_run.click(
1856
+ _show_master_loading,
1857
+ outputs=[status, loading],
1858
+ )
1859
+ master_event = master_start.then(
1860
+ _run_onus,
1861
+ inputs=[
1862
+ library_master,
1863
+ session_state,
1864
+ master_profile,
1865
+ master_purity,
1866
+ master_keep_air,
1867
+ master_margin,
1868
+ master_intensity,
1869
+ master_analyze_only,
1870
+ master_strict_approval,
1871
+ ],
1872
+ outputs=[
1873
+ audio,
1874
+ master_source_audio,
1875
+ master_result_audio,
1876
+ library_create,
1877
+ library_master,
1878
+ download,
1879
+ library_download_create,
1880
+ library_download_master,
1881
+ status,
1882
+ master_source,
1883
+ session_state,
1884
+ master_report,
1885
+ library_analysis_create,
1886
+ library_analysis_master,
1887
+ library_preview_audio_create,
1888
+ library_preview_audio_master,
1889
+ loading,
1890
+ library_empty_create,
1891
+ library_empty_master,
1892
+ master_preview,
1893
+ ],
1894
+ )
1895
+ stop.click(
1896
+ _stop_loading,
1897
+ outputs=[status, loading],
1898
+ cancels=[master_event],
1899
+ )
1900
+
1901
+ remix_outputs = [
1902
+ audio,
1903
+ library_create,
1904
+ library_master,
1905
+ download,
1906
+ library_download_create,
1907
+ library_download_master,
1908
+ status,
1909
+ master_source,
1910
+ session_state,
1911
+ loading,
1912
+ library_analysis_create,
1913
+ library_analysis_master,
1914
+ library_empty_create,
1915
+ library_empty_master,
1916
+ model,
1917
+ genre,
1918
+ lyrics,
1919
+ negative,
1920
+ instrumental,
1921
+ library_preview_audio_create,
1922
+ library_preview_audio_master,
1923
+ ]
1924
+ remix_create_start = library_remix_create.click(
1925
+ _show_remix_loading,
1926
+ outputs=[status, loading],
1927
+ )
1928
+ remix_create_event = remix_create_start.then(
1929
+ lambda track_id, session: _smart_remix(track_id, session, registry),
1930
+ inputs=[library_create, session_state],
1931
+ outputs=remix_outputs,
1932
+ )
1933
+ remix_master_start = library_remix_master.click(
1934
+ _show_remix_loading,
1935
+ outputs=[status, loading],
1936
+ )
1937
+ remix_master_event = remix_master_start.then(
1938
+ lambda track_id, session: _smart_remix(track_id, session, registry),
1939
+ inputs=[library_master, session_state],
1940
+ outputs=remix_outputs,
1941
+ )
1942
+ stop.click(
1943
+ _stop_loading,
1944
+ outputs=[status, loading],
1945
+ cancels=[remix_create_event, remix_master_event],
1946
+ )
1947
+
1948
+ upload_outputs = [
1949
+ audio,
1950
+ library_preview_audio_create,
1951
+ library_preview_audio_master,
1952
+ library_create,
1953
+ library_master,
1954
+ download,
1955
+ library_download_create,
1956
+ library_download_master,
1957
+ status,
1958
+ master_source,
1959
+ session_state,
1960
+ library_analysis_create,
1961
+ library_analysis_master,
1962
+ library_empty_create,
1963
+ library_empty_master,
1964
+ ]
1965
+ library_upload_create.change(
1966
+ _import_library_wav,
1967
+ inputs=[library_upload_create, session_state],
1968
+ outputs=upload_outputs,
1969
+ )
1970
+ library_upload_master.change(
1971
+ _import_library_wav,
1972
+ inputs=[library_upload_master, session_state],
1973
+ outputs=upload_outputs,
1974
+ )
1975
+
1976
+ return app.queue(default_concurrency_limit=2)