How to Deploy Machine Learning Models with Flask in 2026
Machine learning has become an integral part of many modern applications, enabling powerful capabilities like personalized recommendations, fraud detection, image recognition, and much more. However, building an effective ML model is only part of the battle. In order to provide value, that model needs to be deployed to a production environment where it can be accessed and used by other software systems.
This is where Flask comes in. Flask is a popular and lightweight web framework for Python that makes it straightforward to develop and deploy web applications. When it comes to deploying machine learning models, Flask provides an excellent option thanks to its simplicity, flexibility, and ability to easily integrate with the Python data science and ML ecosystem.
In this guide, we‘ll walk through the process of deploying a machine learning model using Flask. We‘ll cover everything from setting up your development environment and training a model to creating a Flask app and deploying it to a production environment. By the end, you‘ll have a solid understanding of how to get your ML models out of a Jupyter notebook and into the hands of users.
Overview of the Model Deployment Process
Before diving into the technical details, let‘s take a high-level look at the typical workflow for deploying a machine learning model:
-
Develop and train an ML model, typically using a framework like scikit-learn, TensorFlow, PyTorch, etc.
-
Save the trained model to disk in a serialized format (e.g. pickle, joblib, HDF5, SavedModel).
-
Create a web service (e.g. using Flask) that loads the serialized model.
-
Define an endpoint in the web service that accepts input data, preprocesses it, gets predictions from the loaded model, and returns the results.
-
Deploy the web service to a server or hosting platform where it can be accessed by other systems over a network.
-
Integrate the deployed model into an application by making requests to the web service endpoint.
The rest of this guide will focus on steps 3-5, showing how to use Flask to create a web service for your model and deploy it to a production environment.
What is Flask?
Flask is a micro web framework written in Python. It is designed to be simple and lightweight, making it easy to develop web applications quickly. Flask is classified as a microframework because it does not require particular tools or libraries, giving developers a lot of flexibility in choosing components.
Some key features of Flask include:
- Built-in development server and debugger
- Integrated support for unit testing
- RESTful request dispatching
- Templating support using Jinja2
- Secure cookies (client-side sessions)
- WSGI 1.0 compliant
- Unicode-based
- Extensive documentation
For deploying machine learning models, the most relevant features are the built-in server, RESTful request handling, and WSGI compliance which allows deploying Flask apps to many different web servers and hosting platforms. Flask makes it straightforward to create a web service exposing endpoints that accept input data, generate predictions from a model, and return the results.
Setting Up Your Environment
To get started, you‘ll need to set up your development environment with Python and Flask. Let‘s walk through the steps:
-
Install Python: Flask requires Python 3.7 or higher. You can download the latest version of Python from the official website: https://www.python.org/downloads/
-
Create a virtual environment: It‘s a good practice to create a separate virtual environment for each Python project to avoid conflicts with other projects. You can create a virtual environment using the venv module:
python3 -m venv myenvThis creates a new virtual environment in a folder named "myenv".
-
Activate the virtual environment:
source myenv/bin/activateYour shell prompt should now indicate that the virtual environment is active.
-
Install Flask:
pip install flaskThis installs Flask and its dependencies in the active virtual environment.
-
Install any additional dependencies needed for your ML model, such as numpy, pandas, scikit-learn, tensorflow, etc. For example:
pip install numpy pandas scikit-learn
With the environment set up, you‘re ready to start building the application.
Training and Saving a Model
Before we create our Flask app, we need to have a trained machine learning model that we can use for inference. For demonstration purposes, we‘ll create a simple model that predicts species of iris flowers based on measurements of the flowers.
Here‘s the code to train and save a logistic regression model using the classic iris dataset:
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import joblib
# Load the iris dataset
iris = load_iris()
X, y = iris.data, iris.target
# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train a logistic regression model
model = LogisticRegression()
model.fit(X_train, y_train)
# Save model to disk
joblib.dump(model, ‘iris_model.pkl‘)
This trains a simple logistic regression model on the iris dataset and saves the model to a file named "iris_model.pkl" using joblib. We‘ll load this model file in our Flask app.
In practice, you would train your ML model separately using a framework like scikit-learn, TensorFlow, PyTorch, etc. The specific process for training and saving the model will depend on which library you use. The key point is to save your trained model object to disk in a serialized format so that it can be loaded into your Flask app.
Creating a Flask App
Now we‘re ready to create our Flask app and define the endpoint for serving predictions from our model. Here‘s the code:
from flask import Flask, request, jsonify
import joblib
import numpy as np
app = Flask(__name__)
# Load the trained model
model = joblib.load(‘iris_model.pkl‘)
@app.route(‘/predict‘, methods=[‘POST‘])
def predict():
# Get the input data from the request
data = request.get_json(force=True)
# Convert input data to a numpy array
input_data = np.array(data[‘input‘])
# Use the loaded model to make predictions
predictions = model.predict(input_data)
# Convert predictions to a list and return as JSON
return jsonify({‘predictions‘: predictions.tolist()})
if __name__ == ‘__main__‘:
app.run(port=5000, debug=True)
Let‘s break this down:
-
We import the necessary libraries: Flask for creating the web app, joblib for loading the saved model, and numpy for handling input data.
-
We create a Flask app instance.
-
We load the saved model using joblib. This makes the model available for use in our app.
-
We define a route for our app at the
/predictendpoint that accepts POST requests. This is where we‘ll receive input data and return predictions. -
In the
predictfunction, we get the input data from the request. Theget_jsonfunction extracts JSON data from the request body. -
We convert the input data to a numpy array so it can be passed to the model.
-
We use the loaded model to generate predictions on the input data.
-
We convert the predictions to a list and return them as a JSON response.
-
Finally, we run the app if the script is run directly (not imported as a module). Setting
debug=Trueenables debug mode which provides detailed error messages.
With this code, we now have a functioning Flask app that exposes an endpoint for generating predictions from our trained model!
Testing the App
Before deploying our app, we should test it to make sure everything is working as expected. We can do this by running the Flask app locally and sending requests to the /predict endpoint.
First, make sure your app code is saved in a file, e.g. app.py, and your trained model file is in the same directory. Then, run the Flask app:
python app.py
You should see output indicating that the app is running locally on port 5000.
Now, open a new terminal window and use curl to send a POST request to the app:
curl -X POST -H "Content-Type: application/json" \
-d ‘{"input": [[5.1, 3.5, 1.4, 0.2], [6.2, 3.4, 5.4, 2.3]]}‘ \
http://localhost:5000/predict
This sends a JSON payload with two sets of iris flower measurements to the /predict endpoint. The app should return predictions in the response:
{
"predictions": [0, 2]
}
The predictions are 0 and 2, corresponding to iris setosa and iris virginica, respectively. If you see this response, congrats! Your Flask app is successfully generating predictions from the model.
Deploying to Production
Once your Flask app is working locally, you‘ll likely want to deploy it to a server or hosting platform so it can be accessed by other systems over a network. There are many options for deploying Flask apps, including:
-
Deploying to a cloud platform like AWS, Azure, or Google Cloud. Each platform has its own tools and services for deploying web apps.
-
Deploying to a Platform-as-a-Service (PaaS) like Heroku or PythonAnywhere. These handle most of the deployment process for you.
-
Deploying to a dedicated server or virtual machine and using a production-grade web server like Gunicorn or uWSGI to run your Flask app.
The specific steps for deployment will depend on which option you choose. However, the general process typically involves:
-
Ensuring your app code and dependencies are packaged and ready for deployment. This may involve creating a
requirements.txtfile listing your Python dependencies. -
Choosing a hosting service and creating an account/project.
-
Configuring your hosting environment, such as provisioning a virtual machine or configuring a PaaS settings.
-
Uploading your application code and model files to the hosting environment.
-
Starting your application and configuring it to run automatically.
-
Configuring any necessary networking or security settings so your app can be accessed externally.
Many hosting services provide detailed documentation and guides for deploying Flask applications. Here are a few examples:
- Deploying a Flask app on AWS Elastic Beanstalk
- Deploying a Flask app on Google Cloud Run
- Deploying a Flask app on Heroku
- Deploying a Flask app on PythonAnywhere
Best Practices and Tips
Here are a few best practices and tips to keep in mind when deploying machine learning models with Flask:
-
Use version control (e.g. Git) to manage your application code and model files. This makes it easier to track changes and deploy updates.
-
Separate your model training code from your Flask application code. Train and save your models separately, then load the saved models in your Flask app. This makes your application more modular and easier to update.
-
Use a production-grade web server like Gunicorn or uWSGI to run your Flask app in production. The built-in Flask server is not suitable for production use.
-
Consider using a workflow management tool like MLflow or Kubeflow to automate and manage your model training and deployment pipelines.
-
Implement logging in your application to help with debugging and monitoring. Flask provides a built-in logger that integrates with Python‘s logging module.
-
Set up monitoring and alerts for your deployed model to ensure it is functioning as expected and to detect any issues or anomalies.
-
Have a plan for rolling back to a previous version of your model if needed.
-
Regularly update your application dependencies to ensure you have the latest security patches and bug fixes.
Alternatives to Flask
While Flask is a popular choice for deploying machine learning models, it‘s not the only option. Here are a few alternatives to consider:
-
Django: A high-level Python web framework that includes more built-in features than Flask, such as an ORM and an admin interface.
-
FastAPI: A modern, fast Python web framework for building APIs. It has built-in support for async programming and automatic API documentation.
-
Streamlit: A framework for building data science and machine learning web apps in Python. It allows you to create interactive UIs with just a few lines of code.
-
TensorFlow Serving: A flexible, high-performance serving system for machine learning models, designed for production environments. It‘s particularly well-suited for TensorFlow models but can be extended to other frameworks.
-
Seldon Core: An open-source platform for deploying machine learning models on Kubernetes. It supports multiple data science frameworks and languages.
The best choice for you will depend on your specific needs and preferences. Flask is a great option if you want a lightweight, flexible framework that‘s easy to get started with.
Challenges and Considerations
Deploying machine learning models to production comes with its own set of challenges and considerations. Here are a few key points to keep in mind:
-
Model performance: Ensure your model performs well on real-world data and can handle edge cases and unexpected inputs. Regularly monitor your model‘s performance and have a plan for retraining and updating it as needed.
-
Scalability: As usage of your model grows, you‘ll need to ensure your deployment can handle the increased load. This may involve scaling up your server resources or implementing load balancing.
-
Security: Protect your model from unauthorized access and ensure the integrity of your input and output data. This may involve implementing authentication, input validation, and data encryption.
-
Interpretability and explainability: In some domains, it‘s important to be able to explain how your model makes its predictions. Consider using interpretable models or implementing techniques for explaining model predictions.
-
Data privacy: Ensure you are handling user data in compliance with relevant regulations and protecting user privacy.
-
Model bias and fairness: Be aware of potential biases in your model and take steps to ensure your model is making fair and unbiased predictions.
Deploying machine learning models is a complex process with many considerations. It‘s important to carefully plan and architect your deployment to ensure it is reliable, scalable, and secure.
Conclusion
In this guide, we‘ve walked through the process of deploying a machine learning model using Flask. We‘ve covered how to set up your development environment, train and save a model, create a Flask app to serve predictions, and deploy the app to a production environment. We‘ve also discussed some best practices, alternatives to Flask, and key challenges to consider when deploying machine learning models.
Deploying ML models is a critical step in the machine learning lifecycle and an essential skill for data scientists and ML engineers. With the tools and techniques covered in this guide, you should be well-equipped to start deploying your own models and putting them into production.
Remember, the specific steps and best practices for deployment will depend on your unique situation and requirements. Always carefully consider the needs of your project and organization when planning and implementing your deployment strategy.
Happy deploying!