Lesson 9: Model Deployment and Ethical AI
Introduction
Welcome to Lesson 9! Today, we're diving into the crucial world of Model Deployment and Ethical AI. By the end of this lesson, you'll understand how to deploy your machine learning models and ensure they're used responsibly and ethically.
We'll cover three main topics: deploying models with Flask and FastAPI, model interpretability and explainability, and fairness in machine learning. These skills are essential for any AI practitioner who wants to make a positive impact with their work.
1. Deploying Models with Flask and FastAPI
Deploying a model is like opening a restaurant. Your trained model is the chef, and you need a way for customers (users) to place orders (make requests) and receive their meals (get predictions). Flask and FastAPI are two popular 'waiters' that can serve your model's predictions to users.
Here's a simple example of deploying a model with Flask:
from flask import Flask, request, jsonify
import pickle
app = Flask(__name__)
# Load the pre-trained model
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
prediction = model.predict(data['input'])
return jsonify({'prediction': prediction.tolist()})
if __name__ == '__main__':
app.run(debug=True)
And here's how you can do the same with FastAPI, which offers automatic API documentation and type checking:
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
app = FastAPI()
# Load the pre-trained model
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
class InputData(BaseModel):
input: list
@app.post("/predict")
def predict(data: InputData):
prediction = model.predict(data.input)
return {"prediction": prediction.tolist()}
Both these examples create a simple API that accepts input data and returns model predictions. In practice, you'd also need to handle data validation, error handling, and potentially authentication and rate limiting.
Interactive Demo: Model Deployment
Let's simulate a deployed model. Adjust the input and see the model's output:
Input value: 5
Model Output: 0
This simple linear model (y = 2x + 1) simulates how a deployed model would take input and provide predictions in real-time.
2. Model Interpretability and Explainability
As our models become more complex, it's crucial to understand why they make certain decisions. Model interpretability is like being able to ask a chef why they chose certain ingredients for a dish.
One popular tool for model explanation is LIME (Local Interpretable Model-agnostic Explanations). LIME works by creating a simple, interpretable model around a specific prediction to explain it.
Here's an example of using LIME to explain a model's predictions:
import lime
import lime.lime_tabular
# Assuming 'model' is your trained model and 'X_train' is your training data
explainer = lime.lime_tabular.LimeTabularExplainer(
X_train,
feature_names=['feature1', 'feature2', 'feature3'],
class_names=['class1', 'class2'],
mode='classification'
)
# Get explanation for a specific instance
explanation = explainer.explain_instance(
X_test[0],
model.predict_proba,
num_features=3
)
# Visualize the explanation
explanation.show_in_notebook()
This code creates a LIME explainer for a tabular dataset, explains a specific instance, and visualizes the explanation.
Interactive Demo: Model Explanation
Let's simulate a model explanation. Click the button to see why the model made its prediction:
3. Fairness and Responsible ML
Ensuring fairness in machine learning is like being a referee in a sports game. We need to make sure our models don't unfairly discriminate against certain groups of people.
One way to audit and improve fairness is using tools like AI Fairness 360 by IBM. This toolkit can help detect and mitigate bias in your models.
Here's an example of how to use AI Fairness 360 to audit and improve a model's fairness:
from aif360.datasets import BinaryLabelDataset
from aif360.metrics import BinaryLabelDatasetMetric
from aif360.algorithms.preprocessing import Reweighing
# Load your dataset
dataset = BinaryLabelDataset(df=your_dataframe,
label_name='target',
protected_attribute_names=['gender'])
# Compute fairness metrics
metric = BinaryLabelDatasetMetric(dataset,
unprivileged_groups=[{'gender': 0}],
privileged_groups=[{'gender': 1}])
print("Disparate Impact:", metric.disparate_impact())
print("Statistical Parity Difference:", metric.statistical_parity_difference())
# Apply fairness intervention
rw = Reweighing(unprivileged_groups=[{'gender': 0}],
privileged_groups=[{'gender': 1}])
dataset_transformed = rw.fit_transform(dataset)
# Recompute metrics after intervention
metric_transformed = BinaryLabelDatasetMetric(dataset_transformed,
unprivileged_groups=[{'gender': 0}],
privileged_groups=[{'gender': 1}])
print("Disparate Impact after intervention:", metric_transformed.disparate_impact())
print("Statistical Parity Difference after intervention:", metric_transformed.statistical_parity_difference())
This code computes fairness metrics for a binary classification problem, applies a fairness intervention (reweighing), and then recomputes the metrics to see the improvement.
Interactive Demo: Fairness Slider
Adjust the slider to see how different fairness thresholds might affect model decisions:
Fairness score: 0.50
This visualization shows how different fairness thresholds might affect the positive outcome rates for two groups. In a perfectly fair model, both groups would have equal rates.
Challenge: Deploy and Audit Your Own Model
Now it's your turn! Try to deploy a model you've created in a previous lesson and perform a fairness audit. Here are some steps:
- Choose a model you've created (e.g., the sentiment analysis model from Lesson 5)
- Deploy it using either Flask or FastAPI
- Use LIME to explain some of its predictions
- Use AI Fairness 360 to audit the model for bias
- If bias is detected, try to mitigate it and redeploy the model
This challenge will give you hands-on experience with the full lifecycle of a responsibly deployed AI model.