I am building an API where I need to load a model through pickle and make a prediction
The code is below:
from fastapi import FastAPI
import uvicorn
import joblib
from utilities import * # upload the functions
from schema import Customer # load the schema
from sklearn.linear_model import LogisticRegression
from sklearn.base import BaseEstimator, TransformerMixin
import os
import numpy as np
import asyncio
app = FastAPI()
path = 'data/'
X_test = import_test_data(path + 'X_test.xlsx')
MODEL_FILE = "./model/model.pkl" # path to the model
async def predict_client(data:Customer):
model = joblib.load(MODEL_FILE) # loading model parameters
y_hat_test = model.predict(X_test)
# get the predicted probabilities
y_hat_test_proba = model.predict_proba(X_test)[:][: , 1]
default = y_hat_test_proba[0] >= 0.5
return {"probability_of_defaulting": float(y_hat_test_proba[0]), "is_defaulting": bool(default)}
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
The code above works fine as it is. When I load the model through async def predict_client(data:Customer):
The API works and I get an output with a prediction. However, my problem is that the variable model
is a local one as it is described inside the function.
When i start loading the model (without using async def predict_client
) I get as error as shown below:
model = joblib.load(MODEL_FILE)
print(model['model'].coef_)
I get an error as follows:
^^^^^^^^^^^^^^^^^^^^^^^
File "xxxxx-packagesjoblibnumpy_pickle.py", line 658, in load
obj = _unpickle(fobj, filename, mmap_mode)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxxx/venvLibsite-packagesjoblibnumpy_pickle.py", line 577, in _unpickle
obj = unpickler.load()
^^^^^^^^^^^^^^^^
File "C:Python311Libpickle.py", line 1213, in load
dispatch[key[0]](self)
File "C:Python311Libpickle.py", line 1538, in load_stack_global
self.append(self.find_class(module, name))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Python311Libpickle.py", line 1582, in find_class
return _getattribute(sys.modules[module], name)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:Python311Libpickle.py", line 331, in _getattribute
raise AttributeError("Can't get attribute {!r} on {!r}"
AttributeError: Can't get attribute 'WoE_Binning' on <module '__main__' (built-in)>
It seems that the script does not have time to load the model and it runs into an error when it tried to read print(model['model'].coef_)
.
I tried to fix that issue by doing the following changes.
async def read_model(MODEL_FILE):
model = joblib.load(MODEL_FILE)
return model
model=asyncio.run(read_model(MODEL_FILE))
print(model['model'].coef_)
but again i get similar error:
AttributeError: Can't get attribute 'WoE_Binning' on <module '__main__' (built-in)>
How can I successfully load the model once? When I do it through a function the model is a local variable that I cannot re-use.
Thank you