File size: 2,712 Bytes
983bcf7 2e87af2 1f065b7 2e87af2 1f065b7 983bcf7 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 2e87af2 1f065b7 |
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 |
import os
import logging
import traceback
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("app")
# caminhos preferidos
PREFERRED_DIRS = ["model", "config", "vocab", "output"]
def ensure_dirs(base_dirs=None):
base_dirs = base_dirs or PREFERRED_DIRS
created = {}
for d in base_dirs:
try:
os.makedirs(d, exist_ok=True)
# testar escrita
testfile = os.path.join(d, ".perm_test")
with open(testfile, "w") as f:
f.write("ok")
os.remove(testfile)
created[d] = d
log.info("Diretório pronto: %s", d)
except PermissionError:
log.warning("Sem permissão para criar %s", d)
created[d] = None
except Exception:
log.error("Erro criando %s: %s", d, traceback.format_exc())
created[d] = None
# se algum não foi criado, usar /tmp/<nome>
for k, v in list(created.items()):
if v is None:
alt = os.path.join("/tmp", k)
try:
os.makedirs(alt, exist_ok=True)
created[k] = alt
log.info("Usando fallback %s para %s", alt, k)
except Exception:
log.error("Não foi possível criar fallback %s", alt)
raise
return created
# chama no início
DIRS = ensure_dirs()
MODEL_DIR = DIRS["model"]
CONFIG_DIR = DIRS["config"]
VOCAB_DIR = DIRS["vocab"]
OUTPUT_DIR = DIRS["output"]
MODEL_FILE = os.path.join(MODEL_DIR, "model_last.safetensors")
CONFIG_FILE = os.path.join(CONFIG_DIR, "config.yaml")
VOCAB_FILE = os.path.join(VOCAB_DIR, "vocab.txt")
OUTPUT_FILE = os.path.join(OUTPUT_DIR, "output.wav")
# segue resto do seu app (import fastapi etc.)
from fastapi import FastAPI, Body, HTTPException
from fastapi.responses import FileResponse
app = FastAPI()
# ... (coloque aqui o seu código que faz download do modelo / inicializa TTS etc.)
# Exemplo simples de endpoint:
@app.get("/health")
def health():
return {"status": "ok", "paths": {"model": MODEL_FILE, "output": OUTPUT_FILE}}
# Endpoint /tts de exemplo (adapte para sua inference)
@app.post("/tts")
def tts(text: str = Body(..., embed=True)):
if not text.strip():
raise HTTPException(status_code=400, detail="text vazio")
# aqui você chama sua função de inferência que escreve OUTPUT_FILE
# por exemplo: tts_model.tts_to_file(text=text, file_path=OUTPUT_FILE)
# para demo, apenas cria um arquivo vazio (substitua isso)
with open(OUTPUT_FILE, "wb") as f:
f.write(b"") # <-- substitua pelo áudio real
return FileResponse(OUTPUT_FILE, media_type="audio/wav", filename=os.path.basename(OUTPUT_FILE))
|