Here’s a detailed explanation of Model Deployment using Flask/Django, covering each step comprehensively.
Model Deployment using Flask/Django
1. Introduction to Model Deployment
Machine learning (ML) models are trained to make predictions, but they must be deployed so that they can be accessed by users or other applications. Deployment allows a model to be used in real-world scenarios, whether through a web application, API, or a mobile app. Flask and Django are two popular Python web frameworks used for deploying machine learning models.
- Flask: A lightweight, minimalistic framework for creating APIs.
- Django: A full-featured web framework with built-in database and admin management.
2. Understanding the Deployment Pipeline
Model deployment follows these key steps:
- Train the Model: Develop and train a machine learning model using libraries like Scikit-learn, TensorFlow, or PyTorch.
- Save the Model: Store the trained model using serialization techniques like
pickle
orjoblib
. - Create a Web Application: Use Flask or Django to build an API that exposes the model.
- Serve Predictions: The application takes input data, processes it, and returns predictions.
- Containerization (Optional): Use Docker to package the model and API into a portable container.
- Cloud Deployment (Optional): Host the model on cloud platforms like AWS, GCP, or Heroku.
3. Model Deployment with Flask
Step 1: Install Dependencies
First, install the required Python packages:
pip install flask pandas numpy scikit-learn pickle-mixin
Step 2: Train and Save the Model
Let’s assume we’re deploying a classification model using Scikit-learn.
import pickle
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load dataset
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
# Train the model
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
# Save the model using pickle
with open('model.pkl', 'wb') as f:
pickle.dump(model, f)
Step 3: Create a Flask App
Create a file named app.py
to serve the model.
from flask import Flask, request, jsonify
import pickle
import numpy as np
app = Flask(__name__)
# Load the trained model
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
@app.route('/')
def home():
return "Welcome to the ML Model API!"
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json(force=True)
input_features = np.array(data['features']).reshape(1, -1)
prediction = model.predict(input_features)
return jsonify({'prediction': int(prediction[0])})
if __name__ == '__main__':
app.run(debug=True)
Step 4: Run the Flask App
Execute the following command:
python app.py
The application will be available at http://127.0.0.1:5000/
.
To test the API, send a POST request using Postman or cURL:
{
"features": [5.1, 3.5, 1.4, 0.2]
}
Expected response:
{
"prediction": 0
}
4. Model Deployment with Django
Step 1: Install Django
pip install django pandas numpy scikit-learn
Step 2: Create a Django Project
Run the following command:
django-admin startproject ml_project
cd ml_project
Step 3: Create a Django App
python manage.py startapp predictor
Add 'predictor'
to INSTALLED_APPS
in settings.py
.
Step 4: Train and Save the Model
Save the trained model in predictor/model.py
using the same Scikit-learn training script as shown in Flask.
Step 5: Create a Django View
Edit predictor/views.py
:
from django.shortcuts import render
from django.http import JsonResponse
import pickle
import numpy as np
# Load the model
with open('predictor/model.pkl', 'rb') as f:
model = pickle.load(f)
def predict(request):
if request.method == 'POST':
data = request.POST.get('features')
input_features = np.array(eval(data)).reshape(1, -1)
prediction = model.predict(input_features)
return JsonResponse({'prediction': int(prediction[0])})
return JsonResponse({'error': 'Invalid request'})
Step 6: Configure URLs
Edit ml_project/urls.py
:
from django.contrib import admin
from django.urls import path
from predictor.views import predict
urlpatterns = [
path('admin/', admin.site.urls),
path('predict/', predict, name='predict'),
]
Step 7: Run the Django Server
python manage.py runserver
The API will be available at http://127.0.0.1:8000/predict/
.
Test it using Postman:
{
"features": [5.1, 3.5, 1.4, 0.2]
}
Response:
{
"prediction": 0
}
5. Deploying Flask/Django to Cloud
Once the API is working locally, deploy it using cloud platforms like:
Option 1: Deploy on Heroku
- Install Heroku CLI.
- Create a
requirements.txt
file:pip freeze > requirements.txt
- Create a
Procfile
for Flask:web: gunicorn app:app
- Deploy to Heroku:
heroku create my-ml-api git init git add . git commit -m "First commit" heroku git:remote -a my-ml-api git push heroku master
Option 2: Deploy with Docker
Create a Dockerfile
:
FROM python:3.8
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Build and run the container:
docker build -t ml-api .
docker run -p 5000:5000 ml-api