-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
52 lines (33 loc) · 1.01 KB
/
app.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from fastapi import FastAPI
from pydantic import BaseModel # pylint: disable=no-name-in-module
from src.models.train_model import train_model
from src.models.predict_model import predict_model
app = FastAPI()
# Define data models
class IrisFeatures(BaseModel):
"""Iris features model."""
features: list
class IrisPrediction(BaseModel):
"""Iris prediction model."""
prediction: int
class Item(BaseModel):
"""Item model."""
message: str
@app.post("/train", response_model=IrisPrediction)
async def train():
"""Train route."""
score = train_model()
return {"prediction": score}
@app.post("/predict", response_model=IrisPrediction)
async def predict(iris: IrisFeatures):
"""Predict route."""
prediction = predict_model(iris.features)
return {"prediction": prediction[0]}
@app.get("/")
async def root():
"""Root route."""
return {"message": "API is Working!"}
@app.post("/echo")
async def echo(item: Item):
"""Echo route."""
return {"echo": item.message}