AIEcosystem commited on
Commit
4f11778
·
verified ·
1 Parent(s): a4631c5

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +84 -30
src/streamlit_app.py CHANGED
@@ -688,36 +688,90 @@ if st.session_state.show_results:
688
  else:
689
  unique_categories = list(FIXED_CATEGORY_MAPPING.keys())
690
  # --- Section 2a: Detailed Tables by Category/Label ---
691
- with tab_category_details:
692
- st.markdown("#### Detailed Entities Table (Grouped by Category)")
693
- if st.session_state.is_custom_mode:
694
- # In custom mode, group by the actual label since the category is just "User Defined Entities"
695
- tabs_list = df['label'].unique().tolist()
696
- tabs_category = st.tabs(tabs_list)
697
- for label, tab in zip(tabs_list, tabs_category):
698
- df_label = df[df['label'] == label][['text', 'label', 'score', 'start', 'end']].sort_values(by='score', ascending=False)
699
- with tab:
700
- st.markdown(f"##### {label.capitalize()} Entities ({len(df_label)} total)")
701
- st.dataframe(
702
- df_label,
703
- use_container_width=True,
704
- column_config={'score': st.column_config.NumberColumn(format="%.4f")}
705
- )
706
- else:
707
- # In fixed mode, group by the category defined in FIXED_CATEGORY_MAPPING
708
- tabs_category = st.tabs(unique_categories)
709
- for category, tab in zip(unique_categories, tabs_category):
710
- df_category = df[df['category'] == category][['text', 'label', 'score', 'start', 'end']].sort_values(by='score', ascending=False)
711
- with tab:
712
- st.markdown(f"##### {category} Entities ({len(df_category)} total)")
713
- if not df_category.empty:
714
- st.dataframe(
715
- df_category,
716
- use_container_width=True,
717
- column_config={'score': st.column_config.NumberColumn(format="%.4f")}
718
- )
719
- else:
720
- st.info(f"No entities of category **{category}** were found in the text.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
721
  # --- INSERTED GLOSSARY HERE ---
722
  with st.expander("See Glossary of tags"):
723
  st.write('''- **text**: ['entity extracted from your text data']- **label**: ['label (tag) assigned to a given extracted entity (custom or fixed)']- **category**: ['the grouping category (e.g., "Locations" or "User Defined Entities")']- **score**: ['accuracy score; how accurately a tag has been assigned to a given entity']- **start**: ['index of the start of the corresponding entity']- **end**: ['index of the end of the corresponding entity']''')
 
688
  else:
689
  unique_categories = list(FIXED_CATEGORY_MAPPING.keys())
690
  # --- Section 2a: Detailed Tables by Category/Label ---
691
+
692
+
693
+
694
+
695
+ # --- Function to Apply Conditional Coloring to Scores ---
696
+ def color_score_gradient(df):
697
+ """
698
+ Applies a color gradient to the 'score' column using Pandas Styler.
699
+ High scores (closer to 1.0) will be darker/more saturated.
700
+ """
701
+ # Use 'YlGnBu' (Yellow-Green-Blue) gradient.
702
+ # We apply the gradient only to the 'score' column subset.
703
+ return df.style.background_gradient(
704
+ cmap='YlGnBu',
705
+ subset=['score']
706
+ ).format(
707
+ {'score': '{:.4f}'} # Re-apply the four decimal place format
708
+ )
709
+
710
+ # --- Your Main Tab Detail Logic ---
711
+
712
+ # Note: This code assumes 'df', 'st.session_state.is_custom_mode', and 'unique_categories'
713
+ # are already defined earlier in your Streamlit application.
714
+
715
+ tab_category_details:
716
+ st.markdown("#### Detailed Entities Table (Grouped by Category)")
717
+
718
+ if st.session_state.is_custom_mode:
719
+ # In custom mode, group by the actual label since the category is just "User Defined Entities"
720
+ tabs_list = df['label'].unique().tolist()
721
+ tabs_category = st.tabs(tabs_list)
722
+
723
+ for label, tab in zip(tabs_list, tabs_category):
724
+ # Prepare the DataFrame for the current label
725
+ df_label = df[df['label'] == label][['text', 'label', 'score', 'start', 'end']].sort_values(by='score', ascending=False)
726
+
727
+ # Apply the coloring function
728
+ styled_df_label = color_score_gradient(df_label)
729
+
730
+ with tab:
731
+ st.markdown(f"##### {label.capitalize()} Entities ({len(df_label)} total)")
732
+ st.dataframe(
733
+ # Pass the STYLED DataFrame object to Streamlit
734
+ styled_df_label,
735
+ use_container_width=True,
736
+ # NOTE: st.column_config for 'score' is removed because Pandas Styler handles formatting and coloring
737
+ )
738
+ else:
739
+ # In fixed mode, group by the category defined in FIXED_CATEGORY_MAPPING
740
+ tabs_category = st.tabs(unique_categories)
741
+
742
+ for category, tab in zip(unique_categories, tabs_category):
743
+ # Prepare the DataFrame for the current category
744
+ df_category = df[df['category'] == category][['text', 'label', 'score', 'start', 'end']].sort_values(by='score', ascending=False)
745
+
746
+ # Apply the coloring function
747
+ styled_df_category = color_score_gradient(df_category)
748
+
749
+ with tab:
750
+ st.markdown(f"##### {category} Entities ({len(df_category)} total)")
751
+ if not df_category.empty:
752
+ st.dataframe(
753
+ # Pass the STYLED DataFrame object to Streamlit
754
+ styled_df_category,
755
+ use_container_width=True,
756
+ # NOTE: st.column_config for 'score' is removed
757
+ )
758
+ else:
759
+ st.info(f"No entities of category **{category}** were found in the text.")
760
+
761
+
762
+
763
+
764
+
765
+
766
+
767
+
768
+
769
+
770
+
771
+
772
+
773
+
774
+
775
  # --- INSERTED GLOSSARY HERE ---
776
  with st.expander("See Glossary of tags"):
777
  st.write('''- **text**: ['entity extracted from your text data']- **label**: ['label (tag) assigned to a given extracted entity (custom or fixed)']- **category**: ['the grouping category (e.g., "Locations" or "User Defined Entities")']- **score**: ['accuracy score; how accurately a tag has been assigned to a given entity']- **start**: ['index of the start of the corresponding entity']- **end**: ['index of the end of the corresponding entity']''')