Spaces:
Runtime error
Runtime error
update percentage breakdown + error message
Browse files
app.py
CHANGED
@@ -27,25 +27,33 @@ def predict(image):
|
|
27 |
label = model.config.id2label[label_index]
|
28 |
|
29 |
# Split the label into leaf category and disease name
|
30 |
-
|
31 |
label_parts = label.split("___")
|
32 |
leaf_category = label_parts[0]
|
33 |
|
34 |
-
|
35 |
if len(label_parts) == 1:
|
36 |
-
|
37 |
-
disease_name = "N/A"
|
38 |
else:
|
39 |
disease_name = label_parts[1]
|
40 |
|
|
|
41 |
# Calculate the percentage breakdown of predicted diseases
|
42 |
percentage_breakdown = {disease: round(float(probability[0, index]) * 100, 2) for index, disease in enumerate(model.config.label2id)}
|
43 |
|
44 |
# Sort the percentage breakdown dictionary by values in descending order
|
45 |
sorted_percentage = dict(sorted(percentage_breakdown.items(), key=lambda item: item[1], reverse=True))
|
46 |
|
47 |
-
#
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
return leaf_category.capitalize(), disease_name.replace('_', ' ').capitalize(), formatted_percentage
|
51 |
|
@@ -54,13 +62,12 @@ interface = gr.Interface(
|
|
54 |
fn=predict,
|
55 |
inputs=gr.Image(label="Upload the Image"),
|
56 |
outputs=[
|
57 |
-
gr.Textbox(label="Leaf Type:"),
|
58 |
gr.Textbox(label="Identified Disease:"),
|
59 |
-
gr.Textbox(label="Percentage Breakdown:")
|
60 |
],
|
61 |
title="Plant Disease Identifier",
|
62 |
description="Quick Tip: If the image results do not match, consider taking a picture of the leaf against a clean white background for improved accuracy and better identification of plant diseases."
|
63 |
)
|
64 |
|
65 |
-
|
66 |
interface.launch(debug=True) # Start server and launch the UI
|
|
|
27 |
label = model.config.id2label[label_index]
|
28 |
|
29 |
# Split the label into leaf category and disease name
|
|
|
30 |
label_parts = label.split("___")
|
31 |
leaf_category = label_parts[0]
|
32 |
|
33 |
+
# Assume "background with leaves" as the category and "N/A" as the disease name if label_parts only has one part
|
34 |
if len(label_parts) == 1:
|
35 |
+
disease_name = "Not applicable"
|
|
|
36 |
else:
|
37 |
disease_name = label_parts[1]
|
38 |
|
39 |
+
|
40 |
# Calculate the percentage breakdown of predicted diseases
|
41 |
percentage_breakdown = {disease: round(float(probability[0, index]) * 100, 2) for index, disease in enumerate(model.config.label2id)}
|
42 |
|
43 |
# Sort the percentage breakdown dictionary by values in descending order
|
44 |
sorted_percentage = dict(sorted(percentage_breakdown.items(), key=lambda item: item[1], reverse=True))
|
45 |
|
46 |
+
# Get the top 5 predicted diseases
|
47 |
+
top_5_percentage = {disease: percentage for disease, percentage in list(sorted_percentage.items())[:5]}
|
48 |
+
|
49 |
+
# Format the percentage breakdown for printing (top 5 only)
|
50 |
+
formatted_percentage = '\n'.join([f"{disease.replace('___', ' Leaf - ').replace('_', ' ').capitalize()}: {percentage}%" for disease, percentage in top_5_percentage.items()])
|
51 |
+
|
52 |
+
# Include the message in the leaf_category textbox if leaf identification fails
|
53 |
+
if disease_name == "Not applicable":
|
54 |
+
leaf_category = "Unfortunately, we are having trouble to identifying a singular leaf in this image. \n(try uploading a different image with your leaf on a white backgound)"
|
55 |
+
formatted_percentage = "Sorry we could not figure it out :("
|
56 |
+
|
57 |
|
58 |
return leaf_category.capitalize(), disease_name.replace('_', ' ').capitalize(), formatted_percentage
|
59 |
|
|
|
62 |
fn=predict,
|
63 |
inputs=gr.Image(label="Upload the Image"),
|
64 |
outputs=[
|
65 |
+
gr.Textbox(label="Leaf Type:"), # Modified label to include the message
|
66 |
gr.Textbox(label="Identified Disease:"),
|
67 |
+
gr.Textbox(label="Percentage Breakdown (top 5):")
|
68 |
],
|
69 |
title="Plant Disease Identifier",
|
70 |
description="Quick Tip: If the image results do not match, consider taking a picture of the leaf against a clean white background for improved accuracy and better identification of plant diseases."
|
71 |
)
|
72 |
|
|
|
73 |
interface.launch(debug=True) # Start server and launch the UI
|