In the previous chapter, we saw how to save our trained machine learning model to a file using techniques like pickle
or joblib
. Now that we're building a Flask web application to serve predictions, we need to bring that saved model back into memory so our application can use it. This process is often called deserialization or simply "loading" the model.
The core idea is straightforward: your Flask application script needs to execute the appropriate function (pickle.load
or joblib.load
) to read the model file and reconstruct the model object in memory.
Where should this loading happen? For many simple prediction services, loading the model once when the Flask application starts is the most efficient approach. This avoids the overhead of reading the file and reconstructing the model object every time a prediction request comes in. We can achieve this by loading the model into a global variable within our Flask script.
Let's assume you saved your model using joblib
into a file named model.joblib
(as covered in Chapter 2). Here's how you can load it within your Flask application script (app.py
):
# app.py
from flask import Flask
import joblib
import os # Import the os module
# Create the Flask application instance
app = Flask(__name__)
# --- Model Loading ---
# Define the path to the model file
MODEL_FILE_PATH = 'model.joblib' # Make sure this path is correct
# Check if the model file exists before attempting to load
if os.path.exists(MODEL_FILE_PATH):
# Load the trained model from the file
try:
model = joblib.load(MODEL_FILE_PATH)
print(f"* Model loaded successfully from {MODEL_FILE_PATH}")
except Exception as e:
print(f"* Error loading model: {e}")
# Handle the error appropriately - maybe exit or use a default behavior
model = None # Set model to None if loading failed
else:
print(f"* Model file not found at {MODEL_FILE_PATH}")
model = None # Set model to None if file doesn't exist
# --- Define Routes (Endpoints) ---
# We will define our prediction endpoint in the next section
@app.route('/')
def home():
# Simple route to check if the app is running
return "Flask app is running!"
# Add a check for the model before running the app
if model is None:
print("! Warning: Model could not be loaded. Prediction endpoint may not function.")
# --- Run the Application ---
if __name__ == '__main__':
# Run the Flask development server
# host='0.0.0.0' makes it accessible from other devices on the network
# debug=True enables auto-reloading and detailed error pages (use for development only)
app.run(host='0.0.0.0', port=5000, debug=True)
Flask
and joblib
. We also import the os
module to help check if the model file actually exists before we try to load it.MODEL_FILE_PATH
) where our saved model resides. Ensure this path is correct relative to where you run the Flask script.os.path.exists(MODEL_FILE_PATH)
, we verify that the file is present. This prevents errors if the file is missing.try...except
block, model = joblib.load(MODEL_FILE_PATH)
reads the file and reconstructs the model object, assigning it to the model
variable. Placing this before defining any routes makes model
a global variable within the application context.try...except
block provides basic error handling. If joblib.load
fails (e.g., corrupted file, incorrect format), an error message is printed, and model
is set to None
. We also handle the case where the file wasn't found. Proper error handling is important in production systems.model
is loaded outside of any specific function (like a route handler), it will be accessible to all route functions defined later in the script.python app.py
).flask
, joblib
(or pickle
), and the libraries required by the model itself (e.g., scikit-learn
, numpy
). Mismatched library versions between the training environment and the deployment environment are a common source of errors. This reinforces the importance of managing dependencies, as discussed in Chapter 2.With the model loaded into our Flask application's memory, we are now ready to create the specific endpoint that will receive input data, use the model to make predictions, and return the results.
© 2025 ApX Machine Learning