|
@@ -1,4 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
app = FastAPI()
|
|
@@ -8,3 +9,9 @@
|
|
| 8 |
def status():
|
| 9 |
"""Check that the API is working."""
|
| 10 |
return "the API is up and running!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from .pydantic_models import Observation, Prediction
|
| 3 |
|
| 4 |
|
| 5 |
app = FastAPI()
|
|
|
|
| 9 |
def status():
|
| 10 |
"""Check that the API is working."""
|
| 11 |
return "the API is up and running!"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@app.post("/predict", status_code=201)
|
| 15 |
+
def predict(obs: Observation) -> Prediction:
|
| 16 |
+
"""For now, just return a dummy prediction."""
|
| 17 |
+
return Prediction(flower_type="setosa")
|
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Literal
|
| 2 |
+
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class Observation(BaseModel):
|
| 7 |
+
"""An observation of a flower's measurements."""
|
| 8 |
+
# For later parts of this code to work, the order of fields here needs to match the
|
| 9 |
+
# order they were listed in training.
|
| 10 |
+
sepal_length: float
|
| 11 |
+
sepal_width: float
|
| 12 |
+
petal_length: float
|
| 13 |
+
petal_width: float
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class Prediction(BaseModel):
|
| 17 |
+
"""A prediction of the species of a flower."""
|
| 18 |
+
flower_type: Literal["setosa", "versicolor", "virginica"]
|
|
@@ -6,3 +6,18 @@
|
|
| 6 |
assert response.status_code == 200
|
| 7 |
payload = response.json()
|
| 8 |
assert payload == "the API is up and running!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
assert response.status_code == 200
|
| 7 |
payload = response.json()
|
| 8 |
assert payload == "the API is up and running!"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_predict(client: TestClient):
|
| 12 |
+
response = client.post(
|
| 13 |
+
"/predict",
|
| 14 |
+
json={
|
| 15 |
+
"sepal_length": 5.1,
|
| 16 |
+
"sepal_width": 3.5,
|
| 17 |
+
"petal_length": 1.4,
|
| 18 |
+
"petal_width": 0.2,
|
| 19 |
+
},
|
| 20 |
+
)
|
| 21 |
+
assert response.status_code == 201
|
| 22 |
+
payload = response.json()
|
| 23 |
+
assert payload["flower_type"] == "setosa"
|