https://github.com/drorata/pm_api
Minimal example of a RESTful API for a prediction model
https://github.com/drorata/pm_api
Last synced: 10 months ago
JSON representation
Minimal example of a RESTful API for a prediction model
- Host: GitHub
- URL: https://github.com/drorata/pm_api
- Owner: drorata
- License: mit
- Created: 2018-05-08T08:31:42.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-13T11:27:28.000Z (over 7 years ago)
- Last Synced: 2025-03-05T10:46:35.650Z (over 1 year ago)
- Language: Python
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Building a minimal Flask based prediction API
So, you finished training your model.
Congratulations!
Now, it is time to expose it to the world (or at least to your peers).
A standard way would be to build a RESTful API; to that end [Flask](http://flask.pocoo.org/) is probably a natural choice.
In this repository, we:
1. Fetch titanic survival data
2. Train a model out of it
3. Persist the model into a Python [pickle](https://docs.python.org/3/library/pickle.html)
4. Build an app which accepts JSON and returns predictions
In reality, it is likely you will need to split this example into three components:
1. *Model training* and persisting/pickling
2. Trained model *storage* (e.g. on S3)
3. *Prediction app* which will load the trained model and provide prediction based on requests
# How to try out this?
## Virtual environment
For your own safety, make sure you work with this example inside a virtual environment.
Simple way to do so is `conda create -n try-out-prediction-app python=3.6` if you're using `conda`.
In the virtual environment, install the requirements: `pip install -r requirements.txt`
## Prepare data and train model
To make life easy, simply run `make train`.
## Run the app
### Local and naive
First, from the root of the project run `python src/app.py`.
You are expecting the following: `* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)`.
If you install `httpie`, you could now easily try out the app.
In another terminal, you can now ask the app for predictions on the test set: `http POST http://0.0.0.0:5000/predict < data/test.json`.
That's it.
### Using docker
You can also use the provided `Dockerfile`. First, build the image:
```bash
docker build -t pm_api .
```
Once built, you can start a container running the image.
It is important to forward the port `5000` from the container to the host; use the `-p` parameter to do so:
```bash
docker run -p 5000:5000 pm_api
```
Once, running, you can send requests using the same line as above (used for the local example).
# Remarks
- I used the skeleton provided by [Amir Ziai](https://medium.com/@amirziai/a-flask-api-for-serving-scikit-learn-models-c8bcdaa41daa)
- To have a consistent way of training and predicting, I use the tools from [ds-pub-utils](https://github.com/drorata/ds-pub-utils).
This allows fitting the model on the training set (including with regards to the data preprocessing).
- You can also play around with the `/sleep` endpoint and use `foo.json` and `foo2.json` to see how two requests are processed in parallel.