Bird
Raised Fist0
ML Pythonml~12 mins

Flask API for model serving in ML Python - Model Pipeline Trace

Choose your learning style10 modes available
Model Pipeline - Flask API for model serving

This pipeline shows how a trained machine learning model is served using a Flask API. The API receives input data, processes it, uses the model to predict, and returns the prediction as a response.

Data Flow - 4 Stages
1Input Data Received
1 row x 4 features→Receive JSON data from API request→1 row x 4 features
{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}
↓
2Data Preprocessing
1 row x 4 features→Convert JSON to numpy array and scale features→1 row x 4 features
[[0.22, -0.42, 0.11, -0.56]]
↓
3Model Prediction
1 row x 4 features→Feed preprocessed data into trained model→1 row x 3 classes
[[0.95, 0.03, 0.02]]
↓
4Output Response
1 row x 3 classes→Convert prediction probabilities to class label and send JSON response→JSON response
{"predicted_class": "setosa"}
Training Trace - Epoch by Epoch

Loss
0.7 |****
0.6 |*** 
0.5 |**  
0.4 |**  
0.3 |*   
0.2 |*   
0.1 |    
    +------------
     1 2 3 4 5 Epochs
EpochLoss ↓Accuracy ↑Observation
10.650.70Model starts learning with moderate accuracy.
20.450.85Loss decreases and accuracy improves significantly.
30.300.92Model converges with high accuracy.
40.250.95Further fine-tuning improves performance.
50.200.97Training stabilizes with low loss and high accuracy.
Prediction Trace - 4 Layers
Layer 1: API Input Parsing
Layer 2: Feature Scaling
Layer 3: Model Prediction
Layer 4: Response Formatting
Model Quiz - 3 Questions
Test your understanding
What shape does the input data have when the API first receives it?
A1 row x 4 features
B100 rows x 4 features
C1 row x 3 classes
D4 rows x 1 feature
Key Insight
Serving a trained model with Flask involves receiving input data, preprocessing it exactly as during training, predicting with the model, and returning clear results. This pipeline ensures consistent and reliable predictions in real-time applications.

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