Guide: load a pre-trained ML model in Flask API step-by-step. Code examples & best practices for seamless integration.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// Import necessary modules
from flask import Flask, request, jsonify // For route handling and JSON responses
import joblib // For loading your serialized model file
import numpy as np // For numerical data manipulation if needed
model.pkl which may have been trained using scikit-learn or another library.
// Load model during app initialization
app = Flask(**name**)
model = joblib.load('model.pkl') // The pre-trained ML model is loaded once when the API starts
/predict, that accepts POST requests with input data for predictions.
@app.route('/predict', methods=['POST'])
def predict():
// Extract data from POST request
input_data = request.get_json(force=True)
// Example: Assume input\_data contains a key "features" with a list of numbers
// Preprocess your data; here we convert it to a numpy array with proper shape
features = np.array(input\_data['features']).reshape(1, -1)
// Use the pre-trained model to predict the outcome
prediction = model.predict(features)
// Return the prediction in JSON format
return jsonify({'prediction': prediction.tolist()})
@app.route('/predict', methods=['POST'])
def predict():
try:
// Attempt to extract and process the input data
input_data = request.get_json(force=True)
features = np.array(input\_data['features']).reshape(1, -1)
// Make prediction with the model
prediction = model.predict(features)
// Return the successful prediction result
return jsonify({'prediction': prediction.tolist()})
except Exception as e:
// Log the error (this could include more detailed logging)
print("Error during prediction:", e)
// Respond with an error status and message
return jsonify({'error': 'Invalid input or server error occurred.'}), 400
// Example: Running with Gunicorn in production might resemble:
// gunicorn --workers=4 app:app
// This ensures that four worker processes handle the predictions concurrently.
/predict endpoint, verifying JSON data format and response accuracy.
// Example curl command (assuming local host on default port):
// curl -H "Content-Type: application/json" -X POST -d '{"features": [1, 2, 3, 4]}' http://localhost:5000/predict
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â