Spaces:
Sleeping
Sleeping
import streamlit as st | |
import torch | |
from transformers import AutoConfig, AutoTokenizer, AutoModel | |
from huggingface_hub import login | |
import re | |
import copy | |
from modeling_st2 import ST2ModelV2, SignalDetector | |
from huggingface_hub import hf_hub_download | |
from safetensors.torch import load_file | |
hf_token = st.secrets["HUGGINGFACE_TOKEN"] | |
login(token=hf_token) | |
# Load model & tokenizer once (cached for efficiency) | |
def load_model(): | |
config = AutoConfig.from_pretrained("roberta-large") | |
tokenizer = AutoTokenizer.from_pretrained("roberta-large", use_fast=True, add_prefix_space=True) | |
class Args: | |
def __init__(self): | |
self.dropout = 0.1 | |
self.signal_classification = True | |
self.pretrained_signal_detector = False | |
args = Args() | |
model = ST2ModelV2(args) | |
repo_id = "anamargarida/SpanExtractionWithSignalCls_2" | |
filename = "model.safetensors" | |
# Download the model file | |
model_path = hf_hub_download(repo_id=repo_id, filename=filename) | |
# Load the model weights | |
state_dict = load_file(model_path) | |
model.load_state_dict(state_dict) | |
return tokenizer, model | |
# Load the model and tokenizer | |
tokenizer, model = load_model() | |
model.eval() # Set model to evaluation mode | |
def extract_arguments(text, tokenizer, model, beam_search=True): | |
class Args: | |
def __init__(self): | |
self.signal_classification = True | |
self.pretrained_signal_detector = False | |
args = Args() | |
inputs = tokenizer(text, return_offsets_mapping=True, return_tensors="pt") | |
# Get tokenized words (for reconstruction later) | |
word_ids = inputs.word_ids() | |
with torch.no_grad(): | |
outputs = model(**inputs) | |
# Extract logits | |
start_cause_logits = outputs["start_arg0_logits"][0] | |
end_cause_logits = outputs["end_arg0_logits"][0] | |
start_effect_logits = outputs["start_arg1_logits"][0] | |
end_effect_logits = outputs["end_arg1_logits"][0] | |
start_signal_logits = outputs["start_sig_logits"][0] | |
end_signal_logits = outputs["end_sig_logits"][0] | |
# Set the first and last token logits to a very low value to ignore them | |
start_cause_logits[0] = -1e-4 | |
end_cause_logits[0] = -1e-4 | |
start_effect_logits[0] = -1e-4 | |
end_effect_logits[0] = -1e-4 | |
start_cause_logits[len(inputs["input_ids"][0]) - 1] = -1e-4 | |
end_cause_logits[len(inputs["input_ids"][0]) - 1] = -1e-4 | |
start_effect_logits[len(inputs["input_ids"][0]) - 1] = -1e-4 | |
end_effect_logits[len(inputs["input_ids"][0]) - 1] = -1e-4 | |
# Beam Search for position selection | |
if beam_search: | |
indices1, indices2, _, _, _ = model.beam_search_position_selector( | |
start_cause_logits=start_cause_logits, | |
end_cause_logits=end_cause_logits, | |
start_effect_logits=start_effect_logits, | |
end_effect_logits=end_effect_logits, | |
topk=5 | |
) | |
start_cause1, end_cause1, start_effect1, end_effect1 = indices1 | |
start_cause2, end_cause2, start_effect2, end_effect2 = indices2 | |
else: | |
start_cause1 = start_cause_logits.argmax().item() | |
end_cause1 = end_cause_logits.argmax().item() | |
start_effect1 = start_effect_logits.argmax().item() | |
end_effect1 = end_effect_logits.argmax().item() | |
start_cause2, end_cause2, start_effect2, end_effect2 = None, None, None, None | |
has_signal = 1 | |
if args.signal_classification: | |
if not args.pretrained_signal_detector: | |
has_signal = outputs["signal_classification_logits"].argmax().item() | |
else: | |
has_signal = signal_detector.predict(text=batch["text"]) | |
if has_signal: | |
start_signal_logits[0] = -1e-4 | |
end_signal_logits[0] = -1e-4 | |
start_signal_logits[len(inputs["input_ids"][0]) - 1] = -1e-4 | |
end_signal_logits[len(inputs["input_ids"][0]) - 1] = -1e-4 | |
start_signal = start_signal_logits.argmax().item() | |
end_signal_logits[:start_signal] = -1e4 | |
end_signal_logits[start_signal + 5:] = -1e4 | |
end_signal = end_signal_logits.argmax().item() | |
if not has_signal: | |
start_signal, end_signal = None, None | |
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) | |
token_ids = inputs["input_ids"][0] | |
offset_mapping = inputs["offset_mapping"][0].tolist() | |
for i, (token, word_id) in enumerate(zip(tokens, word_ids)): | |
st.write(f"Token {i}: {token}, Word ID: {word_id}") | |
st.write("Token & offset:") | |
for i, (token, offset) in enumerate(zip(tokens, offset_mapping)): | |
st.write(f"Token {i}: {token}, Offset: {offset}") | |
st.write("Token Positions, IDs, and Corresponding Tokens:") | |
for position, (token_id, token) in enumerate(zip(token_ids, tokens)): | |
st.write(f"Position: {position}, ID: {token_id}, Token: {token}") | |
st.write(f"Start Cause 1: {start_cause1}, End Cause: {end_cause1}") | |
st.write(f"Start Effect 1: {start_effect1}, End Cause: {end_effect1}") | |
st.write(f"Start Signal: {start_signal}, End Signal: {end_signal}") | |
def extract_span(start, end): | |
return tokenizer.convert_tokens_to_string(tokens[start:end+1]) if start is not None and end is not None else "" | |
cause1 = extract_span(start_cause1, end_cause1) | |
cause2 = extract_span(start_cause2, end_cause2) | |
effect1 = extract_span(start_effect1, end_effect1) | |
effect2 = extract_span(start_effect2, end_effect2) | |
if has_signal: | |
signal = extract_span(start_signal, end_signal) | |
if not has_signal: | |
signal = 'NA' | |
list1 = [start_cause1, end_cause1, start_effect1, end_effect1, start_signal, end_signal] | |
list2 = [start_cause2, end_cause2, start_effect2, end_effect2, start_signal, end_signal] | |
#return cause1, cause2, effect1, effect2, signal, list1, list2 | |
#return start_cause1, end_cause1, start_cause2, end_cause2, start_effect1, end_effect1, start_effect2, end_effect2, start_signal, end_signal | |
# Find the first valid token in a multi-token word | |
def find_valid_start(position): | |
while position > 0 and word_ids[position] == word_ids[position - 1]: | |
position -= 1 | |
return position | |
def find_valid_end(position): | |
while position < len(word_ids) - 1 and word_ids[position] == word_ids[position + 1]: | |
position += 1 | |
return position | |
# Add the argument tags in the sentence directly | |
def add_tags(original_text, word_ids, start_cause, end_cause, start_effect, end_effect, start_signal, end_signal): | |
space_splitted_tokens = original_text.split(" ") | |
this_space_splitted_tokens = copy.deepcopy(space_splitted_tokens) | |
def safe_insert(tag, position, start=True): | |
"""Safely insert a tag, checking for None values and index validity.""" | |
if position is not None and word_ids[position] is not None: | |
word_index = word_ids[position] | |
# Ensure word_index is within range | |
if 0 <= word_index < len(this_space_splitted_tokens): | |
if start: | |
this_space_splitted_tokens[word_index] = tag + this_space_splitted_tokens[word_index] | |
else: | |
this_space_splitted_tokens[word_index] += tag | |
# Add argument tags safely | |
safe_insert('<ARG0>', start_cause, start=True) | |
safe_insert('</ARG0>', end_cause, start=False) | |
safe_insert('<ARG1>', start_effect, start=True) | |
safe_insert('</ARG1>', end_effect, start=False) | |
# Add signal tags safely (if signal exists) | |
if start_signal is not None and end_signal is not None: | |
safe_insert('<SIG0>', start_signal, start=True) | |
safe_insert('</SIG0>', end_signal, start=False) | |
# Join tokens back into a string | |
return ' '.join(this_space_splitted_tokens) | |
def add_tags_find(original_text, word_ids, start_cause, end_cause, start_effect, end_effect, start_signal, end_signal): | |
space_splitted_tokens = original_text.split(" ") | |
this_space_splitted_tokens = copy.deepcopy(space_splitted_tokens) | |
def safe_insert(tag, position, start=True): | |
"""Safely insert a tag, checking for None values and index validity.""" | |
if position is not None and word_ids[position] is not None: | |
word_index = word_ids[position] | |
# Ensure word_index is within range | |
if 0 <= word_index < len(this_space_splitted_tokens): | |
if start: | |
this_space_splitted_tokens[word_index] = tag + this_space_splitted_tokens[word_index] | |
else: | |
this_space_splitted_tokens[word_index] += tag | |
# Find valid start and end positions for words | |
start_cause = find_valid_start(start_cause) | |
end_cause = find_valid_end(end_cause) | |
start_effect = find_valid_start(start_effect) | |
end_effect = find_valid_end(end_effect) | |
if start_signal is not None: | |
start_signal = find_valid_start(start_signal) | |
end_signal = find_valid_end(end_signal) | |
# Adjust for punctuation shifts | |
if tokens[end_cause] in [".", ",", "-", ":", ";"]: | |
end_cause -= 1 | |
if tokens[end_effect] in [".", ",", "-", ":", ";"]: | |
end_effect -= 1 | |
# Add argument tags safely | |
safe_insert('<ARG0>', start_cause, start=True) | |
safe_insert('</ARG0>', end_cause, start=False) | |
safe_insert('<ARG1>', start_effect, start=True) | |
safe_insert('</ARG1>', end_effect, start=False) | |
# Add signal tags safely (if signal exists) | |
if start_signal is not None and end_signal is not None: | |
safe_insert('<SIG0>', start_signal, start=True) | |
safe_insert('</SIG0>', end_signal, start=False) | |
# Join tokens back into a string | |
return ' '.join(this_space_splitted_tokens) | |
def add_tags_offset(text, start_cause, end_cause, start_effect, end_effect, start_signal, end_signal): | |
""" | |
Inserts tags into the original text based on token offsets. | |
Args: | |
text (str): The original input text. | |
tokenizer: The tokenizer used for tokenization. | |
start_cause (int): Start token index of the cause span. | |
end_cause (int): End token index of the cause span. | |
start_effect (int): Start token index of the effect span. | |
end_effect (int): End token index of the effect span. | |
start_signal (int, optional): Start token index of the signal span. | |
end_signal (int, optional): End token index of the signal span. | |
Returns: | |
str: The modified text with annotated spans. | |
""" | |
# Convert token-based indices to character-based indices | |
start_cause_char, end_cause_char = offset_mapping[start_cause][0], offset_mapping[end_cause][1] | |
start_effect_char, end_effect_char = offset_mapping[start_effect][0], offset_mapping[end_effect][1] | |
# Insert tags into the original text | |
annotated_text = text[:start_cause_char] + "<ARG0>" + text[start_cause_char:end_cause_char] + "</ARG0>" + text[end_cause_char:start_effect_char] + "<ARG1>" + text[start_effect_char:end_effect_char] + "</ARG1>" + text[end_effect_char:] | |
# If signal span exists, insert signal tags | |
if start_signal is not None and end_signal is not None: | |
start_signal_char, end_signal_char = offset_mapping[start_signal][0], offset_mapping[end_signal][1] | |
annotated_text = ( | |
annotated_text[:start_signal_char] | |
+ "<SIG0>" + annotated_text[start_signal_char:end_signal_char] + "</SIG0>" | |
+ annotated_text[end_signal_char:] | |
) | |
return annotated_text | |
def add_tags_offset_2(text, start_cause, end_cause, start_effect, end_effect, start_signal, end_signal): | |
""" | |
Inserts tags into the original text based on token offsets. | |
Args: | |
text (str): The original input text. | |
offset_mapping (list of tuples): Maps token indices to character spans. | |
start_cause (int): Start token index of the cause span. | |
end_cause (int): End token index of the cause span. | |
start_effect (int): Start token index of the effect span. | |
end_effect (int): End token index of the effect span. | |
start_signal (int, optional): Start token index of the signal span. | |
end_signal (int, optional): End token index of the signal span. | |
Returns: | |
str: The modified text with annotated spans. | |
""" | |
# Convert token indices to character indices | |
spans = [ | |
(offset_mapping[start_cause][0], offset_mapping[end_cause][1], "<ARG0>", "</ARG0>"), | |
(offset_mapping[start_effect][0], offset_mapping[end_effect][1], "<ARG1>", "</ARG1>") | |
] | |
# Include signal tags if available | |
if start_signal is not None and end_signal is not None: | |
spans.append((offset_mapping[start_signal][0], offset_mapping[end_signal][1], "<SIG0>", "</SIG0>")) | |
# Sort spans in reverse order based on start index (to avoid shifting issues) | |
spans.sort(reverse=True, key=lambda x: x[0]) | |
# Insert tags | |
for start, end, open_tag, close_tag in spans: | |
text = text[:start] + open_tag + text[start:end] + close_tag + text[end:] | |
return text | |
import re | |
def add_tags_offset_3(text, start_cause, end_cause, start_effect, end_effect, start_signal, end_signal): | |
""" | |
Inserts tags into the original text based on token offsets, ensuring correct nesting, | |
avoiding empty tags, preventing duplication, and handling punctuation placement. | |
Args: | |
text (str): The original input text. | |
offset_mapping (list of tuples): Maps token indices to character spans. | |
start_cause (int): Start token index of the cause span. | |
end_cause (int): End token index of the cause span. | |
start_effect (int): Start token index of the effect span. | |
end_effect (int): End token index of the effect span. | |
start_signal (int, optional): Start token index of the signal span. | |
end_signal (int, optional): End token index of the signal span. | |
Returns: | |
str: The modified text with correctly positioned annotated spans. | |
""" | |
# Convert token indices to character indices | |
spans = [] | |
# Function to adjust start position to avoid punctuation issues | |
def adjust_start(text, start): | |
while start < len(text) and text[start] in {',', ' ', '.', ';', ':'}: | |
start += 1 # Move past punctuation | |
return start | |
# Ensure valid spans (avoid empty tags) | |
if start_cause is not None and end_cause is not None and start_cause < end_cause: | |
start_cause_char, end_cause_char = offset_mapping[start_cause][0], offset_mapping[end_cause][1] | |
spans.append((start_cause_char, end_cause_char, "<ARG0>", "</ARG0>")) | |
if start_effect is not None and end_effect is not None and start_effect < end_effect: | |
start_effect_char, end_effect_char = offset_mapping[start_effect][0], offset_mapping[end_effect][1] | |
start_effect_char = adjust_start(text, start_effect_char) # Skip punctuation | |
spans.append((start_effect_char, end_effect_char, "<ARG1>", "</ARG1>")) | |
if start_signal is not None and end_signal is not None and start_signal < end_signal: | |
start_signal_char, end_signal_char = offset_mapping[start_signal][0], offset_mapping[end_signal][1] | |
spans.append((start_signal_char, end_signal_char, "<SIG0>", "</SIG0>")) | |
# Sort spans in reverse order based on start index (to avoid shifting issues) | |
spans.sort(reverse=True, key=lambda x: x[0]) | |
# Insert tags correctly | |
modified_text = text | |
inserted_positions = [] | |
for start, end, open_tag, close_tag in spans: | |
# Adjust positions based on previous insertions | |
shift = sum(len(tag) for pos, tag in inserted_positions if pos <= start) | |
start += shift | |
end += shift | |
# Ensure valid start/end to prevent empty tags | |
if start < end: | |
modified_text = modified_text[:start] + open_tag + modified_text[start:end] + close_tag + modified_text[end:] | |
inserted_positions.append((start, open_tag)) | |
inserted_positions.append((end + len(open_tag), close_tag)) | |
return modified_text | |
tagged_sentence1 = add_tags_offset_3(input_text, start_cause1, end_cause1, start_effect1, end_effect1, start_signal, end_signal) | |
tagged_sentence2 = add_tags_offset_3(input_text, start_cause2, end_cause2, start_effect2, end_effect2, start_signal, end_signal) | |
return tagged_sentence1, tagged_sentence2 | |
def mark_text_by_position(original_text, start_idx, end_idx, color): | |
"""Marks text in the original string based on character positions.""" | |
if start_idx is not None and end_idx is not None and start_idx <= end_idx: | |
return ( | |
original_text[:start_idx] | |
+ f"<mark style='background-color:{color}; padding:2px; border-radius:4px;'>" | |
+ original_text[start_idx:end_idx] | |
+ "</mark>" | |
+ original_text[end_idx:] | |
) | |
return original_text # Return unchanged if indices are invalidt # Return unchanged text if no span is found | |
def mark_text_by_tokens(tokenizer, tokens, start_idx, end_idx, color): | |
"""Highlights a span in tokenized text using HTML.""" | |
highlighted_tokens = copy.deepcopy(tokens) # Avoid modifying original tokens | |
if start_idx is not None and end_idx is not None and start_idx <= end_idx: | |
highlighted_tokens[start_idx] = f"<span style='background-color:{color}; padding:2px; border-radius:4px;'>{highlighted_tokens[start_idx]}" | |
highlighted_tokens[end_idx] = f"{highlighted_tokens[end_idx]}</span>" | |
return tokenizer.convert_tokens_to_string(highlighted_tokens) | |
def mark_text_by_word_ids(original_text, token_ids, start_word_id, end_word_id, color): | |
"""Marks words in the original text based on word IDs from tokenized input.""" | |
words = original_text.split() # Split text into words | |
if start_word_id is not None and end_word_id is not None and start_word_id <= end_word_id: | |
words[start_word_id] = f"<mark style='background-color:{color}; padding:2px; border-radius:4px;'>{words[start_word_id]}" | |
words[end_word_id] = f"{words[end_word_id]}</mark>" | |
return " ".join(words) | |
st.title("Causal Relation Extraction") | |
input_text = st.text_area("Enter your text here:", height=300) | |
beam_search = st.radio("Enable Beam Search?", ('No', 'Yes')) == 'Yes' | |
if st.button("Add Argument Tags"): | |
if input_text: | |
tagged_sentence1, tagged_sentence2 = extract_arguments(input_text, tokenizer, model, beam_search=True) | |
st.write("**Tagged Sentence_1:**") | |
st.write(tagged_sentence1) | |
st.write("**Tagged Sentence_2:**") | |
st.write(tagged_sentence2) | |
else: | |
st.warning("Please enter some text to analyze.") | |
if st.button("Extract"): | |
if input_text: | |
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) | |
cause_text = mark_text_by_word_ids(input_text, inputs["input_ids"][0], start_cause_id, end_cause_id, "#FFD700") # Gold for cause | |
effect_text = mark_text_by_word_ids(input_text, inputs["input_ids"][0], start_effect_id, end_effect_id, "#90EE90") # Light green for effect | |
signal_text = mark_text_by_word_ids(input_text, inputs["input_ids"][0], start_signal_id, end_signal_id, "#FF6347") # Tomato red for signal | |
st.markdown(f"**Cause:**<br>{cause_text}", unsafe_allow_html=True) | |
st.markdown(f"**Effect:**<br>{effect_text}", unsafe_allow_html=True) | |
st.markdown(f"**Signal:**<br>{signal_text}", unsafe_allow_html=True) | |
else: | |
st.warning("Please enter some text before extracting.") | |
if st.button("Extract1"): | |
if input_text: | |
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) | |
# Convert text to tokenized format | |
tokenized_input = tokenizer.tokenize(input_text) | |
cause_text1 = mark_text_by_tokens(tokenizer, tokenized_input, start_cause1, end_cause1, "#FFD700") # Gold for cause | |
effect_text1 = mark_text_by_tokens(tokenizer, tokenized_input, start_effect1, end_effect1, "#90EE90") # Light green for effect | |
signal_text = mark_text_by_tokens(tokenizer, tokenized_input, start_signal, end_signal, "#FF6347") # Tomato red for signal | |
# Display first relation | |
st.markdown(f"<strong>Relation 1:</strong>", unsafe_allow_html=True) | |
st.markdown(f"**Cause:** {cause_text1}", unsafe_allow_html=True) | |
st.markdown(f"**Effect:** {effect_text1}", unsafe_allow_html=True) | |
st.markdown(f"**Signal:** {signal_text}", unsafe_allow_html=True) | |
# Display second relation if beam search is enabled | |
if beam_search: | |
cause_text2 = mark_text_by_tokens(tokenizer, tokenized_input, start_cause2, end_cause2, "#FFD700") | |
effect_text2 = mark_text_by_tokens(tokenizer, tokenized_input, start_effect2, end_effect2, "#90EE90") | |
st.markdown(f"<strong>Relation 2:</strong>", unsafe_allow_html=True) | |
st.markdown(f"**Cause:** {cause_text2}", unsafe_allow_html=True) | |
st.markdown(f"**Effect:** {effect_text2}", unsafe_allow_html=True) | |
st.markdown(f"**Signal:** {signal_text}", unsafe_allow_html=True) | |
else: | |
st.warning("Please enter some text before extracting.") |