Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from TTS.api import TTS
|
| 5 |
+
|
| 6 |
+
print("🚀 Starting XTTS Space...")
|
| 7 |
+
|
| 8 |
+
MODEL_NAME = "tts_models/multilingual/multi-dataset/xtts_v2"
|
| 9 |
+
|
| 10 |
+
# 🔒 Global singleton (loaded once)
|
| 11 |
+
tts = None
|
| 12 |
+
|
| 13 |
+
def load_model_once():
|
| 14 |
+
global tts
|
| 15 |
+
if tts is None:
|
| 16 |
+
print("⬇️ Downloading & loading XTTS model (one-time)...")
|
| 17 |
+
tts = TTS(model_name=MODEL_NAME, gpu=False)
|
| 18 |
+
print("✅ XTTS model loaded.")
|
| 19 |
+
return tts
|
| 20 |
+
|
| 21 |
+
def generate_audio(text, speaker_wav):
|
| 22 |
+
if not text or speaker_wav is None:
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
tts = load_model_once()
|
| 26 |
+
|
| 27 |
+
# Output file
|
| 28 |
+
out_path = os.path.join(tempfile.gettempdir(), "output.wav")
|
| 29 |
+
|
| 30 |
+
tts.tts_to_file(
|
| 31 |
+
text=text,
|
| 32 |
+
speaker_wav=speaker_wav,
|
| 33 |
+
language="hi",
|
| 34 |
+
file_path=out_path
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
return out_path
|
| 38 |
+
|
| 39 |
+
with gr.Blocks(title="XTTS Voice Clone (CPU)") as demo:
|
| 40 |
+
gr.Markdown("## 🎙️ XTTS Voice Cloning (CPU Space)\nUpload voice + enter text → get WAV")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
text_in = gr.Textbox(label="Enter Text (Hindi / English)", lines=4, placeholder="नमस्ते...")
|
| 44 |
+
wav_in = gr.Audio(label="Upload Voice Sample (WAV)", type="filepath")
|
| 45 |
+
|
| 46 |
+
out_audio = gr.Audio(label="Generated Voice")
|
| 47 |
+
|
| 48 |
+
btn = gr.Button("Generate Voice")
|
| 49 |
+
|
| 50 |
+
btn.click(
|
| 51 |
+
fn=generate_audio,
|
| 52 |
+
inputs=[text_in, wav_in],
|
| 53 |
+
outputs=out_audio
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
demo.queue(concurrency_count=1).launch()
|