Describe the bug
When I am trying to serve a trained ludwig model for multi-label classification, on /predict I always get an Internal server error
To Reproduce
Steps to reproduce the behavior:
input_features:
- level: "word"
name: "content"
type: "text"
output_features:
- name: tags
type: set
create a text_data.csv
"content","tags"
"hello world","hello, world, stupid"
"my name is Bob","name, hello"
train the model
ludwig experiment --data_csv text_data.csv --model_definition_file model_definition.yaml
start serve command on the newly created model
ludwig serve --model_path ./results/experiment_run/model
Run /predict request
curl http://localhost:8000/predict -X POST -F "content=something"
Expected behavior
I'd expect to see the 200 OK status with result of prediction
Observed behavior
Request fails with 500 Internal Server Error
Logs of the ludwig:
INFO: Started server process [753]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Error: Object of type 'float32' is not JSON serializable
ERROR:ludwig.serve:Error: Object of type 'float32' is not JSON serializable
INFO: 172.17.0.1:57522 - "POST /predict HTTP/1.1" 500 Internal Server Error
Environment (please complete the following information):
Additional context
Error doesn't happen on ludwig predict, it happens only when ludwig serve + /predict request
From debugging I realized that tags_probability field is a list of numpy.float32 numbers, which are not convertable to JSON via JSONResponse - so error is thrown exactly on this line: https://github.com/uber/ludwig/blob/v0.2.2.8/ludwig/serve.py#L76
I assume that the easiest way to fix it would be to convert values into float somewhere here: https://github.com/uber/ludwig/blob/v0.2.2.8/ludwig/data/postprocessing.py#L108 because when output_feature_type is set, result is processed in this else branch
@catr1ne55 thank you for posting this and for the detailed instruction for reproducing. Ludwig had a JSONEncoder that deals with numpy arrays (utils/data_utils.py::NumpyEncoder). We'll replace the current JSON decoder with that and this should work fine. Will keep you posted.
Pushed a commit that solves the issue. Please confirm if you can by using the code on master.
Some minor comments on you example that may be useful:
@w4nderlust After updating and retraining model everything works as expected.
Thank you very much for quick fix and comments!
Most helpful comment
Pushed a commit that solves the issue. Please confirm if you can by using the code on master.