https://github.com/noahgift/python-mlops-cookbook
This is an example of a Containerized Flask Application that can deploy to many target environments including: AWS, GCP and Azure.
https://github.com/noahgift/python-mlops-cookbook
cookbook mlops
Last synced: over 1 year ago
JSON representation
This is an example of a Containerized Flask Application that can deploy to many target environments including: AWS, GCP and Azure.
- Host: GitHub
- URL: https://github.com/noahgift/python-mlops-cookbook
- Owner: noahgift
- License: cc0-1.0
- Created: 2021-03-11T00:44:28.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-05-19T21:51:21.000Z (about 2 years ago)
- Last Synced: 2024-10-12T15:29:53.266Z (almost 2 years ago)
- Topics: cookbook, mlops
- Language: Jupyter Notebook
- Homepage:
- Size: 116 KB
- Stars: 392
- Watchers: 7
- Forks: 309
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/noahgift/Python-MLOps-Cookbook/actions/workflows/pythonapp.yml)
[
## 🎓 Pragmatic AI Labs | Join 1M+ ML Engineers
### 🔥 Hot Course Offers:
* 🤖 [Master GenAI Engineering](https://ds500.paiml.com/learn/course/0bbb5/) - Build Production AI Systems
* 🦀 [Learn Professional Rust](https://ds500.paiml.com/learn/course/g6u1k/) - Industry-Grade Development
* 📊 [AWS AI & Analytics](https://ds500.paiml.com/learn/course/31si1/) - Scale Your ML in Cloud
* ⚡ [Production GenAI on AWS](https://ds500.paiml.com/learn/course/ehks1/) - Deploy at Enterprise Scale
* 🛠️ [Rust DevOps Mastery](https://ds500.paiml.com/learn/course/ex8eu/) - Automate Everything
### 🚀 Level Up Your Career:
* 💼 [Production ML Program](https://paiml.com) - Complete MLOps & Cloud Mastery
* 🎯 [Start Learning Now](https://ds500.paiml.com) - Fast-Track Your ML Career
* 🏢 Trusted by Fortune 500 Teams
Learn end-to-end ML engineering from industry veterans at [PAIML.COM](https://paiml.com)
# Python MLOps Cookbook
This is an example of a Containerized Flask Application that can be the core ingredient in many "recipes", i.e. deploy targets..

* [Read Practical MLOps Online](https://learning.oreilly.com/library/view/practical-mlops/9781098103002/)
* [Purchase Practical MLOps](https://www.amazon.com/Practical-MLOps-Operationalizing-Machine-Learning/dp/1098103017)
## Github Container Registery
Feel free to test my ML project: `docker pull ghcr.io/noahgift/python-mlops-cookbook:latest`
## Assets in repo
* `Makefile`: [View Makefile](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/Makefile)
* `requirements.txt`: [View requirements.txt](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/requirements.txt)
* `cli.py`: [View cli.py](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/cli.py)
* `utilscli.py`: [View utilscli.py](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/utilscli.py)
* `app.py`: [View app.py](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/app.py)
* `mlib.py`: [View mlib.py](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/mlib.py)Model Handling Library
* `htwtmlb.csv`: [View CSV](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/htwtmlb.csv) Useful for input scaling
* `model.joblib`: [View model.joblib](https://github.com/noahgift/Python-MLOps-Cookbook/raw/781053e4d45ebeeb64ecdf2dc1b896b338530aab/model.joblib)
* `Dockerfile`: [View Dockerfile](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/Dockerfile)
* `Baseball_Predictions_Export_Model.ipynb`: [Baseball_Predictions_Export_Model.ipynb](https://colab.research.google.com/drive/1mTTXGWp6jERDIBDLIQzBDvvwRciuEGcj?usp=sharing)

### CLI Tools
There are two CLI tools. First, the main `cli.py` is the endpoint that serves out predictions.
To predict the height of an MLB player you use the following: ` ./cli.py --weight 180`

The second cli tool is `utilscli.py', and this performs model retraining, and could serve as the entry point to do more things.
For example, this version doesn't change the default `model_name`, but you could add that as an option by forking this repo.
`./utilscli.py retrain --tsize 0.4`
Here is an example retraining the model.

Additionally you can query the API via the CLI, allowing you to change both the host and the value passed into the API.
This is accomplished through the requests library.
`./utilscli.py predict --weight 400`

### Flask Microservice
The Flask ML Microservice can be run many ways.
#### Containerized Flask Microservice Locally
You can run the Flask Microservice as follows with the commmand: `python app.py`.
```
(.venv) ec2-user:~/environment/Python-MLOps-Cookbook (main) $ python app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
INFO:werkzeug: * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
INFO:werkzeug: * Restarting with stat
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger PIN: 251-481-511
```
To serve a prediction against the application, run the `predict.sh`.
```
(.venv) ec2-user:~/environment/Python-MLOps-Cookbook (main) $ ./predict.sh
Port: 8080
{
"prediction": {
"height_human_readable": "6 foot, 2 inches",
"height_inches": 73.61
}
}
```
#### Containerized Flask Microservice
Here is an example of how to build the container and run it locally, this is the contents of [predict.sh](https://github.com/noahgift/Python-MLOps-Cookbook/blob/main/predict.sh)
```
#!/usr/bin/env bash
# Build image
#change tag for new container registery, gcr.io/bob
docker build --tag=noahgift/mlops-cookbook .
# List docker images
docker image ls
# Run flask app
docker run -p 127.0.0.1:8080:8080 noahgift/mlops-cookbook
```
#### Automatically Build Container via Github Actions and Push to Github Container Registery
To setup the container build process do the following. This is also covered by Alfredo Deza in Practical MLOps book in greater detail.
```
build-container:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Loging to Github registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.BUILDCONTAINERS }}
- name: build flask app
uses: docker/build-push-action@v2
with:
context: ./
#tags: alfredodeza/flask-roberta:latest
tags: ghcr.io/noahgift/python-mlops-cookbook:latest
push: true
```

#### Automatically Build Container via Github Actions and Push to Dockerhub Container Registery
## Build Targets
With the project using DevOps/MLOps best practices including linting, testing, and deployment, this project can be the base to deploy to many deployment targets.
[In progress....]
### Other Tools and Frameworks
[In progress....]
#### FastAPI
* [fastapi](https://fastapi.tiangolo.com)
### AWS
#### Elastic Beanstalk
#### AWS Lambda Recipes
Install [SAM as documented here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html), AWS Cloud9 has it installed already.
You can [find the recipes here](https://github.com/noahgift/Python-MLOps-Cookbook/tree/main/recipes/aws-lambda-sam)
##### AWS Lambda-SAM Local

##### AWS Lambda-SAM Containerized Deploy
Follow recipe in recipe section.

When deployed an easy way to verify image is via Console.

A great way to test the API Endpoint is with the Cloud9 Environment:

Another way is the the tool "Postman":

#### AWS App Runner
Watch a YouTube Walkthrough on AWS App Runner for this repo here: https://www.youtube.com/watch?v=zzNnxDTWtXA

#### AWS Co-Pilot
Following setup here and then deploy project using cli
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/getting-started-aws-copilot-cli.html
### GCP
#### Cloudrun (CaaS: Container as a Service)
It is trivial (if you select project):
`gcloud config set project `
A. Get GCP Account
B. Checkout project
C. `cloud run deploy` inside of project
D. Verify it works by using `./utilscli.py`

#### App Engine
#### GKE (Kubernetes)
### Azure App Services
## Production Patterns
[In progress....]
* Cached model (deploy)
* Load-testing
## DataScience Workflow

This repository is focused on MLOps. To see more about Data Storytelling, you can go to this [Github repo on Data Story Telling](https://github.com/noahgift/data-story-telling)
#### Next Steps: Take Coursera MLOps Course

* [Take the Specialization](https://www.coursera.org/learn/cloud-computing-foundations-duke?specialization=building-cloud-computing-solutions-at-scale)
* [Cloud Computing Foundations](https://www.coursera.org/learn/cloud-computing-foundations-duke?specialization=building-cloud-computing-solutions-at-scale)
* [Cloud Virtualization, Containers and APIs](https://www.coursera.org/learn/cloud-virtualization-containers-api-duke?specialization=building-cloud-computing-solutions-at-scale)
* [Cloud Data Engineering](https://www.coursera.org/learn/cloud-data-engineering-duke?specialization=building-cloud-computing-solutions-at-scale)
* [Cloud Machine Learning Engineering and MLOps](https://www.coursera.org/learn/cloud-machine-learning-engineering-mlops-duke?specialization=building-cloud-computing-solutions-at-scale)