-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (45 loc) · 1.55 KB
/
Copy pathmain.py
File metadata and controls
55 lines (45 loc) · 1.55 KB
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
from contextlib import asynccontextmanager
from fastapi import FastAPI, APIRouter, Depends
import logging
from src.middleware.auth import get_api_key
from src.shared.shared import load_model, log_level
from src.api import classify
from src.api import classify_batch
from src.api import mediapipe_tasks
from src.helper.generate_api_key_and_hash import generate_api_key_and_hash_with_salt
numeric_log_level = getattr(logging, log_level, logging.INFO)
logging.basicConfig(level=numeric_log_level)
logger = logging.getLogger(__name__)
tags_metadata = [
{
"name": "Classify Images",
"description": "Endpoints for image classification tasks.",
},
{
"name": "Mediapipe Tasks",
"description": "Endpoints for mediapipe vision tasks.",
},
]
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("Application starting up...")
try:
load_model()
if logger.isEnabledFor(logging.DEBUG):
generate_api_key_and_hash_with_salt()
else:
logger.info("Debug mode not enabled.")
except Exception as e:
logger.error(f"Failed to load model: {e}", exc_info=True)
raise
yield
logger.info("Application shutting down...")
app = FastAPI(lifespan=lifespan, openapi_tags=tags_metadata,)
router = APIRouter()
app.include_router(router)
app.include_router(classify.router)
app.include_router(classify_batch.router)
app.include_router(mediapipe_tasks.router)
@app.get("/", dependencies=[Depends(get_api_key)])
def root():
return {"res": "FastAPI is up and running!"}