Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +10 -6
- inference.py +81 -81
app.py
CHANGED
|
@@ -1,16 +1,20 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
-
from inference import synthesize_voice, load_model
|
| 4 |
import io
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
# 🛠 サーバ起動時にモデルをロードする
|
| 9 |
@app.on_event("startup")
|
| 10 |
async def startup_event():
|
| 11 |
load_model()
|
| 12 |
|
| 13 |
@app.get("/voice")
|
| 14 |
-
async def
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
from fastapi.responses import StreamingResponse
|
|
|
|
| 3 |
import io
|
| 4 |
+
import numpy as np
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
from inference import load_model, synthesize
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 10 |
@app.on_event("startup")
|
| 11 |
async def startup_event():
|
| 12 |
load_model()
|
| 13 |
|
| 14 |
@app.get("/voice")
|
| 15 |
+
async def voice(text: str = Query(..., description="Text to synthesize")):
|
| 16 |
+
audio = synthesize(text)
|
| 17 |
+
buf = io.BytesIO()
|
| 18 |
+
sf.write(buf, audio, 24000, format="WAV")
|
| 19 |
+
buf.seek(0)
|
| 20 |
+
return StreamingResponse(buf, media_type="audio/wav")
|
inference.py
CHANGED
|
@@ -1,81 +1,81 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
return audio
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import json
|
| 5 |
+
from scipy.io import wavfile
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
from src.sbv2.synthesizer_trn import SynthesizerTrn
|
| 8 |
+
from src.sbv2 import commons
|
| 9 |
+
from src.sbv2 import utils
|
| 10 |
+
from src.sbv2.text import text_to_sequence
|
| 11 |
+
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
model = None
|
| 14 |
+
hps = None
|
| 15 |
+
|
| 16 |
+
MODEL_REPO = os.getenv("MODEL_REPO")
|
| 17 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 18 |
+
CACHE_DIR = "./models"
|
| 19 |
+
|
| 20 |
+
def load_model():
|
| 21 |
+
global model, hps
|
| 22 |
+
|
| 23 |
+
config_path = hf_hub_download(repo_id=MODEL_REPO, filename="config.json", token=HF_TOKEN, cache_dir=CACHE_DIR)
|
| 24 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename="model.safetensors", token=HF_TOKEN, cache_dir=CACHE_DIR)
|
| 25 |
+
style_path = hf_hub_download(repo_id=MODEL_REPO, filename="style_vectors.npy", token=HF_TOKEN, cache_dir=CACHE_DIR)
|
| 26 |
+
|
| 27 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 28 |
+
hps = json.load(f)
|
| 29 |
+
|
| 30 |
+
symbols = hps["symbols"]
|
| 31 |
+
|
| 32 |
+
n_speakers = hps["data"].get("n_speakers", 0)
|
| 33 |
+
|
| 34 |
+
model = SynthesizerTrn(
|
| 35 |
+
len(symbols),
|
| 36 |
+
hps["model"]["inter_channels"],
|
| 37 |
+
hps["model"]["hidden_channels"],
|
| 38 |
+
hps["model"]["filter_channels"],
|
| 39 |
+
hps["model"]["n_heads"],
|
| 40 |
+
hps["model"]["n_layers"],
|
| 41 |
+
hps["model"]["kernel_size"],
|
| 42 |
+
hps["model"]["p_dropout"],
|
| 43 |
+
resblock=hps["model"]["resblock"],
|
| 44 |
+
resblock_kernel_sizes=hps["model"]["resblock_kernel_sizes"],
|
| 45 |
+
resblock_dilation_sizes=hps["model"]["resblock_dilation_sizes"],
|
| 46 |
+
upsample_rates=hps["model"]["upsample_rates"],
|
| 47 |
+
upsample_initial_channel=hps["model"]["upsample_initial_channel"],
|
| 48 |
+
upsample_kernel_sizes=hps["model"]["upsample_kernel_sizes"],
|
| 49 |
+
gin_channels=hps["model"].get("gin_channels", 0),
|
| 50 |
+
n_speakers=n_speakers,
|
| 51 |
+
use_spectral_norm=hps["model"].get("use_spectral_norm", False)
|
| 52 |
+
).to(device)
|
| 53 |
+
|
| 54 |
+
_ = utils.load_checkpoint(model_path, model, None, strict=True)
|
| 55 |
+
model.eval()
|
| 56 |
+
|
| 57 |
+
print("✅ Model loaded successfully (strict=True).")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def synthesize(text):
|
| 61 |
+
global model, hps
|
| 62 |
+
|
| 63 |
+
if model is None or hps is None:
|
| 64 |
+
raise RuntimeError("Model not loaded!")
|
| 65 |
+
|
| 66 |
+
stn_tst = torch.LongTensor(text_to_sequence(text, hps["data"]["text_cleaners"], hps["data"].get("cleaned_text", True))).unsqueeze(0).to(device)
|
| 67 |
+
|
| 68 |
+
with torch.no_grad():
|
| 69 |
+
x_tst_lengths = torch.LongTensor([stn_tst.size(1)]).to(device)
|
| 70 |
+
sid = torch.LongTensor([0]).to(device) if hps["data"].get("n_speakers", 0) > 0 else None
|
| 71 |
+
|
| 72 |
+
audio = model.infer(
|
| 73 |
+
stn_tst,
|
| 74 |
+
x_tst_lengths,
|
| 75 |
+
sid=sid,
|
| 76 |
+
noise_scale=0.667,
|
| 77 |
+
noise_scale_w=0.8,
|
| 78 |
+
length_scale=1.0
|
| 79 |
+
)[0][0, 0].data.cpu().float().numpy()
|
| 80 |
+
|
| 81 |
+
return audio
|