Integration
Python Client Integration
python
# mlflow_client.py
import mlflow
import mlflow.sklearn
from mlflow.tracking import MlflowClient
# Configure MLflow
mlflow.set_tracking_uri("http://mlflow.example.com")
mlflow.set_experiment("production-models")
# Initialize client
client = MlflowClient()
# Example: Log experiment
with mlflow.start_run():
# Log parameters
mlflow.log_param("learning_rate", 0.01)
mlflow.log_param("max_depth", 5)
# Log metrics
mlflow.log_metric("accuracy", 0.95)
mlflow.log_metric("precision", 0.92)
# Log model
mlflow.sklearn.log_model(
sk_model=model,
artifact_path="model",
registered_model_name="production-classifier"
)
REST API Integration
bash
# Create experiment
curl -X POST "http://mlflow.example.com/api/2.0/mlflow/experiments/create" \
-H "Content-Type: application/json" \
-d '{
"name": "api-experiment",
"artifact_location": "/opt/mlflow/artifacts/api-experiment"
}'
# Start run
curl -X POST "http://mlflow.example.com/api/2.0/mlflow/runs/create" \
-H "Content-Type: application/json" \
-d '{
"experiment_id": "1",
"start_time": 1640995200000,
"tags": [{"key": "environment", "value": "production"}]
}'
# Log metrics
curl -X POST "http://mlflow.example.com/api/2.0/mlflow/runs/log-metric" \
-H "Content-Type: application/json" \
-d '{
"run_id": "run-123",
"key": "accuracy",
"value": 0.95,
"timestamp": 1640995200000
}'
CI/CD Integration
yaml
# .github/workflows/mlflow-deploy.yml
name: MLflow Model Deployment
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
pip install mlflow==3.0.1
pip install -r requirements.txt
- name: Train and log model
env:
MLFLOW_TRACKING_URI: ${{ secrets.MLFLOW_TRACKING_URI }}
MLFLOW_TRACKING_USERNAME: ${{ secrets.MLFLOW_USERNAME }}
MLFLOW_TRACKING_PASSWORD: ${{ secrets.MLFLOW_PASSWORD }}
run: |
python train_model.py
python deploy_model.py
Kubernetes Integration
yaml
# mlflow-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mlflow-server
spec:
replicas: 3
selector:
matchLabels:
app: mlflow-server
template:
metadata:
labels:
app: mlflow-server
spec:
containers:
- name: mlflow
image: mlflow:3.0.1
ports:
- containerPort: 5000
env:
- name: MLFLOW_BACKEND_STORE_URI
valueFrom:
secretKeyRef:
name: mlflow-secrets
key: database-uri
- name: MLFLOW_DEFAULT_ARTIFACT_ROOT
value: "s3://mlflow-artifacts"
resources:
requests:
memory: "1Gi"
cpu: "500m"
limits:
memory: "2Gi"
cpu: "1000m"
Related content