Spaces:
Sleeping
Sleeping
Rename app_10.py to app_11.py
Browse files- app_10.py → app_11.py +26 -19
app_10.py → app_11.py
RENAMED
@@ -174,6 +174,15 @@ def mark_text_by_position(original_text, start_idx, end_idx, color):
|
|
174 |
)
|
175 |
return original_text # Return unchanged if indices are invalidt # Return unchanged text if no span is found
|
176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
st.title("Causal Relation Extraction")
|
178 |
input_text = st.text_area("Enter your text here:", height=300)
|
179 |
beam_search = st.radio("Enable Beam Search?", ('No', 'Yes')) == 'Yes'
|
@@ -183,28 +192,26 @@ if st.button("Extract1"):
|
|
183 |
if input_text:
|
184 |
start_cause1, end_cause1, start_cause2, end_cause2, start_effect1, end_effect1, start_effect2, end_effect2, start_signal, end_signal = extract_arguments(input_text, tokenizer, model, beam_search=beam_search)
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
st.markdown(f"**Effect:**<br>{effect_text1}", unsafe_allow_html=True)
|
193 |
-
st.markdown(f"**Signal:**<br>{signal_text}", unsafe_allow_html=True)
|
194 |
|
195 |
-
#
|
|
|
|
|
|
|
|
|
196 |
|
|
|
197 |
if beam_search:
|
|
|
|
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
st.markdown(f"<span style='font-size: 24px;'><strong>Relation 2:</strong></span>", unsafe_allow_html=True)
|
204 |
-
st.markdown(f"**Cause:**<br>{cause_text2}", unsafe_allow_html=True)
|
205 |
-
st.markdown(f"**Effect:**<br>{effect_text2}", unsafe_allow_html=True)
|
206 |
-
st.markdown(f"**Signal:**<br>{signal_text}", unsafe_allow_html=True)
|
207 |
-
|
208 |
-
#st.write("List 2:", list2)
|
209 |
else:
|
210 |
st.warning("Please enter some text before extracting.")
|
|
|
174 |
)
|
175 |
return original_text # Return unchanged if indices are invalidt # Return unchanged text if no span is found
|
176 |
|
177 |
+
def mark_text_by_tokens(tokenizer, tokens, start_idx, end_idx, color):
|
178 |
+
"""Highlights a span in tokenized text using HTML."""
|
179 |
+
highlighted_tokens = copy.deepcopy(tokens) # Avoid modifying original tokens
|
180 |
+
if start_idx is not None and end_idx is not None and start_idx < end_idx:
|
181 |
+
highlighted_tokens[start_idx] = f"<span style='background-color:{color}; padding:2px; border-radius:4px;'>{highlighted_tokens[start_idx]}"
|
182 |
+
highlighted_tokens[end_idx] = f"{highlighted_tokens[end_idx]}</span>"
|
183 |
+
return tokenizer.convert_tokens_to_string(highlighted_tokens)
|
184 |
+
|
185 |
+
|
186 |
st.title("Causal Relation Extraction")
|
187 |
input_text = st.text_area("Enter your text here:", height=300)
|
188 |
beam_search = st.radio("Enable Beam Search?", ('No', 'Yes')) == 'Yes'
|
|
|
192 |
if input_text:
|
193 |
start_cause1, end_cause1, start_cause2, end_cause2, start_effect1, end_effect1, start_effect2, end_effect2, start_signal, end_signal = extract_arguments(input_text, tokenizer, model, beam_search=beam_search)
|
194 |
|
195 |
+
# Convert text to tokenized format
|
196 |
+
tokenized_input = tokenizer.tokenize(input_text)
|
197 |
+
|
198 |
+
cause_text1 = mark_text_by_tokens(tokenizer, tokenized_input, start_cause1, end_cause1, "#FFD700") # Gold for cause
|
199 |
+
effect_text1 = mark_text_by_tokens(tokenizer, tokenized_input, start_effect1, end_effect1, "#90EE90") # Light green for effect
|
200 |
+
signal_text = mark_text_by_tokens(tokenizer, tokenized_input, start_signal, end_signal, "#FF6347") # Tomato red for signal
|
|
|
|
|
201 |
|
202 |
+
# Display first relation
|
203 |
+
st.markdown(f"<strong>Relation 1:</strong>", unsafe_allow_html=True)
|
204 |
+
st.markdown(f"**Cause:** {cause_text1}", unsafe_allow_html=True)
|
205 |
+
st.markdown(f"**Effect:** {effect_text1}", unsafe_allow_html=True)
|
206 |
+
st.markdown(f"**Signal:** {signal_text}", unsafe_allow_html=True)
|
207 |
|
208 |
+
# Display second relation if beam search is enabled
|
209 |
if beam_search:
|
210 |
+
cause_text2 = mark_text_by_tokens(tokenizer, tokenized_input, start_cause2, end_cause2, "#FFD700")
|
211 |
+
effect_text2 = mark_text_by_tokens(tokenizer, tokenized_input, start_effect2, end_effect2, "#90EE90")
|
212 |
|
213 |
+
st.markdown(f"<strong>Relation 2:</strong>", unsafe_allow_html=True)
|
214 |
+
st.markdown(f"**Cause:** {cause_text2}", unsafe_allow_html=True)
|
215 |
+
st.markdown(f"**Effect:** {effect_text2}", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
else:
|
217 |
st.warning("Please enter some text before extracting.")
|