A beginner’s guide to train and deploy machine learning pipelines in Python using PyCaret
Inspired by these articles and Github repos:
- How to Move Machine Learning Model to Production Linode
- https://github.com/abalarin/Flask-on-Linode
- Build and Deploy Your First Machine Learning Model
- https://github.com/pycaret/deployment-heroku
This is the breakdown of a fairly simple Flask Application serving a PyCaret pickled model.
Flask-on-Linode
│ README.md
│ FlaskDeployment.md
│ .gitignore
└───flask_app
│ │ app.py
│ │ deploy_28082021.pkl
│ │ requirements.txt
│ └───static
│ │ │ style.css
│ └───templates
│ │ │ home.html
Lets walk through setting up your development environment and deploying this application on your local machine
- Install Python, pip, and virtualenv
- Clone this repo and CD into the projects directory
git clone https://github.com/getcontrol/Flask-model-Linode
cd flask_app
- Create and activate a virtualenv
virtualenv venv
source venv/bin/activate
- Install packages
pip install -r flask_app/requirements.txt
- Create Flask environment variables
export FLASK_APP=flask_app/app.py
export FLASK_ENV=development
- Run it
flask run
- SSH into your Linode
ssh user@<Linode-IP>
- Navigate to your Home directory
cd /home
- Install your essential Linux packages to get started (enter command one by one):
sudo apt update
sudo apt upgrade
sudo apt install git
- Pull from source control (Replace my repo with yours)
git clone https://github.com/getcontrol/Flask-model-Linode && cd Flask-model-Linode
Configure NGINX
NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.
- Install NGINX
sudo apt install nginx
- Create an NGINX Configuration file
sudo nano /etc/nginx/sites-enabled/flaskapp
server {
listen 80;
server_name <Your Linodes IP>;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
- Unlink the NGINX default config
unlink /etc/nginx/sites-enabled/default
- Reload your NGINX server
sudo nginx -s reload
If you try navigating to your Linode's IP in a web browser you should get the following or a similar error.
You should now be in your applications root directory on your Linode.
- Install Python 3
sudo apt install python3
- Install pip and make sure its the latest version
sudo apt install python3-pip
python3 -m pip install --upgrade --force-reinstall pip
- Install Flask Packages/libraries.
cd flask_app && pip install -r requirements.txt
- Start the app
python3 app.py
- Your app is running successfully if you see this!
Transformation Pipeline and Model Sucessfully Loaded
* Serving Flask app 'model' (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
* Running on http://xx.xx.xx.xx:5000/ (Press CTRL+C to quit)
* Restarting with stat
Transformation Pipeline and Model Sucessfully Loaded
* Debugger is active!
* Debugger PIN: 673-488-585
You can train your own Pycret model using the Notebook:
-
Train the model and save it with a unique name "mymodel.pkl"
-
Change line 9 (remove file extension) :
model = load_model('mymodel')
- Start the app
python3 app.py
