Spaces:
Sleeping
Sleeping
| 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"] | |
| # Visit /health β tells you the server is alive | |
| async def health(): | |
| return {"status": "ok"} | |
| # 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]} |