anamargarida commited on
Commit
eefe007
·
verified ·
1 Parent(s): e237e11

Update app_3.py

Browse files
Files changed (1) hide show
  1. app_3.py +26 -31
app_3.py CHANGED
@@ -131,44 +131,41 @@ def extract_arguments(text, tokenizer, model, beam_search=True):
131
 
132
  tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
133
  token_ids = inputs["input_ids"][0]
134
- #offset_mapping = inputs["offset_mapping"][0].tolist()
135
-
136
-
137
-
138
- return start_cause1, end_cause1, start_cause2, end_cause2, start_effect1, end_effect1, start_effect2, end_effect2, start_signal, end_signal
139
-
140
-
141
-
142
- def mark_text_by_position(original_text, start_token, end_token, color):
143
- """Marks text in the original string based on character positions."""
144
- # Inserts tags into the original text based on token offsets.
145
-
146
  offset_mapping = inputs["offset_mapping"][0].tolist()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
- start_idx, end_idx = offset_mapping[start_token][0], offset_mapping[end_token][1]
149
-
150
- if start_idx is not None and end_idx is not None and start_idx <= end_idx:
151
- return (
152
- original_text[:start_idx]
153
- + f"<mark style='background-color:{color}; padding:2px; border-radius:4px;'>"
154
- + original_text[start_idx:end_idx]
155
- + "</mark>"
156
- + original_text[end_idx:]
157
- )
158
- return original_text
159
 
160
  st.title("Causal Relation Extraction")
161
  input_text = st.text_area("Enter your text here:", height=300)
162
  beam_search = st.radio("Enable Beam Search?", ('No', 'Yes')) == 'Yes'
163
 
164
 
165
- if st.button("Extract"):
166
  if input_text:
167
- 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)
168
-
169
- cause_text1 = mark_text_by_position(input_text, start_cause1, end_cause1, "#FFD700") # Gold for cause
170
- effect_text1 = mark_text_by_position(input_text, start_effect1, end_effect1, "#90EE90") # Light green for effect
171
- signal_text = mark_text_by_position(input_text, start_signal, end_signal, "#FF6347") # Tomato red for signal
172
 
173
  # Display first relation
174
  st.markdown(f"<strong>Relation 1:</strong>", unsafe_allow_html=True)
@@ -178,8 +175,6 @@ if st.button("Extract"):
178
 
179
  # Display second relation if beam search is enabled
180
  if beam_search:
181
- cause_text2 = mark_text_by_position(input_text, start_cause2, end_cause2, "#FFD700")
182
- effect_text2 = mark_text_by_position(input_text, start_effect2, end_effect2, "#90EE90")
183
 
184
  st.markdown(f"<strong>Relation 2:</strong>", unsafe_allow_html=True)
185
  st.markdown(f"**Cause:** {cause_text2}", unsafe_allow_html=True)
 
131
 
132
  tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
133
  token_ids = inputs["input_ids"][0]
 
 
 
 
 
 
 
 
 
 
 
 
134
  offset_mapping = inputs["offset_mapping"][0].tolist()
135
+
136
+ def mark_text_by_position(original_text, start_token, end_token, color):
137
+ """Marks text in the original string based on character positions."""
138
+ # Inserts tags into the original text based on token offsets.
139
+
140
+
141
+ start_idx, end_idx = offset_mapping[start_token][0], offset_mapping[end_token][1]
142
+
143
+ if start_idx is not None and end_idx is not None and start_idx <= end_idx:
144
+ return (
145
+ original_text[:start_idx]
146
+ + f"<mark style='background-color:{color}; padding:2px; border-radius:4px;'>"
147
+ + original_text[start_idx:end_idx]
148
+ + "</mark>"
149
+ + original_text[end_idx:]
150
+ )
151
+ return original_text
152
+
153
+ cause_text1 = mark_text_by_position(input_text, start_cause1, end_cause1, "#FFD700") # Gold for cause
154
+ effect_text1 = mark_text_by_position(input_text, start_effect1, end_effect1, "#90EE90") # Light green for effect
155
+ signal_text = mark_text_by_position(input_text, start_signal, end_signal, "#FF6347") # Tomato red for signal
156
+ cause_text2 = mark_text_by_position(input_text, start_cause2, end_cause2, "#FFD700")
157
+ effect_text2 = mark_text_by_position(input_text, start_effect2, end_effect2, "#90EE90")
158
 
159
+ return cause_text1, effect_text1, signal_text, cause_text2, effect_text2
 
 
 
 
 
 
 
 
 
 
160
 
161
  st.title("Causal Relation Extraction")
162
  input_text = st.text_area("Enter your text here:", height=300)
163
  beam_search = st.radio("Enable Beam Search?", ('No', 'Yes')) == 'Yes'
164
 
165
 
166
+ if st.button("Extract_Now"):
167
  if input_text:
168
+ cause_text1, effect_text1, signal_text, cause_text2, effect_text2 = extract_arguments(input_text, tokenizer, model, beam_search=beam_search)
 
 
 
 
169
 
170
  # Display first relation
171
  st.markdown(f"<strong>Relation 1:</strong>", unsafe_allow_html=True)
 
175
 
176
  # Display second relation if beam search is enabled
177
  if beam_search:
 
 
178
 
179
  st.markdown(f"<strong>Relation 2:</strong>", unsafe_allow_html=True)
180
  st.markdown(f"**Cause:** {cause_text2}", unsafe_allow_html=True)