Views
No views yet

@inproceedings{huguet-cabot-navigli-2021-rebel-relation,
title = "{REBEL}: Relation Extraction By End-to-end Language generation",
author = "Huguet Cabot, Pere-Llu{\'\i}s and
Navigli, Roberto",
booktitle = "Findings of the Association for Computational Linguistics: EMNLP 2021",
month = nov,
year = "2021",
address = "Punta Cana, Dominican Republic",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2021.findings-emnlp.204",
pages = "2370--2381",
abstract = "Extracting relation triplets from raw text is a crucial task in Information Extraction, enabling multiple applications such as populating or validating knowledge bases, factchecking, and other downstream tasks. However, it usually involves multiple-step pipelines that propagate errors or are limited to a small number of relation types. To overcome these issues, we propose the use of autoregressive seq2seq models. Such models have previously been shown to perform well not only in language generation, but also in NLU tasks such as Entity Linking, thanks to their framing as seq2seq tasks. In this paper, we show how Relation Extraction can be simplified by expressing triplets as a sequence of text and we present REBEL, a seq2seq model based on BART that performs end-to-end relation extraction for more than 200 different relation types. We show our model{'}s flexibility by fine-tuning it on an array of Relation Extraction and Relation Classification benchmarks, with it attaining state-of-the-art performance in most of them.",
}1from transformers import pipeline
2
3triplet_extractor = pipeline('text2text-generation', model='Babelscape/rebel-large', tokenizer='Babelscape/rebel-large')
4# We need to use the tokenizer manually since we need special tokens.
5extracted_text = triplet_extractor.tokenizer.batch_decode([triplet_extractor("Punta Cana is a resort town in the municipality of Higuey, in La Altagracia Province, the eastern most province of the Dominican Republic", return_tensors=True, return_text=False)[0]["generated_token_ids"]])
6print(extracted_text[0])
7# Function to parse the generated text and extract the triplets
8def extract_triplets(text):
9 triplets = []
10 relation, subject, relation, object_ = '', '', '', ''
11 text = text.strip()
12 current = 'x'
13 for token in text.replace("<s>", "").replace("<pad>", "").replace("</s>", "").split():
14 if token == "<triplet>":
15 current = 't'
16 if relation != '':
17 triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
18 relation = ''
19 subject = ''
20 elif token == "<subj>":
21 current = 's'
22 if relation != '':
23 triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
24 object_ = ''
25 elif token == "<obj>":
26 current = 'o'
27 relation = ''
28 else:
29 if current == 't':
30 subject += ' ' + token
31 elif current == 's':
32 object_ += ' ' + token
33 elif current == 'o':
34 relation += ' ' + token
35 if subject != '' and relation != '' and object_ != '':
36 triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
37 return triplets
38extracted_triplets = extract_triplets(extracted_text[0])
39print(extracted_triplets)1from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
2
3def extract_triplets(text):
4 triplets = []
5 relation, subject, relation, object_ = '', '', '', ''
6 text = text.strip()
7 current = 'x'
8 for token in text.replace("<s>", "").replace("<pad>", "").replace("</s>", "").split():
9 if token == "<triplet>":
10 current = 't'
11 if relation != '':
12 triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
13 relation = ''
14 subject = ''
15 elif token == "<subj>":
16 current = 's'
17 if relation != '':
18 triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
19 object_ = ''
20 elif token == "<obj>":
21 current = 'o'
22 relation = ''
23 else:
24 if current == 't':
25 subject += ' ' + token
26 elif current == 's':
27 object_ += ' ' + token
28 elif current == 'o':
29 relation += ' ' + token
30 if subject != '' and relation != '' and object_ != '':
31 triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
32 return triplets
33
34# Load model and tokenizer
35tokenizer = AutoTokenizer.from_pretrained("Babelscape/rebel-large")
36model = AutoModelForSeq2SeqLM.from_pretrained("Babelscape/rebel-large")
37gen_kwargs = {
38 "max_length": 256,
39 "length_penalty": 0,
40 "num_beams": 3,
41 "num_return_sequences": 3,
42}
43
44# Text to extract triplets from
45text = 'Punta Cana is a resort town in the municipality of Higüey, in La Altagracia Province, the easternmost province of the Dominican Republic.'
46
47# Tokenizer text
48model_inputs = tokenizer(text, max_length=256, padding=True, truncation=True, return_tensors = 'pt')
49
50# Generate
51generated_tokens = model.generate(
52 model_inputs["input_ids"].to(model.device),
53 attention_mask=model_inputs["attention_mask"].to(model.device),
54 **gen_kwargs,
55)
56
57# Extract text
58decoded_preds = tokenizer.batch_decode(generated_tokens, skip_special_tokens=False)
59
60# Extract triplets
61for idx, sentence in enumerate(decoded_preds):
62 print(f'Prediction triplets sentence {idx}')
63 print(extract_triplets(sentence))