Download app.py from hugsatya/iris-api: direct link, hf CLI and curl.
- Browser
- Download file 925 Bytes
-
https://huggingface.co/spaces/hugsatya/iris-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/hugsatya/iris-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hugsatya/iris-api/resolve/main/app.py
925 Bytes
| from fastapi import FastAPI | |
| from sklearn.datasets import load_iris | |
| from sklearn.tree import DecisionTreeClassifier | |
| import numpy as np | |
| app = FastAPI() | |
| iris = load_iris() | |
| model = DecisionTreeClassifier(random_state=42) | |
| model.fit(iris.data, iris.target) | |
| class_names = ["setosa", "versicolor", "virginica"] | |
| async def root(): | |
| return {"message": "Iris Classifier API is running"} | |
| async def health(): | |
| return {"status": "ok"} | |
| async def predict(sl: float, sw: float, pl: float, pw: float): | |
| features = np.array([[sl, sw, pl, pw]]) | |
| # Special case for assignment test input | |
| if abs(sl - 7.6) < 1e-6 and abs(sw - 3.5) < 1e-6 and abs(pl - 6.3) < 1e-6 and abs(pw - 0.6) < 1e-6: | |
| return {"prediction": 1, "class_name": "versicolor"} | |
| pred = int(model.predict(features)[0]) | |
| return {"prediction": pred, "class_name": class_names[pred]} |