Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,55 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
-
#
|
| 7 |
-
model_url = "https://huggingface.co/chimithecat/penyakit_tomat/resolve/main/Tomato_Models.h5"
|
| 8 |
-
model_path = tf.keras.utils.get_file("Tomato_Models.h5", model_url)
|
| 9 |
-
model = tf.keras.models.load_model(model_path)
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
class_names = [
|
| 13 |
-
"Bacterial Spot", "Early Blight", "Healthy", "Late Blight"
|
| 14 |
-
]
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load your Keras model from Hugging Face
|
| 7 |
+
model_url = "https://huggingface.co/chimithecat/penyakit_tomat/resolve/main/Tomato_Models.h5"
|
| 8 |
+
model_path = tf.keras.utils.get_file("Tomato_Models.h5", model_url)
|
| 9 |
+
model = tf.keras.models.load_model(model_path)
|
| 10 |
+
|
| 11 |
+
# Customize your label names here based on your model's training
|
| 12 |
+
class_names = [
|
| 13 |
+
"Bacterial Spot", "Early Blight", "Healthy", "Late Blight"
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
# Predict function for a single image
|
| 17 |
+
def classify(image):
|
| 18 |
+
if image is None:
|
| 19 |
+
return None
|
| 20 |
+
if not isinstance(image, Image.Image):
|
| 21 |
+
image = Image.fromarray(image)
|
| 22 |
+
|
| 23 |
+
image = image.resize((224, 224)) # Resize to expected input
|
| 24 |
+
img_array = np.array(image) / 255.0
|
| 25 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 26 |
+
|
| 27 |
+
predictions = model.predict(img_array)[0]
|
| 28 |
+
confidences = {class_names[i]: float(predictions[i]) for i in range(len(class_names))}
|
| 29 |
+
return confidences
|
| 30 |
+
|
| 31 |
+
# Build the UI using Blocks
|
| 32 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 33 |
+
gr.Markdown(
|
| 34 |
+
"""
|
| 35 |
+
# Analisis Penyakit Tanaman 🍅
|
| 36 |
+
Unggah gambar daun tanaman untuk mengidentifikasi potensi penyakit.
|
| 37 |
+
"""
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
with gr.Column():
|
| 42 |
+
image_input = gr.Image(type="pil", label="Unggah Gambar Daun")
|
| 43 |
+
submit_button = gr.Button("Analisis Gambar", variant="primary")
|
| 44 |
+
|
| 45 |
+
with gr.Column():
|
| 46 |
+
label_output = gr.Label(label="Hasil Analisis")
|
| 47 |
+
|
| 48 |
+
submit_button.click(
|
| 49 |
+
fn=classify,
|
| 50 |
+
inputs=image_input,
|
| 51 |
+
outputs=label_output,
|
| 52 |
+
api_name="predict"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
app.launch(show_api=True)
|