Spaces:
Running
Running
fix: Funktions-Durchlauf – ZeroGPU X-IP-Token weiterreichen, klare Fehlermeldungen, Pausiert-Vorprüfung, Studio-Bibliothek-Sync, Verlauf-Race, Status-Refresh, UX (Stopp nur im Job, Doppelklick-Schutz, Phone-Reihenfolge, leere Zustände)
3c8a257 verified Download studio/adapters/ace_step.py from 2Btainment/2b-studio: direct link, hf CLI and curl.
- Browser
- Download file 5.9 kB
-
https://huggingface.co/spaces/2Btainment/2b-studio/resolve/main/studio/adapters/ace_step.py
- Command line
-
hf download hf://spaces/2Btainment/2b-studio/studio/adapters/ace_step.py
-
curl -L -o ace_step.py https://huggingface.co/spaces/2Btainment/2b-studio/resolve/main/studio/adapters/ace_step.py
5.9 kB
| from __future__ import annotations | |
| import os | |
| import time | |
| from collections.abc import Callable | |
| from pathlib import Path | |
| from typing import Any | |
| from uuid import uuid4 | |
| from gradio_client import Client | |
| from studio.adapters.base import ( | |
| GenerateRequest, | |
| InvalidAudioError, | |
| WorkerTimeoutError, | |
| WorkerUnavailableError, | |
| ) | |
| from studio.adapters.health import HealthCache, huggingface_token, client_headers | |
| from studio.audio_analysis import analyze_wav | |
| from studio.audio_io import ensure_browser_wav, resolve_audio_path | |
| from studio.registry import ModelEntry | |
| from studio.state import Track | |
| class AceStepAdapter: | |
| """Gradio client for the 2Btainment/ace-step ZeroGPU worker (ACE-Step v1.5).""" | |
| def __init__( | |
| self, | |
| output_dir: Path, | |
| models: tuple[ModelEntry, ...], | |
| *, | |
| space_id: str | None = None, | |
| timeout_s: float | None = None, | |
| client_factory: Callable[[], Any] | None = None, | |
| healthcheck: Callable[[], bool] | None = None, | |
| ) -> None: | |
| self.output_dir = output_dir | |
| self.output_dir.mkdir(parents=True, exist_ok=True) | |
| self.download_dir = self.output_dir / ".ace-step-downloads" | |
| self.download_dir.mkdir(exist_ok=True) | |
| self.space_id = space_id or os.environ.get("ACE_STEP_SPACE_ID", "2Btainment/ace-step") | |
| self.timeout_s = timeout_s or float(os.environ.get("ACE_STEP_TIMEOUT_S", "900")) | |
| self._models = {model.id: model for model in models} | |
| self._client_factory = client_factory | |
| self._healthcheck = healthcheck | |
| self._client: Any | None = None | |
| self._health = HealthCache(ttl_s=30.0) | |
| def model_ids(self) -> tuple[str, ...]: | |
| return tuple(self._models) | |
| def _get_client(self) -> Any: | |
| if self._client is None: | |
| if self._client_factory is not None: | |
| self._client = self._client_factory() | |
| else: | |
| token = huggingface_token() | |
| self._client = Client( | |
| self.space_id, | |
| token=token, | |
| verbose=False, | |
| headers=client_headers(), | |
| download_files=self.download_dir, | |
| httpx_kwargs={"timeout": min(self.timeout_s, 60.0)}, | |
| ) | |
| return self._client | |
| def health(self) -> bool: | |
| if self._healthcheck is not None: | |
| return self._healthcheck() | |
| return self._health.ok(self.space_id) | |
| def generate(self, request: GenerateRequest) -> Track: | |
| if request.model_id not in self._models: | |
| raise ValueError(f"AceStepAdapter unterstützt {request.model_id!r} nicht.") | |
| probe = self._health.get(self.space_id) | |
| if not probe["ok"]: | |
| raise WorkerUnavailableError( | |
| f"Worker {self.space_id} nicht bereit" | |
| f" ({probe.get('label')}: {probe.get('error')})." | |
| ) | |
| seed = int(request.advanced.get("seed", 0)) | |
| if seed == 0: | |
| seed = -1 # 0 = Zufall; ACE-Step nutzt -1 für random | |
| duration = max(10, min(600, int(request.duration_s))) | |
| steps = int(request.advanced.get("steps", 8)) | |
| prompt = request.genre_style_instruments.strip() or "modern electronic production" | |
| # Ohne Lyrics -> instrumental (Modell singt sonst frei via CoT). | |
| instrumental = bool(request.advanced.get("instrumental", not request.lyrics.strip())) | |
| client = self._get_client() | |
| started = time.monotonic() | |
| job = client.submit( | |
| prompt, | |
| duration, | |
| seed, | |
| steps, | |
| instrumental, | |
| api_name="/_generate", | |
| ) | |
| try: | |
| result = job.result(timeout=self.timeout_s) | |
| except TimeoutError as error: | |
| job.cancel() | |
| self._client = None | |
| raise WorkerTimeoutError( | |
| f"ACE-Step hat nach {self.timeout_s:.0f} Sekunden nicht geantwortet." | |
| ) from error | |
| except Exception as error: | |
| job.cancel() | |
| self._client = None | |
| raise WorkerUnavailableError(f"ACE-Step-Job abgebrochen: {error}") from error | |
| source_path = resolve_audio_path(result) | |
| if not source_path.is_file(): | |
| raise InvalidAudioError("Die vom Worker gemeldete WAV-Datei fehlt.") | |
| destination = self.output_dir / f"draft-ace-step-{uuid4().hex[:12]}.wav" | |
| try: | |
| ensure_browser_wav(source_path, destination) | |
| analysis = analyze_wav(destination) | |
| except Exception as error: | |
| destination.unlink(missing_ok=True) | |
| raise InvalidAudioError(f"Ungültiges ACE-Step-Audio: {error}") from error | |
| if analysis.duration_s <= 2.6: | |
| destination.unlink(missing_ok=True) | |
| raise InvalidAudioError("ACE-Step lieferte eine verdächtig kurze WAV.") | |
| elapsed_ms = round((time.monotonic() - started) * 1000) | |
| return Track( | |
| id=uuid4().hex, | |
| role="draft", | |
| path=str(destination), | |
| engine=f"ace-step-real:{request.model_id}", | |
| duration_s=analysis.duration_s, | |
| sample_rate=analysis.sample_rate, | |
| metrics={ | |
| "phase": 3, | |
| "duration_ms": elapsed_ms, | |
| "analysis": analysis.as_dict(), | |
| "seed": seed, | |
| "steps": steps, | |
| "instrumental": instrumental, | |
| }, | |
| metadata={ | |
| "model_id": request.model_id, | |
| "adapter_id": "ace-step-real", | |
| "genre_style_instruments": request.genre_style_instruments, | |
| "lyrics": request.lyrics, | |
| "negative": request.negative, | |
| "advanced": request.advanced, | |
| "worker_space": self.space_id, | |
| }, | |
| ) | |