tofighi commited on
Commit
b6ecf31
Β·
verified Β·
1 Parent(s): d1cf3ec

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +84 -3
  2. iris_knn.pkl +3 -0
  3. requirements.txt +4 -0
app.py CHANGED
@@ -1,7 +1,88 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
  demo.launch()
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import joblib
4
 
5
+ # Load trained KNN model
6
+ model, target_names = joblib.load("iris_knn.pkl")
7
+
8
+ def predict_iris(sepal_length, sepal_width, petal_length, petal_width):
9
+ arr = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
10
+ pred = model.predict(arr)[0]
11
+ proba = model.predict_proba(arr)[0]
12
+ return str(target_names[pred]), {str(target_names[i]): float(proba[i]) for i in range(len(target_names))}
13
+
14
+ with gr.Blocks() as demo:
15
+ gr.Markdown("# 🌸 Iris Detector β€” KNN Classifier (k=5)")
16
+ gr.Markdown("Enter 4 iris flower measurements below to predict the species:")
17
+
18
+ with gr.Row():
19
+ with gr.Column():
20
+ sepal_length = gr.Number(label="Sepal Length (cm)")
21
+ sepal_width = gr.Number(label="Sepal Width (cm)")
22
+ petal_length = gr.Number(label="Petal Length (cm)")
23
+ petal_width = gr.Number(label="Petal Width (cm)")
24
+
25
+ predict_btn = gr.Button("Predict")
26
+ output_class = gr.Label(label="Predicted Class")
27
+ output_proba = gr.JSON(label="Probabilities")
28
+
29
+ predict_btn.click(
30
+ fn=predict_iris,
31
+ inputs=[sepal_length, sepal_width, petal_length, petal_width],
32
+ outputs=[output_class, output_proba]
33
+ )
34
+
35
+ with gr.Column():
36
+ gr.Markdown(
37
+ """
38
+ ## πŸ“– Iris Detector API Usage (FastAPI)
39
+
40
+ Your predictions can also be made programmatically using the FastAPI backend deployed at:
41
+
42
+ ### **API Endpoint**
43
+ ```
44
+ POST https://tofighi-iris-detector-api.hf.space/predict
45
+ ```
46
+
47
+ ---
48
+
49
+ ### **πŸ“Œ JSON Request Example**
50
+ ```json
51
+ {
52
+ "sepal_length": 5.1,
53
+ "sepal_width": 3.5,
54
+ "petal_length": 1.4,
55
+ "petal_width": 0.2
56
+ }
57
+ ```
58
+
59
+ ---
60
+
61
+ ### **🐍 Python Example**
62
+ ```python
63
+ import requests
64
+
65
+ url = "https://tofighi-iris-detector-api.hf.space/predict"
66
+
67
+ data = {
68
+ "sepal_length": 5.1,
69
+ "sepal_width": 3.5,
70
+ "petal_length": 1.4,
71
+ "petal_width": 0.2
72
+ }
73
+
74
+ resp = requests.post(url, json=data)
75
+ print(resp.json())
76
+ ```
77
+
78
+ ---
79
+
80
+ ### **πŸ’» cURL Example**
81
+ ```bash
82
+ curl -X POST "https://tofighi-iris-detector-api.hf.space/predict" \
83
+ -H "Content-Type: application/json" \
84
+ -d '{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4,"petal_width":0.2}'
85
+ ```
86
+ """)
87
 
 
88
  demo.launch()
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
+ numpy
3
+ scikit-learn
4
+ joblib