Spaces:
Running
Running
Cast real people with their own voices, sell the product you attached, and take five-minute ads
7dfe8d4 Download scripts/clone_voices.py from userIdc2024/creative-brain-os: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/spaces/userIdc2024/creative-brain-os/resolve/main/scripts/clone_voices.py
- Command line
-
hf download hf://spaces/userIdc2024/creative-brain-os/scripts/clone_voices.py
-
curl -L -o clone_voices.py https://huggingface.co/spaces/userIdc2024/creative-brain-os/resolve/main/scripts/clone_voices.py
2.97 kB
| """Give every avatar a MiniMax cloned voice from its own sample. | |
| python scripts/clone_voices.py # plan: how many, what it costs | |
| python scripts/clone_voices.py --apply # clone, pin, write voice_id | |
| python scripts/clone_voices.py --apply --limit 5 | |
| Resumable: only rows with an empty voice_id are touched, and each id is written | |
| the moment it exists. A cloned voice is deleted unless a TTS call uses it within | |
| 7 days, so each one is pinned with one short line right after the clone — that | |
| also proves the id works before it is saved. $1.50 a clone plus a cent of TTS. | |
| """ | |
| from __future__ import annotations | |
| import signal | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| from app.services import fal, media # noqa: E402 | |
| from app.services.speech import MINIMAX_MODEL # noqa: E402 | |
| from app.services.supabase_store import SupabaseStore # noqa: E402 | |
| CLONE_USD = 1.50 | |
| PIN_LINE = "Hi, it's me. This is my voice, and I'll be reading the script." | |
| def clone(audio: bytes, slug: str) -> str: | |
| url = fal.upload(audio, content_type="audio/mpeg", file_name=f"{slug}.mp3") | |
| out = fal.run("fal-ai/minimax/voice-clone", { | |
| "audio_url": url, "model": "speech-02-hd", | |
| "noise_reduction": True, "need_volume_normalization": True, | |
| }, what=f"voice clone {slug}") | |
| voice_id = out["custom_voice_id"] | |
| fal.run(MINIMAX_MODEL, {"text": PIN_LINE, "voice_setting": {"voice_id": voice_id}}, | |
| what=f"voice pin {slug}") | |
| return voice_id | |
| def main(argv: list[str]) -> int: | |
| apply = "--apply" in argv | |
| limit = int(argv[argv.index("--limit") + 1]) if "--limit" in argv else None | |
| store = SupabaseStore() | |
| rows = [r for r in store.select("avatars", filters={"active": True}, limit=1000) | |
| if not r.get("voice_id") and r.get("audio_key")] | |
| rows = rows[:limit] | |
| print(f"{len(rows)} avatars without a voice — ~${len(rows) * CLONE_USD:.2f}") | |
| if not apply: | |
| print("Dry run. --apply to clone.") | |
| return 0 | |
| done = 0 | |
| # A fal call that never returns (one did, during a network blip) must cost | |
| # one avatar, not the run: every avatar gets a fresh deadline. | |
| signal.signal(signal.SIGALRM, lambda *_: (_ for _ in ()).throw(TimeoutError("clone timed out"))) | |
| for r in rows: | |
| signal.alarm(300) | |
| try: | |
| voice_id = clone(media.get(r["audio_key"]), r["slug"]) | |
| store.update("avatars", {"id": r["id"]}, {"voice_id": voice_id, "voice_name": "minimax"}) | |
| done += 1 | |
| print(f"{done}/{len(rows)} {r['slug']} -> {voice_id}", flush=True) | |
| except Exception as exc: # noqa: BLE001 - one bad sample must not stop the rest | |
| print(f"FAILED {r['slug']}: {str(exc)[:200]}", flush=True) | |
| signal.alarm(0) | |
| print(f"cloned {done} of {len(rows)}") | |
| return 0 if done == len(rows) else 1 | |
| if __name__ == "__main__": | |
| raise SystemExit(main(sys.argv[1:])) | |