Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 1 |
|
| 2 |
-
import gradio as gr
|
| 3 |
import torch, yaml, os, sys, glob, re
|
| 4 |
import librosa
|
| 5 |
import soundfile as sf
|
| 6 |
import torchaudio
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from huggingface_hub import snapshot_download
|
| 8 |
-
from munch import Munch
|
| 9 |
-
from nltk.tokenize import word_tokenize
|
| 10 |
-
import nltk
|
| 11 |
-
nltk.download("punkt_tab", quiet=True)
|
| 12 |
|
| 13 |
-
# ββ Clone + patch StyleTTS2 ββββββββββββββββββββββββββββββββ
|
| 14 |
os.system("git clone https://github.com/yl4579/StyleTTS2 /app/StyleTTS2 2>/dev/null || true")
|
| 15 |
|
| 16 |
for fpath in ["/app/StyleTTS2/models.py", "/app/StyleTTS2/utils.py"]:
|
|
@@ -24,7 +22,6 @@ for fpath in ["/app/StyleTTS2/models.py", "/app/StyleTTS2/utils.py"]:
|
|
| 24 |
)
|
| 25 |
with open(fpath, "w") as f:
|
| 26 |
f.write(patched)
|
| 27 |
-
print("Patched torch.load")
|
| 28 |
|
| 29 |
sys.path.insert(0, "/app/StyleTTS2")
|
| 30 |
os.chdir("/app/StyleTTS2")
|
|
@@ -35,6 +32,10 @@ from models import *
|
|
| 35 |
from utils import *
|
| 36 |
from text_utils import TextCleaner
|
| 37 |
from Modules.diffusion.sampler import DiffusionSampler, ADPM2Sampler, KarrasSchedule
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
device = "cpu"
|
| 40 |
textcleaner = TextCleaner()
|
|
@@ -107,17 +108,30 @@ ref_candidates = (
|
|
| 107 |
glob.glob(f"{model_dir}/ref_wavs/*.wav") +
|
| 108 |
glob.glob(f"{model_dir}/*.wav")
|
| 109 |
)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
import phonemizer
|
| 116 |
pb = phonemizer.backend.EspeakBackend(
|
| 117 |
language="sw", preserve_punctuation=True, with_stress=True)
|
| 118 |
-
ref_s =
|
| 119 |
|
| 120 |
-
ps = " ".join(word_tokenize(pb.phonemize([text.strip()])[0]))
|
| 121 |
tokens = textcleaner(ps)
|
| 122 |
tokens.insert(0, 0)
|
| 123 |
tokens = torch.LongTensor(tokens).to(device).unsqueeze(0)
|
|
@@ -132,11 +146,11 @@ def synthesize(text, ref_audio=None, alpha=0.3, beta=0.7, steps=5):
|
|
| 132 |
sp = sampler(
|
| 133 |
noise=torch.randn((1, 256)).unsqueeze(1).to(device),
|
| 134 |
embedding=bd, embedding_scale=1,
|
| 135 |
-
features=ref_s, num_steps=
|
| 136 |
).squeeze(1)
|
| 137 |
|
| 138 |
-
s = beta * sp[:, 128:] + (1 - beta) * ref_s[:, 128:]
|
| 139 |
-
ref = alpha * sp[:, :128] + (1 - alpha) * ref_s[:, :128]
|
| 140 |
|
| 141 |
d = model.predictor.text_encoder(d_en, s, il, tm)
|
| 142 |
x, _ = model.predictor.lstm(d)
|
|
@@ -166,19 +180,8 @@ def synthesize(text, ref_audio=None, alpha=0.3, beta=0.7, steps=5):
|
|
| 166 |
|
| 167 |
wav = out.squeeze().cpu().numpy()[..., :-50]
|
| 168 |
sf.write("/tmp/output.wav", wav, 24000)
|
| 169 |
-
return "/tmp/output.wav"
|
| 170 |
-
|
| 171 |
-
demo = gr.Interface(
|
| 172 |
-
fn = synthesize,
|
| 173 |
-
inputs = [
|
| 174 |
-
gr.Textbox(label="Luhya text", value="mirembe. obulani lwa bwana nyasaye.", lines=3),
|
| 175 |
-
gr.Audio(label="Reference voice (optional)", type="filepath", value=None),
|
| 176 |
-
gr.Slider(0.0, 1.0, value=0.3, step=0.1, label="Alpha"),
|
| 177 |
-
gr.Slider(0.0, 1.0, value=0.7, step=0.1, label="Beta"),
|
| 178 |
-
gr.Slider(1, 10, value=5, step=1, label="Diffusion steps"),
|
| 179 |
-
],
|
| 180 |
-
outputs = gr.Audio(label="Generated speech", type="filepath"),
|
| 181 |
-
title = "Luhya (Lunyore) TTS",
|
| 182 |
-
)
|
| 183 |
|
| 184 |
-
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
| 2 |
import torch, yaml, os, sys, glob, re
|
| 3 |
import librosa
|
| 4 |
import soundfile as sf
|
| 5 |
import torchaudio
|
| 6 |
+
import numpy as np
|
| 7 |
+
from fastapi import FastAPI
|
| 8 |
+
from fastapi.responses import FileResponse
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
from huggingface_hub import snapshot_download
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
os.system("git clone https://github.com/yl4579/StyleTTS2 /app/StyleTTS2 2>/dev/null || true")
|
| 13 |
|
| 14 |
for fpath in ["/app/StyleTTS2/models.py", "/app/StyleTTS2/utils.py"]:
|
|
|
|
| 22 |
)
|
| 23 |
with open(fpath, "w") as f:
|
| 24 |
f.write(patched)
|
|
|
|
| 25 |
|
| 26 |
sys.path.insert(0, "/app/StyleTTS2")
|
| 27 |
os.chdir("/app/StyleTTS2")
|
|
|
|
| 32 |
from utils import *
|
| 33 |
from text_utils import TextCleaner
|
| 34 |
from Modules.diffusion.sampler import DiffusionSampler, ADPM2Sampler, KarrasSchedule
|
| 35 |
+
from munch import Munch
|
| 36 |
+
from nltk.tokenize import word_tokenize
|
| 37 |
+
import nltk
|
| 38 |
+
nltk.download("punkt_tab", quiet=True)
|
| 39 |
|
| 40 |
device = "cpu"
|
| 41 |
textcleaner = TextCleaner()
|
|
|
|
| 108 |
glob.glob(f"{model_dir}/ref_wavs/*.wav") +
|
| 109 |
glob.glob(f"{model_dir}/*.wav")
|
| 110 |
)
|
| 111 |
+
DEFAULT_STYLE = compute_style(sorted(ref_candidates)[0])
|
| 112 |
+
print("Ready")
|
| 113 |
+
|
| 114 |
+
# ββ FastAPI ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 115 |
+
app = FastAPI()
|
| 116 |
|
| 117 |
+
class TTSRequest(BaseModel):
|
| 118 |
+
text: str
|
| 119 |
+
alpha: float = 0.3
|
| 120 |
+
beta: float = 0.7
|
| 121 |
+
steps: int = 5
|
| 122 |
+
|
| 123 |
+
@app.get("/")
|
| 124 |
+
def root():
|
| 125 |
+
return {"status": "ok", "model": "luhya-tts"}
|
| 126 |
+
|
| 127 |
+
@app.post("/predict")
|
| 128 |
+
def predict(req: TTSRequest):
|
| 129 |
import phonemizer
|
| 130 |
pb = phonemizer.backend.EspeakBackend(
|
| 131 |
language="sw", preserve_punctuation=True, with_stress=True)
|
| 132 |
+
ref_s = DEFAULT_STYLE
|
| 133 |
|
| 134 |
+
ps = " ".join(word_tokenize(pb.phonemize([req.text.strip()])[0]))
|
| 135 |
tokens = textcleaner(ps)
|
| 136 |
tokens.insert(0, 0)
|
| 137 |
tokens = torch.LongTensor(tokens).to(device).unsqueeze(0)
|
|
|
|
| 146 |
sp = sampler(
|
| 147 |
noise=torch.randn((1, 256)).unsqueeze(1).to(device),
|
| 148 |
embedding=bd, embedding_scale=1,
|
| 149 |
+
features=ref_s, num_steps=req.steps
|
| 150 |
).squeeze(1)
|
| 151 |
|
| 152 |
+
s = req.beta * sp[:, 128:] + (1 - req.beta) * ref_s[:, 128:]
|
| 153 |
+
ref = req.alpha * sp[:, :128] + (1 - req.alpha) * ref_s[:, :128]
|
| 154 |
|
| 155 |
d = model.predictor.text_encoder(d_en, s, il, tm)
|
| 156 |
x, _ = model.predictor.lstm(d)
|
|
|
|
| 180 |
|
| 181 |
wav = out.squeeze().cpu().numpy()[..., :-50]
|
| 182 |
sf.write("/tmp/output.wav", wav, 24000)
|
| 183 |
+
return FileResponse("/tmp/output.wav", media_type="audio/wav")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
if __name__ == "__main__":
|
| 186 |
+
import uvicorn
|
| 187 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|