What if your AI model could answer questions from anywhere in the world instantly, without any setup?
Why REST API for predictions in TensorFlow? - Purpose & Use Cases
Imagine you built a smart model that can tell if a photo has a cat or a dog. Now, you want your friends to use it anytime from their phones or websites. But you have to send them the model file and explain how to run it on their computers.
This manual way is slow and confusing. Your friends might not have the right setup, or they could make mistakes running the model. It's hard to update the model for everyone, and sharing results takes too long.
Using a REST API for predictions means your model lives on a server. Your friends just send a photo over the internet, and the server quickly sends back the answer. No setup, no confusion, and updates happen instantly for all users.
load_model('model.h5')
predict(image)POST /predict {"image": ...}
Response: {"label": "cat"}It makes your smart model instantly available to anyone, anywhere, on any device, with just a simple internet request.
Apps like voice assistants or photo filters use REST APIs to send your voice or picture to a server and get smart answers back in seconds.
Manual sharing of models is slow and error-prone.
REST APIs let you serve predictions easily over the internet.
This approach makes your AI accessible and simple for everyone.
Practice
Solution
Step 1: Understand REST API role
A REST API acts as a bridge to send data and receive results from a model.Step 2: Match purpose with options
Only To allow other programs to send data and get predictions easily describes this role correctly as it enables easy data exchange for predictions.Final Answer:
To allow other programs to send data and get predictions easily -> Option DQuick Check:
REST API purpose = Easy data exchange for predictions [OK]
- Confusing API with training process
- Thinking API is for visualization
- Assuming API changes model files
Solution
Step 1: Identify correct Flask route syntax for POST
Flask uses @app.route with methods=['POST'] to define POST endpoints.Step 2: Check code snippets
Only app.route('/predict', methods=['POST'])\ndef predict():\n data = request.get_json() uses @app.route with methods=['POST'] and calls request.get_json() correctly.Final Answer:
app.route('/predict', methods=['POST'])\ndef predict():\n data = request.get_json() -> Option CQuick Check:
Flask POST route = @app.route with methods=['POST'] [OK]
- Using app.post decorator (not standard Flask)
- Missing methods=['POST'] in route
- Using app.get for POST requests
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
inputs = data['input']
prediction = sum(inputs)
return jsonify({'prediction': prediction})Solution
Step 1: Extract input and compute prediction
The input JSON has 'input': [1, 2, 3]. The code sums these values: 1+2+3=6.Step 2: Return prediction as JSON
The code returns jsonify({'prediction': 6}), so output is {"prediction": 6}.Final Answer:
{"prediction": 6} -> Option AQuick Check:
Sum([1,2,3]) = 6 [OK]
- Returning input list instead of sum
- Concatenating numbers as string
- Misreading JSON keys causing KeyError
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/predict')
def predict():
data = request.get_json()
prediction = model.predict(data['input'])
return jsonify({'prediction': prediction.tolist()})Solution
Step 1: Check route decorator for HTTP methods
The route lacks methods=['POST'], so it defaults to GET only, causing errors on POST requests.Step 2: Confirm other parts are correct
model.predict() can accept arrays, get_json() works with POST, and jsonify converts lists fine.Final Answer:
Missing methods=['POST'] in @app.route decorator -> Option BQuick Check:
POST route needs methods=['POST'] [OK]
- Forgetting to specify POST method in route
- Assuming get_json works only with GET
- Thinking jsonify can't handle lists
Solution
Step 1: Understand batch prediction needs
Batch prediction means processing many inputs at once for efficiency.Step 2: Choose method to handle batch inputs
Accept a JSON list of inputs, convert to numpy array, predict batch, return list of predictions accepts JSON list, converts to array, predicts all at once, then returns all predictions, which is efficient and standard.Final Answer:
Accept a JSON list of inputs, convert to numpy array, predict batch, return list of predictions -> Option AQuick Check:
Batch input = JSON list + batch predict [OK]
- Predicting one input at a time (slow)
- Using files instead of direct JSON
- Using GET for large input data
Ready to prove it?
Pass the test and this pattern moves unknown
