kushagrasharma-13 commited on
Commit
b2ed1fb
·
verified ·
1 Parent(s): a78b0d1

Upload 6 files

Browse files
Files changed (6) hide show
  1. .gitignore +2 -0
  2. credentials.py +2 -0
  3. medical-app.py +234 -0
  4. mri_of_brain.jpg +0 -0
  5. requirements.txt +12 -0
  6. utils.py +28 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ /credentials.py
2
+ /old_data
credentials.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ mongo_url = "mongodb+srv://kushagrasharma_13:[email protected]/?retryWrites=true&w=majority&appName=Cluster0"
2
+ password = "Kushagra@13"
medical-app.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import cv2
3
+ import time
4
+ import bcrypt
5
+ import numpy as np
6
+ import streamlit as st
7
+ import tensorflow as tf
8
+ from pymongo import MongoClient
9
+ from credentials import mongo_url
10
+
11
+ @st.cache
12
+ def load_data():
13
+ client = MongoClient(mongo_url)
14
+ db = client['medical-app-auth']
15
+ collection = db['login-data']
16
+ brain_tumor_model = tf.keras.models.load_model('models/brain_tumor.h5')
17
+ alzheimer_model = tf.keras.models.load_model('models/alzheimer.h5')
18
+ return collection, brain_tumor_model, alzheimer_model
19
+
20
+ collection, brain_tumor_model, alzheimer_model = load_data()
21
+
22
+ if 'current_page' not in st.session_state:
23
+ st.session_state.current_user = None
24
+ st.session_state.current_page = 'login'
25
+
26
+ def clear_cache():
27
+ keys = list(st.session_state.keys())
28
+ for key in keys:
29
+ st.session_state.pop(key)
30
+
31
+ def login():
32
+ st.set_page_config(layout='centered', page_title="Brain MRI", page_icon="mri_of_brain.jpg")
33
+ col1, col2 = st.columns([5,1])
34
+ with col2:
35
+ if st.button("Register", use_container_width=True):
36
+ st.session_state.current_page = 'register'
37
+ st.rerun()
38
+
39
+ def reset_passowrd_input():
40
+ st.session_state.password = ""
41
+
42
+ def reset_username_inputs():
43
+ st.session_state.username = ""
44
+ reset_passowrd_input()
45
+
46
+ with st.form(key='login', clear_on_submit=True):
47
+ st.subheader("Login")
48
+
49
+ username = st.text_input("Username", placeholder="Enter Username")
50
+ password = st.text_input("Enter Password", type="password")
51
+ username = username.lower().strip()
52
+
53
+ user = collection.find_one({"username":username})
54
+
55
+ submit = st.form_submit_button("Login")
56
+ if submit:
57
+ with st.spinner('Checking credentials...'):
58
+ if user==None:
59
+ st.warning("Username is does not exits, please register")
60
+ reset_username_inputs()
61
+ elif bcrypt.checkpw(password.encode('utf-8'), user.get("password")):
62
+ st.warning("**Credential Matched**: Redirecting...")
63
+ st.session_state.current_user = username
64
+ st.session_state.current_page = 'medical'
65
+ st.rerun()
66
+ else:
67
+ st.warning("Password is incorrect")
68
+ reset_passowrd_input()
69
+
70
+ def register():
71
+ st.set_page_config(layout='centered', page_title="Brain MRI", page_icon="mri_of_brain.jpg")
72
+ col1, col2 = st.columns([5,1])
73
+ with col2:
74
+ if st.button("Login", use_container_width=True):
75
+ st.session_state.current_page = 'login'
76
+ st.rerun()
77
+
78
+ document = {}
79
+ pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
80
+
81
+ def reset_password_inputs():
82
+ st.session_state.password1 = ""
83
+ st.session_state.password2 = ""
84
+
85
+ def reset_username_inputs():
86
+ st.session_state.username = ""
87
+ reset_password_inputs()
88
+
89
+ def reset_email_inputs():
90
+ st.session_state.email = ""
91
+ reset_username_inputs()
92
+
93
+ with st.form(key='register', clear_on_submit=True):
94
+ st.subheader("Register")
95
+
96
+ email = st.text_input("Email", placeholder="[email protected]", max_chars=64)
97
+ username = st.text_input("Username", placeholder="Enter Username", max_chars=17)
98
+ name = st.text_input("Name", placeholder="Enter Your Full Name")
99
+ password1 = st.text_input("Enter Password", type="password", max_chars=17)
100
+ password2 = st.text_input("Confirm Password", type="password", max_chars=17)
101
+
102
+ email = email.lower().strip()
103
+ username = username.lower().strip()
104
+ name = name.title().strip()
105
+
106
+ submit = st.form_submit_button("Register")
107
+ if submit:
108
+ with st.spinner('Checking credentials...'):
109
+ if re.match(pattern, email)==None:
110
+ st.warning(":red[Please provide a valid email.]", icon="⚠️")
111
+ reset_email_inputs()
112
+ elif collection.find_one({"email":email}):
113
+ st.warning("Email already exits, please Login!")
114
+ reset_email_inputs()
115
+ elif not name.replace(" ", "").isalpha():
116
+ st.warning("Don't use number or special characters for name")
117
+ st.session_state.name = ""
118
+ elif len(username)<5:
119
+ st.warning("Username must atleast be of 5 characters")
120
+ reset_username_inputs()
121
+ elif len(password1)<6:
122
+ st.warning("Password must atleast be of 6 characters")
123
+ reset_password_inputs()
124
+ elif collection.find_one({"username": username}):
125
+ st.warning("Username already exits, please try a different one.")
126
+ reset_username_inputs()
127
+ elif password1 != password2:
128
+ st.warning(":red[Passwords do not match. Please try again.]", icon="⚠️")
129
+ reset_password_inputs()
130
+ elif not (email and name and username and password1 and password2):
131
+ st.warning(":red[Please complete all the fields above.]", icon="⚠️")
132
+ else:
133
+ salt = bcrypt.gensalt(rounds=13)
134
+ hashed_password = bcrypt.hashpw(password1.encode('utf-8'), salt)
135
+ salt = bcrypt.gensalt(rounds=13)
136
+ hashed_password = bcrypt.hashpw(password1.encode('utf-8'), salt)
137
+ document = {"name":name, "username":username, "email":email, "password":hashed_password, "salt":salt}
138
+ collection.insert_one(document)
139
+ st.warning("**Successfully Registered**: Redirecting...")
140
+ st.session_state.current_user = username
141
+ st.session_state.current_page = 'medical'
142
+ st.rerun()
143
+
144
+
145
+
146
+ def medical_page():
147
+ def alzheimer():
148
+ col1, col2, col3 = st.columns([6, 6, 1])
149
+ with col1:
150
+ if st.button(f"Welcome! {st.session_state.current_user}"):
151
+ st.rerun()
152
+ with col3:
153
+ st.button("Logout", use_container_width=True, on_click=clear_cache)
154
+
155
+ st.markdown("***")
156
+ st.subheader("Here's your Alzheimer's Scan")
157
+
158
+ uploaded_file = st.file_uploader("Upload MRI scan image for detecting alzheimer's", type=['png', 'jpg'])
159
+ if uploaded_file is not None:
160
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
161
+ opencv_image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
162
+ resized_img = cv2.resize(opencv_image, (208, 176))
163
+ resized_img = resized_img[np.newaxis, ...]
164
+ class_names = ['ModerateDemented', 'NonDemented', 'VeryMildDemented', 'MildDemented']
165
+
166
+ pred = class_names[np.argmax(st.session_state.alzheimer_model.predict(resized_img))]
167
+ st.text(pred)
168
+
169
+ def brain_tumor():
170
+ col1, col2, col3 = st.columns([6, 6, 1])
171
+ with col1:
172
+ if st.button(f"Welcome! {st.session_state.current_user}"):
173
+ st.rerun()
174
+ with col3:
175
+ st.button("Logout", use_container_width=True, on_click=clear_cache)
176
+
177
+ st.markdown("***")
178
+ st.subheader("Here's your Brain Tumor Scan")
179
+
180
+ uploaded_file = st.file_uploader("Upload MRI scan image for detecting Brain Tumor", type=['png', 'jpg'])
181
+ if uploaded_file is not None:
182
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
183
+ opencv_image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
184
+ resized_img = cv2.resize(opencv_image, (168, 150))
185
+ resized_img = resized_img[np.newaxis, ...]
186
+ class_names = ['Pituitary', 'No-Tumor', 'Meningioma', 'Glioma']
187
+
188
+ pred = class_names[np.argmax(st.session_state.brain_tumor_model.predict(resized_img))]
189
+ st.text(pred)
190
+
191
+ def abscesses():
192
+ col1, col2, col3 = st.columns([6, 6, 1])
193
+ with col1:
194
+ if st.button(f"Welcome! {st.session_state.current_user}"):
195
+ st.rerun()
196
+ with col3:
197
+ st.button("Logout", use_container_width=True, on_click=clear_cache)
198
+ st.markdown("***")
199
+ st.subheader("Here's your Abscesses Scan")
200
+ uploaded_file = st.file_uploader("Upload MRI scan image for detecting Abscesses", type=['png', 'jpg'], disabled=True)
201
+ st.write("Feature currently unavailable")
202
+ if uploaded_file is not None:
203
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
204
+ opencv_image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
205
+ resized_img = cv2.resize(opencv_image, (208, 176))
206
+ resized_img = resized_img[np.newaxis, ...]
207
+ class_names = ['ModerateDemented', 'NonDemented', 'VeryMildDemented', 'MildDemented']
208
+ pred = class_names[np.argmax(abscesses_model.predict(resized_img))]
209
+ st.text(pred)
210
+ st.set_page_config(layout='wide', page_title="Brain MRI", page_icon="mri_of_brain.jpg")
211
+ st.sidebar.image("mri_of_brain.jpg")
212
+ st.sidebar.title("Navigation")
213
+ page_options = ["Alzheimer", "Brain Tumor", "Abscesses"]
214
+ selected_page = st.sidebar.selectbox("Select a Scan", page_options)
215
+
216
+ if selected_page == "Alzheimer":
217
+ alzheimer()
218
+ elif selected_page == "Brain Tumor":
219
+ brain_tumor()
220
+ elif selected_page == "Abscesses":
221
+ abscesses()
222
+
223
+
224
+ def main():
225
+ if st.session_state.current_page == 'login':
226
+ login()
227
+ elif st.session_state.current_page == 'register':
228
+ register()
229
+ elif st.session_state.current_page == 'medical':
230
+ medical_page()
231
+
232
+ if __name__ == "__main__":
233
+
234
+ main()
mri_of_brain.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pip==24.0
2
+ numpy==1.26.4
3
+ keras==2.15.0
4
+ altair==5.2.0
5
+ pandas==2.2.1
6
+ pydeck==0.8.0
7
+ streamlit==1.32.0
8
+ tensorflow==2.15.0
9
+ opencv-python-headless==4.9.0.80
10
+ bcrypt==4.1.2
11
+ pymongo==3.11.0
12
+ dnspython==1.16.0
utils.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022)
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import inspect
16
+ import textwrap
17
+
18
+ import streamlit as st
19
+
20
+
21
+ def show_code(demo):
22
+ """Showing the code of the demo."""
23
+ show_code = st.sidebar.checkbox("Show code", True)
24
+ if show_code:
25
+ # Showing the code of the demo.
26
+ st.markdown("## Code")
27
+ sourcelines, _ = inspect.getsourcelines(demo)
28
+ st.code(textwrap.dedent("".join(sourcelines[1:])))