| import os |
| import logging |
| import traceback |
|
|
| logging.basicConfig(level=logging.INFO) |
| log = logging.getLogger("app") |
|
|
| |
| 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) |
| |
| 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 |
| |
| 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 |
|
|
| |
| 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") |
|
|
| |
| from fastapi import FastAPI, Body, HTTPException |
| from fastapi.responses import FileResponse |
|
|
| app = FastAPI() |
|
|
| |
| |
| @app.get("/health") |
| def health(): |
| return {"status": "ok", "paths": {"model": MODEL_FILE, "output": OUTPUT_FILE}} |
|
|
| |
| @app.post("/tts") |
| def tts(text: str = Body(..., embed=True)): |
| if not text.strip(): |
| raise HTTPException(status_code=400, detail="text vazio") |
| |
| |
| |
| with open(OUTPUT_FILE, "wb") as f: |
| f.write(b"") |
| return FileResponse(OUTPUT_FILE, media_type="audio/wav", filename=os.path.basename(OUTPUT_FILE)) |
|
|