I used pycaret to create an REST API.
like
from pycaret.datasets import get_data
data = get_data('diamond')
from pycaret.regression import *
s = setup(data, target = 'Price',
transform_target = True,
log_experiment = True,
#log_plots = True,
experiment_name = 'diamond')
lightgbm = create_model('lightgbm')
create_api(lightgbm, 'my_second_api')
The generated code is
# -*- coding: utf-8 -*-
import pandas as pd
from pycaret.regression import load_model, predict_model
from fastapi import FastAPI
import uvicorn
from pydantic import create_model
# Create the app
app = FastAPI()
# Load trained Pipeline
model = load_model("my_second_api")
# Create input/output pydantic models
input_model = create_model("my_second_api_input", **{'Carat Weight': 1.5499999523162842, 'Cut': 'Ideal', 'Color': 'F', 'Clarity': 'SI1', 'Polish': 'EX', 'Symmetry': 'EX', 'Report': 'GIA'})
output_model = create_model("my_second_api_output", prediction=5169)
# Define predict function
@app.post("/predict", response_model=output_model)
def predict(data: input_model):
data = pd.DataFrame([data.dict()])
predictions = predict_model(model, data=data)
return {"prediction": predictions["prediction_label"].iloc[0]}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
And when I try to run it with python theappi.py
I get
Transformation Pipeline and Model Successfully Loaded
Traceback (most recent call last):
File "/media/cbe421fe-1303-4821-9392-a849bfdd00e2/MyStudy/Udemy/MLOps1/Entrega/6. Model serving a través de APIs/my_second_api.py", line 16, in <module>
input_model = create_model("my_second_api_input", **{'Carat Weight': 1.5499999523162842, 'Cut': 'Ideal', 'Color': 'F', 'Clarity': 'SI1', 'Polish': 'EX', 'Symmetry': 'EX', 'Report': 'GIA'})
File "/home/miniconda3/envs/mlops39/lib/python3.9/site-packages/pydantic/main.py", line 1441, in create_model
return meta(
File "/home/miniconda3/envs/mlops39/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py", line 92, in __new__
private_attributes = inspect_namespace(
File "/home/miniconda3/envs/mlops39/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py", line 372, in inspect_namespace
raise PydanticUserError(
pydantic.errors.PydanticUserError: A non-annotated attribute was detected: `Carat Weight = 1.5499999523162842`. All model fields require a type annotation; if `Carat Weight` is not meant to be a field, you may be able to resolve this error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.
For further information visit https://errors.pydantic.dev/2.5/u/model-field-missing-annotation
I corrected the code to
# -*- coding: utf-8 -*-
import pandas as pd
from pycaret.regression import load_model, predict_model
from fastapi import FastAPI
import uvicorn
from pydantic import create_model
# Create the app
app = FastAPI()
# Load trained Pipeline
model = load_model("my_second_api")
# Create input/output pydantic models using create_model
input_fields = {
'Carat_Weight': (float, 1.5499999523162842),
'Cut': (str, 'Ideal'),
'Color': (str, 'F'),
'Clarity': (str, 'SI1'),
'Polish': (str, 'EX'),
'Symmetry': (str, 'EX'),
'Report': (str, 'GIA')
}
output_fields = {
'prediction': (float, 5169)
}
InputModel = create_model('InputModel', **input_fields)
OutputModel = create_model('OutputModel', **output_fields)
# Define predict function
@app.post("/predict", response_model=OutputModel)
def predict(data: InputModel):
data = pd.DataFrame([data.dict()])
predictions = predict_model(model, data=data)
return {"prediction": predictions["prediction_label"].iloc[0]}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
Is pycaret generating wrong code a common occurrence? and why would be the cause of this?