Download app/api.py from CLMARRARA/tech_challenge_fase_4_backend: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/spaces/CLMARRARA/tech_challenge_fase_4_backend/resolve/main/app/api.py
- Command line
-
hf download hf://spaces/CLMARRARA/tech_challenge_fase_4_backend/app/api.py
-
curl -L -o api.py https://huggingface.co/spaces/CLMARRARA/tech_challenge_fase_4_backend/resolve/main/app/api.py
3.12 kB
| import app.bootstrap | |
| from fastapi import FastAPI, UploadFile, File, Form, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import shutil | |
| import os | |
| import json | |
| from app.config import LLM_MODE, VIDEO_TEMP_PATH, LOG_FILE | |
| from app.pipeline import run_pipeline | |
| from app.decision import classify | |
| from app.llm import ensure_deepface_model, ensure_model_ollama, generate_alert | |
| from app.logger import get_logger, log | |
| app = FastAPI() | |
| # LIBERAR CORS (para testes) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # ou depois restringe | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| #allow_origins=[ | |
| # "https://seu-space.hf.space" | |
| #] | |
| def get_logger_from_request(request: Request): | |
| return request.app.state.logger | |
| def startup_event(): | |
| app.state.logger = get_logger("API") | |
| app.state.logger.info("Aplicação iniciada.") | |
| ensure_deepface_model(app.state.logger) | |
| def health(request: Request): | |
| logger = get_logger_from_request(request) | |
| log("Iniciando health check.", logger=logger) | |
| return {"status": "ok"} | |
| def read_logs(request: Request): | |
| logger = get_logger_from_request(request) | |
| log("Lendo logs.", logger=logger) | |
| try: | |
| if not os.path.exists(LOG_FILE): | |
| return {"status": "error", "message": "Arquivo de log não encontrado"} | |
| with open(LOG_FILE, "r") as f: | |
| content = f.read() | |
| return { | |
| "status": "success", | |
| "log": content.splitlines() | |
| } | |
| except Exception as e: | |
| return { | |
| "status": "error", | |
| "message": str(e) | |
| } | |
| async def analyze(request: Request, video: UploadFile = File(...), medical_text: str = Form(None)): | |
| logger = get_logger_from_request(request) | |
| log("Iniciando análise.", logger=logger) | |
| log("Salvando vídeo.", logger=logger) | |
| with open(VIDEO_TEMP_PATH, "wb") as buffer: | |
| shutil.copyfileobj(video.file, buffer) | |
| log("Vídeo salvo com sucesso!", logger=logger) | |
| log("Iniciando Pipeline", logger=logger) | |
| data = run_pipeline(VIDEO_TEMP_PATH, logger) | |
| log("Pipeline concluído.", logger=logger) | |
| log("Iniciando classificação", logger=logger) | |
| estado = classify(data) | |
| log("Classificação concluída com sucesso!", logger=logger) | |
| if LLM_MODE == "ollama": | |
| log("Iniciando LLM.", logger=logger) | |
| ensure_model_ollama(logger=logger) | |
| log("LLM iniciada com sucesso!", logger=logger) | |
| log("Iniciando geração de alertas.", logger=logger) | |
| alerta = generate_alert(data, medical_text, logger=logger) | |
| log("Geração de alertas concluída com sucesso!", logger=logger) | |
| log(f"Excluindo arquivo de video: {VIDEO_TEMP_PATH}") | |
| if os.path.exists(VIDEO_TEMP_PATH): | |
| os.remove(VIDEO_TEMP_PATH) | |
| log(f"Arquivo de video '{VIDEO_TEMP_PATH}' excluído com sucesso!", logger=logger) | |
| log("retornando resultados...", logger=logger) | |
| return { | |
| "estado": estado, | |
| "alerta": alerta, | |
| "data": data | |
| } |