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.
pip install gliner2Verified import paths — ran on the pinned version, not inferred.
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.
Rewrite extraction logic to use GLiNER2's unified schema-based interface (e.g., `create_schema().entities().classification().structure()`) and the `extract()` method.
Implement robust post-processing and validation (e.g., using libraries like Pydantic) to handle `null` values or unexpected structures in the extracted output. Be aware of the model's limitations regarding inference-heavy tasks.
Carefully define and test relation types, using distinct and descriptive labels. Provide explicit natural language descriptions for labels within the schema to guide the model more effectively.
Obtain an API key from gliner.pioneer.ai and set it as the `PIONEER_API_KEY` environment variable, or pass it explicitly to `GLiNER2.from_api(api_key="your_key")`.
Install the library using pip: `pip install gliner2`.
Verify the model name against available models on Hugging Face (e.g., `fastino/gliner2-base-v1`, `fastino/gliner2-large-v1`). Ensure a stable internet connection. If behind a firewall, configure proxy settings.
Provide descriptive and specific labels for entity types. For improved accuracy, consider using natural language descriptions for each entity type within the schema to guide the model more effectively (e.g., instead of just `['event']`, try `['historical events, wars, or conflicts']`).