Consider this FastAPI code snippet that serves a simple model prediction:
from fastapi import FastAPI
app = FastAPI()
@app.get('/predict')
async def predict(x: int):
return {'prediction': x * 2}What will be the JSON response when a client requests /predict?x=3?
from fastapi import FastAPI app = FastAPI() @app.get('/predict') async def predict(x: int): return {'prediction': x * 2}
Remember that the endpoint multiplies the input x by 2 and returns it as an integer in JSON.
The function receives x=3, multiplies it by 2, and returns {'prediction': 6} as JSON. The value is an integer, not a string or null.
You want to load a machine learning model once when the FastAPI app starts, so it can be reused for all requests efficiently.
Which method below correctly loads the model at startup?
Think about how to avoid reloading the model multiple times to save time.
Loading the model globally outside functions ensures it loads once when the app starts and is reused for all requests, improving performance.
You want to serve a machine learning model with FastAPI and handle many requests at the same time efficiently.
Which Uvicorn server option helps improve concurrency?
More workers mean more processes to handle requests concurrently.
Using multiple workers (--workers 4) runs several processes to handle requests in parallel, improving concurrency and throughput.
Examine this FastAPI code snippet:
from fastapi import FastAPI
import pickle
app = FastAPI()
model = pickle.load(open('model.pkl', 'rb'))
@app.post('/predict')
async def predict(data: dict):
features = data['features']
prediction = model.predict([features])
return {'prediction': prediction}When sending a POST request with JSON {"features": [1, 2, 3]}, it raises a TypeError. Why?
from fastapi import FastAPI import pickle app = FastAPI() model = pickle.load(open('model.pkl', 'rb')) @app.post('/predict') async def predict(data: dict): features = data['features'] prediction = model.predict([features]) return {'prediction': prediction}
Think about what type model.predict returns and how FastAPI converts it to JSON.
The model.predict method returns a numpy array, which FastAPI cannot convert to JSON directly, causing a TypeError. You need to convert it to a list first.
Choose the best reason why FastAPI is often preferred for serving machine learning models compared to Flask.
Think about developer experience and performance features.
FastAPI provides automatic interactive API documentation and supports asynchronous programming, which helps handle many requests efficiently, making it ideal for model serving.