Sina Media Lab commited on
Commit
a1b5681
·
1 Parent(s): 05bf22a

api change

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -9,12 +9,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 {
13
- "predicted_class": str(target_names[pred]),
14
- "probabilities": {str(target_names[i]): float(proba[i]) for i in range(len(target_names))}
15
- }
16
 
17
- # Gradio UI
18
  with gr.Blocks() as demo:
19
  gr.Markdown("# 🌸 Iris Detector — KNN Classifier (k=5)")
20
  gr.Markdown("Enter 4 iris flower measurements below to predict the species:")
@@ -25,39 +21,42 @@ with gr.Blocks() as demo:
25
  sepal_width = gr.Number(label="Sepal Width (cm)")
26
  petal_length = gr.Number(label="Petal Length (cm)")
27
  petal_width = gr.Number(label="Petal Width (cm)")
 
28
  predict_btn = gr.Button("Predict")
29
- output = gr.JSON(label="Prediction")
 
30
 
31
  predict_btn.click(
32
  fn=predict_iris,
33
  inputs=[sepal_length, sepal_width, petal_length, petal_width],
34
- outputs=[output]
35
  )
36
 
37
  with gr.Column():
38
- gr.Markdown('''## 📖 API Usage Guide
39
  You can call this model programmatically using the Gradio external API.
40
 
41
  **Endpoint:**
42
  ```
43
- POST https://huggingface.co/spaces/tofighi/iris-detector/run/predict
44
  ```
45
 
46
  **Request Body Example:**
47
  ```json
48
  {
49
- "data": [[5.1, 3.5, 1.4, 0.2]]
50
  }
51
  ```
52
 
53
  **Python Example:**
54
  ```python
55
  import requests
56
- url = "https://huggingface.co/spaces/tofighi/iris-detector/run/predict"
57
- payload = {"data": [[5.1, 3.5, 1.4, 0.2]]}
 
58
  resp = requests.post(url, json=payload)
59
  print(resp.json())
60
  ```
61
- ''')
62
 
63
- demo.launch(share=True)
 
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:")
 
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("""## 📖 API Usage Guide
37
  You can call this model programmatically using the Gradio external API.
38
 
39
  **Endpoint:**
40
  ```
41
+ POST https://huggingface.co/spaces/<username>/iris-detector/run/predict
42
  ```
43
 
44
  **Request Body Example:**
45
  ```json
46
  {
47
+ "data": [5.1, 3.5, 1.4, 0.2]
48
  }
49
  ```
50
 
51
  **Python Example:**
52
  ```python
53
  import requests
54
+
55
+ url = "https://huggingface.co/spaces/<username>/iris-detector/run/predict"
56
+ payload = {"data": [5.1, 3.5, 1.4, 0.2]}
57
  resp = requests.post(url, json=payload)
58
  print(resp.json())
59
  ```
60
+ """)
61
 
62
+ demo.launch()