Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import os
|
| 3 |
+
import wave
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
from fastapi import FastAPI, HTTPException, Header
|
| 7 |
+
from fastapi.responses import StreamingResponse
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
from piper import PiperVoice
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Senha simples que o app Flutter precisa enviar em todo request
|
| 14 |
+
API_SECRET = os.environ.get("API_SECRET", "")
|
| 15 |
+
|
| 16 |
+
MODEL_REPO = "Lucasllfs/Razo-piper-voice"
|
| 17 |
+
MODEL_FILE = "pt-BR-razo-medium.onnx"
|
| 18 |
+
CONFIG_FILE = "pt-BR-razo-medium.onnx.json"
|
| 19 |
+
|
| 20 |
+
_voice = None
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def get_voice():
|
| 24 |
+
"""Baixa o modelo (se ainda não estiver em cache) e carrega o Piper."""
|
| 25 |
+
global _voice
|
| 26 |
+
if _voice is None:
|
| 27 |
+
cache_dir = "/tmp/razo_model"
|
| 28 |
+
Path(cache_dir).mkdir(parents=True, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
onnx_path = hf_hub_download(
|
| 31 |
+
repo_id=MODEL_REPO,
|
| 32 |
+
filename=MODEL_FILE,
|
| 33 |
+
cache_dir=cache_dir,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# O config no repositório se chama "config.json", então tentamos
|
| 37 |
+
# os dois nomes possíveis para não quebrar caso o autor renomeie.
|
| 38 |
+
try:
|
| 39 |
+
config_path = hf_hub_download(
|
| 40 |
+
repo_id=MODEL_REPO,
|
| 41 |
+
filename=CONFIG_FILE,
|
| 42 |
+
cache_dir=cache_dir,
|
| 43 |
+
)
|
| 44 |
+
except Exception:
|
| 45 |
+
config_path = hf_hub_download(
|
| 46 |
+
repo_id=MODEL_REPO,
|
| 47 |
+
filename="config.json",
|
| 48 |
+
cache_dir=cache_dir,
|
| 49 |
+
)
|
| 50 |
+
# Piper espera o config com o mesmo nome do .onnx + ".json"
|
| 51 |
+
expected_path = onnx_path + ".json"
|
| 52 |
+
if not os.path.exists(expected_path):
|
| 53 |
+
os.symlink(config_path, expected_path)
|
| 54 |
+
config_path = expected_path
|
| 55 |
+
|
| 56 |
+
_voice = PiperVoice.load(onnx_path, config_path=config_path)
|
| 57 |
+
|
| 58 |
+
return _voice
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def check_auth(authorization: str | None):
|
| 62 |
+
if not API_SECRET:
|
| 63 |
+
# Se nenhuma senha foi configurada no Space, libera (modo dev)
|
| 64 |
+
return
|
| 65 |
+
if authorization != f"Bearer {API_SECRET}":
|
| 66 |
+
raise HTTPException(status_code=401, detail="Não autorizado")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@app.get("/")
|
| 70 |
+
def health():
|
| 71 |
+
return {"status": "ok", "voice": "razo-pt-BR"}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@app.post("/tts")
|
| 75 |
+
def synthesize(payload: dict, authorization: str | None = Header(default=None)):
|
| 76 |
+
check_auth(authorization)
|
| 77 |
+
|
| 78 |
+
text = payload.get("text", "").strip()
|
| 79 |
+
if not text:
|
| 80 |
+
raise HTTPException(status_code=400, detail="Campo 'text' vazio")
|
| 81 |
+
|
| 82 |
+
voice = get_voice()
|
| 83 |
+
|
| 84 |
+
buffer = io.BytesIO()
|
| 85 |
+
with wave.open(buffer, "wb") as wav_file:
|
| 86 |
+
voice.synthesize(text, wav_file)
|
| 87 |
+
|
| 88 |
+
buffer.seek(0)
|
| 89 |
+
return StreamingResponse(buffer, media_type="audio/wav")
|