dielz commited on
Commit
c1aa647
·
verified ·
1 Parent(s): ed34f02

back to old code

Browse files
Files changed (1) hide show
  1. app.py +47 -84
app.py CHANGED
@@ -5,8 +5,6 @@ from PIL import Image
5
  import google.generativeai as genai
6
  import os
7
  import markdown2
8
- import cv2
9
- import matplotlib.cm as cm
10
 
11
  # Load the TensorFlow model
12
  model_path = 'model'
@@ -29,99 +27,64 @@ def get_disease_detail(disease_name):
29
  f"The diagnosis is {disease_name}, what are your recommendations?"
30
  )
31
  try:
32
- response = genai.GenerativeModel("gemini-1.5-flash").generate_content(prompt)
33
  return markdown2.markdown(response.text.strip())
34
  except Exception as e:
35
  return f"Error: {e}"
36
 
37
- def generate_gradcam(model, img_array, layer_name, class_index):
38
- grad_model = tf.keras.models.Model(
39
- inputs=[model.inputs],
40
- outputs=[model.get_layer(layer_name).output, model.output],
41
- )
42
-
43
- with tf.GradientTape() as tape:
44
- conv_outputs, predictions = grad_model(img_array)
45
- loss = predictions[:, class_index]
46
-
47
- grads = tape.gradient(loss, conv_outputs)
48
- pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
49
-
50
- heatmap = tf.matmul(conv_outputs, tf.expand_dims(pooled_grads, axis=-1))[0]
51
- heatmap = tf.reduce_mean(heatmap, axis=-1)
52
-
53
- heatmap = np.maximum(heatmap, 0) / np.max(heatmap)
54
- heatmap = cv2.resize(heatmap, (img_array.shape [1], img_array.shape [0]))
55
- heatmap = np.uint8(255 * heatmap)
56
- heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
57
-
58
- return heatmap
59
-
60
- def overlay_gradcam(img, heatmap, alpha=0.4):
61
- img = img.astype('uint8')
62
- resized_heatmap = cv2.resize(heatmap, (img.shape [1], img.shape [0]))
63
- overlay = cv2.addWeighted(img, 1 - alpha, resized_heatmap, alpha, 0)
64
- return overlay
65
-
66
- def predict_and_visualize(image):
67
  image_resized = image.resize((224, 224))
68
  image_array = np.array(image_resized).astype(np.float32) / 255.0
69
- image_array_expanded = np.expand_dims(image_array, axis=0)
70
-
71
- predictions = model.signatures['serving_default'](tf.convert_to_tensor(image_array_expanded, dtype=tf.float32))['output_0']
72
 
 
73
  top_index = np.argmax(predictions.numpy(), axis=1)[0]
74
  top_label = labels[top_index]
75
  top_probability = predictions.numpy()[0][top_index]
76
- prediction_dict = {top_label: top_probability}
77
-
78
- explanation_html = get_disease_detail(top_label)
79
-
80
- last_conv_layer_name = 'top_activation'
81
- try:
82
- heatmap = generate_gradcam(model, image_array_expanded, last_conv_layer_name, top_index)
83
- gradcam_overlay = overlay_gradcam(np.array(image), heatmap) # Overlay ke gambar asli, bukan yg di-resize
84
- gradcam_image = Image.fromarray(cv2.cvtColor(gradcam_overlay, cv2.COLOR_BGR2RGB))
85
- except Exception as e:
86
- print(f"Error saat membuat Grad-CAM: {e}")
87
- gradcam_image = image
88
-
89
- return gradcam_image, prediction_dict, explanation_html
90
 
91
- css = """.scrollable-html { height: 300px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; box-sizing: border-box; }"""
92
-
93
- with gr.Blocks(css=css) as demo:
94
- gr.Markdown("<h1>Eye Diseases Classifier</h1>")
95
- gr.Markdown(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  "Upload an image of an eye fundus, and the model will predict it.\n\n"
97
  "**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."
98
- )
99
-
100
- with gr.Row():
101
- with gr.Column(scale=1):
102
- image_input = gr.Image(type="pil", label="Upload Eye Fundus Image")
103
- detect_btn = gr.Button("Detect Disease", variant="primary")
104
- gr.Examples(
105
- examples=[
106
- ["exp_eye_images/0_right_h.png"],
107
- ["exp_eye_images/03fd50da928d_dr.png"],
108
- ["exp_eye_images/108_right_h.png"],
109
- ["exp_eye_images/1062_right_c.png"],
110
- ["exp_eye_images/1084_right_c.png"],
111
- ["exp_eye_images/image_1002_g.jpg"]
112
- ],
113
- inputs=image_input
114
- )
115
-
116
- with gr.Column(scale=1):
117
- gradcam_output = gr.Image(label="Grad-CAM Visualization")
118
- prediction_output = gr.Label(num_top_classes=1, label="Prediction")
119
- explanation_output = gr.Markdown(label="Explanation", elem_classes=["scrollable-html"])
120
-
121
- detect_btn.click(
122
- fn=predict_and_visualize,
123
- inputs=image_input,
124
- outputs=[gradcam_output, prediction_output, explanation_output]
125
- )
126
 
127
- demo.launch(share=True)
 
5
  import google.generativeai as genai
6
  import os
7
  import markdown2
 
 
8
 
9
  # Load the TensorFlow model
10
  model_path = 'model'
 
27
  f"The diagnosis is {disease_name}, what are your recommendations?"
28
  )
29
  try:
30
+ response = genai.GenerativeModel("gemini-2.5-flash").generate_content(prompt)
31
  return markdown2.markdown(response.text.strip())
32
  except Exception as e:
33
  return f"Error: {e}"
34
 
35
+ def predict_image(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  image_resized = image.resize((224, 224))
37
  image_array = np.array(image_resized).astype(np.float32) / 255.0
38
+ image_array = np.expand_dims(image_array, axis=0)
39
+
40
+ predictions = model.signatures['serving_default'](tf.convert_to_tensor(image_array, dtype=tf.float32))['output_0']
41
 
42
+ # Highest prediction
43
  top_index = np.argmax(predictions.numpy(), axis=1)[0]
44
  top_label = labels[top_index]
45
  top_probability = predictions.numpy()[0][top_index]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ explanation = get_disease_detail(top_label)
48
+
49
+ return {top_label: top_probability}, explanation
50
+
51
+ # Example images
52
+ example_images = [
53
+ ["exp_eye_images/0_right_h.png"],
54
+ ["exp_eye_images/03fd50da928d_dr.png"],
55
+ ["exp_eye_images/108_right_h.png"],
56
+ ["exp_eye_images/1062_right_c.png"],
57
+ ["exp_eye_images/1084_right_c.png"],
58
+ ["exp_eye_images/image_1002_g.jpg"]
59
+ ]
60
+
61
+ # Custom CSS for HTML height
62
+ css = """
63
+ .scrollable-html {
64
+ height: 206px;
65
+ overflow-y: auto;
66
+ border: 1px solid #ccc;
67
+ padding: 10px;
68
+ box-sizing: border-box;
69
+ }
70
+ """
71
+
72
+ # Gradio Interface
73
+ interface = gr.Interface(
74
+ fn=predict_image,
75
+ inputs=gr.Image(type="pil"),
76
+ outputs=[
77
+ gr.Label(num_top_classes=1, label="Prediction"),
78
+ gr.Markdown(label="Explanation", elem_classes=["scrollable-html"])
79
+ ],
80
+ examples=example_images,
81
+ title="Eye Diseases Classifier",
82
+ description=(
83
  "Upload an image of an eye fundus, and the model will predict it.\n\n"
84
  "**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."
85
+ ),
86
+ allow_flagging="never",
87
+ css=css
88
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
+ interface.launch(share=True)