Spaces:
Running on Zero
Running on Zero
clean 24kHz reference audio and cross-lingual chatterbox tuning
Browse filesFiles changed (2)
32 unmodified lines333435363738394041424319 unmodified lines636465666768697 unmodified lines7778798081821 unmodified line848586878889909192939495969732 unmodified linesfrom llm_chat import build_router# Suara acuan disimpan sebagai teks base64 karena Space menolak file biner di git biasa.REF_AUDIO = os.path.join(tempfile.gettempdir(), "furina_ref.mp3")with open(os.path.join(os.path.dirname(__file__), "furina_ref_b64.txt"), encoding="ascii") as _f:with open(REF_AUDIO, "wb") as _out:_out.write(base64.b64decode(_f.read()))# Router /tts milik llm_chat memanggil endpoint Furina di Space ini sendiri.os.environ.setdefault("FURINA_TTS_URL", "http://127.0.0.1:7860/furina")19 unmodified lines@spaces.GPU(duration=60)def synth(text: str, language: str = "ms"):model = _load()wav = model.generate(text, language_id=language, audio_prompt_path=REF_AUDIO)return wav.cpu(), model.sr7 unmodified linesapp.include_router(build_router())@app.post("/furina")def furina(req: FurinaReq) -> Response:try:1 unmodified lineexcept Exception as exc:raise HTTPException(status_code=503, detail=f"furina_unavailable: {type(exc).__name__}")buf = io.BytesIO()sf.write(buf, wav.squeeze().detach().cpu().numpy(), sr, format="WAV")return Response(content=buf.getvalue(), media_type="audio/wav")def _demo(text):wav, sr = synth(text, "ms")path = tempfile.mktemp(suffix=".wav")sf.write(path, wav.squeeze().detach().cpu().numpy(), sr, format="WAV")return path32 unmodified lines333435363738394041424344454647484950515253545556575819 unmodified lines78798081828384858687888990919293947 unmodified lines1021031041051061071081091101111121131141 unmodified line11611711811912012112212312412512612712812932 unmodified linesfrom llm_chat import build_router# Suara acuan disimpan sebagai teks base64 karena Space menolak file biner di git biasa._RAW_MP3 = os.path.join(tempfile.gettempdir(), "furina_head.mp3")REF_AUDIO = os.path.join(tempfile.gettempdir(), "furina_ref_clean.wav")with open(os.path.join(os.path.dirname(__file__), "furina_ref_b64.txt"), encoding="ascii") as _f:with open(_RAW_MP3, "wb") as _out:_out.write(base64.b64decode(_f.read()))def _prepare_clean_ref() -> str:if not os.path.exists(REF_AUDIO):import librosaimport soundfile as sf# Potong tepat segmen kalimat jernih Furina (7.7s - 20.6s) pada 24kHz monoy, sr = librosa.load(_RAW_MP3, sr=24000, mono=True, offset=7.7, duration=12.9)y, _ = librosa.effects.trim(y, top_db=28)peak = float(max(abs(y.max()), abs(y.min()), 1e-6))y = (y / peak) * 0.90sf.write(REF_AUDIO, y, sr, format="WAV")return REF_AUDIO# Router /tts milik llm_chat memanggil endpoint Furina di Space ini sendiri.os.environ.setdefault("FURINA_TTS_URL", "http://127.0.0.1:7860/furina")19 unmodified lines@spaces.GPU(duration=60)def synth(text: str, language: str = "ms"):model = _load()ref_wav = _prepare_clean_ref()# Tuning cross-lingual (JP ref -> ID/MS): cfg_weight rendah mengurangi aksen robotik/slurwav = model.generate(text,language_id=language,audio_prompt_path=ref_wav,exaggeration=0.45,cfg_weight=0.28,temperature=0.60,repetition_penalty=1.35,)return wav.cpu(), model.sr7 unmodified linesapp.include_router(build_router())def _normalize_wav(wav_tensor):arr = wav_tensor.squeeze().detach().cpu().numpy()arr = arr - float(arr.mean())peak = float(max(abs(arr.max()), abs(arr.min()), 1e-6))return (arr / peak) * 0.90@app.post("/furina")def furina(req: FurinaReq) -> Response:try:1 unmodified lineexcept Exception as exc:raise HTTPException(status_code=503, detail=f"furina_unavailable: {type(exc).__name__}")buf = io.BytesIO()sf.write(buf, _normalize_wav(wav), sr, format="WAV")return Response(content=buf.getvalue(), media_type="audio/wav")def _demo(text):wav, sr = synth(text, "ms")path = tempfile.mktemp(suffix=".wav")sf.write(path, _normalize_wav(wav), sr, format="WAV")return path
app.py+37-5
furina_ref_b64.txt