0
0
ML Pythonml~10 mins

FastAPI for model serving in ML Python - Interactive Code Practice

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

Complete the code to import FastAPI and create an app instance.

ML Python
from fastapi import [1]

app = [1]()
Drag options to blanks, or click blank then click option'
AResponse
BDepends
CRequest
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI
Forgetting to call FastAPI() as a function
2fill in blank
medium

Complete the code to define a POST endpoint '/predict' that accepts JSON input.

ML Python
@app.post('/predict')
async def predict(data: [1]):
    return {'prediction': 'dummy'}
Drag options to blanks, or click blank then click option'
Adict
Bstr
Cint
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using str instead of dict for JSON input
Using numeric types like int or float incorrectly
3fill in blank
hard

Fix the error in the code to load a saved model using joblib.

ML Python
import joblib

model = joblib.[1]('model.joblib')
Drag options to blanks, or click blank then click option'
Asave
Bload
Cdump
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using save or dump instead of load
Trying to use open directly on the model file
4fill in blank
hard

Fill both blanks to extract features from input JSON and make a prediction.

ML Python
features = [data[[1]] for [2] in ['feature1', 'feature2']]
prediction = model.predict([features])
Drag options to blanks, or click blank then click option'
A'feature1'
Bx
C'feature2'
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using a literal string instead of the loop variable in the first blank
Using a variable name that doesn't match the loop in the second blank
5fill in blank
hard

Fill all three blanks to return the prediction as JSON with a status code.

ML Python
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)
Drag options to blanks, or click blank then click option'
A'f1'
Bf
C'application/json'
D'text/plain'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong feature key or loop variable
Using incorrect media type like 'application/json' instead of 'text/plain'