Update app.py
Browse files
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 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# Run
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
-
|
| 31 |
else:
|
| 32 |
-
|
| 33 |
-
"chosen_model": "
|
| 34 |
-
"
|
| 35 |
-
"
|
|
|
|
| 36 |
}
|
| 37 |
-
|
| 38 |
|
| 39 |
-
return
|
| 40 |
|
| 41 |
|
| 42 |
-
#
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("#
|
| 45 |
-
gr.Markdown("
|
| 46 |
|
| 47 |
-
|
| 48 |
-
text_output = gr.Textbox(label="
|
| 49 |
-
json_output = gr.JSON(label="Raw JSON
|
| 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()
|