Spaces:
Sleeping
Sleeping
Add app and model scripts
Browse files
app.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from model import predict
|
| 3 |
+
|
| 4 |
+
iface = gr.Interface(
|
| 5 |
+
fn=predict,
|
| 6 |
+
inputs=gr.Image(type="pil"),
|
| 7 |
+
outputs=gr.Label(num_top_classes=1),
|
| 8 |
+
title="Plant Disease Classifier",
|
| 9 |
+
description="Upload a leaf image and the model predicts the disease."
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
if __name__ == "__main__":
|
| 13 |
+
iface.launch()
|
model.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from tensorflow.keras.applications import MobileNetV2
|
| 2 |
+
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense
|
| 3 |
+
from tensorflow.keras.models import Model
|
| 4 |
+
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image # <-- Add this import
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def build_model(num_classes):
|
| 11 |
+
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
| 12 |
+
base_model.trainable = False
|
| 13 |
+
|
| 14 |
+
x = base_model.output
|
| 15 |
+
x = GlobalAveragePooling2D()(x)
|
| 16 |
+
x = Dense(128, activation='relu')(x)
|
| 17 |
+
output = Dense(num_classes, activation='softmax')(x)
|
| 18 |
+
|
| 19 |
+
model = Model(inputs=base_model.input, outputs=output)
|
| 20 |
+
return model
|
| 21 |
+
|
| 22 |
+
def predict(image: Image.Image):
|
| 23 |
+
"""
|
| 24 |
+
Predict the class of a given PIL image.
|
| 25 |
+
"""
|
| 26 |
+
image = image.resize((224, 224))
|
| 27 |
+
img_array = np.array(image) / 255.0
|
| 28 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 29 |
+
|
| 30 |
+
predictions = model.predict(img_array)
|
| 31 |
+
predicted_class = class_names[np.argmax(predictions)]
|
| 32 |
+
confidence = float(np.max(predictions))
|
| 33 |
+
|
| 34 |
+
return {predicted_class: confidence}
|