Spaces:
Sleeping
Sleeping
File size: 1,153 Bytes
f638bd7 1509278 f638bd7 1509278 f638bd7 |
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 |
from fastapi import APIRouter
from pydantic import BaseModel
from utils.ProcessingClass import PreProcessingClass
import pickle
class RequestType(BaseModel):
MONATSZAHL: str
AUSPRAEGUNG: str
JAHR: int
MONAT: str
router = APIRouter(prefix='/predict')
global encoder, xgb_model
@router.on_event('startup')
def _loadPickleFiles():
with open("lib/encoder.pkl", 'rb') as file:
global encoder
encoder = pickle.load(file)
with open("lib/model.pkl", 'rb') as file:
global xgb_model
xgb_model = pickle.load(file)
print("Pickle Files Loaded. Ready for Inference!")
def _do_inference(df):
global xgb_model
return xgb_model.predict(df)
@router.post("/")
def predict(data: RequestType):
global encoder
pc = PreProcessingClass(
MONATSZAHL = data.MONATSZAHL,
AUSPRAEGUNG = data.AUSPRAEGUNG,
JAHR = data.JAHR,
MONAT = data.MONAT,
encoder = encoder
)
date_processed_df = pc._convert_date()
final_df = pc._one_hot(date_processed_df)
results = _do_inference(final_df)
return {"Final Predictions": results.tolist()[0]}
|