Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,20 @@
|
|
| 1 |
import os
|
| 2 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 3 |
|
| 4 |
import tempfile
|
|
|
|
|
|
|
| 5 |
import requests
|
| 6 |
import torch
|
| 7 |
import torchaudio as ta
|
| 8 |
import gradio as gr
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from chatterbox.tts import ChatterboxTTS
|
| 12 |
from huggingface_hub import hf_hub_download
|
|
@@ -14,56 +22,104 @@ from safetensors.torch import load_file
|
|
| 14 |
|
| 15 |
MODEL_REPO = "grandhigh/Chatterbox-TTS-Indonesian"
|
| 16 |
CHECKPOINT_FILENAME = "t3_cfg.safetensors"
|
|
|
|
| 17 |
|
| 18 |
_model = None
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
def get_model():
|
| 22 |
global _model
|
| 23 |
if _model is None:
|
| 24 |
-
with
|
| 25 |
if _model is None:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
m.t3.load_state_dict(t3_state)
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
_model = m
|
|
|
|
| 32 |
return _model
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
r = requests.get(url, timeout=90)
|
| 36 |
r.raise_for_status()
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
if not text or not text.strip():
|
| 44 |
-
raise gr.Error("Text prompt kosong.")
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if not prompt_path:
|
| 47 |
-
raise gr.Error("Upload WAV atau isi URL WAV.")
|
| 48 |
|
| 49 |
model = get_model()
|
|
|
|
| 50 |
with torch.no_grad():
|
| 51 |
-
wav = model.generate(
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
if wav.dim() == 1:
|
| 54 |
wav = wav.unsqueeze(0)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
ta.save(
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
with gr.Blocks() as demo:
|
| 61 |
-
text = gr.Textbox(label="Text Prompt", lines=4)
|
| 62 |
-
wav = gr.Audio(label="Upload WAV", type="filepath")
|
| 63 |
-
url = gr.Textbox(label="WAV URL (opsional)")
|
| 64 |
btn = gr.Button("Generate")
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
port = int(os.getenv("PORT", "7860"))
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "" # paksa CPU-only
|
| 3 |
|
| 4 |
import tempfile
|
| 5 |
+
from threading import Lock
|
| 6 |
+
|
| 7 |
import requests
|
| 8 |
import torch
|
| 9 |
import torchaudio as ta
|
| 10 |
import gradio as gr
|
| 11 |
+
|
| 12 |
+
# Paksa semua torch.load map ke CPU (hindari error deserialize CUDA)
|
| 13 |
+
_original_torch_load = torch.load
|
| 14 |
+
def _torch_load_cpu(*args, **kwargs):
|
| 15 |
+
kwargs.setdefault("map_location", torch.device("cpu"))
|
| 16 |
+
return _original_torch_load(*args, **kwargs)
|
| 17 |
+
torch.load = _torch_load_cpu
|
| 18 |
|
| 19 |
from chatterbox.tts import ChatterboxTTS
|
| 20 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 22 |
|
| 23 |
MODEL_REPO = "grandhigh/Chatterbox-TTS-Indonesian"
|
| 24 |
CHECKPOINT_FILENAME = "t3_cfg.safetensors"
|
| 25 |
+
DEVICE = "cpu"
|
| 26 |
|
| 27 |
_model = None
|
| 28 |
+
_model_lock = Lock()
|
| 29 |
+
|
| 30 |
|
| 31 |
def get_model():
|
| 32 |
global _model
|
| 33 |
if _model is None:
|
| 34 |
+
with _model_lock:
|
| 35 |
if _model is None:
|
| 36 |
+
print("Loading model on first request (CPU)...")
|
| 37 |
+
m = ChatterboxTTS.from_pretrained(device=DEVICE)
|
| 38 |
+
|
| 39 |
+
ckpt_path = hf_hub_download(
|
| 40 |
+
repo_id=MODEL_REPO,
|
| 41 |
+
filename=CHECKPOINT_FILENAME
|
| 42 |
+
)
|
| 43 |
+
t3_state = load_file(ckpt_path, device="cpu")
|
| 44 |
m.t3.load_state_dict(t3_state)
|
| 45 |
+
|
| 46 |
+
# JANGAN pakai m.to("cpu") -> ChatterboxTTS tidak punya method .to()
|
| 47 |
+
if hasattr(m, "eval"):
|
| 48 |
+
m.eval()
|
| 49 |
+
|
| 50 |
_model = m
|
| 51 |
+
print("Model ready.")
|
| 52 |
return _model
|
| 53 |
|
| 54 |
+
|
| 55 |
+
def _download_wav(url: str) -> str:
|
| 56 |
r = requests.get(url, timeout=90)
|
| 57 |
r.raise_for_status()
|
| 58 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 59 |
+
tmp.write(r.content)
|
| 60 |
+
tmp.close()
|
| 61 |
+
return tmp.name
|
| 62 |
|
| 63 |
+
|
| 64 |
+
def clone_voice(text: str, audio_file, audio_url: str):
|
| 65 |
if not text or not text.strip():
|
| 66 |
+
raise gr.Error("Text prompt tidak boleh kosong.")
|
| 67 |
+
|
| 68 |
+
prompt_path = None
|
| 69 |
+
if audio_file:
|
| 70 |
+
# gr.Audio(type='filepath') -> path string
|
| 71 |
+
prompt_path = audio_file
|
| 72 |
+
elif audio_url and audio_url.strip():
|
| 73 |
+
prompt_path = _download_wav(audio_url.strip())
|
| 74 |
+
|
| 75 |
if not prompt_path:
|
| 76 |
+
raise gr.Error("Upload WAV atau isi Audio URL WAV.")
|
| 77 |
|
| 78 |
model = get_model()
|
| 79 |
+
|
| 80 |
with torch.no_grad():
|
| 81 |
+
wav = model.generate(
|
| 82 |
+
text.strip(),
|
| 83 |
+
audio_prompt_path=prompt_path
|
| 84 |
+
)
|
| 85 |
|
| 86 |
+
# pastikan format [channels, samples]
|
| 87 |
if wav.dim() == 1:
|
| 88 |
wav = wav.unsqueeze(0)
|
| 89 |
|
| 90 |
+
out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
|
| 91 |
+
ta.save(out_path, wav.cpu(), model.sr)
|
| 92 |
+
|
| 93 |
+
return out_path
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(title="Chatterbox Indonesian Voice Cloning (CPU)") as demo:
|
| 97 |
+
gr.Markdown("## Chatterbox-TTS Indonesian - Voice Cloning (CPU)")
|
| 98 |
+
gr.Markdown("Masukkan teks + upload WAV (atau URL WAV)")
|
| 99 |
+
|
| 100 |
+
text_in = gr.Textbox(
|
| 101 |
+
label="Text Prompt",
|
| 102 |
+
lines=4,
|
| 103 |
+
placeholder="Contoh: Halo, ini demo voice cloning saya."
|
| 104 |
+
)
|
| 105 |
+
wav_in = gr.Audio(
|
| 106 |
+
label="Upload WAV Prompt",
|
| 107 |
+
type="filepath"
|
| 108 |
+
)
|
| 109 |
+
url_in = gr.Textbox(
|
| 110 |
+
label="Audio URL WAV (opsional)",
|
| 111 |
+
placeholder="https://example.com/input.wav"
|
| 112 |
+
)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
btn = gr.Button("Generate")
|
| 115 |
+
out_audio = gr.Audio(label="Hasil Audio", type="filepath")
|
| 116 |
+
|
| 117 |
+
btn.click(
|
| 118 |
+
fn=clone_voice,
|
| 119 |
+
inputs=[text_in, wav_in, url_in],
|
| 120 |
+
outputs=[out_audio],
|
| 121 |
+
api_name="clone_voice"
|
| 122 |
+
)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
port = int(os.getenv("PORT", "7860"))
|