Spaces:
Running
Running
File size: 19,911 Bytes
c600f78 2cdb2ce c600f78 580f0af 2cdb2ce c600f78 580f0af 2cdb2ce 580f0af c600f78 2cdb2ce c600f78 580f0af 2cdb2ce 580f0af 2cdb2ce 580f0af 2cdb2ce 580f0af 2cdb2ce 580f0af c600f78 580f0af 2cdb2ce 580f0af | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | """DAW-Backend als Gradio-API (gr.api): Generieren je Element, Übergänge, FX, Export, Projekte.
Der Browser ruft die Endpunkte über /gradio_api/call/<name> auf. Dateien liegen in DAW_DIR und
werden über /gradio_api/file= ausgeliefert; Eingabepfade werden auf erlaubte Ordner geprüft.
"""
from __future__ import annotations
import json
import os
import re
import tempfile
import threading
import time
import zipfile
from pathlib import Path
from typing import Any
from uuid import uuid4
import gradio as gr
import numpy as np
from studio.adapters.base import GenerateRequest
from studio.adapters.health import set_visitor_ip_token
from studio.adapters.router import AdapterRouter
from studio.daw import analyze, engine, jobs, stems
from studio.model_status import is_unusable, model_state
from studio.registry import load_registry
DAW_DIR = Path(os.environ.get("DAW_DIR", Path(tempfile.gettempdir()) / "2bs-daw")).resolve()
DAW_DIR.mkdir(parents=True, exist_ok=True)
GEN_DIR = DAW_DIR / "gen"
REGISTRY = load_registry()
MOCK = os.environ.get("DAW_MOCK") == "1"
if threading.current_thread() is threading.main_thread():
engine.preload()
ELEMENTS: dict[str, dict[str, Any]] = {
"full_song": {"label": "Full Song", "models": ["minimax-music3", "ace-step", "yue2"],
"tpl": "{p}", "lyrics": True, "dur": 60, "loop": False},
"voice": {"label": "Voice A-cappella", "models": ["ace-step", "yue2", "minimax-music3"],
"tpl": "a cappella, solo lead vocals only, dry studio vocal recording, no instruments, no drums, {p}",
"neg": "instruments, drums, bass, guitar, piano, synth", "lyrics": True, "dur": 30, "loop": False},
"drums": {"label": "Drums", "models": ["ace-step", "2b-musicgen-medium", "audioldm2-pub"],
"tpl": "isolated drum kit loop only, {p}, {bpm} BPM, no melody, no bass, no vocals",
"neg": "melody, bass, vocals, pads", "dur": 8, "loop": True},
"bass": {"label": "Bass", "models": ["ace-step", "2b-musicgen-medium"],
"tpl": "isolated bass line only, {p}, {bpm} BPM, key {key}, no drums, no vocals",
"neg": "drums, vocals, pads", "dur": 8, "loop": True},
"instrument": {"label": "Instrument", "models": ["ace-step", "2b-musicgen-medium", "audioldm2-pub"],
"tpl": "solo {p}, isolated instrument recording, {bpm} BPM, key {key}, no drums",
"neg": "drums, vocals", "dur": 8, "loop": True},
"instrumental_song": {"label": "Instrumental Song", "models": ["ace-step", "2b-musicgen-medium"],
"tpl": "instrumental, {p}, {bpm} BPM, key {key}, no vocals", "neg": "vocals, singing",
"dur": 30, "loop": False},
"melody_solo": {"label": "Melody (solo)", "models": ["ace-step", "2b-musicgen-medium"],
"tpl": "single monophonic lead melody, {p}, {bpm} BPM, key {key}, solo instrument, no chords, no drums",
"neg": "drums, chords, vocals", "dur": 8, "loop": True},
"melody_ensemble": {"label": "Melody Ensemble", "models": ["ace-step", "2b-musicgen-medium"],
"tpl": "melodic ensemble section playing together, {p}, {bpm} BPM, key {key}, layered harmonies, no drums",
"neg": "drums, vocals", "dur": 8, "loop": True},
"sfx": {"label": "SFX", "models": ["tangoflux", "audiogen", "audioldm2-pub", "procedural"],
"tpl": "{p}", "dur": 4, "loop": False},
"transition": {"label": "Übergang", "models": ["procedural", "tangoflux", "audiogen"],
"tpl": "{p}, transition sound effect", "dur": 4, "loop": False},
}
def _model_info(mid: str) -> dict[str, Any]:
if mid == "procedural":
return {"id": mid, "label": "2B Synth (prozedural)", "badge": "free", "badge_label": "frei", "state": "live"}
m = REGISTRY.get(mid)
try:
state = model_state(mid, REGISTRY)
except Exception: # noqa: BLE001
state = "unknown"
return {"id": mid, "label": m.label, "badge": m.license_badge, "badge_label": m.license_badge_label,
"state": state, "license": m.license, "max_s": m.max_duration_s}
def daw_catalog() -> str:
"""Elemente, passende Modelle (mit Lizenz-Badge/Status) und Plugin-Status."""
out = {"elements": [], "plugins": engine.plugin_status(), "mock": MOCK,
"stems": {"available": stems.available(), "max_s": stems.MAX_SECONDS, "warm": stems._MODEL is not None}}
for key, el in ELEMENTS.items():
out["elements"].append({"id": key, "label": el["label"], "lyrics": bool(el.get("lyrics")),
"dur": el["dur"], "loop": el["loop"], "models": [_model_info(m) for m in el["models"]]})
return json.dumps(out)
def _allowed(path: str) -> Path:
p = Path(path).resolve()
roots = [DAW_DIR, Path(os.environ.get("GRADIO_TEMP_DIR", Path(tempfile.gettempdir()) / "gradio")).resolve()]
if not any(str(p).startswith(str(r) + os.sep) for r in roots) or not p.is_file():
raise gr.Error("Datei nicht erlaubt oder nicht gefunden.")
return p
def _out(data: np.ndarray, sr: int, stem: str, **meta: Any) -> str:
dest = DAW_DIR / f"{stem}-{uuid4().hex[:10]}.wav"
engine.save(dest, data, sr)
return json.dumps({"path": str(dest), "url": f"/gradio_api/file={dest}", **engine.stats(data, sr), **meta})
def _visitor(request: gr.Request | None) -> None:
try:
token = request.headers.get("x-ip-token") if request is not None else None
except Exception: # noqa: BLE001
token = None
set_visitor_ip_token(token)
def _run_model(mid: str, prompt: str, negative: str, lyrics: str, seconds: float, seed: int) -> tuple[np.ndarray, int, str]:
if mid == "procedural" or MOCK:
kind = engine.procedural_kind(prompt)
return engine.procedural(kind, max(0.5, seconds), seed=seed), engine.SR, f"2B Synth · {kind}"
m = REGISTRY.get(mid)
dur = int(max(1, min(m.max_duration_s, round(seconds))))
req = GenerateRequest(model_id=mid, genre_style_instruments=prompt[:900], lyrics=lyrics, negative=negative,
duration_s=max(1, min(300, dur)),
advanced={"seed": seed, "instrumental": not lyrics.strip()})
track = AdapterRouter(REGISTRY, GEN_DIR).generate(req)
data, sr = engine.load(track.path)
return data, sr, m.label
def daw_generate(element: str, model: str, prompt: str, negative: str, lyrics: str, seconds: float, bpm: float,
key: str, seed: float, request: gr.Request) -> str:
"""Element craften. model='auto' probiert die passenden Modelle der Reihe nach."""
_visitor(request)
el = ELEMENTS.get(element)
if el is None:
raise gr.Error("Unbekanntes Element.")
bpm = float(bpm or 120)
p = el["tpl"].format(p=(prompt or el["label"]).strip(), bpm=int(bpm), key=(key or "C minor").strip())
neg = ", ".join(x for x in (negative.strip(), el.get("neg", "")) if x)
lyr = lyrics if el.get("lyrics") else ""
seconds = float(seconds or el["dur"])
candidates = [model] if model and model != "auto" else [
m for m in el["models"] if m == "procedural" or not is_unusable(model_state(m, REGISTRY))]
errors: list[str] = []
started = time.monotonic()
for mid in candidates[:3]:
try:
data, sr, label = _run_model(mid, p, neg, lyr, seconds, int(seed or 0))
break
except Exception as error: # noqa: BLE001
errors.append(f"{mid}: {str(error)[:160]}")
else:
raise gr.Error("Kein Modell lieferte Audio. " + " | ".join(errors)[:600])
if el["loop"]:
data = engine.stretch_to_bpm(data, sr, _bpm_guess(data, sr), bpm)
data = engine.fit(data, sr, seconds, loop=el["loop"])
if engine.peak_db(data) > -0.3: # Clipping-Schutz für Worker, die über 0 dBFS liefern
data, _ = engine.fx_normalize(data, sr, peak=-1.0)
info = _model_info(mid)
return _out(data, sr, element, model=mid, model_label=label, badge=info["badge"], badge_label=info["badge_label"],
prompt=p, elapsed_s=round(time.monotonic() - started, 1), fallback_errors=errors)
def _bpm_guess(data: np.ndarray, sr: int) -> float | None:
try:
from studio.studio_room.dsp import _cheap_bpm
return _cheap_bpm(data.mean(axis=0), sr)
except Exception: # noqa: BLE001
return None
def daw_transition(path_a: str, path_b: str, prompt: str, model: str, seconds: float, request: gr.Request) -> str:
"""Übergang zwischen zwei Clips: SFX (Riser/Impact) per Prompt + Crossfade-Vorschau."""
_visitor(request)
seconds = float(seconds or 4)
res = json.loads(daw_generate("transition", model or "auto", prompt or "riser", "", "", seconds, 120, "", 0, request))
if path_a and path_b:
a, sr = engine.load(_allowed(path_a))
b, srb = engine.load(_allowed(path_b))
if srb == sr:
xf = engine.mix_crossfade(a[:, -int(seconds * sr):], b[:, :int(seconds * sr)], sr, seconds / 2)
res["crossfade"] = json.loads(_out(xf, sr, "xfade"))
return json.dumps(res)
def daw_fx(path: str, effect: str, params: str, start: float, end: float) -> str:
"""Destruktiver Effekt auf einen Clip-Bereich; liefert neue Datei (Original bleibt)."""
data, sr = engine.load(_allowed(path))
s = max(0, int(float(start or 0) * sr))
e = int(float(end) * sr) if end and float(end) > 0 else data.shape[1]
seg = data[:, s:e]
if seg.shape[1] < 64:
raise gr.Error("Bereich zu kurz.")
before = engine.stats(seg, sr)
out, eng = engine.apply_effect(seg, sr, effect, json.loads(params or "{}"))
return _out(out, sr, f"fx-{effect}", engine=eng, before=before)
def daw_encode(path: str, fmt: str) -> str:
"""Mixdown (WAV aus dem Browser) → MP3 320 oder WAV 24-bit."""
data, sr = engine.load(_allowed(path))
if fmt == "mp3":
dest = DAW_DIR / f"2b-studio-mix-{uuid4().hex[:8]}.mp3"
engine.save(dest, data, sr, quality=320)
else:
dest = DAW_DIR / f"2b-studio-mix-{uuid4().hex[:8]}.wav"
engine.save(dest, data, sr)
return json.dumps({"path": str(dest), "url": f"/gradio_api/file={dest}", **engine.stats(data, sr)})
def _safe_name(name: str) -> str:
return re.sub(r"[^A-Za-z0-9_-]+", "-", name or "projekt").strip("-")[:48] or "projekt"
def daw_save(project: str, title: str, oauth_token: gr.OAuthToken | None = None,
profile: gr.OAuthProfile | None = None) -> str:
"""Projekt (JSON + Audio) als .2bproj.zip; mit HF-Login zusätzlich ins private Dataset des Nutzers."""
proj = json.loads(project)
name = _safe_name(title)
dest = DAW_DIR / f"{name}-{time.strftime('%Y%m%d-%H%M%S')}.2bproj.zip"
with zipfile.ZipFile(dest, "w", zipfile.ZIP_DEFLATED) as z:
for asset in proj.get("assets", []):
src = _allowed(asset["path"])
arc = f"audio/{asset['id']}{src.suffix}"
z.write(src, arc)
asset["path"] = arc
asset.pop("url", None)
z.writestr("project.json", json.dumps(proj, ensure_ascii=False, indent=1))
result: dict[str, Any] = {"path": str(dest), "url": f"/gradio_api/file={dest}", "hf": None}
if oauth_token is not None and profile is not None and os.environ.get("DAW_HF_SAVE", "1") == "1":
try:
from huggingface_hub import HfApi
user = str(profile.username or "")
if not USER_RX.fullmatch(user):
raise ValueError("Ungültiger HF-Benutzername.")
api = HfApi(token=oauth_token.token)
repo = _private_repo(api, user)
api.upload_file(path_or_fileobj=str(dest), path_in_repo=f"projects/{dest.name}", repo_id=repo,
repo_type="dataset", commit_message=f"2B-Studio: {name}")
result["hf"] = f"https://huggingface.co/datasets/{repo}"
except Exception as error: # noqa: BLE001
result["hf_error"] = str(error)[:200]
return json.dumps(result)
def daw_load(path: str) -> str:
""".2bproj.zip entpacken → Projekt-JSON mit abspielbaren URLs."""
src = _allowed(path)
target = DAW_DIR / f"proj-{uuid4().hex[:10]}"
target.mkdir()
with zipfile.ZipFile(src) as z:
for member in z.namelist():
if member.startswith("/") or ".." in member:
raise gr.Error("Ungültige Projektdatei.")
z.extractall(target)
proj = json.loads((target / "project.json").read_text(encoding="utf-8"))
for asset in proj.get("assets", []):
p = (target / asset["path"]).resolve()
asset["path"] = str(p)
asset["url"] = f"/gradio_api/file={p}"
return json.dumps(proj)
STEM_LABELS = {"vocals": "Vocals", "drums": "Drums", "bass": "Bass", "other": "Other"}
def daw_stems(path: str, start: float, end: float) -> str:
"""Startet Demucs-Stem-Trennung (htdemucs, CPU) als Job → {'job': id}."""
if not stems.available():
raise gr.Error("Stem-Trennung ist auf diesem Server nicht installiert.")
data, sr = engine.load(_allowed(path))
s = max(0, int(float(start or 0) * sr))
e = int(float(end) * sr) if end and float(end) > 0 else data.shape[1]
seg = data[:, s:e]
if seg.shape[1] / sr > stems.MAX_SECONDS + 0.5:
raise gr.Error(f"Clip ist {seg.shape[1] / sr:.0f} s lang – auf CPU max. {int(stems.MAX_SECONDS)} s. Bitte vorher schneiden.")
def work(report):
parts, msr = stems.separate(seg, sr, report)
out = []
for name, audio in parts.items():
info = json.loads(_out(audio, msr, f"stem-{name}"))
out.append({"stem": name, "label": STEM_LABELS.get(name, name), **info})
return out
secs = seg.shape[1] / sr
try:
job = jobs.start("stems", work, est_s=stems.estimate_s(secs), timeout_s=max(240.0, secs * 6 + 120))
except RuntimeError as error:
raise gr.Error(str(error)) from None
return json.dumps({"job": job, "seconds": round(secs, 1), "est_s": round(stems.estimate_s(secs))})
def daw_job(job_id: str) -> str:
job = jobs.get(job_id)
if job is None:
raise gr.Error("Job unbekannt oder abgelaufen.")
return json.dumps({k: job[k] for k in ("id", "state", "progress", "msg", "result", "error", "queue", "wait_s", "eta_s")})
def daw_job_cancel(job_id: str) -> str:
return json.dumps({"cancelled": jobs.cancel(job_id)})
_ANALYSIS: dict[str, dict] = {}
def daw_analyze(path: str, start: float, end: float) -> str:
"""Tempo und Tonart eines Clip-Bereichs (CPU, max. 60 s)."""
src = _allowed(path)
key = f"{src}|{float(start or 0):.2f}|{float(end or 0):.2f}"
if key not in _ANALYSIS:
data, sr = engine.load(src)
s = max(0, int(float(start or 0) * sr))
e = int(float(end) * sr) if end and float(end) > 0 else data.shape[1]
_ANALYSIS[key] = analyze.analyze(data[:, s:e], sr)
if len(_ANALYSIS) > 512:
_ANALYSIS.pop(next(iter(_ANALYSIS)))
return json.dumps(_ANALYSIS[key])
def daw_whoami(profile: gr.OAuthProfile | None = None) -> str:
user = str(getattr(profile, "username", "") or "") if profile is not None else ""
ok = bool(user) and os.environ.get("DAW_HF_SAVE", "1") == "1"
return json.dumps({"user": user or None, "cloud": ok})
USER_RX = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,95}")
SESSION_RX = re.compile(r"[a-z0-9]{6,32}")
_UPLOADED: dict[str, set[str]] = {}
def _private_repo(api: Any, user: str) -> str:
"""Privates Dataset <user>/2b-studio-projects anlegen/prüfen – niemals öffentlich schreiben."""
if not USER_RX.fullmatch(user):
raise ValueError("Ungültiger HF-Benutzername.")
repo = f"{user}/2b-studio-projects"
api.create_repo(repo, repo_type="dataset", private=True, exist_ok=True)
info = api.repo_info(repo, repo_type="dataset")
if not getattr(info, "private", False):
raise PermissionError(f"{repo} ist öffentlich – aus Sicherheitsgründen wird dort nichts gespeichert.")
return repo
def daw_autosave(project: str, title: str, session: str, oauth_token: gr.OAuthToken | None = None,
profile: gr.OAuthProfile | None = None) -> str:
"""Cloud-Autosave: Projekt-JSON + noch nicht hochgeladene Audios (inhaltsadressiert) in EINEM Commit
nach autosave/<session>/ im privaten Dataset. Jede Browser-Sitzung schreibt nur ihren eigenen Ordner."""
if oauth_token is None or profile is None or os.environ.get("DAW_HF_SAVE", "1") != "1":
return json.dumps({"ok": False, "reason": "login"})
if not SESSION_RX.fullmatch(session or ""):
raise gr.Error("Ungültige Sitzungs-ID.")
import hashlib
from huggingface_hub import CommitOperationAdd, HfApi
user = str(profile.username or "")
api = HfApi(token=oauth_token.token)
try:
repo = _private_repo(api, user)
except Exception as error: # noqa: BLE001
return json.dumps({"ok": False, "reason": "repo", "error": str(error)[:200]})
proj = json.loads(project)
base = f"autosave/{session}"
known = _UPLOADED.setdefault(repo, set())
if not known:
try:
known.update(f for f in api.list_repo_files(repo, repo_type="dataset") if f.startswith("autosave/"))
except Exception: # noqa: BLE001
pass
ops = []
for asset in proj.get("assets", []):
src = _allowed(asset["path"])
digest = hashlib.sha1(src.read_bytes()).hexdigest()[:20]
arc = f"{base}/audio/{digest}{src.suffix}"
asset["path"], asset["sha1"] = arc, digest
asset.pop("url", None)
if arc not in known:
ops.append(CommitOperationAdd(path_in_repo=arc, path_or_fileobj=str(src)))
proj["saved_at"] = time.time()
ops.append(CommitOperationAdd(path_in_repo=f"{base}/project.json",
path_or_fileobj=json.dumps(proj, ensure_ascii=False).encode("utf-8")))
try:
info = api.create_commit(repo, operations=ops, repo_type="dataset",
commit_message=f"2B-Studio Autosave: {_safe_name(title)} ({session})")
except Exception as error: # noqa: BLE001
return json.dumps({"ok": False, "reason": "commit", "error": str(error)[:200]})
known.update(op.path_in_repo for op in ops)
return json.dumps({"ok": True, "repo": f"https://huggingface.co/datasets/{repo}", "path": base,
"files": len(ops), "commit": getattr(info, "oid", None), "at": proj["saved_at"]})
def _prewarm_stems() -> None:
if stems.available() and os.environ.get("DAW_STEMS_PREWARM", "1") == "1":
def warm():
time.sleep(3)
try:
stems.warm()
except Exception: # noqa: BLE001
pass
threading.Thread(target=warm, daemon=True, name="stems-prewarm").start()
def register(app: gr.Blocks) -> None:
"""Alle Endpunkte registrieren (innerhalb von `with gr.Blocks()`)."""
gr.api(daw_catalog, api_name="daw_catalog")
gr.api(daw_generate, api_name="daw_generate", concurrency_limit=6)
gr.api(daw_transition, api_name="daw_transition", concurrency_limit=6)
gr.api(daw_fx, api_name="daw_fx", concurrency_limit=2)
gr.api(daw_encode, api_name="daw_encode", concurrency_limit=2)
gr.api(daw_save, api_name="daw_save", concurrency_limit=2)
gr.api(daw_load, api_name="daw_load", concurrency_limit=2)
gr.api(daw_stems, api_name="daw_stems", concurrency_limit=2)
gr.api(daw_job, api_name="daw_job", concurrency_limit=None)
gr.api(daw_job_cancel, api_name="daw_job_cancel", concurrency_limit=None)
gr.api(daw_analyze, api_name="daw_analyze", concurrency_limit=2)
gr.api(daw_whoami, api_name="daw_whoami", concurrency_limit=None)
gr.api(daw_autosave, api_name="daw_autosave", concurrency_limit=2)
_prewarm_stems()
|