Sina Media Lab commited on
Commit
03a79a1
·
1 Parent(s): 041a03c

Deploy iris detector (KNN)

Browse files
Files changed (5) hide show
  1. .gitignore +3 -0
  2. README.md +23 -14
  3. app.py +82 -0
  4. iris_knn.pkl +3 -0
  5. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ .venv/
README.md CHANGED
@@ -1,14 +1,23 @@
1
- ---
2
- title: Iris Detector
3
- emoji:
4
- colorFrom: indigo
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 5.49.1
8
- app_file: app.py
9
- pinned: false
10
- license: apache-2.0
11
- short_description: Iris detection
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
1
+ # 🌸 Iris Detector (KNN, k=5)
2
+
3
+ A simple Hugging Face Space that loads a pretrained KNN model (exported via Google Colab) and predicts iris species from 4 numeric inputs.
4
+
5
+ ## Features
6
+ - 4 numeric inputs: sepal length, sepal width, petal length, petal width
7
+ - Predicts one of 3 classes:
8
+ - setosa
9
+ - versicolor
10
+ - virginica
11
+ - Probability distribution included
12
+ - Works with Hugging Face API `/run/predict`
13
+
14
+ ## API Example
15
+
16
+ POST:
17
+ https://<user>-<space>.hf.space/run/predict
18
+ Body:
19
+ ```json
20
+ {
21
+ "data": [5.1, 3.5, 1.4, 0.2]
22
+ }
23
+
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import joblib
4
+
5
+ # Load trained model and target names
6
+ model, target_names = joblib.load("iris_knn.pkl")
7
+
8
+ def predict(sepal_length, sepal_width, petal_length, petal_width):
9
+ """
10
+ Predict the iris species given 4 numeric inputs.
11
+
12
+ Returns a dictionary with 'predicted_class' and 'probabilities'.
13
+ """
14
+ data = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
15
+ pred = model.predict(data)[0]
16
+ proba = model.predict_proba(data)[0]
17
+ return {
18
+ "predicted_class": str(target_names[pred]),
19
+ "probabilities": {str(target_names[i]): float(proba[i]) for i in range(len(target_names))}
20
+ }
21
+
22
+ with gr.Blocks() as demo:
23
+ gr.Markdown("# 🌸 Iris Detector — KNN Classifier (k=5)")
24
+ gr.Markdown("Enter 4 iris flower measurements below to predict the species.")
25
+
26
+ with gr.Row():
27
+ with gr.Column():
28
+ sepal_length = gr.Number(label="Sepal Length (cm)")
29
+ sepal_width = gr.Number(label="Sepal Width (cm)")
30
+ petal_length = gr.Number(label="Petal Length (cm)")
31
+ petal_width = gr.Number(label="Petal Width (cm)")
32
+
33
+ predict_btn = gr.Button("Predict Iris Class")
34
+ output = gr.JSON(label="Prediction")
35
+
36
+ predict_btn.click(
37
+ fn=predict,
38
+ inputs=[sepal_length, sepal_width, petal_length, petal_width],
39
+ outputs=[output]
40
+ )
41
+
42
+ with gr.Column():
43
+ gr.Markdown("## 📖 API Usage Guide")
44
+ gr.Markdown("""
45
+ You can access this model programmatically using the Hugging Face Space API.
46
+
47
+ **Endpoint:**
48
+ ```
49
+ POST https://huggingface.co/spaces/tofighi/iris-detector/run/predict
50
+ ```
51
+
52
+ **Request Body (JSON):**
53
+ ```json
54
+ {
55
+ "data": [5.1, 3.5, 1.4, 0.2]
56
+ }
57
+ ```
58
+
59
+ **Response:**
60
+ ```json
61
+ {
62
+ "predicted_class": "setosa",
63
+ "probabilities": {
64
+ "setosa": 1.0,
65
+ "versicolor": 0.0,
66
+ "virginica": 0.0
67
+ }
68
+ }
69
+ ```
70
+
71
+ **Python Example:**
72
+ ```python
73
+ import requests
74
+
75
+ url = "https://huggingface.co/spaces/tofighi/iris-detector/run/predict"
76
+ payload = {"data": [5.1, 3.5, 1.4, 0.2]}
77
+ resp = requests.post(url, json=payload)
78
+ print(resp.json())
79
+ ```
80
+ """)
81
+
82
+ demo.launch(share=True)
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,4 @@
 
 
 
 
 
1
+ gradio
2
+ scikit-learn
3
+ numpy
4
+ joblib