Team Ai
Apppublic

clef/PRENT-Codebook

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
Data_Loading.py124 linesDownload Raw Back to root
1import os2import sys3 4import streamlit as st5from st_aggrid import AgGrid, DataReturnMode6 7current = os.path.dirname(os.path.realpath(__file__))8parent = os.path.dirname(current)9sys.path.append(parent)10from helpers import apply_style, get_idx_column, read_csv_from_web, read_json_from_web11 12apply_style()13 14codebook = {}15 16# TODO: Anonymize for paper17st.markdown(18    """19    # Codebook Creation/Edition Tool based on the PR-ENT Approach.20    ### *Rethinking the Event Coding Pipeline with Prompt Entailment*21    ### https://arxiv.org/abs/2210.0525722    ### Clément Lefebvre (Swiss Data Science Center)23    ### Niklas Stoehr (ETH Zürich, https://niklas-stoehr.com/)24    ##### Version: 1.025"""26)27 28st.markdown("***********")29 30st.markdown(31    """32## Data Loading33"""34)35 36 37st.markdown(38    """39    ### Upload a CSV of event descriptions.40"""41)42uploaded_file = st.file_uploader("Upload a csv file containing event descriptions")43if uploaded_file is not None:44    st.session_state.data = read_csv_from_web(uploaded_file)45 46 47if "data" in st.session_state:48    # Filter will be reset if the page is left and then used again49    loading_df = st.text("Loading data display...")50    st.write(51        """52        The below display of the data can be used to filter the data. Click on the *3 bars logo* when hovering over a column name and the filtering53        tool will appear. Filters are kept in memory on the whole dashboard as long as the `Reset Filters` button is not clicked.54 55        Current limitation: If a filter is set and the user change page. Then it can not be modified anymore and needs to be reset.56    """57    )58    if "filtered_df" not in st.session_state:59        st.session_state.filtered_df = st.session_state.data60    if st.button("Reset Filters"):61        st.session_state.filtered_df = st.session_state.data62 63    st.session_state.filtered_df = AgGrid(64        st.session_state.filtered_df,65        height=400,66        data_return_mode=DataReturnMode.FILTERED,67        update_mode="MANUAL",68    )["data"]69 70    if "text_column_design_perm" not in st.session_state:71        st.session_state[72            "text_column_design_perm"73        ] = st.session_state.filtered_df.columns[0]74 75    def callback_function(mod, key):76        st.session_state[mod] = st.session_state[key]77 78    st.write("Select the column which contains the event descriptions.")79    st.selectbox(80        "Select the event description column:",81        st.session_state.filtered_df.columns,82        key="text_column_design",83        on_change=callback_function,84        args=("text_column_design_perm", "text_column_design"),85        index=get_idx_column(86            st.session_state["text_column_design_perm"],87            list(st.session_state.filtered_df.columns),88        ),89    )90    loading_df.text("")91 92    # Remove NaN Texts93    if st.button("Remove Empty Event Descriptions"):94        st.session_state.filtered_df = st.session_state.filtered_df.dropna(95            subset=[st.session_state["text_column_design_perm"]]96        )97 98 99st.write("********")100st.markdown("## Optional Upload")101 102 103st.markdown(104    """105    ### Upload a codebook if available. It needs to be in the format used in this dashboard.106"""107)108uploaded_codebook = st.file_uploader("Upload a codebook if available (OPTIONAL)")109if uploaded_codebook is not None:110    codebook = read_json_from_web(uploaded_codebook)111    st.session_state.codebook = codebook112 113st.markdown(114    """115    ### Upload a validated dataset (accept, reject, ignored) in the format of this dashboard.116"""117)118 119uploaded_validated_data = st.file_uploader(120    "Upload a json file containing validated data (OPTIONAL)"121)122if uploaded_validated_data is not None:123    st.session_state.validated_data = read_json_from_web(uploaded_validated_data)124