Building a Machine Learning Model API with Flask: A Step-by-Step Guide

Nilesh Shinde
2 min readNov 10, 2023

--

Introduction:

In today's data-driven world, the integration of machine learning models into web applications has become increasingly common. This allows developers to harness the power of predictive analytics and make their applications smarter. In this blog post, we'll explore how to create a simple Machine Learning Model API using Flask, a lightweight and versatile web framework for Python.

Prerequisites:

Before we begin, make sure you have the following installed:

1. Python:

https://www.python.org/downloads/
2. Flask: Install Flask using

pip install flask

3. Your favorite machine learning library (e.g., scikit-learn, TensorFlow, PyTorch)

Step 1: Set Up Your Project:

Create a new directory for your project and navigate to it in the terminal. Inside this directory, create a virtual environment to manage your project dependencies:

python -m venv venv

Activate the virtual environment:
- On Unix or MacOS:

source venv/bin/activate

- On Windows:

venv\Scripts\activate

Step 2: Build Your Machine Learning Model:

For this example, let's use a simple linear regression model from scikit-learn. Create a file named `model.py` and implement your model:

# model.py
from sklearn.linear_model import LinearRegression

class MLModel:
def __init__(self):
self.model = LinearRegression()

def train(self, X, y):
self.model.fit(X, y)

def predict(self, X):
return self.model.predict(X)

Step 3: Create the Flask App:

Now, let’s set up the Flask application. Create a file named app.py:

# app.py
from flask import Flask, request, jsonify
from model import MLModel

app = Flask(__name__)
model = MLModel()

@app.route('/predict', methods=['POST'])
def predict():
data = request.json['data']
prediction = model.predict([data])
return jsonify({'prediction': prediction.tolist()})

if __name__ == '__main__':
app.run(debug=True)

Step 4: Run Your Flask App:

In the terminal, run your Flask application:

python app.py

Visit in your browser to ensure your app is running.

http://127.0.0.1:5000/

Step 5: Test Your API:

You can use tools like `curl` or Postman to test your API. Send a POST request to

http://127.0.0.1:5000/predict

with JSON data containing the features needed for your model.

Congratulations! You've successfully built a Machine Learning Model API using Flask. This is a foundational example, and you can expand upon it by integrating more complex models or adding features like input validation and error handling.

Conclusion:

In this blog post, we've walked through the process of creating a simple Machine Learning Model API using Flask. This is just the beginning; you can enhance your API by incorporating authentication, deploying it to cloud platforms, and integrating it into larger applications. Flask provides a solid foundation for building powerful and scalable machine learning APIs.

--

--

Nilesh Shinde

Hi 👋 myself Nilesh Shinde. Freelance Full Stack Web Developer. Capable in MERN Stack,Next.js, System design , Scalable Application and other technologies.