GLiNER2: Unified Schema-Based Information Extraction

1.3.0 · active · verified Thu Apr 16

GLiNER2 is an efficient, unified information extraction system that combines Named Entity Recognition (NER), Text Classification, Structured Data Extraction, and Relation Extraction into a single 205M-parameter model. Built on a fine-tuned transformer encoder, it provides CPU-based inference for local processing without requiring complex pipelines or external API dependencies, offering a powerful alternative to larger language models.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a GLiNER2 model from the Hugging Face Hub and use it to extract entities from a given text based on a list of defined labels. For API-based models (like GLiNER XL 1B), an API key is required, typically set via an environment variable.

import os
from gliner2 import GLiNER2

# For GLiNER XL 1B via API, uncomment and set environment variable:
# os.environ['PIONEER_API_KEY'] = os.getenv('PIONEER_API_KEY', 'your_api_key_here')
# extractor = GLiNER2.from_api() 

# Load a local pre-trained model
extractor = GLiNER2.from_pretrained("fastino/gliner2-base-v1")

text = "Apple CEO Tim Cook announced iPhone 15 in Cupertino yesterday."
labels = ["company", "person", "product", "location"]

# Perform entity extraction
result = extractor.extract_entities(text, labels)

print(result)

view raw JSON →