File size: 6,097 Bytes
2fd698c b28ebfa 2fd698c b28ebfa 2fd698c c31aa12 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa 2fd698c b28ebfa | 1 2 3 4 5 6 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | import os
import re
import sys
import uuid
import subprocess
from pathlib import Path
import torch
from fastapi import FastAPI, HTTPException, Header
from fastapi.responses import FileResponse
from pydantic import BaseModel
from huggingface_hub import snapshot_download
MODEL_ID = "beleata74/bg-tts-v5"
TMP_DIR = Path("/tmp/books-tts")
TMP_DIR.mkdir(parents=True, exist_ok=True)
API_KEY = os.getenv("BOOKS_TTS_API_KEY")
app = FastAPI(title="Books Bulgarian TTS API")
print("Downloading/loading BG-TTS V5 repo...")
MODEL_DIR = Path(snapshot_download(repo_id=MODEL_ID))
CHECKPOINT_DIR = MODEL_DIR / "checkpoint"
sys.path.insert(0, str(MODEL_DIR))
from tts_v5.model import load_for_inference
from tts_v5.tokenizer import TTSTokenizer
from tts_v5.codec import CodecV5
from tts_v5.inference import generate
from tts_v5.config import CODEC_NUM_CODEBOOKS
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Loading model on {DEVICE}...")
tts_model = load_for_inference(str(CHECKPOINT_DIR), device=DEVICE)
tts_tokenizer = TTSTokenizer()
tts_codec = CodecV5(device=DEVICE)
print("Model loaded.")
class TTSRequest(BaseModel):
text: str
speaker_id: int | None = 1
temperature: float | None = 0.25
top_k: int | None = 50
top_p: float | None = 0.8
format: str | None = "mp3"
@app.get("/")
def root():
return {
"ok": True,
"message": "Books Bulgarian TTS API is running",
"model": MODEL_ID,
"device": DEVICE,
}
@app.get("/health")
def health():
return {
"ok": True,
"model": MODEL_ID,
"device": DEVICE,
}
def check_auth(authorization: str | None):
if not API_KEY:
raise HTTPException(
status_code=500,
detail="BOOKS_TTS_API_KEY is not configured on the Space."
)
expected = f"Bearer {API_KEY}"
if authorization != expected:
raise HTTPException(
status_code=401,
detail="Invalid or missing Authorization header."
)
def split_text(text: str, max_chars: int = 300):
text = re.sub(r"\s+", " ", text).strip()
if not text:
return []
sentences = re.split(r"(?<=[.!?…])\s+", text)
chunks = []
current = ""
for sentence in sentences:
sentence = sentence.strip()
if not sentence:
continue
if len(current) + len(sentence) + 1 <= max_chars:
current = f"{current} {sentence}".strip()
else:
if current:
chunks.append(current)
if len(sentence) > max_chars:
for i in range(0, len(sentence), max_chars):
chunks.append(sentence[i:i + max_chars])
current = ""
else:
current = sentence
if current:
chunks.append(current)
return chunks
def generate_wav_for_chunk(
text: str,
output_path: Path,
speaker_id: int,
temperature: float,
top_k: int,
top_p: float,
):
tokens = generate(
model=tts_model,
tokenizer=tts_tokenizer,
text=text,
speaker_id=speaker_id,
max_new_tokens=2000,
temperature=temperature,
top_k=top_k,
top_p=top_p,
rep_penalty=1.1,
device=DEVICE,
)
if tokens is None or len(tokens) == 0:
raise RuntimeError("No audio tokens generated.")
tokens = tokens[:len(tokens) - len(tokens) % CODEC_NUM_CODEBOOKS]
if len(tokens) == 0:
raise RuntimeError("Generated token count is invalid.")
tts_codec.tokens_to_wav(tokens, str(output_path))
def merge_wavs_to_mp3(wav_paths: list[Path], output_mp3: Path):
list_file = TMP_DIR / f"{uuid.uuid4()}-list.txt"
with open(list_file, "w", encoding="utf-8") as f:
for wav in wav_paths:
f.write(f"file '{wav}'\n")
cmd = [
"ffmpeg",
"-y",
"-f",
"concat",
"-safe",
"0",
"-i",
str(list_file),
"-codec:a",
"libmp3lame",
"-qscale:a",
"4",
str(output_mp3),
]
result = subprocess.run(cmd, capture_output=True, text=True)
try:
list_file.unlink(missing_ok=True)
except Exception:
pass
if result.returncode != 0:
raise RuntimeError(result.stderr or "ffmpeg failed")
@app.post("/tts")
def tts(payload: TTSRequest, authorization: str | None = Header(default=None)):
check_auth(authorization)
text = payload.text.strip()
if not text:
raise HTTPException(status_code=400, detail="Text is empty.")
if len(text) > 5000:
raise HTTPException(
status_code=400,
detail="Text is too long. Max 5000 characters for now."
)
speaker_id = payload.speaker_id if payload.speaker_id in [0, 1] else 1
request_id = str(uuid.uuid4())
request_dir = TMP_DIR / request_id
request_dir.mkdir(parents=True, exist_ok=True)
try:
max_chars = 300 if speaker_id == 1 else 350
chunks = split_text(text, max_chars=max_chars)
if not chunks:
raise HTTPException(status_code=400, detail="No valid text chunks found.")
wav_paths = []
for index, chunk in enumerate(chunks):
wav_path = request_dir / f"chunk-{index}.wav"
generate_wav_for_chunk(
text=chunk,
output_path=wav_path,
speaker_id=speaker_id,
temperature=payload.temperature or 0.25,
top_k=payload.top_k or 50,
top_p=payload.top_p or 0.8,
)
wav_paths.append(wav_path)
output_mp3 = request_dir / "output.mp3"
merge_wavs_to_mp3(wav_paths, output_mp3)
return FileResponse(
path=str(output_mp3),
media_type="audio/mpeg",
filename="tts.mp3",
)
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"TTS generation failed: {str(e)}"
) |