File size: 5,161 Bytes
338873f
50c4f62
ff6ae63
50c4f62
ff6ae63
 
 
50c4f62
 
ff6ae63
 
0d73bd2
 
ff6ae63
50c4f62
ff6ae63
 
 
 
 
 
 
 
 
50c4f62
 
 
 
 
 
9da9782
ff6ae63
 
 
50c4f62
ff6ae63
 
0d73bd2
ff6ae63
50c4f62
 
 
 
 
ff6ae63
0d73bd2
50c4f62
 
 
 
 
 
195d91a
 
50c4f62
 
 
 
 
 
0d73bd2
 
195d91a
0d73bd2
50c4f62
 
ff6ae63
 
 
 
 
 
 
 
50c4f62
ff6ae63
 
50c4f62
ff6ae63
 
50c4f62
 
 
 
 
 
 
ff6ae63
 
 
 
 
 
 
50c4f62
ff6ae63
 
50c4f62
 
ff6ae63
 
 
 
 
 
 
 
 
 
 
50c4f62
ff6ae63
 
50c4f62
ff6ae63
50c4f62
ff6ae63
 
 
 
50c4f62
ff6ae63
 
50c4f62
0d73bd2
 
 
 
 
 
 
 
50c4f62
 
 
 
 
0d73bd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50c4f62
9da9782
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# uvicorn app:app --reload
import os
import uvicorn
from fastapi import Body, FastAPI, UploadFile, File, Response
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import traceback
import numpy as np
import json

from detect import DengueDetector
from municipal_predictor import DenguePredictor
from state_predictor import StatePredictor


def default_json_serializer(obj):
    if isinstance(obj, np.integer):
        return int(obj)
    elif isinstance(obj, np.floating):
        return float(obj)
    elif isinstance(obj, np.ndarray):
        return obj.tolist()
    raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")


detector: DengueDetector | None = None
predictor: DenguePredictor | None = None
state_predictor: StatePredictor | None = None

# Se api irá utilizar datasets baixados do hugging face ou os locais
ONLINE: bool = True

app = FastAPI()


@app.on_event("startup")
async def startup_event():
    global detector, predictor, state_predictor
    print("Executando evento de startup: Carregando os módulos de IA...")

    offline_flag = (not ONLINE)
    local_city_inf = None
    local_state_inf = None

    detector = DengueDetector()
    try:
        predictor = DenguePredictor(
            offline=offline_flag,
            local_inference_path=local_city_inf,
        )
    except Exception as e:
        print("[WARN] DenguePredictor (municipal) não inicializado:", str(e))
        # print full traceback to help debugging (was previously only printing str(e))
        traceback.print_exc()
        predictor = None
    try:
        state_predictor = StatePredictor(
            offline=offline_flag,
            local_inference_path=local_state_inf,
        )
    except Exception as e:
        print("[WARN] StatePredictor não inicializado:", str(e))
        traceback.print_exc()
        state_predictor = None
    print("Módulos de IA carregados com sucesso. API pronta. Modo:", "online" if ONLINE else "offline")


# --- CORS ---
origins = ["https://previdengue.vercel.app", "http://localhost:3000", "*"]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/")
def health_check():
    return {
        "status": "ok",
        "message": "API de Dengue rodando!",
        "mode": "online" if ONLINE else "offline",
        "online": ONLINE,
    }


@app.post("/detect/")
async def detect(file: UploadFile = File(...)):
    if detector is None:
        return JSONResponse(status_code=503, content={"error": "Detector ainda não foi inicializado."})
    try:
        content = await file.read()
        result = detector.detect_image(content)
        return JSONResponse(content=result)
    except Exception as e:
        tb_str = traceback.format_exc()
        print(tb_str)
        return JSONResponse(status_code=500, content={"error": str(e)})


@app.post("/predict/")
async def predict_dengue_route(payload: dict = Body(...)):
    if predictor is None:
        return JSONResponse(status_code=503, content={"error": "Preditor ainda não foi inicializado."})
    try:
        ibge_code_str = payload.get("ibge_code")
        if ibge_code_str is None:
            raise ValueError("O campo 'ibge_code' é obrigatório.")

        ibge_code = int(ibge_code_str)
        result = predictor.predict(ibge_code)

        json_content = json.dumps(result, default=default_json_serializer)

        return Response(content=json_content, media_type="application/json")

    except Exception as e:
        tb_str = traceback.format_exc()
        print(tb_str)
        return JSONResponse(status_code=500, content={
            "error": str(e),
            "traceback": tb_str,
        })


@app.post("/predict/state/")
async def predict_dengue_state_route(payload: dict = Body(...)):
    global state_predictor
    if state_predictor is None:
        try:
            local_state_inf = None
            state_predictor = StatePredictor(
                offline=(not ONLINE),
                local_inference_path=local_state_inf,
            )
        except Exception as e:
            return JSONResponse(status_code=503, content={"error": f"Preditor estadual ainda não foi inicializado: {str(e)}"})
    try:
        state_sigla = payload.get("state") or payload.get("state_sigla") or payload.get("uf")
        year = payload.get("year")
        week = payload.get("week")
        if not state_sigla:
            raise ValueError("O campo 'state' (sigla) é obrigatório.")

        result = state_predictor.predict(
            str(state_sigla).upper(),
            year=int(year) if year is not None else None,
            week=int(week) if week is not None else None,
        )

        json_content = json.dumps(result, default=default_json_serializer)
        return Response(content=json_content, media_type="application/json")

    except Exception as e:
        tb_str = traceback.format_exc()
        print(tb_str)
        return JSONResponse(status_code=500, content={
            "error": str(e),
            "traceback": tb_str,
        })