Complete the code to import FastAPI and create an app instance.
from fastapi import [1] app = [1]()
We import FastAPI from the fastapi package and create an app instance by calling FastAPI().
Complete the code to define a POST endpoint '/predict' that accepts JSON input.
@app.post('/predict') async def predict(data: [1]): return {'prediction': 'dummy'}
str instead of dict for JSON inputint or float incorrectlyThe endpoint expects JSON data, which is parsed as a Python dictionary (dict).
Fix the error in the code to load a saved model using joblib.
import joblib model = joblib.[1]('model.joblib')
save or dump instead of loadopen directly on the model fileTo load a saved model with joblib, use joblib.load().
Fill both blanks to extract features from input JSON and make a prediction.
features = [data[[1]] for [2] in ['feature1', 'feature2']] prediction = model.predict([features])
We extract each feature by its name from the input dictionary data using a loop variable x.
Fill all three blanks to return the prediction as JSON with a status code.
from fastapi.responses import Response @app.post('/predict') async def predict(data: dict): features = [data[[1]] for [2] in ['f1', 'f2']] pred = model.predict([features])[0] return Response(content=str(pred), media_type=[3], status_code=200)
The loop variable f is used for feature extraction in both blanks, and the response media type for plain text is 'text/plain'.