-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (37 loc) · 1.24 KB
/
main.py
File metadata and controls
51 lines (37 loc) · 1.24 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
from fastapi import FastAPI
from contextlib import asynccontextmanager
from src.app.router import BaseRouter
from src.app.service import Service
from src.client.configure_bq import ConfigureBigQuery
from dotenv import load_dotenv
from os import getenv
import json
from joblib import load
load_dotenv()
@asynccontextmanager
async def lifespan(app: FastAPI):
# load env
project_id = getenv("PROJECT_ID")
credential_file_path = getenv("CRED_FILE_PATH")
team_mapping_path = getenv("TEAM_NAME_MAP")
ml_model_path = getenv("PREDICTION_MODEL")
client = ConfigureBigQuery(project_id=project_id, credential_file_path=credential_file_path)
with open(team_mapping_path) as f:
team_mapping = json.load(f)
team_mappings = team_mapping["name_to_id"]
prediction_model = load(ml_model_path)
utils = {
"team_mapping" : team_mappings,
"prediction_model" : prediction_model,
"project_id" : project_id,
}
service = Service(client= client, utils=utils)
base_router = BaseRouter(
service=service,
prefix="/v1/data",
tags=["data"]
)
# Initialize services
app.include_router(base_router.include_routes())
yield
app = FastAPI(lifespan=lifespan)