Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,127 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import soundfile as sf
|
| 3 |
-
import torch
|
| 4 |
-
import sys, os
|
| 5 |
-
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
| 6 |
from huggingface_hub import snapshot_download
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
# --------------------------
|
| 11 |
-
repo_path = snapshot_download("IndexTeam/Index-TTS", local_dir="checkpoints")
|
| 12 |
-
sys.path.append(repo_path)
|
| 13 |
|
|
|
|
| 14 |
from indextts.infer import IndexTTS
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
gr.Markdown("
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column():
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
with gr.Column():
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
btn.click(
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
|
| 6 |
+
# If torch is optional for you, you can keep this minimal
|
| 7 |
+
import torch
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Import after deps are installed (handled by requirements.txt)
|
| 10 |
from indextts.infer import IndexTTS
|
| 11 |
|
| 12 |
+
|
| 13 |
+
CHECKPOINTS_DIR = os.path.abspath("checkpoints")
|
| 14 |
+
|
| 15 |
+
def load_model():
|
| 16 |
+
"""
|
| 17 |
+
Download model weights (if needed) and initialize IndexTTS once.
|
| 18 |
+
Avoids the 'checkpoints/checkpoints' double-path bug by using the exact
|
| 19 |
+
path returned from snapshot_download.
|
| 20 |
+
"""
|
| 21 |
+
os.makedirs(CHECKPOINTS_DIR, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
# Download to a fixed directory; do NOT prefix this path again later.
|
| 24 |
+
repo_path = snapshot_download(
|
| 25 |
+
repo_id="mlx-community/IndexTTS",
|
| 26 |
+
local_dir=CHECKPOINTS_DIR,
|
| 27 |
+
local_dir_use_symlinks=False, # ensures real files (safer in Spaces)
|
| 28 |
+
allow_patterns=[
|
| 29 |
+
"config.yaml",
|
| 30 |
+
"bpe.model",
|
| 31 |
+
"unigram_12000.vocab",
|
| 32 |
+
"gpt.pth",
|
| 33 |
+
"bigvgan_generator.pth",
|
| 34 |
+
"bigvgan_discriminator.pth",
|
| 35 |
+
"dvae.pth",
|
| 36 |
+
],
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Optional: keep CPU stable in Spaces and prevent over-threading
|
| 40 |
+
os.environ.setdefault("OMP_NUM_THREADS", "1")
|
| 41 |
+
os.environ.setdefault("MKL_NUM_THREADS", "1")
|
| 42 |
+
try:
|
| 43 |
+
torch.set_num_threads(1)
|
| 44 |
+
except Exception:
|
| 45 |
+
pass
|
| 46 |
+
|
| 47 |
+
# Initialize IndexTTS. IMPORTANT: pass repo_path directly.
|
| 48 |
+
tts = IndexTTS(model_dir=repo_path, cfg_path=os.path.join(repo_path, "config.yaml"))
|
| 49 |
+
return tts
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Global singleton (loaded once on Space startup)
|
| 53 |
+
_tts = None
|
| 54 |
+
def get_tts():
|
| 55 |
+
global _tts
|
| 56 |
+
if _tts is None:
|
| 57 |
+
_tts = load_model()
|
| 58 |
+
return _tts
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def synthesize(voice_path, text):
|
| 62 |
+
"""
|
| 63 |
+
Gradio inference function.
|
| 64 |
+
- voice_path: path to uploaded reference voice (WAV strongly recommended)
|
| 65 |
+
- text: the text to speak
|
| 66 |
+
Returns (output_wav_path)
|
| 67 |
+
"""
|
| 68 |
+
if not voice_path or not os.path.exists(voice_path):
|
| 69 |
+
raise gr.Error("Please upload a short reference voice clip (WAV recommended).")
|
| 70 |
+
|
| 71 |
+
if not text or not text.strip():
|
| 72 |
+
raise gr.Error("Please enter the text to speak.")
|
| 73 |
+
|
| 74 |
+
tts = get_tts()
|
| 75 |
+
|
| 76 |
+
# Write output to a temporary WAV file; Gradio will serve it.
|
| 77 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 78 |
+
out_path = tmp.name
|
| 79 |
+
|
| 80 |
+
# Minimal call; IndexTTS handles normalization/phonemization internally.
|
| 81 |
+
# You can add extra kwargs if the library exposes them (e.g., speed, seed).
|
| 82 |
+
tts.infer(voice_path, text.strip(), out_path)
|
| 83 |
+
|
| 84 |
+
return out_path
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
title = "IndexTTS β Zero-shot Voice Cloning (HF Space)"
|
| 88 |
+
description = """
|
| 89 |
+
Upload a short **reference voice** (5β10s, clean speech works best) and enter text.
|
| 90 |
+
This Space runs **IndexTTS** in CPU mode by default, so first run may take a bit to warm up.
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
+
gr.Markdown(f"# {title}\n{description}")
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
| 98 |
+
voice = gr.Audio(
|
| 99 |
+
sources=["upload"],
|
| 100 |
+
type="filepath",
|
| 101 |
+
label="Reference Voice (WAV preferred)"
|
| 102 |
+
)
|
| 103 |
+
text = gr.Textbox(
|
| 104 |
+
label="Text to Synthesize",
|
| 105 |
+
placeholder="Hello, how are you?",
|
| 106 |
+
lines=3
|
| 107 |
+
)
|
| 108 |
+
btn = gr.Button("Generate Speech")
|
| 109 |
|
| 110 |
with gr.Column():
|
| 111 |
+
audio_out = gr.Audio(label="Output Audio", type="filepath")
|
| 112 |
+
log = gr.Markdown("")
|
| 113 |
|
| 114 |
+
btn.click(fn=synthesize, inputs=[voice, text], outputs=[audio_out])
|
| 115 |
+
|
| 116 |
+
# Optional: pre-load at startup so first user call is faster
|
| 117 |
+
def _startup():
|
| 118 |
+
try:
|
| 119 |
+
get_tts()
|
| 120 |
+
except Exception as e:
|
| 121 |
+
# Don't crash the Space if warmup fails; show a note in Logs.
|
| 122 |
+
print("Warmup failed:", e)
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
_startup()
|
| 126 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 127 |
|
|
|