Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,46 @@
|
|
| 1 |
import os
|
| 2 |
-
import uuid
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
import requests
|
| 5 |
import gradio as gr
|
| 6 |
from TTS.api import TTS
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# ---------------------------
|
| 11 |
-
MODEL_DIR = Path("models/xtts_v2")
|
| 12 |
-
MODEL_DIR.mkdir(parents=True, exist_ok=True)
|
| 13 |
-
|
| 14 |
-
# ---------------------------
|
| 15 |
-
# Files to download
|
| 16 |
-
# ---------------------------
|
| 17 |
-
FILES = {
|
| 18 |
-
"model.pth": "https://huggingface.co/coqui/XTTS-v2/resolve/main/model.pth",
|
| 19 |
-
"config.json": "https://huggingface.co/coqui/XTTS-v2/resolve/main/config.json",
|
| 20 |
-
"vocab.json": "https://huggingface.co/coqui/XTTS-v2/resolve/main/vocab.json",
|
| 21 |
-
"speakers_xtts.pth": "https://huggingface.co/coqui/XTTS-v2/resolve/main/speakers_xtts.pth",
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
# ---------------------------
|
| 25 |
-
# Download files function
|
| 26 |
-
# ---------------------------
|
| 27 |
-
def download_file(url, path):
|
| 28 |
-
if path.exists():
|
| 29 |
-
print(f"β
Already exists: {path}")
|
| 30 |
-
return
|
| 31 |
-
print(f"β¬οΈ Downloading {path.name}...")
|
| 32 |
-
response = requests.get(url, stream=True)
|
| 33 |
-
total = int(response.headers.get('content-length', 0))
|
| 34 |
-
with open(path, 'wb') as f:
|
| 35 |
-
downloaded = 0
|
| 36 |
-
for data in response.iter_content(chunk_size=1024*1024):
|
| 37 |
-
f.write(data)
|
| 38 |
-
downloaded += len(data)
|
| 39 |
-
print(f"\r{path.name}: {downloaded/1024/1024:.2f}/{total/1024/1024:.2f} MB", end="")
|
| 40 |
-
print(f"\nβ
Downloaded: {path.name}")
|
| 41 |
-
|
| 42 |
-
# Download all model files
|
| 43 |
-
for fname, url in FILES.items():
|
| 44 |
-
download_file(url, MODEL_DIR / fname)
|
| 45 |
-
|
| 46 |
-
# ---------------------------
|
| 47 |
-
# Load TTS model (once)
|
| 48 |
-
# ---------------------------
|
| 49 |
-
print("β¬οΈ Loading XTTS-v2 model...")
|
| 50 |
|
|
|
|
| 51 |
|
| 52 |
tts = TTS(
|
| 53 |
-
model_path=
|
| 54 |
-
config_path=
|
| 55 |
gpu=False
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
-
# ---------------------------
|
| 65 |
-
# Outputs directory
|
| 66 |
-
# ---------------------------
|
| 67 |
-
OUTPUT_DIR = Path("outputs")
|
| 68 |
-
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 69 |
-
|
| 70 |
-
# ---------------------------
|
| 71 |
-
# TTS generation function
|
| 72 |
-
# ---------------------------
|
| 73 |
-
def generate_tts(text, voice_file):
|
| 74 |
-
if not text or voice_file is None:
|
| 75 |
-
return None, "β Please provide both text and voice sample."
|
| 76 |
-
|
| 77 |
-
# Generate unique output filename
|
| 78 |
-
out_name = f"{uuid.uuid4().hex}.wav"
|
| 79 |
-
out_path = OUTPUT_DIR / out_name
|
| 80 |
-
|
| 81 |
-
# Generate audio
|
| 82 |
tts.tts_to_file(
|
| 83 |
text=text,
|
| 84 |
-
speaker_wav=
|
| 85 |
-
language="hi",
|
| 86 |
-
file_path=
|
| 87 |
)
|
| 88 |
|
| 89 |
-
return
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# ---------------------------
|
| 94 |
-
demo = gr.Interface(
|
| 95 |
fn=generate_tts,
|
| 96 |
inputs=[
|
| 97 |
-
gr.Textbox(label="Text"
|
| 98 |
-
gr.Audio(type="filepath", label="
|
| 99 |
-
],
|
| 100 |
-
outputs=[
|
| 101 |
-
gr.Audio(label="Generated Audio"),
|
| 102 |
-
gr.Textbox(label="Status")
|
| 103 |
],
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
-
# Launch Gradio app
|
| 109 |
if __name__ == "__main__":
|
| 110 |
-
|
| 111 |
-
demo.launch(share=True) # HF Space automatically creates URL
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from TTS.api import TTS
|
| 4 |
|
| 5 |
+
MODEL_PATH = "/models/xtts/model.pth"
|
| 6 |
+
CONFIG_PATH = "/models/xtts/config.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
print("π Loading XTTS model once...")
|
| 9 |
|
| 10 |
tts = TTS(
|
| 11 |
+
model_path=MODEL_PATH,
|
| 12 |
+
config_path=CONFIG_PATH,
|
| 13 |
gpu=False
|
| 14 |
)
|
| 15 |
|
| 16 |
+
print("β
Model loaded successfully")
|
|
|
|
| 17 |
|
| 18 |
+
def generate_tts(text, speaker_wav):
|
| 19 |
+
if not text or speaker_wav is None:
|
| 20 |
+
return None
|
| 21 |
|
| 22 |
+
out_path = "output.wav"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
tts.tts_to_file(
|
| 25 |
text=text,
|
| 26 |
+
speaker_wav=speaker_wav,
|
| 27 |
+
language="hi",
|
| 28 |
+
file_path=out_path
|
| 29 |
)
|
| 30 |
|
| 31 |
+
return out_path
|
| 32 |
|
| 33 |
+
|
| 34 |
+
app = gr.Interface(
|
|
|
|
|
|
|
| 35 |
fn=generate_tts,
|
| 36 |
inputs=[
|
| 37 |
+
gr.Textbox(label="Text"),
|
| 38 |
+
gr.Audio(type="filepath", label="Sample Voice (WAV)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
],
|
| 40 |
+
outputs=gr.Audio(type="filepath", label="Generated Voice"),
|
| 41 |
+
title="XTTS Voice Cloning (CPU)",
|
| 42 |
+
description="Upload voice + text β get cloned speech"
|
| 43 |
)
|
| 44 |
|
|
|
|
| 45 |
if __name__ == "__main__":
|
| 46 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|
|
|