AIEcosystem commited on
Commit
d7497b2
·
verified ·
1 Parent(s): 607f86d

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +177 -122
src/streamlit_app.py CHANGED
@@ -12,6 +12,7 @@ from streamlit_extras.stylable_container import stylable_container
12
  from typing import Optional
13
  from gliner import GLiNER
14
  from comet_ml import Experiment
 
15
  st.markdown(
16
  """
17
  <style>
@@ -58,7 +59,9 @@ st.markdown(
58
  }
59
  </style>
60
  """,
61
- unsafe_allow_html=True)
 
 
62
  # --- Page Configuration and UI Elements ---
63
  st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
64
  st.subheader("MediExtract", divider="gray")
@@ -66,13 +69,13 @@ st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
66
  expander = st.expander("**Important notes**")
67
  expander.write("""**Named Entities:** This MediExtract web app predicts sixteen (16) labels: "Disease", "Symptom", "Medication", "Dosage", "Frequency", "Procedure", "Diagnostic_test", "Lab_value", "Gene", "Protein", "Anatomy", "Cell_type", "Chemical", "Person", "Organization", "Date"
68
 
69
- Results are presented in easy-to-read tables, visualized in an interactive tree map, pie chart and bar chart, and are available for download along with a Glossary of tags.
70
 
71
- **How to Use:** Type or paste your text into the text area below, then press Ctrl + Enter. Click the 'Results' button to extract and tag entities in your text data.
72
 
73
- **Usage Limits:** You can request results unlimited times for one (1) month.
74
 
75
- **Supported Languages:** English
76
 
77
  **Technical issues:** If your connection times out, please refresh the page or reopen the app's URL.
78
 
@@ -95,6 +98,7 @@ with st.sidebar:
95
  st.divider()
96
  st.subheader("🚀 Ready to build your own AI Web App?", divider="gray")
97
  st.link_button("AI Web App Builder", "https://nlpblogs.com/build-your-named-entity-recognition-app/", type="primary")
 
98
  # --- Comet ML Setup ---
99
  COMET_API_KEY = os.environ.get("COMET_API_KEY")
100
  COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
@@ -102,6 +106,7 @@ COMET_PROJECT_NAME = os.environ.get("COMET_PROJECT_NAME")
102
  comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
103
  if not comet_initialized:
104
  st.warning("Comet ML not initialized. Check environment variables.")
 
105
  # --- Label Definitions ---
106
  labels = [
107
  "Disease",
@@ -119,8 +124,8 @@ labels = [
119
  "Chemical",
120
  "Person",
121
  "Organization",
122
- "Date"]
123
- # Corrected mapping dictionary
124
  # Create a mapping dictionary for labels to categories
125
  category_mapping = {
126
  "Clinical & Procedural": [
@@ -150,143 +155,193 @@ category_mapping = {
150
  ],
151
  "Temporal": [
152
  "Date"
153
- ]}
 
 
154
  # --- Model Loading ---
155
- @st.cache_resourcedef load_ner_model():
 
156
  """Loads the GLiNER model and caches it."""
157
  try:
158
- return GLiNER.from_pretrained("Ihor/gliner-biomed-bi-large-v1.0", nested_ner=True, num_gen_sequences=2, gen_constraints= labels)
159
  except Exception as e:
160
  st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
161
  st.stop()
 
162
  model = load_ner_model()
163
  # Flatten the mapping to a single dictionary
164
  reverse_category_mapping = {label: category for category, label_list in category_mapping.items() for label in label_list}
 
 
 
 
 
 
 
 
 
 
 
 
165
  # --- Text Input and Clear Button ---
166
  word_limit = 200
167
  text = st.text_area(f"Type or paste your text below (max {word_limit} words), and then press Ctrl + Enter", height=250, key='my_text_area')
168
  word_count = len(text.split())
169
  st.markdown(f"**Word count:** {word_count}/{word_limit}")
 
170
  def clear_text():
171
- """Clears the text area."""
172
  st.session_state['my_text_area'] = ""
 
 
 
 
 
173
  st.button("Clear text", on_click=clear_text)
 
174
  # --- Results Section ---
175
  if st.button("Results"):
176
- start_time = time.time()
177
  if not text.strip():
178
  st.warning("Please enter some text to extract entities.")
 
179
  elif word_count > word_limit:
180
  st.warning(f"Your text exceeds the {word_limit} word limit. Please shorten it to continue.")
 
181
  else:
182
- with st.spinner("Extracting entities...", show_time=True):
183
- entities = model.predict_entities(text, labels)
184
- df = pd.DataFrame(entities)
185
- if not df.empty:
186
- df['category'] = df['label'].map(reverse_category_mapping)
187
- if comet_initialized:
188
- experiment = Experiment(
189
- api_key=COMET_API_KEY,
190
- workspace=COMET_WORKSPACE,
191
- project_name=COMET_PROJECT_NAME,
192
- )
193
- experiment.log_parameter("input_text", text)
194
- experiment.log_table("predicted_entities", df)
195
- st.subheader("Grouped Entities by Category", divider = "gray")
196
- # Create tabs for each category
197
- category_names = sorted(list(category_mapping.keys()))
198
- category_tabs = st.tabs(category_names)
199
- for i, category_name in enumerate(category_names):
200
- with category_tabs[i]:
201
- df_category_filtered = df[df['category'] == category_name]
202
- if not df_category_filtered.empty:
203
- st.dataframe(df_category_filtered.drop(columns=['category']), use_container_width=True)
204
- else:
205
- st.info(f"No entities found for the '{category_name}' category.")
206
- with st.expander("See Glossary of tags"):
207
- st.write('''
208
- - **text**: ['entity extracted from your text data']
209
- - **score**: ['accuracy score; how accurately a tag has been assigned to a given entity']
210
- - **label**: ['label (tag) assigned to a given extracted entity']
211
- - **start**: ['index of the start of the corresponding entity']
212
- - **end**: ['index of the end of the corresponding entity']
213
- ''')
214
- st.divider()
215
- # Tree map
216
- st.subheader("Tree map", divider = "gray")
217
- fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
218
- fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F5F5F5', plot_bgcolor='#F5F5F5')
219
- st.plotly_chart(fig_treemap)
220
- # Pie and Bar charts
221
- grouped_counts = df['category'].value_counts().reset_index()
222
- grouped_counts.columns = ['category', 'count']
223
- col1, col2 = st.columns(2)
224
- with col1:
225
- st.subheader("Pie chart", divider = "gray")
226
- fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
227
- fig_pie.update_traces(textposition='inside', textinfo='percent+label')
228
- fig_pie.update_layout(
229
- paper_bgcolor='#F5F5F5',
230
- plot_bgcolor='#F5F5F5'
231
- )
232
- st.plotly_chart(fig_pie)
233
- with col2:
234
- st.subheader("Bar chart", divider = "gray")
235
- fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
236
- fig_bar.update_layout( # Changed from fig_pie to fig_bar
237
- paper_bgcolor='#F5F5F5',
238
- plot_bgcolor='#F5F5F5'
239
- )
240
- st.plotly_chart(fig_bar)
241
- # Most Frequent Entities
242
- st.subheader("Most Frequent Entities", divider="gray")
243
- word_counts = df['text'].value_counts().reset_index()
244
- word_counts.columns = ['Entity', 'Count']
245
- repeating_entities = word_counts[word_counts['Count'] > 1]
246
- if not repeating_entities.empty:
247
- st.dataframe(repeating_entities, use_container_width=True)
248
- fig_repeating_bar = px.bar(repeating_entities, x='Entity', y='Count', color='Entity')
249
- fig_repeating_bar.update_layout(xaxis={'categoryorder': 'total descending'},
250
- paper_bgcolor='#F5F5F5',
251
- plot_bgcolor='#F5F5F5')
252
- st.plotly_chart(fig_repeating_bar)
253
  else:
254
- st.warning("No entities were found that occur more than once.")
255
- # Download Section
256
- st.divider()
257
- dfa = pd.DataFrame(
258
- data={
259
- 'Column Name': ['text', 'label', 'score', 'start', 'end'],
260
- 'Description': [
261
- 'entity extracted from your text data',
262
- 'label (tag) assigned to a given extracted entity',
263
- 'accuracy score; how accurately a tag has been assigned to a given entity',
264
- 'index of the start of the corresponding entity',
265
- 'index of the end of the corresponding entity',
266
- ]
267
- }
268
- )
269
- buf = io.BytesIO()
270
- with zipfile.ZipFile(buf, "w") as myzip:
271
- myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
272
- myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
273
- with stylable_container(
274
- key="download_button",
275
- css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
276
- ):
277
- st.download_button(
278
- label="Download results and glossary (zip)",
279
- data=buf.getvalue(),
280
- file_name="nlpblogs_results.zip",
281
- mime="application/zip",
282
- )
283
- if comet_initialized:
284
- experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
285
- experiment.end()
286
- else: # If df is empty
287
- st.warning("No entities were found in the provided text.")
288
- end_time = time.time()
289
- elapsed_time = end_time - start_time
290
- st.text("")
291
- st.text("")
292
- st.info(f"Results processed in **{elapsed_time:.2f} seconds**.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  from typing import Optional
13
  from gliner import GLiNER
14
  from comet_ml import Experiment
15
+
16
  st.markdown(
17
  """
18
  <style>
 
59
  }
60
  </style>
61
  """,
62
+ unsafe_allow_html=True
63
+ )
64
+
65
  # --- Page Configuration and UI Elements ---
66
  st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
67
  st.subheader("MediExtract", divider="gray")
 
69
  expander = st.expander("**Important notes**")
70
  expander.write("""**Named Entities:** This MediExtract web app predicts sixteen (16) labels: "Disease", "Symptom", "Medication", "Dosage", "Frequency", "Procedure", "Diagnostic_test", "Lab_value", "Gene", "Protein", "Anatomy", "Cell_type", "Chemical", "Person", "Organization", "Date"
71
 
72
+ Results are presented in easy-to-read tables, visualized in an interactive tree map, pie chart and bar chart, and are available for download along with a Glossary of tags.
73
 
74
+ **How to Use:** Type or paste your text into the text area below, then press Ctrl + Enter. Click the 'Results' button to extract and tag entities in your text data.
75
 
76
+ **Usage Limits:** You can request results unlimited times for one (1) month.
77
 
78
+ **Supported Languages:** English
79
 
80
  **Technical issues:** If your connection times out, please refresh the page or reopen the app's URL.
81
 
 
98
  st.divider()
99
  st.subheader("🚀 Ready to build your own AI Web App?", divider="gray")
100
  st.link_button("AI Web App Builder", "https://nlpblogs.com/build-your-named-entity-recognition-app/", type="primary")
101
+
102
  # --- Comet ML Setup ---
103
  COMET_API_KEY = os.environ.get("COMET_API_KEY")
104
  COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
 
106
  comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
107
  if not comet_initialized:
108
  st.warning("Comet ML not initialized. Check environment variables.")
109
+
110
  # --- Label Definitions ---
111
  labels = [
112
  "Disease",
 
124
  "Chemical",
125
  "Person",
126
  "Organization",
127
+ "Date"
128
+ ]
129
  # Create a mapping dictionary for labels to categories
130
  category_mapping = {
131
  "Clinical & Procedural": [
 
155
  ],
156
  "Temporal": [
157
  "Date"
158
+ ]
159
+ }
160
+
161
  # --- Model Loading ---
162
+ @st.cache_resource
163
+ def load_ner_model():
164
  """Loads the GLiNER model and caches it."""
165
  try:
166
+ return GLiNER.from_pretrained("Ihor/gliner-biomed-bi-large-v1.0", nested_ner=True, num_gen_sequences=2, gen_constraints=labels)
167
  except Exception as e:
168
  st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
169
  st.stop()
170
+
171
  model = load_ner_model()
172
  # Flatten the mapping to a single dictionary
173
  reverse_category_mapping = {label: category for category, label_list in category_mapping.items() for label in label_list}
174
+
175
+ # --- Session State Initialization ---
176
+ # This is the key fix. We use session state to control what is displayed.
177
+ if 'show_results' not in st.session_state:
178
+ st.session_state.show_results = False
179
+ if 'last_text' not in st.session_state:
180
+ st.session_state.last_text = ""
181
+ if 'results_df' not in st.session_state:
182
+ st.session_state.results_df = pd.DataFrame()
183
+ if 'elapsed_time' not in st.session_state:
184
+ st.session_state.elapsed_time = 0.0
185
+
186
  # --- Text Input and Clear Button ---
187
  word_limit = 200
188
  text = st.text_area(f"Type or paste your text below (max {word_limit} words), and then press Ctrl + Enter", height=250, key='my_text_area')
189
  word_count = len(text.split())
190
  st.markdown(f"**Word count:** {word_count}/{word_limit}")
191
+
192
  def clear_text():
193
+ """Clears the text area and hides results."""
194
  st.session_state['my_text_area'] = ""
195
+ st.session_state.show_results = False
196
+ st.session_state.last_text = ""
197
+ st.session_state.results_df = pd.DataFrame()
198
+ st.session_state.elapsed_time = 0.0
199
+
200
  st.button("Clear text", on_click=clear_text)
201
+
202
  # --- Results Section ---
203
  if st.button("Results"):
 
204
  if not text.strip():
205
  st.warning("Please enter some text to extract entities.")
206
+ st.session_state.show_results = False
207
  elif word_count > word_limit:
208
  st.warning(f"Your text exceeds the {word_limit} word limit. Please shorten it to continue.")
209
+ st.session_state.show_results = False
210
  else:
211
+ # Check if the text is different from the last time
212
+ if text != st.session_state.last_text:
213
+ st.session_state.show_results = True
214
+ st.session_state.last_text = text
215
+ start_time = time.time()
216
+ with st.spinner("Extracting entities...", show_time=True):
217
+ entities = model.predict_entities(text, labels)
218
+ df = pd.DataFrame(entities)
219
+ st.session_state.results_df = df
220
+ if not df.empty:
221
+ df['category'] = df['label'].map(reverse_category_mapping)
222
+ if comet_initialized:
223
+ experiment = Experiment(
224
+ api_key=COMET_API_KEY,
225
+ workspace=COMET_WORKSPACE,
226
+ project_name=COMET_PROJECT_NAME,
227
+ )
228
+ experiment.log_parameter("input_text", text)
229
+ experiment.log_table("predicted_entities", df)
230
+ experiment.end()
231
+ end_time = time.time()
232
+ st.session_state.elapsed_time = end_time - start_time
233
+ else:
234
+ # If the text is the same, just show the cached results without re-running
235
+ st.session_state.show_results = True
236
+
237
+ # Display results if the state variable is True
238
+ if st.session_state.show_results:
239
+ df = st.session_state.results_df
240
+ if not df.empty:
241
+ df['category'] = df['label'].map(reverse_category_mapping)
242
+ st.subheader("Grouped Entities by Category", divider="gray")
243
+
244
+ # Create tabs for each category
245
+ category_names = sorted(list(category_mapping.keys()))
246
+ category_tabs = st.tabs(category_names)
247
+
248
+ for i, category_name in enumerate(category_names):
249
+ with category_tabs[i]:
250
+ df_category_filtered = df[df['category'] == category_name]
251
+ if not df_category_filtered.empty:
252
+ st.dataframe(df_category_filtered.drop(columns=['category']), use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  else:
254
+ st.info(f"No entities found for the '{category_name}' category.")
255
+
256
+ with st.expander("See Glossary of tags"):
257
+ st.write('''
258
+ - **text**: ['entity extracted from your text data']
259
+ - **score**: ['accuracy score; how accurately a tag has been assigned to a given entity']
260
+ - **label**: ['label (tag) assigned to a given extracted entity']
261
+ - **start**: ['index of the start of the corresponding entity']
262
+ - **end**: ['index of the end of the corresponding entity']
263
+ ''')
264
+ st.divider()
265
+
266
+ # Tree map
267
+ st.subheader("Tree map", divider="gray")
268
+ fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
269
+ fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F5F5F5', plot_bgcolor='#F5F5F5')
270
+ st.plotly_chart(fig_treemap)
271
+
272
+ # Pie and Bar charts
273
+ grouped_counts = df['category'].value_counts().reset_index()
274
+ grouped_counts.columns = ['category', 'count']
275
+ col1, col2 = st.columns(2)
276
+
277
+ with col1:
278
+ st.subheader("Pie chart", divider="gray")
279
+ fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
280
+ fig_pie.update_traces(textposition='inside', textinfo='percent+label')
281
+ fig_pie.update_layout(
282
+ paper_bgcolor='#F5F5F5',
283
+ plot_bgcolor='#F5F5F5'
284
+ )
285
+ st.plotly_chart(fig_pie)
286
+
287
+ with col2:
288
+ st.subheader("Bar chart", divider="gray")
289
+ fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
290
+ fig_bar.update_layout(
291
+ paper_bgcolor='#F5F5F5',
292
+ plot_bgcolor='#F5F5F5'
293
+ )
294
+ st.plotly_chart(fig_bar)
295
+
296
+ # Most Frequent Entities
297
+ st.subheader("Most Frequent Entities", divider="gray")
298
+ word_counts = df['text'].value_counts().reset_index()
299
+ word_counts.columns = ['Entity', 'Count']
300
+ repeating_entities = word_counts[word_counts['Count'] > 1]
301
+
302
+ if not repeating_entities.empty:
303
+ st.dataframe(repeating_entities, use_container_width=True)
304
+ fig_repeating_bar = px.bar(repeating_entities, x='Entity', y='Count', color='Entity')
305
+ fig_repeating_bar.update_layout(xaxis={'categoryorder': 'total descending'},
306
+ paper_bgcolor='#F5F5F5',
307
+ plot_bgcolor='#F5F5F5')
308
+ st.plotly_chart(fig_repeating_bar)
309
+ else:
310
+ st.warning("No entities were found that occur more than once.")
311
+
312
+ # Download Section
313
+ st.divider()
314
+ dfa = pd.DataFrame(
315
+ data={
316
+ 'Column Name': ['text', 'label', 'score', 'start', 'end'],
317
+ 'Description': [
318
+ 'entity extracted from your text data',
319
+ 'label (tag) assigned to a given extracted entity',
320
+ 'accuracy score; how accurately a tag has been assigned to a given entity',
321
+ 'index of the start of the corresponding entity',
322
+ 'index of the end of the corresponding entity',
323
+ ]
324
+ }
325
+ )
326
+ buf = io.BytesIO()
327
+ with zipfile.ZipFile(buf, "w") as myzip:
328
+ myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
329
+ myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
330
+
331
+ with stylable_container(
332
+ key="download_button",
333
+ css_styles="""button { background-color: #8C8C8C; border: 1px solid black; padding: 5px; color: white; }""",
334
+ ):
335
+ st.download_button(
336
+ label="Download results and glossary (zip)",
337
+ data=buf.getvalue(),
338
+ file_name="nlpblogs_results.zip",
339
+ mime="application/zip",
340
+ )
341
+
342
+ st.text("")
343
+ st.text("")
344
+ st.info(f"Results processed in **{st.session_state.elapsed_time:.2f} seconds**.")
345
+
346
+ else: # If df is empty after the button click
347
+ st.warning("No entities were found in the provided text.")