zelotech / api /routes_text.py
matheusalencar's picture
first commit
1b64148
Raw History Blame Contribute Delete
918 Bytes
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
from api.deps import get_extractor
from domain.extractor import TextExtractor
from services.detect import sniff_kind
from services.files import cleanup, save_upload_to_temp
router = APIRouter(tags=["texto"])
@router.post("/extract-text")
async def extract_text(
file: UploadFile = File(...),
extractor: TextExtractor = Depends(get_extractor),
):
"""Recebe PDF, JPG ou PNG e devolve o texto extraido (cru, sem tratamento)."""
src = await save_upload_to_temp(file)
try:
kind = sniff_kind(src)
if kind == "unknown":
raise HTTPException(
status_code=415,
detail="Formato nao suportado. Envie PDF, JPG ou PNG.",
)
text = extractor.extract(src, kind=kind)
return {"text": text}
finally:
cleanup(src)