File size: 2,024 Bytes
2a2c9b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import threading
import warnings
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv

warnings.filterwarnings("ignore", category=FutureWarning, module="google")
load_dotenv()

from firebase_auth import init_firebase
from models.loader import ModelLoader
from models.uncertainty_loss import HomoscedasticUncertaintyLoss
from config import CORS_ORIGINS, CORS_ORIGIN_REGEX, NUM_TASKS

model_loader = ModelLoader()
criterion = None


def _warmup():
    global criterion
    try:
        model = model_loader.load_model()
        criterion = HomoscedasticUncertaintyLoss(num_tasks=NUM_TASKS)
        print("Model loaded and warmed up successfully")
    except Exception as e:
        print(f"Warmup warning (non-fatal): {e}")


@asynccontextmanager
async def lifespan(app: FastAPI):
    init_firebase()
    threading.Thread(target=_warmup, daemon=True).start()
    print("ToxiPredict API started")
    yield
    print("Shutting down")


app = FastAPI(
    title="ToxiPredict API",
    description="Uncertainty-Aware Multi-Task GNN for Toxicophore Prediction",
    version="1.0.0",
    lifespan=lifespan,
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=CORS_ORIGINS,
    allow_origin_regex=CORS_ORIGIN_REGEX,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

from routes.health import router as health_router
from routes.prediction import router as prediction_router
from routes.explain import router as explain_router
from routes.agent import router as agent_router
from routes.history import router as history_router
from routes.models import router as models_router

app.include_router(health_router)
app.include_router(prediction_router)
app.include_router(explain_router)
app.include_router(agent_router)
app.include_router(history_router)
app.include_router(models_router)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)