SoraRyuu commited on
Commit
44e4223
·
verified ·
1 Parent(s): df629a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -40
app.py CHANGED
@@ -1,59 +1,88 @@
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
 
3
  import json
4
  import tempfile
5
 
6
- # Initialize both external Spaces
7
- client1 = Client("raqiat123/crop_disease_detection")
8
- client2 = Client("SoraRyuu/cv_first")
9
 
10
- def combined_predict(image):
11
- # Temporarily save uploaded file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
13
- image.save(tmp.name)
14
- tmp_path = tmp.name
15
-
16
- # Run prediction on both models
17
- result1 = client1.predict(image=handle_file(tmp_path), api_name="/predict")
18
- result2 = client2.predict(image=handle_file(tmp_path), api_name="/predict")
19
-
20
- # Extract confidence values (adjust if needed)
21
- conf1 = result1.get("confidence") if isinstance(result1, dict) else 0
22
- conf2 = result2.get("confidence") if isinstance(result2, dict) else 0
23
-
24
- if conf1 >= conf2:
25
- chosen = {
26
- "chosen_model": "raqiat123/crop_disease_detection",
27
- "confidence": conf1,
28
- "output": result1
 
 
 
 
 
 
 
 
 
 
29
  }
30
- result_text = f"Model Selected: crop_disease_detection\nConfidence: {conf1}"
31
  else:
32
- chosen = {
33
- "chosen_model": "SoraRyuu/cv_first",
34
- "confidence": conf2,
35
- "output": result2
 
36
  }
37
- result_text = f"Model Selected: cv_first\nConfidence: {conf2}"
38
 
39
- return result_text, json.dumps(chosen, indent=4)
40
 
41
 
42
- # Build Gradio UI
43
  with gr.Blocks() as demo:
44
- gr.Markdown("# 🌱 Crop Classifier")
45
- gr.Markdown("Upload an image. The Space will return the disease detected.")
46
 
47
- image_input = gr.Image(type="numpy")
48
- text_output = gr.Textbox(label="Result (Text)")
49
- json_output = gr.JSON(label="Raw JSON Result")
50
 
51
  btn = gr.Button("Run Prediction")
52
-
53
- btn.click(
54
- fn=combined_predict,
55
- inputs=image_input,
56
- outputs=[text_output, json_output]
57
- )
58
 
59
  demo.launch()
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
+ from PIL import Image
4
  import json
5
  import tempfile
6
 
7
+ # Load both external Spaces
8
+ resnet_client = Client("raqiat123/crop_disease_detection")
9
+ yolo_client = Client("SoraRyuu/cv_first")
10
 
11
+
12
+ def extract_best_prediction(result_dict):
13
+ """
14
+ Extracts the best label + best confidence from:
15
+ {
16
+ "label1": 0.82,
17
+ "label2": 0.13,
18
+ ...
19
+ }
20
+ """
21
+ if not result_dict:
22
+ return None, 0.0
23
+
24
+ best_label = max(result_dict, key=result_dict.get)
25
+ best_conf = result_dict[best_label]
26
+ return best_label, best_conf
27
+
28
+
29
+ def combined_predict(image_pil):
30
+ """
31
+ Input = PIL image from Gradio
32
+ """
33
+
34
+ # Save the PIL image to a temp file for HF client
35
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
36
+ image_pil.save(tmp.name)
37
+ img_path = tmp.name
38
+
39
+ # Run both external models
40
+ resnet_output = resnet_client.predict(
41
+ image=handle_file(img_path),
42
+ api_name="/predict"
43
+ )
44
+
45
+ # YOLO space returns (dict, image)
46
+ yolo_output, _ = yolo_client.predict(
47
+ image=handle_file(img_path),
48
+ api_name="/predict"
49
+ )
50
+
51
+ # Extract best predictions
52
+ resnet_label, resnet_conf = extract_best_prediction(resnet_output)
53
+ yolo_label, yolo_conf = extract_best_prediction(yolo_output)
54
+
55
+ # Choose best model
56
+ if resnet_conf >= yolo_conf:
57
+ final = {
58
+ "chosen_model": "ResNet (crop_disease_detection)",
59
+ "label": resnet_label,
60
+ "confidence": resnet_conf,
61
+ "full_output": resnet_output
62
  }
63
+ text = f"Model Selected: ResNet\nPrediction: {resnet_label}\nConfidence: {resnet_conf:.4f}"
64
  else:
65
+ final = {
66
+ "chosen_model": "YOLO (cv_first)",
67
+ "label": yolo_label,
68
+ "confidence": yolo_conf,
69
+ "full_output": yolo_output
70
  }
71
+ text = f"Model Selected: YOLO\nPrediction: {yolo_label}\nConfidence: {yolo_conf:.4f}"
72
 
73
+ return text, final
74
 
75
 
76
+ # Gradio UI
77
  with gr.Blocks() as demo:
78
+ gr.Markdown("# 🌿 Crop Disease Classifier")
79
+ gr.Markdown("Give an image to detect disease in crop, if any.")
80
 
81
+ img = gr.Image(type="pil")
82
+ text_output = gr.Textbox(label="Prediction")
83
+ json_output = gr.JSON(label="Raw JSON Output")
84
 
85
  btn = gr.Button("Run Prediction")
86
+ btn.click(fn=combined_predict, inputs=img, outputs=[text_output, json_output])
 
 
 
 
 
87
 
88
  demo.launch()