What is the main purpose of using Flask in model serving?
Flask is used to create a simple web server that can receive data, run the machine learning model on it, and send back predictions as responses.
Click to reveal answer
beginner
Which HTTP method is commonly used to send data to a Flask API for prediction?
The POST method is commonly used because it allows sending data in the request body, which the model can use to make predictions.
Click to reveal answer
intermediate
In a Flask API for model serving, what is the role of the 'request' object?
The 'request' object holds the incoming data sent by the client, such as JSON or form data, which the API uses as input for the model.
Click to reveal answer
beginner
Why do we use 'jsonify' in Flask API responses?
'jsonify' converts Python data like dictionaries into JSON format, which is a standard way to send data over the web and easy for clients to understand.
Click to reveal answer
intermediate
What is a simple way to load a saved machine learning model in a Flask API?
You can load a saved model using libraries like 'pickle' or 'joblib' at the start of the Flask app, so it’s ready to make predictions when requests come in.
Click to reveal answer
Which Flask function is used to start the web server for model serving?
Aapp.launch()
Bapp.start()
Capp.execute()
Dapp.run()
✗ Incorrect
The 'app.run()' function starts the Flask web server to listen for incoming requests.
What format should input data be in when sending it to a Flask API for prediction?
AXML
BJSON
CPlain text only
DHTML
✗ Incorrect
JSON is the most common and easy-to-use format for sending structured data to APIs.
Which Python library is commonly used to save and load machine learning models for Flask APIs?
Apickle
Bmatplotlib
Cnumpy
Drequests
✗ Incorrect
'pickle' is used to serialize and deserialize Python objects like machine learning models.
What does the Flask 'route' decorator do in model serving?
ADefines the URL path that triggers a function
BStarts the Flask server
CLoads the machine learning model
DSaves the prediction results
✗ Incorrect
The 'route' decorator tells Flask which URL path should run the decorated function.
Why is it important to handle errors in a Flask API for model serving?
ATo reduce the size of the model
BTo make the server run faster
CTo send clear messages when input data is wrong or the model fails
DTo change the model’s predictions
✗ Incorrect
Error handling helps users understand what went wrong and prevents the server from crashing.
Explain the steps to create a Flask API that serves a machine learning model.
Think about how data flows from client to model and back.
You got /6 concepts.
Describe how you would test a Flask API for model serving after building it.
Testing means sending data and checking responses carefully.
You got /5 concepts.
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
Step 1: Understand Flask's role
Flask is a lightweight web framework used to build web applications and APIs.
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.
Final Answer:
To create a web API that allows clients to send data and get predictions -> Option B
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
Step 1: Recall Flask route syntax
Flask routes use @app.route decorator with a path and a list of HTTP methods.
Step 2: Identify correct method for sending data
POST method is used to send data to the server for processing, suitable for model prediction.
Final Answer:
@app.route('/predict', methods=['POST']) -> Option C
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
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].
Step 2: Calculate sum of input list
sum([1, 2, 3]) equals 6, which is stored in result and returned as JSON.
Final Answer:
{"prediction": 6} -> Option A
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:
request.json() is incorrect; Flask uses request.get_json() to parse JSON body.
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.
Final Answer:
Change request.json() to request.get_json() -> Option A
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
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.
Step 2: Prepare input accordingly
Wrapping data['features'] (a list of features) inside another list creates a 2D list suitable for prediction.
Final Answer:
input_features = [data['features']] # Wrap list inside another list -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.