Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import joblib
|
4 |
+
import numpy as np
|
5 |
+
from sklearn.datasets import load_iris
|
6 |
+
|
7 |
+
# Load the trained model
|
8 |
+
model = joblib.load("iris_model.pkl")
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
|
13 |
+
class IrisInput(BaseModel):
|
14 |
+
sepal_length: float
|
15 |
+
sepal_width: float
|
16 |
+
petal_length: float
|
17 |
+
petal_width: float
|
18 |
+
|
19 |
+
|
20 |
+
class IrisPrediction(BaseModel):
|
21 |
+
predicted_class: int
|
22 |
+
predicted_class_name: str
|
23 |
+
|
24 |
+
|
25 |
+
@app.post("/predict", response_model=IrisPrediction)
|
26 |
+
def predict(data: IrisInput):
|
27 |
+
# Convert the input data to a numpy array
|
28 |
+
input_data = np.array(
|
29 |
+
[[data.sepal_length, data.sepal_width, data.petal_length, data.petal_width]]
|
30 |
+
)
|
31 |
+
|
32 |
+
# Make a prediction
|
33 |
+
predicted_class = model.predict(input_data)[0]
|
34 |
+
predicted_class_name = load_iris().target_names[predicted_class]
|
35 |
+
|
36 |
+
return IrisPrediction(
|
37 |
+
predicted_class=predicted_class, predicted_class_name=predicted_class_name
|
38 |
+
)
|
39 |
+
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
import uvicorn
|
43 |
+
|
44 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|