AIEcosystem commited on
Commit
c627d82
·
verified ·
1 Parent(s): f07e9ce

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +88 -58
src/streamlit_app.py CHANGED
@@ -18,57 +18,66 @@ st.markdown(
18
  <style>
19
  /* Main app background and text color */
20
  .stApp {
21
- background-color: #E8F5E9; /* A very light green */
22
- color: #1B5E20; /* Dark green for the text */
23
  }
24
- /* Sidebar background color */
 
25
  .css-1d36184 {
26
- background-color: #A5D6A7; /* A medium light green */
27
- secondary-background-color: #A5D6A7;
28
  }
29
- /* Expander background color and header */
 
30
  .streamlit-expanderContent, .streamlit-expanderHeader {
31
- background-color: #E8F5E9;
32
  }
33
- /* Text Area background and text color */
 
34
  .stTextArea textarea {
35
- background-color: #81C784; /* A slightly darker medium green */
36
- color: #1B5E20; /* Dark green for text */
37
  }
38
- /* Button background and text color */
 
39
  .stButton > button {
40
- background-color: #81C784;
41
- color: #1B5E20;
42
  }
43
- /* Warning box background and text color */
 
44
  .stAlert.st-warning {
45
- background-color: #66BB6A; /* A medium-dark green for the warning box */
46
- color: #1B5E20;
47
  }
48
- /* Success box background and text color */
 
49
  .stAlert.st-success {
50
- background-color: #66BB6A; /* A medium-dark green for the success box */
51
- color: #1B5E20;
52
  }
53
  </style>
54
  """,
55
- unsafe_allow_html=True)
 
 
56
  # --- Page Configuration and UI Elements ---
57
  st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
58
- st.subheader("ChainSense", divider="violet")
59
  st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
 
60
  expander = st.expander("**Important notes**")
61
- expander.write("""**Named Entities:** This ChainSense web app predicts eight (8) labels:"Location", "Organization", "Product_or_Good", "Date", "Quantity", "Transportation_Mode", "Person", "Document_or_Form_ID"
62
 
63
- 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.
64
 
65
- **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.
66
 
67
- **Usage Limits:** You can request results unlimited times for one (1) month.
68
 
69
- **Supported Languages:** English
70
 
71
- **Technical issues:** If your connection times out, please refresh the page or reopen the app's URL.
72
 
73
  For any errors or inquiries, please contact us at [email protected]""")
74
 
@@ -87,15 +96,18 @@ with st.sidebar:
87
  st.text("")
88
  st.text("")
89
  st.divider()
90
- st.subheader("🚀 Ready to build your own AI Web App?", divider="violet")
91
- st.link_button("AI Web App Builder", "https://nlpblogs.com/build-your-named-entity-recognition-app/", type="primary")
 
92
  # --- Comet ML Setup ---
93
  COMET_API_KEY = os.environ.get("COMET_API_KEY")
94
  COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
95
  COMET_PROJECT_NAME = os.environ.get("COMET_PROJECT_NAME")
96
  comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
 
97
  if not comet_initialized:
98
  st.warning("Comet ML not initialized. Check environment variables.")
 
99
  # --- Label Definitions ---
100
  labels = [
101
  "Location",
@@ -105,14 +117,18 @@ labels = [
105
  "Quantity",
106
  "Transportation_Mode",
107
  "Person",
108
- "Document_or_Form_ID"]
109
- # Corrected mapping dictionary
 
110
  # Create a mapping dictionary for labels to categories
111
  category_mapping = {
112
  "People & Groups": ["Person", "Organization"],
113
- "Goods & Transactions": ["Product_or_Good", "Quantity", "Document_or_Form_ID"],
114
- "Temporal & Events": ["Date", "Transportation_Mode"],
115
- "Locations": ["Location"]}
 
 
 
116
  # --- Model Loading ---
117
  @st.cache_resource
118
  def load_ner_model():
@@ -123,32 +139,34 @@ def load_ner_model():
123
  st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
124
  st.stop()
125
  model = load_ner_model()
 
126
  # Flatten the mapping to a single dictionary
127
  reverse_category_mapping = {label: category for category, label_list in category_mapping.items() for label in label_list}
 
128
  # --- Text Input and Clear Button ---
129
- # Define the word limit
130
  word_limit = 200
131
- # Update text area with the word limit
132
  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')
133
- # Calculate and display the word count
134
  word_count = len(text.split())
135
  st.markdown(f"**Word count:** {word_count}/{word_limit}")
 
136
  def clear_text():
137
  """Clears the text area."""
138
  st.session_state['my_text_area'] = ""
 
139
  st.button("Clear text", on_click=clear_text)
 
140
  # --- Results Section ---
141
  if st.button("Results"):
142
- # Check for word limit and empty text first
143
  if not text.strip():
144
  st.warning("Please enter some text to extract entities.")
145
  elif word_count > word_limit:
146
  st.warning(f"Your text exceeds the {word_limit} word limit. Please shorten it to continue.")
147
  else:
148
- start_time = time.time()
149
  with st.spinner("Extracting entities...", show_time=True):
150
  entities = model.predict_entities(text, labels)
151
  df = pd.DataFrame(entities)
 
152
  if not df.empty:
153
  df['category'] = df['label'].map(reverse_category_mapping)
154
  if comet_initialized:
@@ -159,17 +177,21 @@ if st.button("Results"):
159
  )
160
  experiment.log_parameter("input_text", text)
161
  experiment.log_table("predicted_entities", df)
162
- st.subheader("Grouped Entities by Category", divider = "violet")
 
 
163
  # Create tabs for each category
164
  category_names = sorted(list(category_mapping.keys()))
165
  category_tabs = st.tabs(category_names)
 
166
  for i, category_name in enumerate(category_names):
167
  with category_tabs[i]:
168
  df_category_filtered = df[df['category'] == category_name]
169
  if not df_category_filtered.empty:
170
- st.dataframe(df_category_filtered.drop(columns=['category']), use_container_width=True)
171
  else:
172
- st.info(f"No entities found for the '{category_name}' category.")
 
173
  with st.expander("See Glossary of tags"):
174
  st.write('''
175
  - **text**: ['entity extracted from your text data']
@@ -179,34 +201,39 @@ if st.button("Results"):
179
  - **end**: ['index of the end of the corresponding entity']
180
  ''')
181
  st.divider()
 
182
  # Tree map
183
- st.subheader("Tree map", divider = "violet")
184
  fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
185
- fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#E8F5E9', plot_bgcolor='#E8F5E9')
186
  st.plotly_chart(fig_treemap)
 
187
  # Pie and Bar charts
188
  grouped_counts = df['category'].value_counts().reset_index()
189
  grouped_counts.columns = ['category', 'count']
190
  col1, col2 = st.columns(2)
 
191
  with col1:
192
- st.subheader("Pie chart", divider = "violet")
193
  fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
194
  fig_pie.update_traces(textposition='inside', textinfo='percent+label')
195
  fig_pie.update_layout(
196
- paper_bgcolor='#E8F5E9',
197
- plot_bgcolor='#E8F5E9'
198
  )
199
  st.plotly_chart(fig_pie)
 
200
  with col2:
201
- st.subheader("Bar chart", divider = "violet")
202
  fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
203
- fig_bar.update_layout( # Changed from fig_pie to fig_bar
204
- paper_bgcolor='#E8F5E9',
205
- plot_bgcolor='#E8F5E9'
206
  )
207
  st.plotly_chart(fig_bar)
 
208
  # Most Frequent Entities
209
- st.subheader("Most Frequent Entities", divider="violet")
210
  word_counts = df['text'].value_counts().reset_index()
211
  word_counts.columns = ['Entity', 'Count']
212
  repeating_entities = word_counts[word_counts['Count'] > 1]
@@ -214,13 +241,15 @@ if st.button("Results"):
214
  st.dataframe(repeating_entities, use_container_width=True)
215
  fig_repeating_bar = px.bar(repeating_entities, x='Entity', y='Count', color='Entity')
216
  fig_repeating_bar.update_layout(xaxis={'categoryorder': 'total descending'},
217
- paper_bgcolor='#E8F5E9',
218
- plot_bgcolor='#E8F5E9')
219
  st.plotly_chart(fig_repeating_bar)
220
  else:
221
  st.warning("No entities were found that occur more than once.")
 
222
  # Download Section
223
  st.divider()
 
224
  dfa = pd.DataFrame(
225
  data={
226
  'Column Name': ['text', 'label', 'score', 'start', 'end'],
@@ -237,9 +266,10 @@ if st.button("Results"):
237
  with zipfile.ZipFile(buf, "w") as myzip:
238
  myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
239
  myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
 
240
  with stylable_container(
241
  key="download_button",
242
- css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
243
  ):
244
  st.download_button(
245
  label="Download results and glossary (zip)",
@@ -247,15 +277,15 @@ if st.button("Results"):
247
  file_name="nlpblogs_results.zip",
248
  mime="application/zip",
249
  )
 
250
  if comet_initialized:
251
  experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
252
  experiment.end()
253
-
254
- # Corrected placement for time calculation and display
255
  end_time = time.time()
256
  elapsed_time = end_time - start_time
257
  st.text("")
258
  st.text("")
259
  st.info(f"Results processed in **{elapsed_time:.2f} seconds**.")
260
- else: # If df is empty
261
  st.warning("No entities were found in the provided text.")
 
18
  <style>
19
  /* Main app background and text color */
20
  .stApp {
21
+ background-color: #F0F8FF; /* AliceBlue */
22
+ color: #000080; /* Navy */
23
  }
24
+
25
+ /* Sidebar background color */
26
  .css-1d36184 {
27
+ background-color: #B0E0E6; /* PowderBlue */
28
+ secondary-background-color: #B0E0E6;
29
  }
30
+
31
+ /* Expander background color and header */
32
  .streamlit-expanderContent, .streamlit-expanderHeader {
33
+ background-color: #F0F8FF;
34
  }
35
+
36
+ /* Text Area background and text color */
37
  .stTextArea textarea {
38
+ background-color: #ADD8E6; /* LightBlue */
39
+ color: #000080; /* Navy */
40
  }
41
+
42
+ /* Button background and text color */
43
  .stButton > button {
44
+ background-color: #ADD8E6;
45
+ color: #000080;
46
  }
47
+
48
+ /* Warning box background and text color */
49
  .stAlert.st-warning {
50
+ background-color: #87CEFA; /* LightSkyBlue */
51
+ color: #000080;
52
  }
53
+
54
+ /* Success box background and text color */
55
  .stAlert.st-success {
56
+ background-color: #87CEFA; /* LightSkyBlue */
57
+ color: #000080;
58
  }
59
  </style>
60
  """,
61
+ unsafe_allow_html=True
62
+ )
63
+
64
  # --- Page Configuration and UI Elements ---
65
  st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
66
+ st.subheader("ChainSense", divider="blue")
67
  st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
68
+
69
  expander = st.expander("**Important notes**")
70
+ expander.write("""**Named Entities:** This ChainSense web app predicts eight (8) labels: "Location", "Organization", "Product_or_Good", "Date", "Quantity", "Transportation_Mode", "Person", "Document_or_Form_ID"
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
 
82
  For any errors or inquiries, please contact us at [email protected]""")
83
 
 
96
  st.text("")
97
  st.text("")
98
  st.divider()
99
+ st.subheader("🚀 Ready to build your own AI Web App?", divider="blue")
100
+ st.link_button("AI Web App Builder", "https://nlpblogs.com/custom-web-app-development/", 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")
105
  COMET_PROJECT_NAME = os.environ.get("COMET_PROJECT_NAME")
106
  comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
107
+
108
  if not comet_initialized:
109
  st.warning("Comet ML not initialized. Check environment variables.")
110
+
111
  # --- Label Definitions ---
112
  labels = [
113
  "Location",
 
117
  "Quantity",
118
  "Transportation_Mode",
119
  "Person",
120
+ "Document_or_Form_ID"
121
+ ]
122
+
123
  # Create a mapping dictionary for labels to categories
124
  category_mapping = {
125
  "People & Groups": ["Person", "Organization"],
126
+ "Goods & Transactions": ["Product_or_Good", "Quantity"],
127
+ "Temporal": ["Date"],
128
+ "Logistics & Operations": ["Transportation_Mode", "Document_or_Form_ID"],
129
+ "Locations": ["Location"]
130
+ }
131
+
132
  # --- Model Loading ---
133
  @st.cache_resource
134
  def load_ner_model():
 
139
  st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
140
  st.stop()
141
  model = load_ner_model()
142
+
143
  # Flatten the mapping to a single dictionary
144
  reverse_category_mapping = {label: category for category, label_list in category_mapping.items() for label in label_list}
145
+
146
  # --- Text Input and Clear Button ---
 
147
  word_limit = 200
 
148
  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')
 
149
  word_count = len(text.split())
150
  st.markdown(f"**Word count:** {word_count}/{word_limit}")
151
+
152
  def clear_text():
153
  """Clears the text area."""
154
  st.session_state['my_text_area'] = ""
155
+
156
  st.button("Clear text", on_click=clear_text)
157
+
158
  # --- Results Section ---
159
  if st.button("Results"):
160
+ start_time = time.time()
161
  if not text.strip():
162
  st.warning("Please enter some text to extract entities.")
163
  elif word_count > word_limit:
164
  st.warning(f"Your text exceeds the {word_limit} word limit. Please shorten it to continue.")
165
  else:
 
166
  with st.spinner("Extracting entities...", show_time=True):
167
  entities = model.predict_entities(text, labels)
168
  df = pd.DataFrame(entities)
169
+
170
  if not df.empty:
171
  df['category'] = df['label'].map(reverse_category_mapping)
172
  if comet_initialized:
 
177
  )
178
  experiment.log_parameter("input_text", text)
179
  experiment.log_table("predicted_entities", df)
180
+
181
+ st.subheader("Grouped Entities by Category", divider="blue")
182
+
183
  # Create tabs for each category
184
  category_names = sorted(list(category_mapping.keys()))
185
  category_tabs = st.tabs(category_names)
186
+
187
  for i, category_name in enumerate(category_names):
188
  with category_tabs[i]:
189
  df_category_filtered = df[df['category'] == category_name]
190
  if not df_category_filtered.empty:
191
+ st.dataframe(df_category_filtered.drop(columns=['category']), use_container_width=True)
192
  else:
193
+ st.info(f"No entities found for the '{category_name}' category.")
194
+
195
  with st.expander("See Glossary of tags"):
196
  st.write('''
197
  - **text**: ['entity extracted from your text data']
 
201
  - **end**: ['index of the end of the corresponding entity']
202
  ''')
203
  st.divider()
204
+
205
  # Tree map
206
+ st.subheader("Tree map", divider="blue")
207
  fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
208
+ fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F0F8FF', plot_bgcolor='#F0F8FF')
209
  st.plotly_chart(fig_treemap)
210
+
211
  # Pie and Bar charts
212
  grouped_counts = df['category'].value_counts().reset_index()
213
  grouped_counts.columns = ['category', 'count']
214
  col1, col2 = st.columns(2)
215
+
216
  with col1:
217
+ st.subheader("Pie chart", divider="blue")
218
  fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
219
  fig_pie.update_traces(textposition='inside', textinfo='percent+label')
220
  fig_pie.update_layout(
221
+ paper_bgcolor='#F0F8FF',
222
+ plot_bgcolor='#F0F8FF'
223
  )
224
  st.plotly_chart(fig_pie)
225
+
226
  with col2:
227
+ st.subheader("Bar chart", divider="blue")
228
  fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
229
+ fig_bar.update_layout(
230
+ paper_bgcolor='#F0F8FF',
231
+ plot_bgcolor='#F0F8FF'
232
  )
233
  st.plotly_chart(fig_bar)
234
+
235
  # Most Frequent Entities
236
+ st.subheader("Most Frequent Entities", divider="blue")
237
  word_counts = df['text'].value_counts().reset_index()
238
  word_counts.columns = ['Entity', 'Count']
239
  repeating_entities = word_counts[word_counts['Count'] > 1]
 
241
  st.dataframe(repeating_entities, use_container_width=True)
242
  fig_repeating_bar = px.bar(repeating_entities, x='Entity', y='Count', color='Entity')
243
  fig_repeating_bar.update_layout(xaxis={'categoryorder': 'total descending'},
244
+ paper_bgcolor='#F0F8FF',
245
+ plot_bgcolor='#F0F8FF')
246
  st.plotly_chart(fig_repeating_bar)
247
  else:
248
  st.warning("No entities were found that occur more than once.")
249
+
250
  # Download Section
251
  st.divider()
252
+
253
  dfa = pd.DataFrame(
254
  data={
255
  'Column Name': ['text', 'label', 'score', 'start', 'end'],
 
266
  with zipfile.ZipFile(buf, "w") as myzip:
267
  myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
268
  myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
269
+
270
  with stylable_container(
271
  key="download_button",
272
+ css_styles="""button { background-color: blue; border: 1px solid black; padding: 5px; color: white; }""",
273
  ):
274
  st.download_button(
275
  label="Download results and glossary (zip)",
 
277
  file_name="nlpblogs_results.zip",
278
  mime="application/zip",
279
  )
280
+
281
  if comet_initialized:
282
  experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
283
  experiment.end()
284
+
 
285
  end_time = time.time()
286
  elapsed_time = end_time - start_time
287
  st.text("")
288
  st.text("")
289
  st.info(f"Results processed in **{elapsed_time:.2f} seconds**.")
290
+ else: # If df is empty
291
  st.warning("No entities were found in the provided text.")