Spaces:
Sleeping
Sleeping
Update tabs/single_prediction.py
Browse files- tabs/single_prediction.py +71 -76
tabs/single_prediction.py
CHANGED
|
@@ -1,77 +1,72 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from utils.predictions import predict_single_image, get_disease_info
|
| 3 |
-
from utils.chart_vis import create_prediction_plot
|
| 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 |
-
["ui_text/examples/
|
| 38 |
-
["ui_text/examples/
|
| 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 |
-
fn=predict_with_visualization,
|
| 73 |
-
inputs=[input_image, top_n_slider],
|
| 74 |
-
outputs=[output_plot, output_info, output_label]
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
return input_image, top_n_slider, predict_btn
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils.predictions import predict_single_image, get_disease_info
|
| 3 |
+
from utils.chart_vis import create_prediction_plot
|
| 4 |
+
|
| 5 |
+
def create_single_prediction_tab(model, class_names, disease_db, device):
|
| 6 |
+
"""Create the single image prediction tab"""
|
| 7 |
+
|
| 8 |
+
def predict_with_visualization(image, show_top_n):
|
| 9 |
+
"""Prediction function with all outputs"""
|
| 10 |
+
if image is None:
|
| 11 |
+
return None, "Please upload an image", None
|
| 12 |
+
|
| 13 |
+
top_preds = predict_single_image(image, model, class_names, device, show_top_n)
|
| 14 |
+
|
| 15 |
+
plot = create_prediction_plot(top_preds)
|
| 16 |
+
|
| 17 |
+
top_disease = top_preds[0][0]
|
| 18 |
+
confidence = top_preds[0][1]
|
| 19 |
+
|
| 20 |
+
info_text = f"## Top Prediction: {top_disease}\n"
|
| 21 |
+
info_text += f"**Confidence:** {confidence:.2%}\n\n"
|
| 22 |
+
info_text += f"{get_disease_info(top_disease, disease_db)}\n\n"
|
| 23 |
+
|
| 24 |
+
if confidence < 0.5:
|
| 25 |
+
info_text += "**Note:** Low confidence. Consider expert verification."
|
| 26 |
+
|
| 27 |
+
# Results dictionary
|
| 28 |
+
results_dict = {label: round(float(prob), 4) for label, prob in top_preds}
|
| 29 |
+
|
| 30 |
+
return plot, info_text, results_dict
|
| 31 |
+
|
| 32 |
+
examples = [
|
| 33 |
+
["ui_text/examples/Apple___Apple_scab.jpg"],
|
| 34 |
+
["ui_text/examples/Tomato___healthy.jpg"],
|
| 35 |
+
["ui_text/examples/Tomato___Bacterial_spot.jpg"],
|
| 36 |
+
["ui_text/examples/Tomato___Tomato_Yellow_Leaf_Curl_Virus.jpg"],
|
| 37 |
+
["ui_text/examples/Tomato___Septoria_leaf_spot.jpg"],
|
| 38 |
+
["ui_text/examples/Soybean___healthy.jpg"]
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
with gr.Column(scale=1):
|
| 43 |
+
input_image = gr.Image(type="pil", label="Upload Plant Leaf Image")
|
| 44 |
+
top_n_slider = gr.Slider(
|
| 45 |
+
minimum=3,
|
| 46 |
+
maximum=15,
|
| 47 |
+
value=10,
|
| 48 |
+
step=1,
|
| 49 |
+
label="Number of top predictions to show"
|
| 50 |
+
)
|
| 51 |
+
predict_btn = gr.Button("Analyze Disease", variant="primary")
|
| 52 |
+
|
| 53 |
+
gr.Markdown("### Example Images")
|
| 54 |
+
gr.Examples(
|
| 55 |
+
examples=examples,
|
| 56 |
+
inputs=input_image,
|
| 57 |
+
label="Click an example to try it out",
|
| 58 |
+
cache_examples=False # Disable caching to avoid duplicates
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
with gr.Column(scale=1):
|
| 62 |
+
output_plot = gr.Plot(label="Prediction Confidence Chart")
|
| 63 |
+
output_info = gr.Markdown()
|
| 64 |
+
output_label = gr.Label(label="Detailed Predictions", num_top_classes=10)
|
| 65 |
+
|
| 66 |
+
predict_btn.click(
|
| 67 |
+
fn=predict_with_visualization,
|
| 68 |
+
inputs=[input_image, top_n_slider],
|
| 69 |
+
outputs=[output_plot, output_info, output_label]
|
| 70 |
+
)
|
| 71 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return input_image, top_n_slider, predict_btn
|