Swift/MLX implementation of GLiNER2 - a unified schema-based information extraction framework. (Article)
- Features
- Requirements
- Installation
- Quick Start
- Available Models
- API Reference
- Architecture
- Performance
- Work in Progress
- Contributing
- Acknowledgments
- Named Entity Recognition (NER)
- Text Classification
- Structured Data Extraction
- Relation Extraction
- Native Apple Silicon support via MLX
- CPU-first design - no GPU required
- macOS 14.0+
- Swift 5.9+
- Apple Silicon (M1/M2/M3)
Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/MacPaw/Gliner2Swift", branch: "main"),
]Or in Xcode: File → Add Package Dependencies → Enter the repository URL.
import GLiNER2Swift
// Load model (downloads automatically from HuggingFace)
let model = try await GLiNER2.fromPretrained("fastino/gliner2-base-v1")
// Extract entities
let text = "Tim Cook is CEO of Apple in Cupertino."
let entities = try model.extractEntities(
from: text,
labels: ["person", "company", "location"]
)
for entity in entities {
print("\(entity.label): \(entity.text) [\(entity.start)-\(entity.end)]")
}
// Output:
// person: Tim Cook [0-8]
// company: Apple [23-28]
// location: Cupertino [32-41]| Model | Parameters | HuggingFace ID |
|---|---|---|
| Base | 205M | fastino/gliner2-base-v1 |
let entities = try model.extractEntities(
from: "Your text here",
labels: ["person", "organization", "location"]
)let classification = try model.classifyText(
"Great product, highly recommend!",
labels: ["positive", "negative", "neutral"]
)let schema = model.createSchema()
.entities(["person", "company"])
.classification(task: "sentiment", labels: ["positive", "negative"])
let result = try model.extract(from: text, schema: schema)GLiNER2Swift is a direct port of the Python GLiNER2 implementation, achieving numerical parity with the reference implementation:
- Encoder: DeBERTa v3 with disentangled attention
- Span Marker: MLP-based span representation
- Count LSTM: For predicting entity counts
- Downscaled Transformer: For schema embedding
On Apple Silicon (M1/M2/M3):
- Model loading: ~2 seconds
- Inference: ~50ms per sentence (varies by length)
This is an active port of the Python GLiNER2 implementation. The following features are not yet implemented:
- Training loop - Fine-tuning and training from scratch are not yet supported
- Relation extraction - Schema-based relation extraction between entities
- LoRA adapters - Low-rank adaptation for efficient fine-tuning
- Additional GLiNER models - Currently only
deberta-v3-baseis supported; other model variants are not yet available
Contributions and PRs are welcome!
See CONTRIBUTING.md for guidelines on submitting PRs, branch naming conventions, and parity testing requirements.