fatimaxa commited on
Commit
73df3ca
·
verified ·
1 Parent(s): 1a003a0

Update tabs/single_prediction.py

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