Hi, Im doing some tests with the Azure services. In this case Im trying to deploy a ML model, trained with the Azure ML service AutoML in a web app for inference. I have the following files in the project directory:
main.py
requirements.txt
model.pkl
startup.sh
The main.py script is really simple:
import pickle
import logging
import os
from fastapi import FastAPI, Request
app = FastAPI()
logging.basicConfig(level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
LOGGER = logging.getLogger(__name__)
def load_model():
path = os.path.join(os.path.dirname(__file__), "model.pkl")
with open(path, 'rb') as f:
model = pickle.load(f)
return model
model = load_model()
TARGET_NAMES = ['No Failure', 'Failure']
@app.post("/predict_class")
async def predict_class(request: Request):
req = await request.json()
data = req["data"]
y_pred = model.predict(data)[0]
pred_class = TARGET_NAMES[y_pred]
LOGGER.info(f"Request: {data} y_pred: {pred_class}")
response = {"class": pred_class}
return response
Im getting the following traceback error:
2024-05-21T23:31:35.516743913Z File "/tmp/8dc79afc5b4d09d/antenv/lib/python3.9/site-packages/dataclasses.py", line 800, in _process_class
2024-05-21T23:31:35.516951440Z cls_fields = [_get_field(cls, name, type)
2024-05-21T23:31:35.516961970Z File "/tmp/8dc79afc5b4d09d/antenv/lib/python3.9/site-packages/dataclasses.py", line 800, in <listcomp>
2024-05-21T23:31:35.517165168Z cls_fields = [_get_field(cls, name, type)
2024-05-21T23:31:35.517175407Z File "/tmp/8dc79afc5b4d09d/antenv/lib/python3.9/site-packages/dataclasses.py", line 659, in _get_field
2024-05-21T23:31:35.517361835Z if (_is_classvar(a_type, typing)
2024-05-21T23:31:35.517372214Z File "/tmp/8dc79afc5b4d09d/antenv/lib/python3.9/site-packages/dataclasses.py", line 550, in _is_classvar
2024-05-21T23:31:35.517546770Z return type(a_type) is typing._ClassVar
2024-05-21T23:31:35.517556809Z AttributeError: module 'typing' has no attribute '_ClassVar'/home/LogFiles/2024_05_21_10-30-0-213_docker.log (https://test-poc-azure.scm.azurewebsites.net/api/vfs/LogFiles/2024_05_21_10-30-0-213_docker.log)
In the requirements.txt I have the following packages:
fastapi
scikit-learn
uvicorn
azureml
azureml-train
azureml-core
azureml-sdk
azureml-train-core
azureml-train-automl-runtime
I tried using python 3.8, 3.9, 3.10, 3.11 and 3.12. All the python versions available in web apps.
I have tried every workaround found on google also, none of those worked.
Any idea how can this be solved?