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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import en_ner_bc5cdr_md
|
2 |
+
import spacy
|
3 |
+
from spacy import displacy
|
4 |
+
import streamlit as st
|
5 |
+
import pandas as pd
|
6 |
+
import pickle
|
7 |
+
from flair.data import Sentence
|
8 |
+
from flair.models import SequenceTagger
|
9 |
+
import regex as re
|
10 |
+
|
11 |
+
#### Defining Functions
|
12 |
+
def listToString(s):
|
13 |
+
str1 = " ; "
|
14 |
+
return (str1.join(s))
|
15 |
+
|
16 |
+
### Defining List
|
17 |
+
lst_chk = ["PHI Masking","Disease & Drug Extraction","Question Answering"]
|
18 |
+
|
19 |
+
sent_lst = ["Doctor in OHIO helped the covid patients to recover quickly"
|
20 |
+
"The patient has committed suicide due to high stress",
|
21 |
+
"Indian Research scientist found covaxin and saved all people",
|
22 |
+
"Students are affected with covid and are expected to die soon",
|
23 |
+
"Having high fever and not able to wake up"]
|
24 |
+
|
25 |
+
st.set_page_config(page_title="My WebPage", page_icon=":hugs:")
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
hom =st.sidebar.radio("Text Predictions",["Home","Text Prediction Models","Feedback"])
|
30 |
+
|
31 |
+
|
32 |
+
### Main Code
|
33 |
+
if hom == "Home":
|
34 |
+
#colm1 , colm2, colm3, colm4, colm5 = st.columns(5)
|
35 |
+
#with colm5:
|
36 |
+
# st.image("data//Goofy.jpg", width = 100)
|
37 |
+
|
38 |
+
col1 , col2 = st.columns([0.8,3])
|
39 |
+
with col2:
|
40 |
+
st.title("GOOFY THE ASSIST :male-doctor:")
|
41 |
+
# :male-doctor:, :person_doing_cartwheel:
|
42 |
+
#with col3:
|
43 |
+
#st.write("[Gavs](https://www.gavstech.com/)")
|
44 |
+
st.caption("")
|
45 |
+
st.video("https://www.youtube.com/watch?v=_io1BzRwdWc")
|
46 |
+
|
47 |
+
|
48 |
+
if hom == "Text Prediction Models":
|
49 |
+
st.sidebar.caption("")
|
50 |
+
rad =st.sidebar.radio("Text Prediction Model",lst_chk)
|
51 |
+
|
52 |
+
##### PHI Masking
|
53 |
+
if rad == "PHI Masking":
|
54 |
+
|
55 |
+
phi_masking_input = ["DR. RAMANUJAM FROM AMAR LEELA HOSPITALS, HYDERABAD REQUIRES A PACK OF SURGICAL PROCEDURE COVER. MAIL RAMANUJAM at [email protected]",
|
56 |
+
"POLYDIOXANONE MONOFILAMENT SUTURE PRESCRIBED PATIENT NAME PHILIPS ADMITTED AT APEX HOSPITALS, JAIPUR",
|
57 |
+
"SURGICAL KIT PACKED DELIVERED TO ASIAN HEART INSTITUTE ON 12/11/21",
|
58 |
+
"FORMER PRESIDENT OF INDIA ZAKIR HUSAIN ADMITTED AT CITY HOSPITAL, DELHI DATED 03-MAY-1969",
|
59 |
+
"HOSPITAL RELATED QUERIES CAN BE SENT TO [email protected]"]
|
60 |
+
|
61 |
+
st.header("PHI Masking")
|
62 |
+
|
63 |
+
sb1 = st.radio("Choose Data",["Choose From Sample","Upload Data"],index = 0)
|
64 |
+
|
65 |
+
if sb1 == "Choose From Sample":
|
66 |
+
sb2 = st.selectbox("Select Input Data",phi_masking_input,index = 0)
|
67 |
+
ip_txt = sb2
|
68 |
+
if st.button("Clear"):
|
69 |
+
ip_txt = ""
|
70 |
+
txt_input1 = st.text_area("Your text input is ",ip_txt)
|
71 |
+
if st.button("Submit"):
|
72 |
+
|
73 |
+
##### Hugging Face Model
|
74 |
+
lst_org = []
|
75 |
+
lst_date = []
|
76 |
+
lst_person = []
|
77 |
+
lst_gpe = []
|
78 |
+
|
79 |
+
tagger = SequenceTagger.load("flair/ner-english-ontonotes-large")
|
80 |
+
sentence = Sentence(txt_input1)
|
81 |
+
tagger.predict(sentence)
|
82 |
+
|
83 |
+
out_txt = txt_input1
|
84 |
+
for entity in sentence.get_spans('ner'):
|
85 |
+
str_len = len(str(entity.text))
|
86 |
+
out_txt = out_txt.replace(str(entity.text), "*"*str_len)
|
87 |
+
if entity.tag == "ORG":
|
88 |
+
lst_org.append(entity.text)
|
89 |
+
elif entity.tag == "DATE":
|
90 |
+
lst_date.append(entity.text)
|
91 |
+
elif entity.tag == "PERSON":
|
92 |
+
lst_person.append(entity.text)
|
93 |
+
elif entity.tag == "GPE":
|
94 |
+
lst_gpe.append(entity.text)
|
95 |
+
|
96 |
+
lst_mail = re.findall("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", txt_input1)
|
97 |
+
for mail in lst_mail:
|
98 |
+
str_len = len(mail)
|
99 |
+
out_txt = out_txt.replace(str(mail), "*"*str_len)
|
100 |
+
|
101 |
+
details = {
|
102 |
+
'TEXT' : txt_input1,
|
103 |
+
'ORG' : listToString(lst_org),
|
104 |
+
'DATE' : listToString(lst_date),
|
105 |
+
'PERSON' : listToString(lst_person),
|
106 |
+
'LOCATION' : listToString(lst_gpe),
|
107 |
+
'MAIL' : listToString(lst_mail)}
|
108 |
+
phi_df = pd.DataFrame([details])
|
109 |
+
|
110 |
+
st.text_area("Output",out_txt)
|
111 |
+
st.table(phi_df)
|
112 |
+
st.success("The output has been generated")
|
113 |
+
|
114 |
+
|
115 |
+
if sb1 == "Upload Data":
|
116 |
+
img = st.file_uploader("Upload a file")
|
117 |
+
if img:
|
118 |
+
df = pd.read_excel(img,sheet_name="PHI_Masking")
|
119 |
+
st.table(df)
|
120 |
+
if st.button("Submit"):
|
121 |
+
|
122 |
+
#### Hugging Face Model full dataframe
|
123 |
+
phi_df_full = pd.DataFrame()
|
124 |
+
for row in df.itertuples():
|
125 |
+
txt_input1 = str(row.PHI_DESCRIPTION)
|
126 |
+
lst_org = []
|
127 |
+
lst_date = []
|
128 |
+
lst_person = []
|
129 |
+
lst_gpe = []
|
130 |
+
|
131 |
+
tagger = SequenceTagger.load("flair/ner-english-ontonotes-large")
|
132 |
+
sentence = Sentence(str(row.PHI_DESCRIPTION))
|
133 |
+
tagger.predict(sentence)
|
134 |
+
|
135 |
+
out_txt = txt_input1
|
136 |
+
|
137 |
+
for entity in sentence.get_spans('ner'):
|
138 |
+
str_len = len(str(entity.text))
|
139 |
+
out_txt = out_txt.replace(str(entity.text), "*"*str_len)
|
140 |
+
if entity.tag == "ORG":
|
141 |
+
lst_org.append(entity.text)
|
142 |
+
elif entity.tag == "DATE":
|
143 |
+
lst_date.append(entity.text)
|
144 |
+
elif entity.tag == "PERSON":
|
145 |
+
lst_person.append(entity.text)
|
146 |
+
elif entity.tag == "GPE":
|
147 |
+
lst_gpe.append(entity.text)
|
148 |
+
|
149 |
+
lst_mail = re.findall("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", txt_input1)
|
150 |
+
for mail in lst_mail:
|
151 |
+
str_len = len(mail)
|
152 |
+
out_txt = out_txt.replace(str(mail), "*"*str_len)
|
153 |
+
|
154 |
+
details = {
|
155 |
+
'TEXT' : txt_input1,
|
156 |
+
'MASKED_TEXT' : out_txt,
|
157 |
+
'ORG' : listToString(lst_org),
|
158 |
+
'DATE' : listToString(lst_date),
|
159 |
+
'PERSON' : listToString(lst_person),
|
160 |
+
'LOCATION' : listToString(lst_gpe),
|
161 |
+
'MAIL' : listToString(lst_mail)}
|
162 |
+
#phi_df_full = phi_df_full.append(details, ignore_index=True)
|
163 |
+
phi_df = pd.DataFrame([details])
|
164 |
+
st.table(phi_df)
|
165 |
+
st.success("The output has been generated")
|
166 |
+
|
167 |
+
|
168 |
+
##### Disease & Drug Extraction
|
169 |
+
|
170 |
+
if rad == "Disease & Drug Extraction":
|
171 |
+
|
172 |
+
phi_drug = ["Mr Mital has a history of medical conditions. He has had hypertension and hyperlipidemia since 1990 and suffered several strokes in 2005. He subsequently developed heart problems (cardiomyopathy), cardiac failure and chronic renal disease and was treated in Betroth Hospital. He was last admitted to the Betroth Hospital on 1 April 2010 till 15 April 2010, during which he was diagnosed to have suffered from a stroke. This was confirmed by CT and MRI brain scans. Thereafter, he was transferred to Ximz Hospital for stroke rehabilitation on 15 April 2010. After that, Mr Mital was referred to Blackacre Hospital for follow-up treatment from in November 2010. The clinical impression was that he was manifesting behavioural and psychological symptoms secondary to Dementia. Mr Mital’s dementia and stroke have impaired the functioning of his mind and brain. His failure to remember where he was (i.e. in the hospital) and the day and date, despite being told a short while ago, shows his inability to retain information. He was also not able to remember basic information such as his age, and the address where he lives.",
|
173 |
+
"This patient is diagnosed with Amnesia. It is a deficit in memory caused by brain damage or disease, but it can also be caused temporarily using various sedatives and hypnotic drugs. The memory can be either wholly or partially lost due to the extent of damage that was caused. The patient has Anterograde amnesia, the inability to transfer new information from the short-term store into the long-term store. People with anterograde amnesia cannot remember things for long periods of time. These two types are not mutually exclusive; both can occur simultaneously. Case studies also show that amnesia is typically associated with damage to the medial temporal lobe. Reports shows that the areas of the diencephalon are damaged, a correlation between deficiency of RbAp48 protein and memory loss. A severe reduction in the ability to learn new material and retrieve old information is observed. ",
|
174 |
+
"Mr.D is diagnosed with Arthritis, joint pain and stiffness, which typically worsen with age. Reports shows a sign of Osteoarthritis, slippery tissue that covers the ends of bones where they form a joint to break down, changes in the bones and deterioration of the connective tissues that attach muscle to bone and hold the joint together. Physical examination on joints for swelling, redness and warmth and obtained samples of blood, urine and joint fluid. Medications prescribed for treatment are NSAIDs. like ibuprofen Advil, Motrin IB and naproxen sodium (Aleve).",
|
175 |
+
"Reports declare the patient has Tuberculosis (TB), a potentially serious infectious disease that mainly affects the lungs. Symptoms include coughing for three or more weeks, coughing up blood or mucus, chest pain, unintentional weight loss, fatigue, fever, loss of appetite. Body parts affected including the kidneys and spine. Diagnostic tool used - skin test. Medications prescribed Isoniazid, Rifampin (Rifadin, Rimactane), Ethambutol (Myambutol), Pyrazinamide, a combination of antibiotics called fluoroquinolones and injectable medications, such as amikacin or capreomycin (Capastat).",
|
176 |
+
"Disease detected – Malaria, spread from the bite of infected mosquitoes. Symptoms on report - high fever and shaking chills, general discomfort, Headache, Nausea and vomiting, Diarrhea, Abdominal pain, Muscle or joint pain, Fatigue, Rapid breathing, Rapid heart rate, Cough. Antimalarial drugs prescribed - Chloroquine phosphate. Chloroquine is the preferred treatment for any parasite that is sensitive to the drug. Artemisinin-based combination therapies (ACTs), artemether-lumefantrine (Coartem) and artesunate-mefloquine."]
|
177 |
+
|
178 |
+
st.header("Disease & Drug Extraction")
|
179 |
+
|
180 |
+
sb1 = st.radio("Choose Data",["Choose From Sample","Upload Data"],index = 0)
|
181 |
+
|
182 |
+
if sb1 == "Choose From Sample":
|
183 |
+
sb2 = st.selectbox("Select Input Data",phi_drug,index = 0)
|
184 |
+
ip_txt = sb2
|
185 |
+
if st.button("Clear"):
|
186 |
+
ip_txt = ""
|
187 |
+
txt_input1 = st.text_area("Your text input is ",ip_txt)
|
188 |
+
if st.button("Submit"):
|
189 |
+
|
190 |
+
##### Scispacy Model
|
191 |
+
nlp_bc5cdr = en_ner_bc5cdr_md.load()
|
192 |
+
doc_nlp_bc5cdr = nlp_bc5cdr(txt_input1)
|
193 |
+
#displacy_image = displacy.render(doc_nlp_bc5cdr, jupyter=True,style='ent')
|
194 |
+
lst_disease = []
|
195 |
+
lst_chemical = []
|
196 |
+
|
197 |
+
for ent in doc_nlp_bc5cdr.ents:
|
198 |
+
if ent.label_ == "DISEASE":
|
199 |
+
lst_disease.append(str(ent.text))
|
200 |
+
elif ent.label_ == "CHEMICAL":
|
201 |
+
lst_chemical.append(str(ent.text))
|
202 |
+
lst_disease = list(set(lst_disease))
|
203 |
+
lst_chemical = list(set(lst_chemical))
|
204 |
+
|
205 |
+
details = {
|
206 |
+
'TEXT' : txt_input1,
|
207 |
+
'Disease' : listToString(lst_disease),
|
208 |
+
'Chemical' : listToString(lst_chemical)}
|
209 |
+
|
210 |
+
#phi_df_full = phi_df_full.append(details, ignore_index=True)
|
211 |
+
df = pd.DataFrame([details])
|
212 |
+
|
213 |
+
#st.image(displacy_image)
|
214 |
+
st.table(df)
|
215 |
+
st.success("The output has been generated")
|
216 |
+
|
217 |
+
|
218 |
+
if sb1 == "Upload Data":
|
219 |
+
img = st.file_uploader("Upload a file")
|
220 |
+
if img:
|
221 |
+
df = pd.read_excel(img,sheet_name="Disease_Drug_Extraction")
|
222 |
+
st.table(df)
|
223 |
+
if st.button("Submit"):
|
224 |
+
|
225 |
+
#### Scispacy Model full dataframe
|
226 |
+
phi_df_full = pd.DataFrame()
|
227 |
+
for row in df.itertuples():
|
228 |
+
nlp_bc5cdr = en_ner_bc5cdr_md.load()
|
229 |
+
doc_nlp_bc5cdr = nlp_bc5cdr(str(row.Medical_Record))
|
230 |
+
#displacy_image = displacy.render(doc_nlp_bc5cdr, jupyter=True,style='ent')
|
231 |
+
lst_disease = []
|
232 |
+
lst_chemical = []
|
233 |
+
|
234 |
+
for ent in doc_nlp_bc5cdr.ents:
|
235 |
+
if ent.label_ == "DISEASE":
|
236 |
+
lst_disease.append(str(ent.text))
|
237 |
+
elif ent.label_ == "CHEMICAL":
|
238 |
+
lst_chemical.append(str(ent.text))
|
239 |
+
lst_disease = list(set(lst_disease))
|
240 |
+
lst_chemical = list(set(lst_chemical))
|
241 |
+
|
242 |
+
details = {
|
243 |
+
'TEXT' : str(row.Medical_Record),
|
244 |
+
'Disease' : listToString(lst_disease),
|
245 |
+
'Chemical' : listToString(lst_chemical)}
|
246 |
+
|
247 |
+
#phi_df_full = phi_df_full.append(details, ignore_index=True)
|
248 |
+
phi_df = pd.DataFrame([details])
|
249 |
+
st.table(phi_df)
|
250 |
+
st.success("The output has been generated")
|
251 |
+
|
252 |
+
|
253 |
+
##### Question Answering
|
254 |
+
if rad == "Question Answering":
|
255 |
+
str_txt = "Mr.Mark Jade is diagnosed with Arthritis, joint pain and stiffness, which typically worsen with age. Reports shows a sign of Osteoarthritis, slippery tissue that covers the ends of bones where they form a joint to break down, changes in the bones and deterioration of the connective tissues that attach muscle to bone and hold the joint together. Physical examination on joints for swelling, redness and warmth and obtained samples of blood, urine and joint fluid. Medications prescribed for treatment are NSAIDs. like ibuprofen Advil, Motrin IB and naproxen sodium (Aleve)."
|
256 |
+
questions = ["What is the patient name?",
|
257 |
+
"The patient is diagnosed with?",
|
258 |
+
"What does the report say?",
|
259 |
+
"What all are the samples obtained?",
|
260 |
+
"What medications are prescribed?"]
|
261 |
+
|
262 |
+
st.header("Question Answering")
|
263 |
+
context = st.text_area("Your text input is ",str_txt,)
|
264 |
+
qstn = st.selectbox("Select Question",questions,index = 0)
|
265 |
+
|
266 |
+
if st.button("Submit"):
|
267 |
+
from transformers import AutoTokenizer
|
268 |
+
import transformers
|
269 |
+
from transformers import pipeline
|
270 |
+
qa_model = pipeline("question-answering")
|
271 |
+
qa_op = qa_model(question = qstn, context = context)
|
272 |
+
|
273 |
+
cnt = 0
|
274 |
+
for i in qa_op.values():
|
275 |
+
cnt += 1
|
276 |
+
if cnt == 4:
|
277 |
+
op_fin = str(i)
|
278 |
+
|
279 |
+
#st.write("Output",op_fin)
|
280 |
+
st.success(op_fin)
|
281 |
+
|
282 |
+
|
283 |
+
|
284 |
+
#### Feedback
|
285 |
+
|
286 |
+
if hom == "Feedback":
|
287 |
+
col3 , col4 = st.columns([1,4])
|
288 |
+
with col4:
|
289 |
+
st.subheader("Do let us know your Feedback :sunglasses:")
|
290 |
+
|
291 |
+
st.caption("")
|
292 |
+
name = st.text_input("Enter your Name")
|
293 |
+
feedback = st.text_area("Enter your Feedback")
|
294 |
+
if st.button("Submit"):
|
295 |
+
to_add = {"Name":[name],"Feedback":[feedback]}
|
296 |
+
to_add = pd.DataFrame(to_add)
|
297 |
+
to_add.to_csv("data//feedback.csv",mode='a',header = False,index= False)
|
298 |
+
st.success("Submitted")
|
299 |
+
st.caption("")
|
300 |
+
|
301 |
+
col5 , col6 = st.columns([1,4])
|
302 |
+
with col6:
|
303 |
+
st.caption("")
|
304 |
+
if st.checkbox("View Previous Feedback"):
|
305 |
+
fb_csv = pd.read_csv("data//Feedback.csv")
|
306 |
+
st.table(fb_csv)
|