anamargarida commited on
Commit
2203049
·
verified ·
1 Parent(s): c75e162

Rename app_11.py to app_12.py

Browse files
Files changed (1) hide show
  1. app_11.py → app_12.py +69 -3
app_11.py → app_12.py RENAMED
@@ -154,11 +154,37 @@ def extract_arguments(text, tokenizer, model, beam_search=True):
154
  list1 = [start_cause1, end_cause1, start_effect1, end_effect1, start_signal, end_signal]
155
  list2 = [start_cause2, end_cause2, start_effect2, end_effect2, start_signal, end_signal]
156
  #return cause1, cause2, effect1, effect2, signal, list1, list2
157
- return start_cause1, end_cause1, start_cause2, end_cause2, start_effect1, end_effect1, start_effect2, end_effect2, start_signal, end_signal
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  def mark_text_by_position(original_text, start_idx, end_idx, color):
160
  """Marks text in the original string based on character positions."""
161
- if start_idx is not None and end_idx is not None and start_idx < end_idx:
162
  return (
163
  original_text[:start_idx]
164
  + f"<mark style='background-color:{color}; padding:2px; border-radius:4px;'>"
@@ -171,16 +197,55 @@ def mark_text_by_position(original_text, start_idx, end_idx, color):
171
  def mark_text_by_tokens(tokenizer, tokens, start_idx, end_idx, color):
172
  """Highlights a span in tokenized text using HTML."""
173
  highlighted_tokens = copy.deepcopy(tokens) # Avoid modifying original tokens
174
- if start_idx is not None and end_idx is not None and start_idx < end_idx:
175
  highlighted_tokens[start_idx] = f"<span style='background-color:{color}; padding:2px; border-radius:4px;'>{highlighted_tokens[start_idx]}"
176
  highlighted_tokens[end_idx] = f"{highlighted_tokens[end_idx]}</span>"
177
  return tokenizer.convert_tokens_to_string(highlighted_tokens)
178
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  st.title("Causal Relation Extraction")
181
  input_text = st.text_area("Enter your text here:", height=300)
182
  beam_search = st.radio("Enable Beam Search?", ('No', 'Yes')) == 'Yes'
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
  if st.button("Extract1"):
186
  if input_text:
@@ -207,5 +272,6 @@ if st.button("Extract1"):
207
  st.markdown(f"<strong>Relation 2:</strong>", unsafe_allow_html=True)
208
  st.markdown(f"**Cause:** {cause_text2}", unsafe_allow_html=True)
209
  st.markdown(f"**Effect:** {effect_text2}", unsafe_allow_html=True)
 
210
  else:
211
  st.warning("Please enter some text before extracting.")
 
154
  list1 = [start_cause1, end_cause1, start_effect1, end_effect1, start_signal, end_signal]
155
  list2 = [start_cause2, end_cause2, start_effect2, end_effect2, start_signal, end_signal]
156
  #return cause1, cause2, effect1, effect2, signal, list1, list2
157
+ #return start_cause1, end_cause1, start_cause2, end_cause2, start_effect1, end_effect1, start_effect2, end_effect2, start_signal, end_signal
158
+
159
+
160
+
161
+ # Add the argument tags in the sentence directly
162
+ def add_tags(tokens, start_cause1, end_cause1, start_effect1, end_effect1, start_signal, end_signal):
163
+ if start_cause is not None and end_cause is not None:
164
+ tokens[start_cause1] = '<ARG0>' + tokens[start_cause1]
165
+ tokens[end_cause1] = tokens[end_cause1] + '</ARG0>'
166
+
167
+ if start_effect is not None and end_effect is not None:
168
+ tokens[start_effect1] = '<ARG1>' + tokens[start_effect1]
169
+ tokens[end_effect1] = tokens[end_effect1] + '</ARG1>'
170
+
171
+ if start_signal is not None and end_signal is not None:
172
+ tokens[start_signal] = '<SIG0>' + tokens[start_signal]
173
+ tokens[end_signal] = tokens[end_signal] + '</SIG0>'
174
+
175
+ return ' '.join(tokens)
176
+
177
+ # Apply the tags to the sentence tokens
178
+ tagged_sentence1 = add_tags(tokens, start_cause1, end_cause1, start_effect1, end_effect1, start_signal, end_signal)
179
+ tagged_sentence2 = add_tags(tokens, start_cause2, end_cause2, start_effect2, end_effect2, start_signal, end_signal)
180
+ return tagged_sentence1, tagged_sentence2
181
+
182
+
183
+
184
 
185
  def mark_text_by_position(original_text, start_idx, end_idx, color):
186
  """Marks text in the original string based on character positions."""
187
+ if start_idx is not None and end_idx is not None and start_idx <= end_idx:
188
  return (
189
  original_text[:start_idx]
190
  + f"<mark style='background-color:{color}; padding:2px; border-radius:4px;'>"
 
197
  def mark_text_by_tokens(tokenizer, tokens, start_idx, end_idx, color):
198
  """Highlights a span in tokenized text using HTML."""
199
  highlighted_tokens = copy.deepcopy(tokens) # Avoid modifying original tokens
200
+ if start_idx is not None and end_idx is not None and start_idx <= end_idx:
201
  highlighted_tokens[start_idx] = f"<span style='background-color:{color}; padding:2px; border-radius:4px;'>{highlighted_tokens[start_idx]}"
202
  highlighted_tokens[end_idx] = f"{highlighted_tokens[end_idx]}</span>"
203
  return tokenizer.convert_tokens_to_string(highlighted_tokens)
204
 
205
+ def mark_text_by_word_ids(original_text, token_ids, start_word_id, end_word_id, color):
206
+ """Marks words in the original text based on word IDs from tokenized input."""
207
+ words = original_text.split() # Split text into words
208
+ if start_word_id is not None and end_word_id is not None and start_word_id <= end_word_id:
209
+ words[start_word_id] = f"<mark style='background-color:{color}; padding:2px; border-radius:4px;'>{words[start_word_id]}"
210
+ words[end_word_id] = f"{words[end_word_id]}</mark>"
211
+
212
+ return " ".join(words)
213
+
214
+
215
+
216
 
217
  st.title("Causal Relation Extraction")
218
  input_text = st.text_area("Enter your text here:", height=300)
219
  beam_search = st.radio("Enable Beam Search?", ('No', 'Yes')) == 'Yes'
220
 
221
+ if st.button("Add Argument Tags"):
222
+ if text_input:
223
+ tagged_sentence1, tagged_sentence2 = extract_arguments(input_text, tokenizer, model, beam_search=True)
224
+
225
+ st.write("**Tagged Sentence_1:**")
226
+ st.write(tagged_sentence1)
227
+ st.write("**Tagged Sentence_2:**")
228
+ st.write(tagged_sentence2)
229
+ else:
230
+ st.warning("Please enter some text to analyze.")
231
+
232
+
233
+ if st.button("Extract"):
234
+ if input_text:
235
+ start_cause_id, end_cause_id, start_effect_id, end_effect_id, start_signal_id, end_signal_id = extract_arguments(input_text, tokenizer, model, beam_search=beam_search)
236
+
237
+ cause_text = mark_text_by_word_ids(input_text, inputs["input_ids"][0], start_cause_id, end_cause_id, "#FFD700") # Gold for cause
238
+ effect_text = mark_text_by_word_ids(input_text, inputs["input_ids"][0], start_effect_id, end_effect_id, "#90EE90") # Light green for effect
239
+ signal_text = mark_text_by_word_ids(input_text, inputs["input_ids"][0], start_signal_id, end_signal_id, "#FF6347") # Tomato red for signal
240
+
241
+ st.markdown(f"**Cause:**<br>{cause_text}", unsafe_allow_html=True)
242
+ st.markdown(f"**Effect:**<br>{effect_text}", unsafe_allow_html=True)
243
+ st.markdown(f"**Signal:**<br>{signal_text}", unsafe_allow_html=True)
244
+ else:
245
+ st.warning("Please enter some text before extracting.")
246
+
247
+
248
+
249
 
250
  if st.button("Extract1"):
251
  if input_text:
 
272
  st.markdown(f"<strong>Relation 2:</strong>", unsafe_allow_html=True)
273
  st.markdown(f"**Cause:** {cause_text2}", unsafe_allow_html=True)
274
  st.markdown(f"**Effect:** {effect_text2}", unsafe_allow_html=True)
275
+ st.markdown(f"**Signal:** {signal_text}", unsafe_allow_html=True)
276
  else:
277
  st.warning("Please enter some text before extracting.")