Awesome
DS Build Week
Starter code to deploy your machine learning model as an API on Heroku. You can deploy a baseline in 10 minutes.
Tech stack
- FastAPI: Web framework. Like Flask, but faster, with automatic interactive docs.
- Flake8: Linter, enforces PEP8 style guide.
- Heroku: Platform as a service, hosts your API.
- Pipenv: Reproducible virtual environment, manages dependencies.
- Pytest: Testing framework, runs your unit tests.
Getting started
Create a new repository from this template.
Clone the repo
git clone https://github.com/YOUR-GITHUB-USERNAME/YOUR-REPO-NAME.git
cd YOUR-REPO-NAME
Install dependencies
pipenv install --dev
git add Pipfile.lock
git commit -m "Add Pipfile.lock"
Activate the virtual environment
pipenv shell
Launch the app
uvicorn app.main:app --reload
File structure
.
└── app
├── __init__.py
├── main.py
├── routers
│ ├── __init__.py
│ └── predict.py
└── tests
├── __init__.py
├── test_main.py
└── test_predict.py
app/main.py
is where you edit your app's title and description, which are displayed at the top of the your automatically generated documentation. This file also configures "Cross-Origin Resource Sharing", which you shouldn't need to edit.
app/routers/predict.py
defines an API endpoint /predict
which currently returns random predictions. In a notebook, train your model and pickle it. Then in this source code sfile, unpickle your model and edit the predict
function to return real predictions.
When your API receives a POST request, FastAPI automatically parses and validates the request body JSON, using the Item
class attributes and functions. Edit this class so it's consistent with the column names and types from your training dataframe.
- FastAPI docs - Request Body
- FastAPI docs - Field additional arguments
- calmcode.io video - FastAPI - Json
- calmcode.io video - FastAPI - Type Validation
- pydantic docs - Validators
app/tests/test_*.py
is where you edit your pytest unit tests.
More instructions
Install additional packages
pipenv install PYPI-PACKAGE-NAME
Launch a Jupyter notebook
jupyter notebook
Run tests
pytest
Run linter
flake8
Deploying to Heroku
Prepare Heroku
heroku login
heroku create YOUR-APP-NAME-GOES-HERE
heroku git:remote -a YOUR-APP-NAME-GOES-HERE
Deploy to Heroku
git add --all
git commit -m "Deploy to Heroku"
git push heroku main:master
heroku open
Deactivate the virtual environment
exit