Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import shutil
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from TTS.api import TTS
|
| 6 |
+
|
| 7 |
+
MODEL_NAME = "tts_models/multilingual/multi-dataset/xtts_v2"
|
| 8 |
+
|
| 9 |
+
print("🚀 Starting server...")
|
| 10 |
+
print("⬇️ Downloading/loading model only once...")
|
| 11 |
+
|
| 12 |
+
tts = TTS(
|
| 13 |
+
model_name=MODEL_NAME,
|
| 14 |
+
gpu=False # CPU only for HF Spaces free tier
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
print("✅ Model loaded. Ready for requests.")
|
| 18 |
+
|
| 19 |
+
OUTPUT_DIR = "outputs"
|
| 20 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
def generate_tts(text, voice_file):
|
| 23 |
+
if not text or voice_file is None:
|
| 24 |
+
return None, "❌ Please provide both text and voice sample."
|
| 25 |
+
|
| 26 |
+
out_name = f"{uuid.uuid4().hex}.wav"
|
| 27 |
+
out_path = os.path.join(OUTPUT_DIR, out_name)
|
| 28 |
+
|
| 29 |
+
tts.tts_to_file(
|
| 30 |
+
text=text,
|
| 31 |
+
speaker_wav=voice_file,
|
| 32 |
+
language="hi", # change if needed
|
| 33 |
+
file_path=out_path
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return out_path, "✅ Audio generated!"
|
| 37 |
+
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=generate_tts,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Textbox(label="Text", placeholder="Enter text here..."),
|
| 42 |
+
gr.Audio(type="filepath", label="Upload Voice Sample (WAV)")
|
| 43 |
+
],
|
| 44 |
+
outputs=[
|
| 45 |
+
gr.Audio(label="Generated Audio"),
|
| 46 |
+
gr.Textbox(label="Status")
|
| 47 |
+
],
|
| 48 |
+
title="XTTS Voice Cloning (CPU, No API Key)",
|
| 49 |
+
description="Upload a voice sample + text → get cloned voice audio. Model loads once and is reused."
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|