AIEcosystem commited on
Commit
8f31f8b
·
verified ·
1 Parent(s): 5ef7d0f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +76 -47
src/streamlit_app.py CHANGED
@@ -205,6 +205,41 @@ if 'df_ner' in st.session_state and not st.session_state.df_ner.empty:
205
  fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F5FFFA', plot_bgcolor='#F5FFFA')
206
  st.plotly_chart(fig_treemap)
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  # --- Question Answering Section ---
209
  @st.cache_resource
210
  def load_gliner_model():
@@ -280,56 +315,50 @@ if st.button("Extract Answers"):
280
  st.session_state.df_qa = df_qa # Store QA results in session state
281
  st.subheader("Extracted Answers", divider="green")
282
  st.dataframe(df_qa, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  else:
284
  st.warning("No answers were found for the provided questions.")
285
  if 'df_qa' in st.session_state:
286
  del st.session_state.df_qa
287
  except Exception as e:
288
  st.error(f"An error occurred during answer extraction: {e}")
289
- if 'df_qa' in st.session_state:
290
- del st.session_state.df_qa
291
-
292
- # --- Download Button Section ---
293
- def create_zip_file_and_get_bytes():
294
- """Generates a zip file in memory with all available dataframes."""
295
- # Define the glossary DataFrame here to ensure it's always available
296
- dfa = pd.DataFrame(
297
- data={
298
- 'Column Name': ['text', 'label', 'score', 'start', 'end', 'category'],
299
- 'Description': [
300
- 'entity extracted from your text data',
301
- 'label (tag) assigned to a given extracted entity',
302
- 'accuracy score; how accurately a tag has been assigned to a given entity',
303
- 'index of the start of the corresponding entity',
304
- 'index of the end of the corresponding entity',
305
- 'the broader category the entity belongs to',
306
- ]
307
- }
308
- )
309
- if 'df_ner' not in st.session_state and 'df_qa' not in st.session_state:
310
- return None, None
311
- buf = io.BytesIO()
312
- with zipfile.ZipFile(buf, "w") as myzip:
313
- if 'df_ner' in st.session_state and not st.session_state.df_ner.empty:
314
- myzip.writestr("Extracted_Entities.csv", st.session_state.df_ner.to_csv(index=False))
315
- if 'df_qa' in st.session_state and not st.session_state.df_qa.empty:
316
- myzip.writestr("Extracted_Answers.csv", st.session_state.df_qa.to_csv(index=False))
317
- myzip.writestr("Glossary_of_tags.csv", dfa.to_csv(index=False))
318
- return buf.getvalue(), "nlpblogs_results.zip"
319
-
320
- st.divider()
321
-
322
- if ('df_ner' in st.session_state and not st.session_state.df_ner.empty) or \
323
- ('df_qa' in st.session_state and not st.session_state.df_qa.empty):
324
- zip_data, file_name = create_zip_file_and_get_bytes()
325
- if zip_data:
326
- with stylable_container(
327
- key="download_button",
328
- css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
329
- ):
330
- st.download_button(
331
- label="Download results and glossary (zip)",
332
- data=zip_data,
333
- file_name=file_name,
334
- mime="application/zip",
335
- )
 
205
  fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F5FFFA', plot_bgcolor='#F5FFFA')
206
  st.plotly_chart(fig_treemap)
207
 
208
+
209
+
210
+ dfa = pd.DataFrame(
211
+ data={
212
+ 'Column Name': ['text', 'label', 'score', 'start', 'end', 'category'],
213
+ 'Description': [
214
+ 'entity extracted from your text data',
215
+ 'label (tag) assigned to a given extracted entity',
216
+ 'accuracy score; how accurately a tag has been assigned to a given entity',
217
+ 'index of the start of the corresponding entity',
218
+ 'index of the end of the corresponding entity',
219
+ 'the broader category the entity belongs to',]}
220
+ )
221
+ buf = io.BytesIO()
222
+ with zipfile.ZipFile(buf, "w") as myzip:
223
+ myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
224
+ myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
225
+
226
+ with stylable_container(
227
+ key="download_button",
228
+ css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
229
+ ):
230
+ st.download_button(
231
+ label="Download results and glossary (zip)",
232
+ data=buf.getvalue(),
233
+ file_name="nlpblogs_results.zip",
234
+ mime="application/zip",)
235
+
236
+ if comet_initialized:
237
+ experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
238
+ experiment.end()
239
+ else:
240
+ st.warning("No entities were found in the provided text.")
241
+
242
+
243
  # --- Question Answering Section ---
244
  @st.cache_resource
245
  def load_gliner_model():
 
315
  st.session_state.df_qa = df_qa # Store QA results in session state
316
  st.subheader("Extracted Answers", divider="green")
317
  st.dataframe(df_qa, use_container_width=True)
318
+ csv_data = df_qa.to_csv(index=False).encode('utf-8')
319
+ with stylable_container(
320
+ key="download_button",
321
+ css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
322
+ ):
323
+ st.download_button(
324
+ label="Download CSV",
325
+ data=csv_data,
326
+ file_name="nlpblogs_extracted_answers.csv",
327
+ mime="text/csv",
328
+ )
329
+
330
+ if comet_initialized:
331
+ experiment.log_metric("processing_time_seconds", elapsed_time)
332
+ experiment.log_table("predicted_entities", df)
333
+ experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap")
334
+
335
+ experiment.end()
336
+ else:
337
+ st.info("No answers were found in the text with the defined questions.")
338
+ if comet_initialized:
339
+ experiment.end()
340
+ except Exception as e:
341
+ st.error(f"An error occurred during processing: {e}")
342
+ st.write(f"Error details: {e}")
343
+ if comet_initialized:
344
+ experiment.log_text(f"Error: {e}")
345
+ experiment.end()
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+
356
+
357
  else:
358
  st.warning("No answers were found for the provided questions.")
359
  if 'df_qa' in st.session_state:
360
  del st.session_state.df_qa
361
  except Exception as e:
362
  st.error(f"An error occurred during answer extraction: {e}")
363
+
364
+