File size: 1,373 Bytes
8dbf9ad
3e805ab
53808b4
8dbf9ad
047ce55
8dbf9ad
6fc43b2
047ce55
 
 
7f323ef
8dbf9ad
 
047ce55
6fc43b2
047ce55
 
 
8dbf9ad
047ce55
 
 
6fc43b2
8dbf9ad
047ce55
6fc43b2
047ce55
53808b4
8dbf9ad
047ce55
53808b4
 
047ce55
 
 
 
53808b4
8dbf9ad
047ce55
cafd4ec
047ce55
 
 
 
 
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
import asyncio
import os
from contextlib import asynccontextmanager

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

from src.core.config import MAX_CONCURRENT_INFERENCES
from src.core.logging import log, init_logging_session, close_logging_session
from src.api import danger, explorer, search, system, upload

@asynccontextmanager
async def lifespan(app: FastAPI):
    await init_logging_session()
    log("INFO", "server.startup", message="Loading AI models...")
    
    from src.services.ai_manager import AIModelManager
    
    loop = asyncio.get_event_loop()
    app.state.ai = await loop.run_in_executor(None, AIModelManager)
    app.state.ai_semaphore = asyncio.Semaphore(MAX_CONCURRENT_INFERENCES)

    log("INFO", "server.ready", message="All models loaded. API ready.")
    yield
    
    log("INFO", "server.shutdown", message="API shutting down.")
    await close_logging_session()

app = FastAPI(lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # Update this to your frontend URL in production
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

os.makedirs("temp_uploads", exist_ok=True)

app.include_router(system.router)
app.include_router(upload.router)
app.include_router(search.router)
app.include_router(explorer.router)
app.include_router(danger.router)