Diff to HTML by rtfpessoa

Files changed (3) hide show
  1. app-section-2/app/main.py 2023-04-08 18:02:03 → app-section-3/app/main.py 2023-04-08 18:02:25 +7 -0
  2. app-section-2/app/pydantic_models.py 1969-12-31 17:00:00 → app-section-3/app/pydantic_models.py 2023-04-18 09:34:31 +18 -0
  3. app-section-2/tests/test_app.py 2023-04-02 12:01:33 → app-section-3/tests/test_app.py 2023-04-03 16:54:22 +15 -0
app-section-2/app/main.py 2023-04-08 18:02:03 → app-section-3/app/main.py 2023-04-08 18:02:25 RENAMED
@@ -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")
app-section-2/app/pydantic_models.py 1969-12-31 17:00:00 → app-section-3/app/pydantic_models.py 2023-04-18 09:34:31 RENAMED
@@ -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"]
app-section-2/tests/test_app.py 2023-04-02 12:01:33 → app-section-3/tests/test_app.py 2023-04-03 16:54:22 RENAMED
@@ -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"