Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,15 @@ import numpy as np
|
|
| 6 |
import zipfile
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
# π
|
| 10 |
if not os.path.exists("examples") and os.path.exists("examples.zip"):
|
| 11 |
with zipfile.ZipFile("examples.zip", 'r') as zip_ref:
|
| 12 |
zip_ref.extractall("examples")
|
| 13 |
|
| 14 |
-
# π§ Load
|
| 15 |
model = tf.keras.models.load_model("efficientnet_final_model.keras")
|
| 16 |
|
| 17 |
-
# π
|
| 18 |
CLASS_NAMES = [
|
| 19 |
"Pomegranate__diseased", "mango_Sooty Mould", "mango_Powdery Mildew",
|
| 20 |
"mango_Healthy", "mango_Gall Midge", "mango_Die Back",
|
|
@@ -31,12 +31,10 @@ def predict_disease(img):
|
|
| 31 |
img_array = image.img_to_array(img)
|
| 32 |
img_array = preprocess_input(img_array)
|
| 33 |
img_array = np.expand_dims(img_array, axis=0)
|
| 34 |
-
|
| 35 |
prediction = model.predict(img_array)[0]
|
| 36 |
top_idx = np.argmax(prediction)
|
| 37 |
confidence = prediction[top_idx] * 100
|
| 38 |
label = CLASS_NAMES[top_idx]
|
| 39 |
-
|
| 40 |
return f"{label} ({confidence:.2f}%)"
|
| 41 |
|
| 42 |
# ποΈ Gradio Interface
|
|
@@ -45,16 +43,23 @@ interface = gr.Interface(
|
|
| 45 |
inputs=gr.Image(type="pil"),
|
| 46 |
outputs="text",
|
| 47 |
title="πΏ Fruit Leaf Disease Classifier",
|
| 48 |
-
description="Upload a fruit or leaf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
examples=[
|
| 50 |
-
["examples/Phytopthora.jpg"],
|
| 51 |
-
["examples/
|
| 52 |
-
["examples/
|
| 53 |
-
["examples/
|
| 54 |
],
|
| 55 |
-
|
|
|
|
| 56 |
)
|
| 57 |
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
interface.launch()
|
| 60 |
|
|
|
|
| 6 |
import zipfile
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# π Automatically unzip example images if needed
|
| 10 |
if not os.path.exists("examples") and os.path.exists("examples.zip"):
|
| 11 |
with zipfile.ZipFile("examples.zip", 'r') as zip_ref:
|
| 12 |
zip_ref.extractall("examples")
|
| 13 |
|
| 14 |
+
# π§ Load model
|
| 15 |
model = tf.keras.models.load_model("efficientnet_final_model.keras")
|
| 16 |
|
| 17 |
+
# π Disease class labels
|
| 18 |
CLASS_NAMES = [
|
| 19 |
"Pomegranate__diseased", "mango_Sooty Mould", "mango_Powdery Mildew",
|
| 20 |
"mango_Healthy", "mango_Gall Midge", "mango_Die Back",
|
|
|
|
| 31 |
img_array = image.img_to_array(img)
|
| 32 |
img_array = preprocess_input(img_array)
|
| 33 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
| 34 |
prediction = model.predict(img_array)[0]
|
| 35 |
top_idx = np.argmax(prediction)
|
| 36 |
confidence = prediction[top_idx] * 100
|
| 37 |
label = CLASS_NAMES[top_idx]
|
|
|
|
| 38 |
return f"{label} ({confidence:.2f}%)"
|
| 39 |
|
| 40 |
# ποΈ Gradio Interface
|
|
|
|
| 43 |
inputs=gr.Image(type="pil"),
|
| 44 |
outputs="text",
|
| 45 |
title="πΏ Fruit Leaf Disease Classifier",
|
| 46 |
+
description="Upload an image of a fruit or leaf to detect its disease type.",
|
| 47 |
+
allow_flagging="never"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# πΌοΈ Show image examples using Gradio v4+ Examples class
|
| 51 |
+
interface.examples = gr.Examples(
|
| 52 |
examples=[
|
| 53 |
+
["examples/Phytopthora (97).jpg"],
|
| 54 |
+
["examples/Red Rust(60).jpg"],
|
| 55 |
+
["examples/20211231_162315 (Custom).jpg"],
|
| 56 |
+
["examples/0021_0060.jpg"]
|
| 57 |
],
|
| 58 |
+
inputs=interface.input_components,
|
| 59 |
+
cache_examples=False
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# π Launch the app
|
| 63 |
if __name__ == "__main__":
|
| 64 |
interface.launch()
|
| 65 |
|