0
0
ML Pythonml~20 mins

Flask API for model serving in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Model Serving Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Flask API response code?
Consider a Flask API that loads a trained model and predicts the class for input data. What will be the JSON response when sending input data [5.1, 3.5, 1.4, 0.2] to this endpoint?
ML Python
from flask import Flask, request, jsonify
import numpy as np
import pickle

app = Flask(__name__)

# Load model
with open('iris_model.pkl', 'rb') as f:
    model = pickle.load(f)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.json['data']
    arr = np.array(data).reshape(1, -1)
    pred = model.predict(arr)
    return jsonify({'prediction': int(pred[0])})
A{"prediction": 0}
B{"prediction": "setosa"}
C{"prediction": 1}
D{"error": "Invalid input"}
Attempts:
2 left
💡 Hint
The model is trained on Iris dataset where class 0 corresponds to 'setosa'. The input matches typical setosa features.
Model Choice
intermediate
2:00remaining
Which model type is best suited for this Flask API serving handwritten digit recognition?
You want to serve a model via Flask API that recognizes handwritten digits (0-9) from 28x28 grayscale images. Which model type below is most appropriate for this task?
AA k-means clustering model
BA linear regression model
CA decision tree classifier
DA convolutional neural network (CNN)
Attempts:
2 left
💡 Hint
Consider the input type and the need to capture spatial features in images.
Hyperparameter
advanced
2:00remaining
Which hyperparameter tuning improves Flask API model latency the most?
You deployed a deep learning model in a Flask API. The API latency is high. Which hyperparameter tuning below will most likely reduce prediction latency?
AIncrease the number of layers in the model
BReduce model size by pruning or quantization
CIncrease batch size during training
DIncrease learning rate during training
Attempts:
2 left
💡 Hint
Think about what affects model size and inference speed.
🔧 Debug
advanced
2:00remaining
Why does this Flask API code raise a KeyError?
This Flask API code raises a KeyError when a POST request is sent. What is the cause?
ML Python
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.json['input']
    # model prediction code here
    return jsonify({'result': 'ok'})
AThe route should accept GET instead of POST
BFlask app is missing debug=True
CThe JSON payload does not contain the key 'input'
DThe jsonify function is used incorrectly
Attempts:
2 left
💡 Hint
Check the JSON keys sent in the request body.
Metrics
expert
2:00remaining
Which metric best evaluates a Flask API model for imbalanced binary classification?
Your Flask API serves a model that predicts rare disease presence (positive class is 1% of data). Which metric below is best to evaluate model performance?
AF1-score
BPrecision
CRecall
DAccuracy
Attempts:
2 left
💡 Hint
Consider a metric that balances false positives and false negatives in imbalanced data.