Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,59 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pathlib
|
| 3 |
-
import random
|
| 4 |
-
import tensorflow as tf
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from timeit import default_timer as timer
|
| 7 |
-
from keras.models import load_model
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pathlib
|
| 3 |
+
import random
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from timeit import default_timer as timer
|
| 7 |
+
from keras.models import load_model
|
| 8 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 9 |
+
|
| 10 |
+
# Load the model
|
| 11 |
+
model = load_model('MyResNet101Model_final.keras')
|
| 12 |
+
|
| 13 |
+
# Define label mappings based on your dataset
|
| 14 |
+
label2id = {'Hispa': 0, 'Bercak Coklat': 1, 'Blast Daun': 2, 'Sehat': 3}
|
| 15 |
+
class_names = list(label2id.keys())
|
| 16 |
+
|
| 17 |
+
# Function to predict an image
|
| 18 |
+
def predict(img):
|
| 19 |
+
start = timer()
|
| 20 |
+
|
| 21 |
+
img = img.resize((224, 224)) # Ensure size matches the model's expected input
|
| 22 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
| 23 |
+
img_array = tf.expand_dims(img_array, 0) # Create batch dimension
|
| 24 |
+
|
| 25 |
+
# Normalize image
|
| 26 |
+
img_array /= 255.0
|
| 27 |
+
|
| 28 |
+
# Prediction
|
| 29 |
+
predictions = model.predict(img_array)
|
| 30 |
+
pred_prob = tf.nn.softmax(predictions[0])
|
| 31 |
+
pred_dict = {class_names[i]: float(pred_prob[i]) for i in range(len(class_names))}
|
| 32 |
+
|
| 33 |
+
pred_time = round(timer() - start, 5)
|
| 34 |
+
|
| 35 |
+
return pred_dict, pred_time
|
| 36 |
+
|
| 37 |
+
# Example images for demonstration
|
| 38 |
+
example_paths = list(pathlib.Path('examples').glob("*/*.jpg"))
|
| 39 |
+
example_list = [[str(filepath)] for filepath in random.sample(example_paths, k=4)]
|
| 40 |
+
|
| 41 |
+
# Set up the Gradio interface
|
| 42 |
+
title = 'Klasifikasi Penyakit Daun Padi'
|
| 43 |
+
description = 'Upload gambar daun padi untuk mengklasifikasikan penyakitnya.'
|
| 44 |
+
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
fn=predict,
|
| 47 |
+
inputs=gr.Image(type='pil'),
|
| 48 |
+
outputs=[
|
| 49 |
+
gr.Label(num_top_classes=4, label='Prediksi'),
|
| 50 |
+
gr.Number(label="Waktu prediksi (detik)")
|
| 51 |
+
],
|
| 52 |
+
description=description,
|
| 53 |
+
title=title,
|
| 54 |
+
allow_flagging='never',
|
| 55 |
+
examples=example_list
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
demo.launch(debug=True)
|