clef/PRENT-Codebook
0
1import json2 3import streamlit as st4 5from helpers import apply_style, callback_add_to_multiselect, get_idx_column6 7apply_style()8 9 10# Avoid having ellipsis in the multi select options11styl = """12 <style>13 .stMultiSelect span{14 max-width: none;15 16 }17 </style>18 """19st.markdown(styl, unsafe_allow_html=True)20 21st.write("# Codebook Edit")22 23st.write(24 """In this tab you can:25- Add or remove templates26- Add or remove additional answer candidates27- Modify the filled templates by adding new ones manually"""28)29 30if "templates" not in st.session_state.codebook:31 32 st.warning("No codebook loaded")33 st.stop()34 35st.write("## Codebook: Template")36 37 38with st.expander("Templates"):39 40 template = st.text_input(41 "Template with a mask [Z].",42 "This event involves [Z].",43 key="add_template_text_input",44 )45 st.button(46 "Add template",47 on_click=callback_add_to_multiselect,48 args=(49 template,50 "multiselect_templates",51 "add_template_text_input",52 "codebook",53 "templates",54 ),55 )56 57 if "multiselect_templates" not in st.session_state:58 st.session_state["multiselect_templates"] = st.session_state.codebook[59 "templates"60 ]61 62 st.write("Removed templates will be removed from the codebook.")63 templates = st.multiselect(64 "Templates",65 set(st.session_state.codebook["templates"]),66 st.session_state["multiselect_templates"],67 key="multiselect_templates",68 )69 st.session_state.codebook["templates"] = templates70 71st.write("## Codebook: Additional Answer Candidates")72st.write(73 """74 You can manually add answer candidates. Then they will be tested for entailment on every event75 description and every template even if they are not present in the prompting results.76 This is intended for case when the event that you try to describe is quite rare (e.g. shelling, missiles).77 78 **Caution**: Each word added will increase the computation time (about +3%).79 80 **Caution**: The PR-ENT model will always try to output the singular form of the word.81"""82)83with st.expander("Add answer candidates"):84 85 new_word = st.text_input(86 "Answer Candidate (1 word)", "", key="add_words_text_input"87 )88 st.button(89 "Add Word",90 on_click=callback_add_to_multiselect,91 args=(92 new_word,93 "multiselect_addwords",94 "add_words_text_input",95 "codebook",96 "add_words",97 ),98 )99 100 if "add_words" not in st.session_state.codebook:101 st.session_state.codebook["add_words"] = []102 103 if "multiselect_addwords" not in st.session_state:104 st.session_state["multiselect_addwords"] = st.session_state.codebook[105 "add_words"106 ]107 108 templates = st.multiselect(109 "Add Words",110 set(st.session_state.codebook["add_words"]),111 st.session_state["multiselect_addwords"],112 key="multiselect_addwords",113 )114 st.session_state.codebook["add_words"] = templates115 116# TODO: Change by giving a list of templates and allow only filling a word.117st.write("## Codebook: Additional Filled Templates")118st.write(119 """120 You can also manually add filled templates to the codebook. This is for the case when you know that a121 filled template could appear but you don't find corresponding events. This does not increase much the122 computation time. For example you could add `This event involves kidnapping.` if you have no kidnapping123 event in your dataset but you know it could happen.124 125 **Caution**: The PR-ENT model will always try to output the singular form of the word. (e.g. "Protests" -> "Protest")126"""127)128class_list = list(st.session_state.codebook["events"].keys())129 130 131if "filled_templates" not in st.session_state:132 st.session_state["filled_templates"] = []133 134 135with st.expander("Add Filled Template"):136 137 template_chosen = st.selectbox(138 "Choose a template:",139 st.session_state.codebook["templates"],140 # index=get_idx_column(template, st.session_state.codebook["templates"]),141 key="template_sct",142 )143 144 def add_template_with_word(template_chosen, new_word, key_text_input):145 if len(new_word) == 0:146 st.warning("Word is empty, did you press Enter on the field text?")147 else:148 st.session_state["filled_templates"].append(149 template_chosen.replace("[Z]", new_word)150 )151 st.session_state[key_text_input] = ""152 153 new_word = st.text_input("1 Word Mask", "", key="filled_template_text_input")154 if st.button(155 "Add Filled Template",156 on_click=add_template_with_word,157 args=(template_chosen, new_word, "filled_template_text_input"),158 ):159 st.write("Filled template added.")160 st.write("The template can then be selected for each class below.")161 162 163st.write("## Codebook: Event Types")164 165st.write(166 """167 Here you have access to all filled templates independently of the template. You can add/remove some of them for168 each event type.169"""170)171 172 173for event_type in st.session_state.codebook["events"].keys():174 for any_not_all in st.session_state.codebook["events"][event_type].keys():175 if (any_not_all == "all_any_rel") or (any_not_all == "not_all_any_rel"):176 pass177 else:178 st.session_state["filled_templates"].extend(179 st.session_state.codebook["events"][event_type][any_not_all]180 )181 182for event_type in class_list:183 st.session_state.codebook["events"].setdefault(event_type, {})184 event_type_chosen = event_type185 with st.expander(event_type):186 187 def declare_ms_codebook_edit(widget_key, codebook_key, widget_display):188 if widget_key not in st.session_state:189 st.session_state[widget_key] = st.session_state.codebook["events"][190 event_type_chosen191 ].setdefault(codebook_key, [])192 193 tokens_all = st.multiselect(194 widget_display,195 set(st.session_state["filled_templates"]),196 st.session_state[widget_key],197 key=widget_key,198 )199 st.session_state.codebook["events"][event_type_chosen][200 codebook_key201 ] = tokens_all202 203 declare_ms_codebook_edit("ms_all_{}".format(event_type_chosen), "all", "ALL")204 205 st.session_state.codebook["events"][event_type_chosen][206 "all_any_rel"207 ] = st.selectbox(208 "Relation",209 ["AND", "OR"],210 index=get_idx_column(211 st.session_state.codebook["events"][event_type_chosen].setdefault(212 "all_any_rel", "OR"213 ),214 ["AND", "OR"],215 ),216 key="select_relation_any_all_{}".format(event_type_chosen),217 )218 219 declare_ms_codebook_edit("ms_any_{}".format(event_type_chosen), "any", "ANY")220 declare_ms_codebook_edit(221 "ms_not_all_{}".format(event_type_chosen), "not_all", "NOT ALL"222 )223 224 st.session_state.codebook["events"][event_type_chosen][225 "not_all_any_rel"226 ] = st.selectbox(227 "Relation",228 ["AND", "OR"],229 index=get_idx_column(230 st.session_state.codebook["events"][event_type_chosen].setdefault(231 "not_all_any_rel", "OR"232 ),233 ["AND", "OR"],234 ),235 key="select_relation_not_any_all_{}".format(event_type_chosen),236 )237 declare_ms_codebook_edit(238 "ms_not_any_{}".format(event_type_chosen), "not_any", "NOT ANY"239 )240 241 if st.button("Remove Class", key="remove_class_{}".format(event_type_chosen)):242 del st.session_state.codebook["events"][event_type_chosen]243st.write("## Codebook: Download")244 245 246st.download_button(247 label="Download codebook as JSON",248 data=json.dumps(st.session_state.codebook, indent=3).encode("ASCII"),249 file_name="codebook.json",250 mime="application/json",251)252 