Bird
Raised Fist0
ML Pythonml~7 mins

Flask API for model serving in ML Python

Choose your learning style10 modes available
Introduction

We use a Flask API to let other programs ask our machine learning model for predictions easily. It acts like a friendly waiter taking requests and giving answers.

You want to share your trained model so others can use it over the internet.
You need a simple way to get predictions from your model without running code locally.
You want to build a web or mobile app that uses your machine learning model.
You want to test your model with real data from different sources.
You want to automate predictions by sending data to your model programmatically.
Syntax
ML Python
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    # process data and get prediction
    prediction = model.predict(data['input'])
    return jsonify({'prediction': prediction.tolist()})

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

Use @app.route to define the URL path and HTTP method.

Use request.get_json() to get input data sent as JSON.

Examples
A simple Flask app that returns a greeting when you visit /hello.
ML Python
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hello, world!'

if __name__ == '__main__':
    app.run()
This example doubles a number sent in JSON and returns the result.
ML Python
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    number = data['number']
    result = number * 2
    return jsonify({'result': result})

if __name__ == '__main__':
    app.run()
Sample Model

This Flask app trains a model on iris flower data. It listens for POST requests at /predict with flower features and returns the predicted species name.

ML Python
from flask import Flask, request, jsonify
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import numpy as np

# Train a simple model
iris = load_iris()
X, y = iris.data, iris.target
model = RandomForestClassifier()
model.fit(X, y)

app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    features = np.array(data['features']).reshape(1, -1)
    prediction = model.predict(features)[0]
    species = iris.target_names[prediction]
    return jsonify({'prediction': species})

if __name__ == '__main__':
    app.run(debug=False)
OutputSuccess
Important Notes

Flask runs a simple web server that listens for requests on your computer.

Use JSON format to send and receive data easily between client and server.

For production, use a more robust server like Gunicorn and secure your API.

Summary

Flask API lets you share your ML model so others can get predictions over the web.

Define routes with @app.route and handle data with JSON.

Test your API by sending POST requests with input data and reading the JSON response.

Practice

(1/5)
1. What is the main purpose of using Flask in model serving for machine learning?
easy
A. To train machine learning models faster
B. To create a web API that allows clients to send data and get predictions
C. To visualize data in charts and graphs
D. To store large datasets efficiently

Solution

  1. Step 1: Understand Flask's role

    Flask is a lightweight web framework used to build web applications and APIs.
  2. Step 2: Connect Flask to model serving

    In machine learning, Flask is used to create an API endpoint where clients can send input data and receive model predictions.
  3. Final Answer:

    To create a web API that allows clients to send data and get predictions -> Option B
  4. Quick Check:

    Flask for API = Create prediction endpoint [OK]
Hint: Flask makes your model accessible via web requests [OK]
Common Mistakes:
  • Thinking Flask trains models
  • Confusing Flask with data storage tools
  • Assuming Flask is for data visualization
2. Which of the following is the correct way to define a Flask route for a POST request to serve a model prediction?
easy
A. @app.route('/predict', methods=['GET'])
B. @app.route('/predict')
C. @app.route('/predict', methods=['POST'])
D. @app.route('/predict', method='POST')

Solution

  1. Step 1: Recall Flask route syntax

    Flask routes use @app.route decorator with a path and a list of HTTP methods.
  2. Step 2: Identify correct method for sending data

    POST method is used to send data to the server for processing, suitable for model prediction.
  3. Final Answer:

    @app.route('/predict', methods=['POST']) -> Option C
  4. Quick Check:

    POST method for data sending = Correct route [OK]
Hint: Use methods=['POST'] to accept data in Flask route [OK]
Common Mistakes:
  • Using GET instead of POST for sending data
  • Missing 'methods' keyword or using wrong syntax
  • Assuming default route accepts POST
3. Given the Flask code snippet below, what will be the output when a POST request with JSON {"input": [1, 2, 3]} is sent?
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    result = sum(data['input'])
    return jsonify({'prediction': result})
medium
A. {"prediction": 6}
B. {"prediction": [1, 2, 3]}
C. {"prediction": 123}
D. Error: KeyError

Solution

  1. Step 1: Extract input data from JSON

    The code uses request.get_json() to get {'input': [1, 2, 3]} and accesses data['input'] which is [1, 2, 3].
  2. Step 2: Calculate sum of input list

    sum([1, 2, 3]) equals 6, which is stored in result and returned as JSON.
  3. Final Answer:

    {"prediction": 6} -> Option A
  4. Quick Check:

    Sum of [1,2,3] = 6 [OK]
Hint: Sum list input to get prediction value [OK]
Common Mistakes:
  • Returning the list instead of sum
  • Concatenating numbers as string
  • Missing JSON key causing KeyError
4. Identify the error in the following Flask API code for model serving and select the fix:
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.json()
    prediction = model.predict(data['input'])
    return jsonify({'prediction': prediction.tolist()})
medium
A. Change request.json() to request.get_json()
B. Change methods=['POST'] to methods=['GET']
C. Remove .tolist() from prediction
D. Add @app.run() after function

Solution

  1. Step 1: Check how JSON data is accessed

    request.json() is incorrect; Flask uses request.get_json() to parse JSON body.
  2. Step 2: Confirm other parts are correct

    methods=['POST'] is correct for sending data; .tolist() is valid if prediction is numpy array; @app.run() is not a decorator.
  3. Final Answer:

    Change request.json() to request.get_json() -> Option A
  4. Quick Check:

    Use request.get_json() to parse JSON [OK]
Hint: Use request.get_json() to read JSON body [OK]
Common Mistakes:
  • Using request.json() instead of get_json()
  • Confusing HTTP methods for data sending
  • Misusing Flask decorators
5. You want to serve a scikit-learn model using Flask. The model expects input features as a 2D list. Which approach correctly prepares the input inside the Flask route before prediction?
@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    # Prepare input for model
    input_features = ???
    prediction = model.predict(input_features)
    return jsonify({'prediction': prediction.tolist()})
hard
A. input_features = data['features'].tolist() # Convert to list
B. input_features = data['features'] # Use list directly
C. input_features = data['features'][0] # Use first element only
D. input_features = [data['features']] # Wrap list inside another list

Solution

  1. Step 1: Understand model input shape

    Scikit-learn models expect 2D arrays: shape (n_samples, n_features). A single sample must be wrapped in an outer list.
  2. Step 2: Prepare input accordingly

    Wrapping data['features'] (a list of features) inside another list creates a 2D list suitable for prediction.
  3. Final Answer:

    input_features = [data['features']] # Wrap list inside another list -> Option D
  4. Quick Check:

    Wrap features in list for 2D input [OK]
Hint: Wrap single feature list in another list for 2D input [OK]
Common Mistakes:
  • Passing 1D list directly causing shape errors
  • Using only first element losing features
  • Calling .tolist() on a list (already a list)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard