File size: 2,282 Bytes
03a79a1
 
 
 
cecee04
03a79a1
 
05bf22a
 
2cfaaa4
 
a1b5681
03a79a1
 
 
05bf22a
03a79a1
 
 
 
 
 
 
a1b5681
05bf22a
a1b5681
 
03a79a1
 
05bf22a
03a79a1
a1b5681
03a79a1
 
 
91b16c9
 
 
03a79a1
91b16c9
 
 
03a79a1
91b16c9
03a79a1
 
91b16c9
 
 
03a79a1
 
91b16c9
 
 
 
03a79a1
 
 
91b16c9
 
 
03a79a1
 
a1b5681
91b16c9
 
 
 
 
 
 
 
 
 
03a79a1
 
91b16c9
 
 
 
 
 
 
 
 
a1b5681
03a79a1
a1b5681
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gradio as gr
import numpy as np
import joblib

# Load trained KNN model
model, target_names = joblib.load("iris_knn.pkl")

def predict_iris(sepal_length, sepal_width, petal_length, petal_width):
    arr = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
    pred = model.predict(arr)[0]
    proba = model.predict_proba(arr)[0]
    return str(target_names[pred]), {str(target_names[i]): float(proba[i]) for i in range(len(target_names))}

with gr.Blocks() as demo:
    gr.Markdown("# 🌸 Iris Detector β€” KNN Classifier (k=5)")
    gr.Markdown("Enter 4 iris flower measurements below to predict the species:")

    with gr.Row():
        with gr.Column():
            sepal_length = gr.Number(label="Sepal Length (cm)")
            sepal_width = gr.Number(label="Sepal Width (cm)")
            petal_length = gr.Number(label="Petal Length (cm)")
            petal_width = gr.Number(label="Petal Width (cm)")

            predict_btn = gr.Button("Predict")
            output_class = gr.Label(label="Predicted Class")
            output_proba = gr.JSON(label="Probabilities")

            predict_btn.click(
                fn=predict_iris,
                inputs=[sepal_length, sepal_width, petal_length, petal_width],
                outputs=[output_class, output_proba]
            )

        with gr.Column():
            gr.Markdown(
"""
## πŸ“– Iris Detector API Usage (FastAPI)

Your predictions can also be made programmatically using the FastAPI backend deployed at:

### **API Endpoint**
```
POST https://tofighi-iris-detector-api.hf.space/predict
```

---

### **πŸ“Œ JSON Request Example**
```json
{
  "sepal_length": 5.1,
  "sepal_width": 3.5,
  "petal_length": 1.4,
  "petal_width": 0.2
}
```

---

### **🐍 Python Example**
```python
import requests

url = "https://tofighi-iris-detector-api.hf.space/predict"

data = {
    "sepal_length": 5.1,
    "sepal_width": 3.5,
    "petal_length": 1.4,
    "petal_width": 0.2
}

resp = requests.post(url, json=data)
print(resp.json())
```

---

### **πŸ’» cURL Example**
```bash
curl -X POST "https://tofighi-iris-detector-api.hf.space/predict" \
     -H "Content-Type: application/json" \
     -d '{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4,"petal_width":0.2}'
```
""")

demo.launch()