Views
No views yet
"other" label to extract entities whose types are not explicitly defined but can be inferred from the specified relationspip install gliner -U1from gliner import GLiNER
2
3# Load the model
4model = GLiNER.from_pretrained("knowledgator/gliner-relex-large-v1.0")
5
6# Define your entity types and relation types
7entity_labels = ["location", "person", "date", "structure"]
8relation_labels = ["located in", "designed by", "completed in"]
9
10# Input text
11text = "The Eiffel Tower, located in Paris, France, was designed by engineer Gustave Eiffel and completed in 1889."
12
13# Run inference - returns both entities and relations
14entities, relations = model.inference(
15 texts=[text],
16 labels=entity_labels,
17 relations=relation_labels,
18 threshold=0.3,
19 relation_threshold=0.5,
20 return_relations=True,
21 flat_ner=False
22)
23
24# Print entities
25print("Entities:")
26for entity in entities[0]:
27 print(f" {entity['text']} -> {entity['label']} (score: {entity['score']:.3f})")
28
29# Print relations
30print("\nRelations:")
31for relation in relations[0]:
32 head = relation["head"]["text"]
33 tail = relation["tail"]["text"]
34 rel_type = relation["relation"]
35 score = relation["score"]
36 print(f" {head} --[{rel_type}]--> {tail} (score: {score:.3f})")1Entities:
2 Eiffel Tower -> structure (score: 0.912)
3 Paris -> location (score: 0.934)
4 France -> location (score: 0.891)
5 Gustave Eiffel -> person (score: 0.923)
6 1889 -> date (score: 0.856)
7
8Relations:
9 Eiffel Tower --[located in]--> Paris (score: 0.823)
10 Eiffel Tower --[designed by]--> Gustave Eiffel (score: 0.847)
11 Eiffel Tower --[completed in]--> 1889 (score: 0.789)1entity_labels = {
2 "person": "A human individual, including fictional characters",
3 "organization": "A company, institution, agency, or other group of people",
4 "location": "A physical place, geographic region, or address",
5 "date": "A calendar date, time period, or temporal expression"
6}
7relation_labels = ["works for", "located in", "founded on"]
8
9text = "Tim Cook has been leading Apple Inc. from its headquarters in Cupertino since 2011."
10
11entities, relations = model.inference(
12 texts=[text],
13 labels=entity_labels,
14 relations=relation_labels,
15 threshold=0.5,
16 relation_threshold=0.7,
17 return_relations=True,
18 flat_ner=False
19)"other" label. The model will identify entities based on the relations they participate in, even if their type does not match any of the explicitly defined labels:1entity_labels = ["person"]
2relation_labels = ["author of", "born in"]
3
4text = "Gabriel García Márquez, born in Aracataca, wrote One Hundred Years of Solitude."
5
6entities, relations = model.inference(
7 texts=[text],
8 labels=entity_labels + ["other"], # "other" captures relation-driven entities
9 relations=relation_labels,
10 threshold=0.5,
11 relation_threshold=0.7,
12 return_relations=True,
13 flat_ner=False
14)1Entities:
2 Gabriel García Márquez -> person (score: 0.931)
3 Aracataca -> other (score: 0.724)
4 One Hundred Years of Solitude -> other (score: 0.689)
5
6Relations:
7 Gabriel García Márquez --[born in]--> Aracataca (score: 0.812)
8 Gabriel García Márquez --[author of]--> One Hundred Years of Solitude (score: 0.795)1texts = [
2 "Elon Musk founded SpaceX in Hawthorne, California.",
3 "Microsoft, led by Satya Nadella, acquired GitHub in 2018.",
4 "The Louvre Museum in Paris houses the Mona Lisa."
5]
6
7entity_labels = ["person", "organization", "location", "artwork"]
8relation_labels = ["founder of", "CEO of", "located in", "acquired", "houses"]
9
10entities, relations = model.inference(
11 texts=texts,
12 labels=entity_labels,
13 relations=relation_labels,
14 threshold=0.5,
15 relation_threshold=0.5,
16 batch_size=8,
17 return_relations=True,
18 flat_ner=False
19)
20
21for i, (text_entities, text_relations) in enumerate(zip(entities, relations)):
22 print(f"\nText {i + 1}:")
23 print(f" Entities: {[e['text'] for e in text_entities]}")
24 print(f" Relations: {[(r['head']['text'], r['relation'], r['tail']['text']) for r in text_relations]}")1entities = model.inference(
2 texts=[text],
3 labels=entity_labels,
4 relations=[], # Empty list for relations
5 threshold=0.5,
6 return_relations=False, # Skip relation extraction
7 flat_ner=False
8)1entities, relations = model.inference(
2 texts=texts,
3 labels=entity_labels,
4 relations=relation_labels,
5 threshold=0.5, # Entity confidence threshold
6 adjacency_threshold=0.6, # Threshold for entity pair candidates
7 relation_threshold=0.7, # Relation classification threshold
8 flat_ner=True, # Enforce non-overlapping entities
9 multi_label=False, # Single label per entity span
10 return_relations=True
11)0.3–0.5. For adjacency_threshold, the model provides good results in the 0.5–0.65 range. For relation_threshold, use larger values like 0.7–0.9. Feel free to adjust all of these values based on your project requirements.1{
2 "start": int, # Start character position
3 "end": int, # End character position
4 "text": str, # Entity text span
5 "label": str, # Entity type
6 "score": float # Confidence score (0-1)
7}1{
2 "head": {
3 "start": int,
4 "end": int,
5 "text": str,
6 "type": str,
7 "entity_idx": int # Index in entities list
8 },
9 "tail": {
10 "start": int,
11 "end": int,
12 "text": str,
13 "type": str,
14 "entity_idx": int
15 },
16 "relation": str, # Relation type
17 "score": float # Confidence score (0-1)
18}