Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,13 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
model = tf.keras.models.load_model("model.keras")
|
| 9 |
IMG_SIZE = (224, 224)
|
| 10 |
class_labels = ['Black Headed caterpillar', 'Eriophyid Mite Damage', 'Red Palm Weevil', 'Rhinoceros Beetle', 'White Grub', 'Whitefly']
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
# This is much cleaner than a long if/elif chain.
|
| 14 |
-
# You can easily add more pests and their information here.
|
| 15 |
info_data = {
|
| 16 |
"Black Headed caterpillar": {
|
| 17 |
"info": """
|
|
@@ -114,8 +112,8 @@ def predict(image):
|
|
| 114 |
predicted_class_index = np.argmax(predictions[0])
|
| 115 |
predicted_label = class_labels[predicted_class_index]
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
#
|
| 119 |
pest_info = info_data.get(predicted_label, {"info": "Info not found.", "management": "Management info not found."})
|
| 120 |
|
| 121 |
info = pest_info["info"]
|
|
@@ -126,7 +124,7 @@ def predict(image):
|
|
| 126 |
|
| 127 |
|
| 128 |
def about_text():
|
| 129 |
-
return "
|
| 130 |
|
| 131 |
def how_to():
|
| 132 |
return "How to use this app"
|
|
@@ -161,11 +159,11 @@ with gr.Blocks() as image_tab:
|
|
| 161 |
with gr.Row():
|
| 162 |
with gr.Column():
|
| 163 |
gr.Markdown("### Info")
|
| 164 |
-
#
|
| 165 |
info_output = gr.Markdown(label="info")
|
| 166 |
management_output = gr.Markdown(label="management")
|
| 167 |
|
| 168 |
-
# Link
|
| 169 |
classify_btn.click(
|
| 170 |
fn=predict,
|
| 171 |
inputs=image_input,
|
|
@@ -175,7 +173,7 @@ with gr.Blocks() as image_tab:
|
|
| 175 |
with gr.Blocks(title="CocoML") as demo:
|
| 176 |
gr.Markdown("# CocoML #")
|
| 177 |
gr.Markdown("This is a Machine learning based coconut pest classifier")
|
| 178 |
-
# Combine
|
| 179 |
gr.TabbedInterface(
|
| 180 |
[image_tab, how_to_tab, about_tab],
|
| 181 |
["Image Classifier", "How To", "About"]
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
|
| 7 |
+
# loaded only once when the app starts.
|
| 8 |
model = tf.keras.models.load_model("model.keras")
|
| 9 |
IMG_SIZE = (224, 224)
|
| 10 |
class_labels = ['Black Headed caterpillar', 'Eriophyid Mite Damage', 'Red Palm Weevil', 'Rhinoceros Beetle', 'White Grub', 'Whitefly']
|
| 11 |
|
| 12 |
+
# dictionary
|
|
|
|
|
|
|
| 13 |
info_data = {
|
| 14 |
"Black Headed caterpillar": {
|
| 15 |
"info": """
|
|
|
|
| 112 |
predicted_class_index = np.argmax(predictions[0])
|
| 113 |
predicted_label = class_labels[predicted_class_index]
|
| 114 |
|
| 115 |
+
# Retrieve the info and management text from the dictionary ---
|
| 116 |
+
#.get() to safely retrieve the data.
|
| 117 |
pest_info = info_data.get(predicted_label, {"info": "Info not found.", "management": "Management info not found."})
|
| 118 |
|
| 119 |
info = pest_info["info"]
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
def about_text():
|
| 127 |
+
return "Machine Learning based coconut pest classifier"
|
| 128 |
|
| 129 |
def how_to():
|
| 130 |
return "How to use this app"
|
|
|
|
| 159 |
with gr.Row():
|
| 160 |
with gr.Column():
|
| 161 |
gr.Markdown("### Info")
|
| 162 |
+
# gr.Markdown to display formatted text
|
| 163 |
info_output = gr.Markdown(label="info")
|
| 164 |
management_output = gr.Markdown(label="management")
|
| 165 |
|
| 166 |
+
# Link button to prediction function
|
| 167 |
classify_btn.click(
|
| 168 |
fn=predict,
|
| 169 |
inputs=image_input,
|
|
|
|
| 173 |
with gr.Blocks(title="CocoML") as demo:
|
| 174 |
gr.Markdown("# CocoML #")
|
| 175 |
gr.Markdown("This is a Machine learning based coconut pest classifier")
|
| 176 |
+
# Combine into a single tabbed interface
|
| 177 |
gr.TabbedInterface(
|
| 178 |
[image_tab, how_to_tab, about_tab],
|
| 179 |
["Image Classifier", "How To", "About"]
|