Sina Media Lab commited on
Commit
9018775
·
1 Parent(s): e0b5499

api change

Browse files
Files changed (5) hide show
  1. Dockerfile +13 -0
  2. README.md +4 -6
  3. app.py +45 -0
  4. iris_knn.pkl +3 -0
  5. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . /code/
10
+
11
+ EXPOSE 7860
12
+
13
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,11 +1,9 @@
1
  ---
2
- title: Iris Detector Api
3
- emoji: 📊
4
- colorFrom: gray
5
- colorTo: red
6
  sdk: docker
7
  pinned: false
8
  license: mit
9
  ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Iris Detector API
3
+ emoji: 🌸
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: docker
7
  pinned: false
8
  license: mit
9
  ---
 
 
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import numpy as np
4
+ import joblib
5
+
6
+ app = FastAPI(
7
+ title="Iris KNN Prediction API",
8
+ description="API for predicting Iris species using KNN model",
9
+ version="1.0.0"
10
+ )
11
+
12
+ try:
13
+ model, target_names = joblib.load("iris_knn.pkl")
14
+ except:
15
+ model = None
16
+ target_names = []
17
+
18
+ class IrisData(BaseModel):
19
+ sepal_length: float
20
+ sepal_width: float
21
+ petal_length: float
22
+ petal_width: float
23
+
24
+ @app.get("/")
25
+ def root():
26
+ return {"message": "Iris KNN API Running! Visit /docs to test the API."}
27
+
28
+ @app.post("/predict")
29
+ def predict_iris(data: IrisData):
30
+ if model is None:
31
+ return {"error": "Model not found on server"}
32
+
33
+ arr = np.array([[
34
+ data.sepal_length,
35
+ data.sepal_width,
36
+ data.petal_length,
37
+ data.petal_width
38
+ ]])
39
+
40
+ pred = model.predict(arr)[0]
41
+ return {
42
+ "input": data.dict(),
43
+ "predicted_class": str(target_names[pred])
44
+ }
45
+
iris_knn.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:af2f61d7c8a95f19394fb776308cd844f5ce159bff513b77e808efe98241100b
3
+ size 14315
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ numpy
4
+ joblib
5
+ scikit-learn
6
+ pydantic