pycaret icon indicating copy to clipboard operation
pycaret copied to clipboard

[BUG]: create_api errors!

Open celestinoxp opened this issue 2 years ago • 4 comments

pycaret version checks

  • [X] I have checked that this issue has not already been reported here.

  • [X] I have confirmed this bug exists on the latest version of pycaret.

  • [X] I have confirmed this bug exists on the master branch of pycaret (pip install -U git+https://github.com/pycaret/pycaret.git@master).

Issue Description

try create api and receive error IndentationError: unexpected indent

Reproducible Example

create_api(final_model, 'final_model_api')
!python final_model_api.py

Expected Behavior

no errors

Actual Results

File "C:\...\final_model_api.py", line 2
    import pandas as pd
IndentationError: unexpected indent

Installed Versions

pycaret 3 rc3

celestinoxp avatar Jul 26 '22 22:07 celestinoxp

After edit file final_model_api.py, remove identation and run notebook cell to run file, i receive new error: Traceback (most recent call last): File "C:...\final_model_api.py", line 3, in from pycaret.MLUsecase.CLASSIFICATION import load_model, predict_model ModuleNotFoundError: No module named 'pycaret.MLUsecase'

celestinoxp avatar Jul 26 '22 22:07 celestinoxp

Yes, I'm facing the same issuue

PabloJMoreno avatar Jul 28 '22 15:07 PabloJMoreno

@ngupta23 create_api maybe work on pycaret 2.3.x, however pycaret 3.x code as changed but create_api command needs changes too. Can you have a look on that?

I found some issues that need to be fixed:

  • indentation in code of created file by create_api (indentation should be removed)
  • Line 3 "from pycaret. MLUsecase.CLASSIFICATION import load_model, predict_model" .shoud be changed by "from pycaret.classification import load_model, predict_model " ?
  • Possible problem in windows. Use of !python filename.py (created by create_api) maybe not work. I had to open a tab manually. I think it should be automatic, that is, open a new tab automatically with url: http://127.0.0.1:8000/docs .

I think you already have an initial idea of how to solve the problem.

Well i do all manually but is in my laptop, can you make this changes in pycaret?

thanks

celestinoxp avatar Jul 28 '22 15:07 celestinoxp

I tried to test the create_app command and it also doesn't work in pycaret 3. It doesn't assume the features that pycaret itself created, like the year, month, day extracted from date_time feature...

celestinoxp avatar Jul 29 '22 08:07 celestinoxp

Reproduced in August 15th meeting. Fix needed in these 2 places

  • Indent
  • correct import per latest library structure

image

ngupta23 avatar Aug 15 '22 10:08 ngupta23

Reproduced in August 15th meeting...where is the meeting? Live on Youtube?

celestinoxp avatar Aug 15 '22 10:08 celestinoxp

Reproduced in August 15th meeting...where is the meeting? Live on Youtube?

The core developers try to meet once a week to discuss next steps, etc.

ngupta23 avatar Aug 15 '22 11:08 ngupta23

Per August 29th meeting, reassigned to @tvdboom.

ngupta23 avatar Aug 29 '22 10:08 ngupta23

@tvdboom I have tested this after the fix this morning, the indent and import issue is fixed however a new thing I noticed:

lr = create_model('lr')
create_api(lr, 'my_lr_api')
!python my_lr_api.py

(pycaret-dev) C:\Users\owner\pycaret>python my_lr_api.py Transformation Pipeline and Model Successfully Loaded Traceback (most recent call last): File "my_lr_api.py", line 17, in pydanticoutputmodel=create_model("my_lr_api_output", **{targetname: self.y.iloc[0]}) NameError: name 'targetname' is not defined

Line 17 of api failing. The API generated:

import pandas as pd
from pycaret.classification 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_lr_api")

# Create input/output pydantic models
pydanticinputmodel=create_model("my_lr_api_input", **{'Id': 585, 'WeekofPurchase': 264, 'StoreID': 7, 'PriceCH': 1.8600000143051147, 'PriceMM': 2.130000114440918, 'DiscCH': 0.3700000047683716, 'DiscMM': 0.0, 'SpecialCH': 1, 'SpecialMM': 0, 'LoyalCH': 0.8361600041389465, 'SalePriceMM': 2.130000114440918, 'SalePriceCH': 1.4900000095367432, 'PriceDiff': 0.6399999856948853, 'Store7': 'Yes', 'PctDiscMM': 0.0, 'PctDiscCH': 0.19892500340938568, 'ListPriceDiff': 0.27000001072883606, 'STORE': 0})
pydanticoutputmodel=create_model("my_lr_api_output", **{targetname: self.y.iloc[0]})


# Define predict function
@app.post("/predict", response_model=pydanticoutputmodel)
def predict(datainput:pydanticinputmodel):
    data = pd.DataFrame([datainput.dict()])
    predictions = predict_model(model, data=data)
    return {"Purchase_prediction": predictions["prediction_label"][0]}

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)

image

moezali1 avatar Aug 30 '22 20:08 moezali1

I fixed this error but my app is not launching correctly. @moezali1 can you try using the change_engine_param_name repo? Not sure what the error is now

tvdboom avatar Aug 31 '22 08:08 tvdboom

@tvdboom Just did. The exact same error.

image

moezali1 avatar Sep 06 '22 21:09 moezali1

@moezali1 That can't be because that code is no longer there. Are you using the change_engine_param_name repo?

tvdboom avatar Sep 07 '22 06:09 tvdboom