DS Model Deployment

A trained model that lives inside a notebook helps nobody except its author. Deployment means placing the model where apps and people can send data and receive predictions. A deployed model might score loan forms, rank search results, or flag unusual card activity in real time.

Picture the Flow

 User or App --> sends data --> [ API ] --> [ Model ] --> prediction
      ^                                                       |
      +-------------------- response in JSON -----------------+

An API is a doorway. Other software knocks on the door with input data and receives an answer. The caller never needs to know how the model works inside.

Step 1: Save the Trained Model

import joblib
from sklearn.ensemble import RandomForestRegressor

model = RandomForestRegressor(n_estimators=100, random_state=0)
model.fit(X_train, y_train)

joblib.dump(model, "house_model.pkl")

The file stores everything the model learned. Save the preprocessing steps too, such as scalers and encoders. A pipeline object bundles preprocessing and the model into a single file, which prevents mismatches.

Step 2: Wrap the Model in an API

Flask is a small Python web framework. The code below creates one endpoint named predict.

from flask import Flask, request, jsonify
import joblib

app = Flask(__name__)
model = joblib.load("house_model.pkl")

@app.route("/predict", methods=["POST"])
def predict():
    data = request.get_json()
    features = [[data["area"], data["rooms"], data["age"]]]
    price = model.predict(features)[0]
    return jsonify({"predicted_price": round(float(price), 2)})

if __name__ == "__main__":
    app.run(port=5000)

Run the file and the server waits for requests on port 5000.

Step 3: Test the Endpoint

curl -X POST http://localhost:5000/predict \
  -H "Content-Type: application/json" \
  -d '{"area": 1200, "rooms": 3, "age": 8}'

The server replies with a small JSON message holding the predicted price. Test with normal inputs, missing fields, and extreme values.

Step 4: Validate Inputs

Real users send surprising data. A negative area, a text value in a number field, or a missing key can crash the service or produce nonsense. Check every field for presence, type, and a sensible range before calling the model. Return a clear error message with a 400 status code when checks fail.

Step 5: Package and Host

OptionDescriptionGood For
Docker containerBundles code, libraries, and settings into one portable unitAny team that needs repeatable setups
Cloud platformRents servers that scale with trafficPublic apps with changing demand
Serverless functionRuns code only when requests arriveLight, occasional workloads
Batch jobScores many records on a scheduleNightly reports with no live requests
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Step 6: Monitor After Launch

Models age. Customer habits shift, prices change, and new products appear. Data drift means the incoming data no longer resembles the training data. Concept drift means the link between inputs and outcomes has changed.

  • Track response time and error counts.
  • Log inputs and predictions, with personal details removed.
  • Compare live predictions against real outcomes when they arrive.
  • Set alerts for sudden shifts in input patterns.
  • Retrain on fresh data at a regular schedule.

Version Everything

Give each model a version number, such as house_model_v3. Record the training data date, the code version, and the test scores beside it. A clear record lets the team roll back to a previous version within minutes if the new one misbehaves.

Security and Privacy Basics

  • Require an API key or login for every request.
  • Serve traffic over HTTPS.
  • Limit request rates to block abuse.
  • Store only the data you truly need, and follow privacy laws in your region.
  • Keep secrets such as passwords out of the code.

Key Points

  • Save the model with its preprocessing, expose it through an API, and validate every input.
  • Containers and cloud hosting make deployment repeatable.
  • Monitoring, versioning, and scheduled retraining keep predictions reliable.