Muhammad Abdiel Al Hafiz
commited on
Commit
·
d566fee
1
Parent(s):
54fea9e
building the interface
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
model_path = 'model'
|
| 7 |
+
model = tf.saved_model.load(model_path)
|
| 8 |
+
|
| 9 |
+
labels = ['cataract', 'diabetic_retinopathy', 'glaucoma', 'normal']
|
| 10 |
+
|
| 11 |
+
def predict_image(image):
|
| 12 |
+
image_resized = image.resize((224, 224))
|
| 13 |
+
image_array = np.array(image_resized).astype(np.float32) / 255.0
|
| 14 |
+
image_array = np.expand_dims(image_array, axis=0)
|
| 15 |
+
|
| 16 |
+
predictions = model.signatures['serving_default'](tf.convert_to_tensor(image_array, dtype=tf.float32))['output_0']
|
| 17 |
+
|
| 18 |
+
# Highest prediction
|
| 19 |
+
top_index = np.argmax(predictions.numpy(), axis=1)[0]
|
| 20 |
+
top_label = labels[top_index]
|
| 21 |
+
top_probability = predictions.numpy()[0][top_index]
|
| 22 |
+
|
| 23 |
+
return {top_label:top_probability}
|
| 24 |
+
|
| 25 |
+
# Example images
|
| 26 |
+
example_images = [
|
| 27 |
+
["exp_eye_images/0_right_h.png"],
|
| 28 |
+
["exp_eye_images/03fd50da928d_dr"],
|
| 29 |
+
["exp_eye_images/108_right_h"],
|
| 30 |
+
["exp_eye_images/1062_right_c"],
|
| 31 |
+
["exp_eye_images/1084_right_c"],
|
| 32 |
+
["exp_eye_images/image_1002_g"]
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
# Gradio Interface
|
| 36 |
+
interface = gr.Interface(
|
| 37 |
+
fn=predict_image,
|
| 38 |
+
inputs=gr.Image(type="pil"),
|
| 39 |
+
outputs=gr.Label(num_top_classes=1, label="Prediction"),
|
| 40 |
+
examples=example_images,
|
| 41 |
+
title="Eye Diseases Classifier",
|
| 42 |
+
description="Upload an image of an eye fundus, and the model will predict it.\n\n**Disclaimer:** This model is intended as a form of learning process in the field of health-related machine learning and was trained with a limited amount and variety of data with a total of about 4000 data, so the prediction results may not always be correct. There is still a lot of room for improvisation on this model in the future.",
|
| 43 |
+
allow_flagging="never"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
interface.launch(share=True)
|