This tutorial aims to guide you on how to deploy a machine learning model using Flask, a micro web framework written in Python. By the end of this tutorial, you will have a machine learning model running on a Flask server that can be accessed via HTTP requests.
You will learn the following:
Before starting this tutorial, you should have:
First, install Flask using pip:
pip install Flask
In a new Python file, import Flask and create an app instance:
from flask import Flask
app = Flask(__name__)
Assuming you have a trained model saved as model.pkl
, you can load it using joblib:
from joblib import load
model = load('model.pkl')
Next, let's create an endpoint that uses the model to predict. We'll use the @app.route
decorator to specify the URL and request
to get the data sent to the API:
from flask import request
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
prediction = model.predict(data)
return {'prediction': prediction.tolist()}
Finally, run the Flask app:
if __name__ == '__main__':
app.run(debug=True)
Here is the full code of our Flask app:
from flask import Flask, request
from joblib import load
app = Flask(__name__)
model = load('model.pkl')
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
prediction = model.predict(data)
return {'prediction': prediction.tolist()}
if __name__ == '__main__':
app.run(debug=True)
In this tutorial, we've covered:
Next, you may want to learn how to deploy your Flask app to a server so it can be accessed from anywhere. Check out the Flask documentation for more information.
Create a Flask app that loads a different machine learning model and creates an endpoint for predicting.
Modify the /predict
endpoint to accept GET requests and get data from the query string.
Add error handling to the /predict
endpoint to return a useful message if the prediction fails.
Remember, the key to learning programming is practice!