Spaces:
Running
Running
Download studio/adapters/mock.py from 2Btainment/2b-studio: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/spaces/2Btainment/2b-studio/resolve/main/studio/adapters/mock.py
- Command line
-
hf download hf://spaces/2Btainment/2b-studio/studio/adapters/mock.py
-
curl -L -o mock.py https://huggingface.co/spaces/2Btainment/2b-studio/resolve/main/studio/adapters/mock.py
3.43 kB
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import math | |
| import sys | |
| import wave | |
| from array import array | |
| from pathlib import Path | |
| from uuid import uuid4 | |
| from studio.adapters.base import GenerateRequest | |
| from studio.state import Track | |
| class MockAdapter: | |
| """Create a deterministic, lightweight chord preview without a model worker.""" | |
| def __init__( | |
| self, | |
| output_dir: Path, | |
| model_ids: tuple[str, ...], | |
| adapter_id: str = "generic-mock", | |
| ) -> None: | |
| self.output_dir = output_dir | |
| self.output_dir.mkdir(parents=True, exist_ok=True) | |
| self._model_ids = model_ids | |
| self.adapter_id = adapter_id | |
| def model_ids(self) -> tuple[str, ...]: | |
| return self._model_ids | |
| def health(self) -> bool: | |
| return self.output_dir.is_dir() and self.output_dir.exists() | |
| def generate(self, request: GenerateRequest) -> Track: | |
| if request.model_id not in self._model_ids: | |
| raise ValueError(f"MockAdapter unterstützt {request.model_id!r} nicht.") | |
| signature = json.dumps(request.model_dump(), sort_keys=True, ensure_ascii=False) | |
| digest = hashlib.sha256(signature.encode("utf-8")).hexdigest() | |
| output_path = self.output_dir / f"draft-{digest[:12]}.wav" | |
| sample_rate = 24_000 | |
| self._write_preview(output_path, request.duration_s, sample_rate, digest) | |
| return Track( | |
| id=uuid4().hex, | |
| role="draft", | |
| path=str(output_path), | |
| engine=f"{self.adapter_id}:{request.model_id}", | |
| duration_s=float(request.duration_s), | |
| sample_rate=sample_rate, | |
| metrics={"deterministic": True, "phase": 2, "adapter_id": self.adapter_id}, | |
| metadata={ | |
| "model_id": request.model_id, | |
| "adapter_id": self.adapter_id, | |
| "genre_style_instruments": request.genre_style_instruments, | |
| "lyrics": request.lyrics, | |
| "negative": request.negative, | |
| "advanced": request.advanced, | |
| }, | |
| ) | |
| def _write_preview(path: Path, duration_s: int, sample_rate: int, digest: str) -> None: | |
| frequencies = [ | |
| 110 + int(digest[0:2], 16), | |
| 220 + int(digest[2:4], 16), | |
| 330 + int(digest[4:6], 16), | |
| ] | |
| frame_count = duration_s * sample_rate | |
| fade_frames = max(1, int(sample_rate * 0.12)) | |
| pcm = array("h") | |
| for index in range(frame_count): | |
| time_s = index / sample_rate | |
| fade_in = min(1.0, index / fade_frames) | |
| fade_out = min(1.0, (frame_count - index - 1) / fade_frames) | |
| envelope = min(fade_in, fade_out) | |
| pulse = 0.70 + (0.30 * max(0.0, math.sin(2 * math.pi * 2 * time_s))) | |
| signal = sum( | |
| math.sin(2 * math.pi * frequency * time_s) / (position + 1) | |
| for position, frequency in enumerate(frequencies) | |
| ) | |
| sample = int(max(-1.0, min(1.0, signal * 0.22 * envelope * pulse)) * 32767) | |
| pcm.extend((sample, sample)) | |
| if sys.byteorder != "little": | |
| pcm.byteswap() | |
| with wave.open(str(path), "wb") as wav_file: | |
| wav_file.setnchannels(2) | |
| wav_file.setsampwidth(2) | |
| wav_file.setframerate(sample_rate) | |
| wav_file.writeframes(pcm.tobytes()) | |