0
0
MLOpsdevops~10 mins

REST API serving with FastAPI in MLOps - 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.

MLOps
from fastapi import [1]

app = [1]()
Drag options to blanks, or click blank then click option'
ADepends
BRequest
CResponse
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Importing other classes like Request instead of FastAPI.
Forgetting to call FastAPI() to create the app instance.
2fill in blank
medium

Complete the code to define a GET endpoint at path '/' that returns a welcome message.

MLOps
@app.[1]("/")
def read_root():
    return {"message": "Welcome to FastAPI!"}
Drag options to blanks, or click blank then click option'
Apost
Bput
Cget
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or PUT decorators for a simple read endpoint.
Misspelling the decorator name.
3fill in blank
hard

Fix the error in the code to correctly accept a path parameter 'item_id' as an integer.

MLOps
@app.get("/items/{item_id}")
def read_item(item_id: [1]):
    return {"item_id": item_id}
Drag options to blanks, or click blank then click option'
Aint
Bfloat
Cstr
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type which accepts anything, losing validation.
Using float or bool which do not match the expected parameter.
4fill in blank
hard

Fill both blanks to define a POST endpoint '/items/' that accepts a JSON body with a Pydantic model 'Item'.

MLOps
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    price: float

@app.[1]("/items/")
def create_item(item: [2]):
    return item
Drag options to blanks, or click blank then click option'
Apost
Bget
CItem
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET decorator for POST endpoint.
Typing the parameter as string instead of the model.
5fill in blank
hard

Fill all three blanks to run the FastAPI app with uvicorn on host '127.0.0.1' and port 8000.

MLOps
import uvicorn

if __name__ == "__main__":
    uvicorn.run([1], host=[2], port=[3])
Drag options to blanks, or click blank then click option'
A"app:app"
B"0.0.0.0"
C8000
D"127.0.0.1"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong host like '0.0.0.0' for local-only run.
Passing port as string instead of integer.
Wrong app string format.