-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path4_app_with_data_validation.py
More file actions
37 lines (28 loc) · 1.12 KB
/
4_app_with_data_validation.py
File metadata and controls
37 lines (28 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from fastapi import FastAPI
from pydantic import BaseModel, validator
from mlmodel import IrisModel
app = FastAPI(title = "🌸 Iris Species", description = "an app supporting gardeners in data-driven decisions")
model = IrisModel()
# A Pydantic model
class IrisDimensions(BaseModel):
sepal_length: float
sepal_width: float
petal_length: float
petal_width: float
@validator("sepal_length")
def check_sepal_length(cls, v):
if v > 7.9:
raise ValueError('The model was not trained for this range. Do you use a correct unit? cm?')
if v < 4.3:
raise ValueError('The model was not trained for this range. Do you use a correct unit? cm?')
return v
@app.get("/model_introduction")
def intro():
return {"model_version": "0.0.1",
"model_name": "Iris Species",
"model_author": "Accenture",
"app_author": "Best Courses"}
@app.post("/prediction")
def predict(iris: IrisDimensions):
species = model.predict(iris.sepal_length, iris.sepal_width, iris.petal_length, iris.petal_width)
return species