Views
No views yet
pip install glirelGLiREL class. You can then load this model using GLiREL.from_pretrained and predict entities with predict_relations.1from glirel import GLiREL
2import spacy
3
4model = GLiREL.from_pretrained("jackboyla/glirel_beta")
5
6nlp = spacy.load('en_core_web_sm')
7
8text = 'Derren Nesbitt had a history of being cast in "Doctor Who", having played villainous warlord Tegana in the 1964 First Doctor serial "Marco Polo".'
9doc = nlp(text)
10tokens = [token.text for token in doc]
11
12labels = ['country of origin', 'licensed to broadcast to', 'father', 'followed by', 'characters']
13
14ner = [[26, 27, 'PERSON', 'Marco Polo'], [22, 23, 'Q2989412', 'First Doctor']] # 'type' is not used -- it can be any string!
15
16relations = model.predict_relations(tokens, labels, threshold=0.0, ner=ner, top_k=1)
17
18print('Number of relations:', len(relations))
19
20sorted_data_desc = sorted(relations, key=lambda x: x['score'], reverse=True)
21print("\nDescending Order by Score:")
22for item in sorted_data_desc:
23 print(f"{item['head_text']} --> {item['label']} --> {item['tail_text']} | score: {item['score']}")Number of relations: 2
Descending Order by Score:
{'head_pos': [26, 28], 'tail_pos': [22, 24], 'head_text': ['Marco', 'Polo'], 'tail_text': ['First', 'Doctor'], 'label': 'characters', 'score': 0.9923334121704102}
{'head_pos': [22, 24], 'tail_pos': [26, 28], 'head_text': ['First', 'Doctor'], 'tail_text': ['Marco', 'Polo'], 'label': 'characters', 'score': 0.9915636777877808}1labels = {"glirel_labels": {
2 'co-founder': {"allowed_head": ["PERSON"], "allowed_tail": ["ORG"]},
3 'no relation': {}, # head and tail can be any entity type
4 'country of origin': {"allowed_head": ["PERSON", "ORG"], "allowed_tail": ["LOC", "GPE"]},
5 'parent': {"allowed_head": ["PERSON"], "allowed_tail": ["PERSON"]},
6 'located in or next to body of water': {"allowed_head": ["LOC", "GPE", "FAC"], "allowed_tail": ["LOC", "GPE"]},
7 'spouse': {"allowed_head": ["PERSON"], "allowed_tail": ["PERSON"]},
8 'child': {"allowed_head": ["PERSON"], "allowed_tail": ["PERSON"]},
9 'founder': {"allowed_head": ["PERSON"], "allowed_tail": ["ORG"]},
10 'founded on date': {"allowed_head": ["ORG"], "allowed_tail": ["DATE"]},
11 'headquartered in': {"allowed_head": ["ORG"], "allowed_tail": ["LOC", "GPE", "FAC"]},
12 'acquired by': {"allowed_head": ["ORG"], "allowed_tail": ["ORG", "PERSON"]},
13 'subsidiary of': {"allowed_head": ["ORG"], "allowed_tail": ["ORG", "PERSON"]},
14 }
15}1import spacy
2import glirel
3
4# Load a blank spaCy model or an existing one
5nlp = spacy.load('en_core_web_sm')
6
7# Add the GLiREL component to the pipeline
8nlp.add_pipe("glirel", after="ner")
9
10# Now you can use the pipeline with the GLiREL component
11text = "Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in April 1976. The company is headquartered in Cupertino, California."
12
13labels = {"glirel_labels": {
14 'co-founder': {"allowed_head": ["PERSON"], "allowed_tail": ["ORG"]},
15 'country of origin': {"allowed_head": ["PERSON", "ORG"], "allowed_tail": ["LOC", "GPE"]},
16 'licensed to broadcast to': {"allowed_head": ["ORG"]},
17 'no relation': {},
18 'parent': {"allowed_head": ["PERSON"], "allowed_tail": ["PERSON"]},
19 'followed by': {"allowed_head": ["PERSON", "ORG"], "allowed_tail": ["PERSON", "ORG"]},
20 'located in or next to body of water': {"allowed_head": ["LOC", "GPE", "FAC"], "allowed_tail": ["LOC", "GPE"]},
21 'spouse': {"allowed_head": ["PERSON"], "allowed_tail": ["PERSON"]},
22 'child': {"allowed_head": ["PERSON"], "allowed_tail": ["PERSON"]},
23 'founder': {"allowed_head": ["PERSON"], "allowed_tail": ["ORG"]},
24 'headquartered in': {"allowed_head": ["ORG"], "allowed_tail": ["LOC", "GPE", "FAC"]},
25 'acquired by': {"allowed_head": ["ORG"], "allowed_tail": ["ORG", "PERSON"]},
26 'subsidiary of': {"allowed_head": ["ORG"], "allowed_tail": ["ORG", "PERSON"]},
27 }
28}
29
30# Add the labels to the pipeline at inference time
31docs = list( nlp.pipe([(text, labels)], as_tuples=True) )
32relations = docs[0][0]._.relations
33
34print('Number of relations:', len(relations))
35
36sorted_data_desc = sorted(relations, key=lambda x: x['score'], reverse=True)
37print("\nDescending Order by Score:")
38for item in sorted_data_desc:
39 print(f"{item['head_text']} --> {item['label']} --> {item['tail_text']} | score: {item['score']}")
40Number of relations: 5
Descending Order by Score:
['Apple', 'Inc.'] --> headquartered in --> ['California'] | score: 0.9854260683059692
['Apple', 'Inc.'] --> headquartered in --> ['Cupertino'] | score: 0.9569844603538513
['Steve', 'Wozniak'] --> co-founder --> ['Apple', 'Inc.'] | score: 0.09025496244430542
['Steve', 'Jobs'] --> co-founder --> ['Apple', 'Inc.'] | score: 0.08805803954601288
['Ronald', 'Wayne'] --> co-founder --> ['Apple', 'Inc.'] | score: 0.07996643334627151"Binsey" is [7, 7]. This differs from spaCy where the end index is exclusive (in this case spaCy would set the indices to [7, 8])1{
2 "ner": [
3 [7, 7, "Q4914513", "Binsey"],
4 [11, 12, "Q19686", "River Thames"]
5 ],
6 "relations": [
7 {
8 "head": {"mention": "Binsey", "position": [7, 7], "type": "LOC"}, # 'type' is not used -- it can be any string!
9 "tail": {"mention": "River Thames", "position": [11, 12], "type": "Q19686"},
10 "relation_text": "located in or next to body of water"
11 }
12 ],
13 "tokenized_text": ["The", "race", "took", "place", "between", "Godstow", "and", "Binsey", "along", "the", "Upper", "River", "Thames", "."]
14},
15{
16 "ner": [
17 [9, 10, "Q4386693", "Legislative Assembly"],
18 [1, 3, "Q1848835", "Parliament of Victoria"]
19 ],
20 "relations": [
21 {
22 "head": {"mention": "Legislative Assembly", "position": [9, 10], "type": "Q4386693"},
23 "tail": {"mention": "Parliament of Victoria", "position": [1, 3], "type": "Q1848835"},
24 "relation_text": "part of"
25 }
26 ],
27 "tokenized_text": ["The", "Parliament", "of", "Victoria", "consists", "of", "the", "lower", "house", "Legislative", "Assembly", ",", "the", "upper", "house", "Legislative", "Council", "and", "the", "Queen", "of", "Australia", "."]
28}@misc{boylan2025glirelgeneralistmodel,
title={GLiREL -- Generalist Model for Zero-Shot Relation Extraction},
author={Jack Boylan and Chris Hokamp and Demian Gholipour Ghalandari},
year={2025},
eprint={2501.03172},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2501.03172},
}