lingbionlp commited on
Commit
5d94a6e
1 Parent(s): 49c5cf1

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +271 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on May 15 16:21:25 2023
4
+
5
+ @author: luol2
6
+ """
7
+
8
+ import streamlit as st
9
+ from src.nn_model import bioTag_CNN,bioTag_BERT
10
+ from src.dic_ner import dic_ont
11
+ from src.tagging_text import bioTag
12
+ import os
13
+ import json
14
+ from pandas import DataFrame
15
+ import nltk
16
+ nltk.download('punkt')
17
+ nltk.download('averaged_perceptron_tagger')
18
+ nltk.download('wordnet')
19
+
20
+ st.set_page_config(
21
+ page_title="PhenoTagger_CL",
22
+ page_icon="🎈",
23
+ layout="wide",
24
+ menu_items={
25
+ 'Get Help': 'https://www.ncbi.nlm.nih.gov/research/bionlp/',
26
+ 'About': "PhenoTagger-CL"
27
+ }
28
+ )
29
+
30
+
31
+ # def _max_width_():
32
+ # max_width_str = f"max-width: 2400px;"
33
+ # st.markdown(
34
+ # f"""
35
+ # <style>
36
+ # .reportview-container .main .block-container{{
37
+ # {max_width_str}
38
+ # }}
39
+ # </style>
40
+ # """,
41
+ # unsafe_allow_html=True,
42
+ # )
43
+
44
+
45
+ # _max_width_()
46
+
47
+ # c30, c31, c32 = st.columns([2.5, 1, 3])
48
+
49
+ # with c30:
50
+ # # st.image("logo.png", width=400)
51
+ st.title("👨‍⚕️ PhenoTagger_CL Demo")
52
+
53
+ with st.expander("ℹ️ - About this app", expanded=True):
54
+
55
+ st.write(
56
+ """
57
+ - This app is an easy-to-use interface built in Streamlit for [PhenoTagger](https://github.com/ncbi-nlp/PhenoTagger) library!
58
+ - PhenoTagger is a hybrid method that combines dictionary and deep learning-based methods to recognize Human Phenotype Ontology (HPO) concepts in unstructured biomedical text. Please refer to [our paper](https://doi.org/10.1093/bioinformatics/btab019) for more details.
59
+ - Contact: [NLM/NCBI BioNLP Research Group](https://www.ncbi.nlm.nih.gov/research/bionlp/)
60
+ """
61
+ )
62
+
63
+ st.markdown("")
64
+
65
+ st.markdown("")
66
+ st.markdown("## 📌 Paste document ")
67
+ with st.form(key="my_form"):
68
+
69
+
70
+ ce, c1, ce, c2, c3 = st.columns([0.07, 1, 0.07, 4, 0.07])
71
+ with c1:
72
+ ModelType = st.radio(
73
+ "Choose your Ontology",
74
+ ['CL'], #["HPO(Default)", "UBERON"],
75
+ #help="Bioformer is more precise, CNN is more efficient",
76
+ )
77
+
78
+ if ModelType == "CL":
79
+ # kw_model = KeyBERT(model=roberta)
80
+
81
+ #@st.cache(allow_output_mutation=True)
82
+ def load_model():
83
+ ontfiles={'dic_file':'./dict_cl/noabb_lemma.dic',
84
+ 'word_id_file':'./dict_cl/word_id_map.json',
85
+ 'id_word_file':'./dict_cl/id_word_map.json'}
86
+
87
+
88
+ vocabfiles={'labelfile':'./dict_cl/lable.vocab',
89
+ 'checkpoint_path':'./models/bioformer-cased-v1.0/',
90
+ 'lowercase':False}
91
+ modelfile='./models/bioformer-CL.h5'
92
+
93
+
94
+ biotag_dic=dic_ont(ontfiles)
95
+
96
+ nn_model=bioTag_BERT(vocabfiles)
97
+ nn_model.load_model(modelfile)
98
+ return nn_model,biotag_dic
99
+
100
+ nn_model,biotag_dic = load_model()
101
+
102
+ else:
103
+ pass
104
+ '''
105
+ #@st.cache(allow_output_mutation=True)
106
+ def load_model():
107
+ ontfiles={'dic_file':'./dict_uberon/noabb_lemma.dic',
108
+ 'word_id_file':'./dict_uberon/word_id_map.json',
109
+ 'id_word_file':'./dict_uberon/id_word_map.json'}
110
+
111
+ vocabfiles={'labelfile':'./dict_uberon/lable.vocab',
112
+ 'checkpoint_path':'./models_v1.2/bioformer-cased-v1.0/',
113
+ 'lowercase':False}
114
+
115
+ modelfile='./models_v1.2/bioformer-UBERON.h5'
116
+
117
+ biotag_dic=dic_ont(ontfiles)
118
+
119
+ nn_model=bioTag_BERT(vocabfiles)
120
+ nn_model.load_model(modelfile)
121
+
122
+ return nn_model,biotag_dic
123
+
124
+ nn_model,biotag_dic = load_model()
125
+ '''
126
+
127
+ para_overlap = st.checkbox(
128
+ "Overlap concept",
129
+ value=False,
130
+ help="Tick this box to identify overlapping concepts",
131
+ )
132
+ para_abbr = st.checkbox(
133
+ "Abbreviaitons",
134
+ value=True,
135
+ help="Tick this box to identify abbreviations",
136
+ )
137
+
138
+ para_threshold = st.slider(
139
+ "Threshold",
140
+ min_value=0.5,
141
+ max_value=1.0,
142
+ value=0.95,
143
+ step=0.05,
144
+ help="Retrun the preditions which socre over the threshold.",
145
+ )
146
+
147
+
148
+
149
+
150
+ with c2:
151
+
152
+
153
+ doc = st.text_area(
154
+ "Paste your text below",
155
+ value = 'The clinical features of Angelman syndrome (AS) comprise severe mental retardation, postnatal microcephaly, macrostomia and prognathia, absence of speech, ataxia, and a happy disposition. We report on seven patients who lack most of these features, but presented with obesity, muscular hypotonia and mild mental retardation. Based on the latter findings, the patients were initially suspected of having Prader-Willi syndrome. DNA methylation analysis of SNRPN and D15S63, however, revealed an AS pattern, ie the maternal band was faint or absent. Cytogenetic studies and microsatellite analysis demonstrated apparently normal chromosomes 15 of biparental inheritance. We conclude that these patients have an imprinting defect and a previously unrecognised form of AS. The mild phenotype may be explained by an incomplete imprinting defect or by cellular mosaicism.',
156
+ height=400,
157
+ )
158
+
159
+
160
+
161
+
162
+ # MAX_WORDS = 500
163
+ # import re
164
+ # res = len(re.findall(r"\w+", doc))
165
+ # if res > MAX_WORDS:
166
+ # st.warning(
167
+ # "⚠️ Your text contains "
168
+ # + str(res)
169
+ # + " words."
170
+ # + " Only the first 500 words will be reviewed. Stay tuned as increased allowance is coming! 😊"
171
+ # )
172
+
173
+ # doc = doc[:MAX_WORDS]
174
+
175
+ submit_button = st.form_submit_button(label="✨ Submit!")
176
+
177
+
178
+ if not submit_button:
179
+ st.stop()
180
+
181
+ #st.write(para_overlap,para_abbr,para_threshold)
182
+ para_set={
183
+ #model_type':para_model, # cnn or bioformer
184
+ 'onlyLongest': not para_overlap, # False: return overlap concepts, True only longgest
185
+ 'abbrRecog':para_abbr,# False: don't identify abbr, True: identify abbr
186
+ 'ML_Threshold':para_threshold,# the Threshold of deep learning model
187
+ }
188
+ st.markdown("")
189
+ st.markdown("## 💡 Tagging results:")
190
+ with st.spinner('Wait for tagging...'):
191
+ tag_result=bioTag(doc,biotag_dic,nn_model,onlyLongest=para_set['onlyLongest'], abbrRecog=para_set['abbrRecog'],Threshold=para_set['ML_Threshold'])
192
+
193
+ st.markdown('<font style="color: rgb(128, 128, 128);">Move the mouse🖱️ over the entity to display the HPO id.</font>', unsafe_allow_html=True)
194
+ # print('dic...........:',biotag_dic.keys())
195
+ # st.write('parameters:', para_overlap,para_abbr,para_threshold)
196
+
197
+ html_results=''
198
+ text_results=doc+'\n'
199
+ entity_end=0
200
+ hpoid_count={}
201
+ if len(tag_result)>=0:
202
+ for ele in tag_result:
203
+ entity_start=int(ele[0])
204
+ html_results+=doc[entity_end:entity_start]
205
+ entity_end=int(ele[1])
206
+ entity_id=ele[2]
207
+ entity_score=ele[3]
208
+ text_results+=ele[0]+'\t'+ele[1]+'\t'+doc[entity_start:entity_end]+'\t'+ele[2]+'\t'+format(float(ele[3]),'.2f')+'\n'
209
+ if entity_id not in hpoid_count.keys():
210
+ hpoid_count[entity_id]=1
211
+ else:
212
+ hpoid_count[entity_id]+=1
213
+
214
+ html_results+='<font style="background-color: rgb(255, 204, 0)'+';" title="'+entity_id+'">'+doc[entity_start:entity_end]+'</font>'
215
+ html_results+=doc[entity_end:]
216
+
217
+ else:
218
+ html_results=doc
219
+
220
+ st.markdown('<table border="1"><tr><td>'+html_results+'</td></tr></table>', unsafe_allow_html=True)
221
+
222
+
223
+ #table
224
+ data_entity=[]
225
+ for ele in hpoid_count.keys():
226
+ segs=ele.split(';')
227
+ term_name=''
228
+ for seg in segs:
229
+ term_name+=biotag_dic.id_word[seg][0]+';'
230
+ temp=[ele,term_name,hpoid_count[ele]] #hpoid, term name, count
231
+ data_entity.append(temp)
232
+
233
+
234
+ st.markdown("")
235
+ st.markdown("")
236
+ # st.markdown("## Table output:")
237
+
238
+ # cs, c1, c2, c3, cLast = st.columns([2, 1.5, 1.5, 1.5, 2])
239
+
240
+ # with c1:
241
+ # CSVButton2 = download_button(keywords, "Data.csv", "📥 Download (.csv)")
242
+ # with c2:
243
+ # CSVButton2 = download_button(keywords, "Data.txt", "📥 Download (.txt)")
244
+ # with c3:
245
+ # CSVButton2 = download_button(keywords, "Data.json", "📥 Download (.json)")
246
+
247
+ # st.header("")
248
+
249
+ df = (
250
+ DataFrame(data_entity, columns=["Ontology_id", "Term name","Frequency"])
251
+ .sort_values(by="Frequency", ascending=False)
252
+ .reset_index(drop=True)
253
+ )
254
+
255
+ df.index += 1
256
+
257
+ c1, c2, c3 = st.columns([1, 4, 1])
258
+
259
+ # format_dictionary = {
260
+ # "Relevancy": "{:.1%}",
261
+ # }
262
+
263
+ # df = df.format(format_dictionary)
264
+
265
+ with c2:
266
+ st.table(df)
267
+
268
+ c1, c2, c3 = st.columns([1, 1, 1])
269
+ with c2:
270
+ st.download_button('Download annotations', text_results)
271
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ TensorFlow==2.12.0
2
+ nltk==3.8.1
3
+ bioc==2.0.post5
4
+ streamlit
5
+ numpy==1.23.5
6
+ torch==1.11.0
7
+ transformers
8
+ altair==4.0