Spaces:
Sleeping
Sleeping
File size: 836 Bytes
fc98586 d26dc07 3e537af fc98586 d26dc07 fc98586 3e537af fc98586 | 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 | from fastapi import FastAPI
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
import numpy as np
app = FastAPI(title="Iris Classifier API", version="1.0")
# Train model at startup
iris = load_iris()
model = DecisionTreeClassifier(random_state=42)
model.fit(iris.data, iris.target)
class_names = ["setosa", "versicolor", "virginica"]
@app.get("/") # ← ADD THIS ROOT ENDPOINT
async def root():
return {"message": "Iris Classifier API is running!", "endpoints": ["/health", "/predict"]}
@app.get("/health")
async def health():
return {"status": "ok"}
@app.get("/predict")
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]} |