AbdulmalikAdeyemo commited on
Commit
fe493d5
·
1 Parent(s): be936ad

update app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -2
app.py CHANGED
@@ -1,4 +1,170 @@
 
 
 
 
 
 
 
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #-----Make all the Necessary imports-----
2
+
3
+ from fastai.vision import *
4
+ from fastai.imports import *
5
+ from fastai.learner import *
6
+ from fastai.vision.all import *
7
+
8
  import streamlit as st
9
+ import numpy as np
10
+ import matplotlib.image as mpimg
11
+ import os
12
+ import time
13
+ from PIL import Image
14
+ import requests
15
+ from io import BytesIO
16
+ import pathlib
17
+
18
+
19
+ # st.set_page_config(layout="wide")
20
+
21
+
22
+ #for windows deployment
23
+ # temp = pathlib.PosixPath
24
+ # pathlib.PosixPath = pathlib.WindowsPath
25
+
26
+
27
+ #For linux deployment
28
+ plt = platform.system()
29
+ if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath
30
+
31
+
32
+ path = Path('.')
33
+
34
+ with open('style.css') as f:
35
+ st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
36
+
37
+
38
+
39
+ #------Create/Define all functions
40
+
41
+ def get_x(r):
42
+ return image_path/r['train_image_name']
43
+
44
+ def get_y(r):
45
+ return r['class']
46
+
47
+
48
+ def load_model():
49
+ model = load_learner(path/'binary_model.pkl')
50
+ return model
51
+
52
+ model = load_model()
53
+
54
+ def display_image(display_img):
55
+ st.image(display_img, width=400)
56
+ # use_column_width=True
57
+
58
+
59
+ def make_pred(model, img):
60
+
61
+ # Temporarily displays a message while executing
62
+ with st.spinner('Performing Diagnosis...'):
63
+ time.sleep(1)
64
+
65
+ pred, pred_idx, prob = model.predict(img)
66
+ pred_prob = f'{prob[pred_idx]*100:.0f}%'
67
+
68
+ # Display the prediction
69
+ if pred == '1':
70
+ pred_state = 'Presence of Diabetic Retinopathy'
71
+ # st.success("Presence of Diabetic Retinopathy.")
72
+
73
+ if prob[pred_idx]*100 <= 85.0:
74
+ rec_action = 'Please visit an Opthalmologist to confirm your Daignosis'
75
+ else:
76
+ rec_action = 'Please visit an Opthalmologist for prompt treatment.'
77
+
78
+
79
+ else:
80
+ pred_state = 'Absence of Diabetic Retinopathy'
81
+ # st.success("Absence of Diabetic Retinopathy.")
82
+
83
+ if prob[pred_idx]*100 <= 85.0:
84
+ rec_action = 'Please visit an Opthalmologist to confirm your Daignosis'
85
+ else:
86
+ rec_action = 'Please be sure to go for Diagnosis once a year.'
87
+
88
+ return pred_state, rec_action, pred_prob
89
+
90
+
91
+ ########--------Setup Diagnosis Page--------########
92
+
93
+ # if selected_nav == 'Diagnosis':
94
+
95
+ ########-------Create Side Bar---------########
96
+
97
+ #For image upload
98
+ img_upload = st.sidebar.file_uploader(label = 'Upload a Fundus Image for Diagnosis',
99
+ type=['png', 'jpg', 'jpeg'])
100
+
101
+ # For image selection
102
+ test_images = os.listdir(path/'sample')
103
+ img_selected = st.sidebar.selectbox(
104
+ 'Please Select a Sample Fundus Image:', test_images)
105
+
106
+
107
+ if img_selected:
108
+ # Read the image
109
+ file_path = path/'sample'/img_selected
110
+ # Get the image to display
111
+ display_img = Image.open(file_path)
112
+ # display_img = display_img.resize((244,224))
113
+ img = PILImage.create(file_path)
114
+
115
+
116
+ if img_upload:
117
+ display_img = Image.open(img_upload)
118
+ img = PILImage.create(img_upload)
119
+
120
+
121
+
122
+
123
+ st.markdown("""
124
+ <h3 style="text-align:center;color:#006ef5;">Diagnosing Diabetic Retinopathy with AI (DEMO)</h3>
125
+ """, unsafe_allow_html=True)
126
+
127
+ st.markdown("##")
128
+
129
+ st.markdown("""
130
+ <p> <b>Instruction:</b> Please upload a fundus image (using the sidebar) for diagnosis or select a sample image</p>
131
+ """, unsafe_allow_html=True)
132
+
133
+ with st.container():
134
+
135
+ left_col, right_col = st.columns((2,1))
136
+
137
+ with left_col:
138
+
139
+ display_image(display_img)
140
+
141
+ with right_col:
142
+
143
+ pred_state, rec_action, pred_prob = make_pred(model, img)
144
+
145
+ # st.markdown("##")
146
 
147
+ if pred_state=="Presence of Diabetic Retinopathy":
148
+ st.markdown(f"""
149
+ <p style="text-align:left;"> <b>Diagnosis Result:</b></p> <br>
150
+ <p style="color:red;text-align:left;font-size:16px;">{pred_state}</p>
151
+ """
152
+ , unsafe_allow_html=True)
153
+
154
+ else:
155
+ st.markdown(f"""
156
+ <p style="text-align:left;"> <b>Diagnosis Result:</b></p>
157
+ <p style="color:green;text-align:left;font-size:16px;">{pred_state}</p>
158
+ """
159
+ , unsafe_allow_html=True)
160
+
161
+ st.markdown(f"""
162
+ <p style="text-align:left;"> <b>Diagnosis Confidence:</b></b></p>
163
+ <p style="color:#006ef5;text-align:left;font-size:16px;">{pred_prob}</p>""", unsafe_allow_html=True)
164
+
165
+ # st.markdown(f" **Diagnosis Confidence:** {pred_prob}")
166
+ st.markdown(f"""
167
+ <p style="text-align:left;"> <b>Recommendaton:</b></p>
168
+ <p style="color:#006ef5;text-align:left;font-size:16px;">{rec_action}</p>
169
+
170
+ """, unsafe_allow_html=True)