Skip to content

Lesson 5 of 5

Put It Together: Train, Serve, and Check in One Workflow

Combine the script, registry, service, and monitoring into a single local workflow and test it with a batch of new data to trigger a retraining alert.

9 minMLOps

After this lesson, you will be able to run a complete local MLOps workflow: train a model, register it, serve it, log predictions, and check for drift to decide whether to retrain.

The workflow

You have built the pieces in earlier lessons: a training script that logs and registers a model, a Flask service that serves predictions and logs them, and a drift check script. Now you will run them in sequence to see how they work together. The workflow is: train and register a new model version, start the service, send a batch of new data, stop the service, run the drift check, and then decide whether to retrain.

You will use the same files and names from earlier lessons: train.py, serve.py, prediction_log.csv, and check_drift.py. If you have not completed the earlier lessons, go back and create those files first. This lesson adds one new script, workflow.py, that runs the whole sequence with a single command.

Before you start, make sure no serve.py from an earlier lesson is still running. If it is, stop it by pressing Ctrl+C in its terminal. Otherwise the service started by workflow.py will not be able to use port 5000, or requests may go to the old service.

Run the workflow manually

First run the training script from lesson 2. It trains a random forest on the iris dataset, logs the run to MLflow, and registers the model under the name iris_classifier. Each run creates a new version. Open a terminal and run:

python train.py

The script prints the accuracy and registers the model. You can check the MLflow UI by running mlflow ui --port 5001 in another terminal and opening http://localhost:5001, but you do not need the UI for this workflow.

Next clear the old prediction log so the drift check sees only the new batch. You can delete prediction_log.csv if it exists, or run this command to create an empty file:

python -c "open('prediction_log.csv', 'w').close()"

Then start the prediction service from lesson 4. It loads the latest version of iris_classifier and serves predictions at http://localhost:5000/predict. It also logs each request to prediction_log.csv. Start the service in a terminal:

python serve.py

Keep this terminal open. The service runs until you stop it with Ctrl+C.

You need new data that the model has not seen. Create a small batch of iris-like samples. Create a new file called send_batch.py with this code:

import requests
import json

# New data: 5 samples with feature values slightly different from training data
new_samples = [
    [5.0, 3.6, 1.4, 0.2],
    [6.7, 3.0, 5.0, 1.7],
    [5.9, 3.0, 4.2, 1.5],
    [4.9, 3.1, 1.5, 0.1],
    [6.3, 2.8, 5.1, 1.5]
]

url = "http://localhost:5000/predict"
for sample in new_samples:
    response = requests.post(url, json={"features": [sample]})
    print(f"Sent {sample}, got {response.json()}")

This script uses the requests library to send HTTP requests. If you do not have it installed, run pip install requests. The samples are similar to iris flowers but with small variations. Run the script in a new terminal while the service is running:

python send_batch.py

The service logs each sample and its prediction to prediction_log.csv. The script prints the response for each request.

Stop the service by pressing Ctrl+C in its terminal. Then run the drift check script from lesson 4. If you modified it in the exercise, use your version. The script compares the statistics of the logged requests to the training data and warns if any feature mean differs by more than two training standard deviations.

python check_drift.py

The script prints the training statistics and the logged request statistics. Look at the output. If any feature mean in the logged data is more than two standard deviations away from the training mean, the script prints a warning. In this example, the samples are close to the training data, so you may see no warnings. That means the model is still seeing data similar to what it was trained on, and retraining is not urgent.

If you see warnings, it means the new data is different from the training data. The model may still perform well, but you should consider retraining with the new data. To simulate drift, edit send_batch.py to send ten samples where six have all features set to 20 and the other four are normal iris-like samples. Run the service again, send the batch, stop the service, and run check_drift.py. You should see warnings for all four features because six extreme samples shift the mean far outside the training range.

Automate the workflow with a script

You can run the whole sequence with one script. Create a new file called workflow.py with this code:

import subprocess
import time
import requests

def run_command(command):
    return subprocess.Popen(command.split())

def stop_process(process):
    process.terminate()
    process.wait()

if __name__ == "__main__":
    # Clear any previous request logs so the drift check sees only this run
    open("prediction_log.csv", "w").close()

    # Step 1: Train and register
    print("Training and registering model...")
    train_process = run_command("python train.py")
    train_process.wait()

    # Step 2: Start service
    print("Starting service...")
    serve_process = run_command("python serve.py")
    time.sleep(5)  # Wait for service to start

    # Step 3: Send batch
    print("Sending batch...")
    new_samples = [
        [5.0, 3.6, 1.4, 0.2],
        [6.7, 3.0, 5.0, 1.7],
        [5.9, 3.0, 4.2, 1.5],
        [4.9, 3.1, 1.5, 0.1],
        [6.3, 2.8, 5.1, 1.5]
    ]
    url = "http://localhost:5000/predict"
    for sample in new_samples:
        response = requests.post(url, json={"features": [sample]})
        print(f"Sent {sample}, got {response.json()}")

    # Step 4: Stop service
    print("Stopping service...")
    stop_process(serve_process)

    # Step 5: Run drift check
    print("Running drift check...")
    drift_process = run_command("python check_drift.py")
    drift_process.wait()

    print("Workflow complete.")

This script runs each step in order. It clears prediction_log.csv before sending the batch so old requests cannot hide drift. It uses subprocess to run the training and drift check scripts, and it starts the service as a background process. It waits 5 seconds for the service to start, sends the batch, stops the service, and runs the drift check. The stop_process function calls terminate() on the service process. This works on Windows and Unix-like systems, and it stops the Flask server so port 5000 is free for the next run. Run it with:

python workflow.py

The output shows the training accuracy, the predictions for each sample, and the drift check results. If the drift check prints warnings, you know it is time to retrain.

What you have built

You now have a complete local MLOps workflow. You can train a model, register it, serve it through an API, log predictions, and check for data drift. This is a simplified version of what production systems do, but the principles are the same. You can extend it by adding more logging, automated retraining triggers, or a real database for logs.

Key takeaways

  • A complete MLOps workflow includes training, registration, serving, logging, and monitoring.
  • Stop any old prediction service before starting a new one on the same port.
  • Clear the prediction log before each drift check so old requests do not hide new drift.
  • Drift checking compares new input data to training data to decide if retraining is needed.
  • A new script, workflow.py, runs the whole sequence with one command.

Try it

Modify workflow.py to send a batch of 10 samples: at least 6 samples with all feature values set to 20, and the remaining samples with normal iris-like values. Before running it, stop any old serve.py on port 5000. Run the workflow. A good result is that check_drift.py prints warnings for all four features, because the extreme samples shift the mean far outside the training range. If any feature does not warn, increase the number of extreme samples or their values until all four warnings appear.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.

    Put It Together: Train, Serve, and Check in One Workflow | Track, Serve, and Monitor a Model After the Notebook | Rolebound