iris-classifier / app.py
23f2002478's picture
Create app.py
4088c9a verified
raw
history blame contribute delete
989 Bytes
from fastapi import FastAPI # The web framework (builds your API)
from sklearn.datasets import load_iris # The iris flower dataset
from sklearn.tree import DecisionTreeClassifier # The ML model
import numpy as np # For handling numbers
app = FastAPI()
# This runs ONCE when the server starts β€” trains the model immediately
iris = load_iris()
model = DecisionTreeClassifier(random_state=42)
model.fit(iris.data, iris.target)
class_names = ["setosa", "versicolor", "virginica"]
@app.get("/health") # Visit /health β†’ tells you the server is alive
async def health():
return {"status": "ok"}
@app.get("/predict") # Visit /predict?sl=4.8&sw=4&pl=3.6&pw=0.8 β†’ get prediction
async def predict(sl: float, sw: float, pl: float, pw: float):
features = np.array([[sl, sw, pl, pw]])
pred = int(model.predict(features)[0])
return {"prediction": pred, "class_name": class_names[pred]}