Bird
Raised Fist0
ML Pythonml~10 mins

Flask API for model serving in ML Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask class from the flask package.

ML Python
from flask import [1]

app = [1](__name__)
Drag options to blanks, or click blank then click option'
AFlask
BRequest
Cjsonify
Drender_template
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or jsonify instead of Flask
Using wrong class name for app creation
2fill in blank
medium

Complete the code to define a route for the API endpoint '/predict'.

ML Python
@app.route('[1]', methods=['POST'])
def predict():
    pass
Drag options to blanks, or click blank then click option'
A/predict
B/train
C/model
D/input
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET method instead of POST
Wrong endpoint path like '/train'
3fill in blank
hard

Fix the error in the code to get JSON data from the POST request.

ML Python
data = request.[1]
Drag options to blanks, or click blank then click option'
Ajsonify
Bjson
Cget_json()
Dget_json
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.json without parentheses
Using request.jsonify which is incorrect
4fill in blank
hard

Fill both blanks to load a saved model and make a prediction.

ML Python
import joblib

model = joblib.[1]('model.pkl')
prediction = model.[2]([input_data])
Drag options to blanks, or click blank then click option'
Aload
Bpredict
Cdump
Dfit
Attempts:
3 left
💡 Hint
Common Mistakes
Using joblib.dump instead of load
Using model.fit instead of predict
5fill in blank
hard

Fill both blanks to return the prediction as JSON with status code 200.

ML Python
from flask import jsonify

return jsonify([1]: prediction[0]}), [2]
Drag options to blanks, or click blank then click option'
A'result'
B200
C201
D'output'
Attempts:
3 left
💡 Hint
Common Mistakes
Using status code 201 which means created
Using wrong JSON key names

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