Minimal example of serving a scikit-learn model over a Python server, using the following Python web frameworks
- Flask
- Tornado
- Sanic (Forthcoming)
- AIOHTTP (Forthcoming)
- Fit a scikit-learn model - I do some light preprocessing, then fit a simple ridge regression model on the California housing dataset.
python train.py
- Run flask server
cd flask && python server.py
- Hit the server
curl -i -H "Content-Type: application/json" -X POST http://127.0.0.1:5000/api/v0/house_value -d @flask/sample_request.json
- Update the model by re-running step 1. This just takes a bootstrap sample, but in production, you might retrain in batch.
- Tell the server to reload the model from disk
curl -X POST http://127.0.0.1:5000/api/v0/update
- Re-run the curl request from step 3 and note that you get a different answer!
- A little more involved, to try to test the async capabilities of this framework
- Start the server:
docker run --rm -v $(pwd):/root/sanic -p 8000:8000 -v /Users/timothysweetser/git/flask-example/artifacts:/root/sanic/artifacts -w /root/sanic tsweetser/sanic python main.py
- Hit the server with a sample request:
curl localhost:8000/api/v0/house_value -d @flask/sample_request.json
- Retrain the model:
python train.py
- Tell the server to reload the model:
curl localhost:8000/api/v0/update
- Rerun step 2 and note that you get a different answer!
- TODO
- This is not a real production system - no error handling, data validation, authentication, logging, etc etc etc
- Please file an issue or make a PR if you see any glaring errors