gliformer 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gliformer-0.1.0/PKG-INFO +408 -0
- gliformer-0.1.0/README.md +361 -0
- gliformer-0.1.0/gliformer/__init__.py +96 -0
- gliformer-0.1.0/gliformer/backbones/__init__.py +161 -0
- gliformer-0.1.0/gliformer/backbones/deberta_2d.py +898 -0
- gliformer-0.1.0/gliformer/backbones/flash_deberta.py +231 -0
- gliformer-0.1.0/gliformer/backbones/qwen3.py +483 -0
- gliformer-0.1.0/gliformer/backbones/qwen3_5.py +366 -0
- gliformer-0.1.0/gliformer/config.py +3141 -0
- gliformer-0.1.0/gliformer/encoders/__init__.py +56 -0
- gliformer-0.1.0/gliformer/encoders/audio.py +424 -0
- gliformer-0.1.0/gliformer/encoders/base.py +634 -0
- gliformer-0.1.0/gliformer/encoders/media.py +311 -0
- gliformer-0.1.0/gliformer/encoders/omni.py +879 -0
- gliformer-0.1.0/gliformer/encoders/text.py +122 -0
- gliformer-0.1.0/gliformer/encoders/vision.py +425 -0
- gliformer-0.1.0/gliformer/gliformer.py +2139 -0
- gliformer-0.1.0/gliformer/layers/__init__.py +123 -0
- gliformer-0.1.0/gliformer/layers/anchor_layer.py +970 -0
- gliformer-0.1.0/gliformer/layers/anchor_modeling.py +341 -0
- gliformer-0.1.0/gliformer/layers/anchor_normalization.py +335 -0
- gliformer-0.1.0/gliformer/layers/anchor_relations.py +258 -0
- gliformer-0.1.0/gliformer/layers/anchored_scorer.py +78 -0
- gliformer-0.1.0/gliformer/layers/attention.py +136 -0
- gliformer-0.1.0/gliformer/layers/attention_bias.py +601 -0
- gliformer-0.1.0/gliformer/layers/groups.py +1222 -0
- gliformer-0.1.0/gliformer/layers/mlp.py +55 -0
- gliformer-0.1.0/gliformer/layers/pair_rep.py +136 -0
- gliformer-0.1.0/gliformer/layers/pooling.py +77 -0
- gliformer-0.1.0/gliformer/layers/position.py +1383 -0
- gliformer-0.1.0/gliformer/layers/positional_layer.py +66 -0
- gliformer-0.1.0/gliformer/layers/rnn.py +29 -0
- gliformer-0.1.0/gliformer/layers/rotary.py +59 -0
- gliformer-0.1.0/gliformer/layers/structuring_relations.py +404 -0
- gliformer-0.1.0/gliformer/model.py +3414 -0
- gliformer-0.1.0/gliformer/outputs.py +135 -0
- gliformer-0.1.0/gliformer/processing/__init__.py +5 -0
- gliformer-0.1.0/gliformer/processing/_structuring_alignment.py +1462 -0
- gliformer-0.1.0/gliformer/processing/_structuring_hierarchy.py +892 -0
- gliformer-0.1.0/gliformer/processing/collator.py +241 -0
- gliformer-0.1.0/gliformer/processing/decoder.py +133 -0
- gliformer-0.1.0/gliformer/processing/formatting.py +574 -0
- gliformer-0.1.0/gliformer/processing/label_augmentation.py +496 -0
- gliformer-0.1.0/gliformer/processing/mappings.py +285 -0
- gliformer-0.1.0/gliformer/processing/pdf.py +589 -0
- gliformer-0.1.0/gliformer/processing/processor.py +2258 -0
- gliformer-0.1.0/gliformer/processing/schema.py +1115 -0
- gliformer-0.1.0/gliformer/processing/structuring_decoder.py +792 -0
- gliformer-0.1.0/gliformer/processing/structuring_processor.py +992 -0
- gliformer-0.1.0/gliformer/processing/structuring_types.py +647 -0
- gliformer-0.1.0/gliformer/tasks/__init__.py +711 -0
- gliformer-0.1.0/gliformer/tasks/anchored_extraction.py +176 -0
- gliformer-0.1.0/gliformer/tasks/audio/__init__.py +11 -0
- gliformer-0.1.0/gliformer/tasks/audio/decoder.py +61 -0
- gliformer-0.1.0/gliformer/tasks/audio/model.py +332 -0
- gliformer-0.1.0/gliformer/tasks/audio/processor.py +161 -0
- gliformer-0.1.0/gliformer/tasks/box_ops.py +141 -0
- gliformer-0.1.0/gliformer/tasks/classification/__init__.py +10 -0
- gliformer-0.1.0/gliformer/tasks/classification/decoder.py +77 -0
- gliformer-0.1.0/gliformer/tasks/classification/model.py +123 -0
- gliformer-0.1.0/gliformer/tasks/classification/processor.py +164 -0
- gliformer-0.1.0/gliformer/tasks/classification/scorer.py +134 -0
- gliformer-0.1.0/gliformer/tasks/count/__init__.py +2 -0
- gliformer-0.1.0/gliformer/tasks/count/decoder.py +33 -0
- gliformer-0.1.0/gliformer/tasks/count/model.py +69 -0
- gliformer-0.1.0/gliformer/tasks/count/processor.py +56 -0
- gliformer-0.1.0/gliformer/tasks/embedding/__init__.py +2 -0
- gliformer-0.1.0/gliformer/tasks/embedding/decoder.py +23 -0
- gliformer-0.1.0/gliformer/tasks/embedding/model.py +237 -0
- gliformer-0.1.0/gliformer/tasks/embedding/processor.py +116 -0
- gliformer-0.1.0/gliformer/tasks/joint_relex/__init__.py +2 -0
- gliformer-0.1.0/gliformer/tasks/joint_relex/decoder.py +259 -0
- gliformer-0.1.0/gliformer/tasks/joint_relex/model.py +1398 -0
- gliformer-0.1.0/gliformer/tasks/joint_relex/processor.py +349 -0
- gliformer-0.1.0/gliformer/tasks/losses.py +193 -0
- gliformer-0.1.0/gliformer/tasks/matcher.py +291 -0
- gliformer-0.1.0/gliformer/tasks/media.py +611 -0
- gliformer-0.1.0/gliformer/tasks/media_processor.py +308 -0
- gliformer-0.1.0/gliformer/tasks/ner/__init__.py +2 -0
- gliformer-0.1.0/gliformer/tasks/ner/decoder.py +144 -0
- gliformer-0.1.0/gliformer/tasks/ner/model.py +180 -0
- gliformer-0.1.0/gliformer/tasks/ner/processor.py +590 -0
- gliformer-0.1.0/gliformer/tasks/open_relex/__init__.py +11 -0
- gliformer-0.1.0/gliformer/tasks/open_relex/decoder.py +444 -0
- gliformer-0.1.0/gliformer/tasks/open_relex/model.py +734 -0
- gliformer-0.1.0/gliformer/tasks/open_relex/processor.py +637 -0
- gliformer-0.1.0/gliformer/tasks/span_decoder.py +324 -0
- gliformer-0.1.0/gliformer/tasks/span_processor.py +378 -0
- gliformer-0.1.0/gliformer/tasks/structuring/__init__.py +11 -0
- gliformer-0.1.0/gliformer/tasks/structuring/decoder.py +189 -0
- gliformer-0.1.0/gliformer/tasks/structuring/model.py +1073 -0
- gliformer-0.1.0/gliformer/tasks/structuring/processor.py +13 -0
- gliformer-0.1.0/gliformer/tasks/vision/__init__.py +13 -0
- gliformer-0.1.0/gliformer/tasks/vision/decoder.py +84 -0
- gliformer-0.1.0/gliformer/tasks/vision/model.py +1081 -0
- gliformer-0.1.0/gliformer/tasks/vision/processor.py +219 -0
- gliformer-0.1.0/gliformer/training.py +513 -0
- gliformer-0.1.0/gliformer/utils.py +18 -0
- gliformer-0.1.0/gliformer.egg-info/PKG-INFO +408 -0
- gliformer-0.1.0/gliformer.egg-info/SOURCES.txt +128 -0
- gliformer-0.1.0/gliformer.egg-info/dependency_links.txt +1 -0
- gliformer-0.1.0/gliformer.egg-info/requires.txt +47 -0
- gliformer-0.1.0/gliformer.egg-info/top_level.txt +2 -0
- gliformer-0.1.0/gliformer_eval/__init__.py +1 -0
- gliformer-0.1.0/gliformer_eval/eval_box_accuracy.py +105 -0
- gliformer-0.1.0/gliformer_eval/eval_classification.py +483 -0
- gliformer-0.1.0/gliformer_eval/eval_ner.py +414 -0
- gliformer-0.1.0/gliformer_eval/eval_qa.py +684 -0
- gliformer-0.1.0/gliformer_eval/eval_relex.py +1105 -0
- gliformer-0.1.0/gliformer_eval/eval_structuring.py +1310 -0
- gliformer-0.1.0/gliformer_eval/eval_sts.py +402 -0
- gliformer-0.1.0/pyproject.toml +80 -0
- gliformer-0.1.0/setup.cfg +4 -0
- gliformer-0.1.0/tests/test_audio_encoder.py +208 -0
- gliformer-0.1.0/tests/test_backbone_registry.py +95 -0
- gliformer-0.1.0/tests/test_box_ops.py +109 -0
- gliformer-0.1.0/tests/test_configs.py +438 -0
- gliformer-0.1.0/tests/test_embed_text.py +60 -0
- gliformer-0.1.0/tests/test_head_only_training.py +25 -0
- gliformer-0.1.0/tests/test_hungarian_matcher.py +207 -0
- gliformer-0.1.0/tests/test_losses.py +130 -0
- gliformer-0.1.0/tests/test_matcher.py +253 -0
- gliformer-0.1.0/tests/test_media_label_pooling_dtype.py +127 -0
- gliformer-0.1.0/tests/test_model_variants.py +1631 -0
- gliformer-0.1.0/tests/test_pdf_processing.py +196 -0
- gliformer-0.1.0/tests/test_public_api_contract.py +114 -0
- gliformer-0.1.0/tests/test_schema_api.py +90 -0
- gliformer-0.1.0/tests/test_token_conventions.py +52 -0
- gliformer-0.1.0/tests/test_training.py +968 -0
- gliformer-0.1.0/tests/test_vision_encoder.py +165 -0
gliformer-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gliformer
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Multi-task information extraction framework with text, layout, vision, audio, and omni-modal GLiFormer models.
|
|
5
|
+
Author: GLiFormer contributors
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: accelerate>=1.1.0
|
|
9
|
+
Requires-Dist: gliner>=0.2.0
|
|
10
|
+
Requires-Dist: huggingface-hub>=0.20.0
|
|
11
|
+
Requires-Dist: numpy>=1.23.0
|
|
12
|
+
Requires-Dist: pillow>=9.0.0
|
|
13
|
+
Requires-Dist: pydantic<3,>=1.10
|
|
14
|
+
Requires-Dist: scipy>=1.9.0
|
|
15
|
+
Requires-Dist: torch>=2.0.0
|
|
16
|
+
Requires-Dist: transformers>=4.40.0
|
|
17
|
+
Provides-Extra: audio
|
|
18
|
+
Requires-Dist: torchaudio>=2.0.0; extra == "audio"
|
|
19
|
+
Provides-Extra: demo
|
|
20
|
+
Requires-Dist: gradio>=4.0.0; extra == "demo"
|
|
21
|
+
Requires-Dist: gliner2[local]>=1.1.2; extra == "demo"
|
|
22
|
+
Requires-Dist: sentence-transformers>=2.2.0; extra == "demo"
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
25
|
+
Requires-Dist: ruff>=0.4.0; extra == "dev"
|
|
26
|
+
Provides-Extra: flash
|
|
27
|
+
Requires-Dist: flashdeberta>=0.0.7; extra == "flash"
|
|
28
|
+
Provides-Extra: data
|
|
29
|
+
Requires-Dist: datasets>=2.16.0; extra == "data"
|
|
30
|
+
Provides-Extra: pdf
|
|
31
|
+
Requires-Dist: pymupdf>=1.23.0; extra == "pdf"
|
|
32
|
+
Requires-Dist: pdfplumber>=0.10.0; extra == "pdf"
|
|
33
|
+
Provides-Extra: vision
|
|
34
|
+
Requires-Dist: torchvision>=0.15.0; extra == "vision"
|
|
35
|
+
Provides-Extra: all
|
|
36
|
+
Requires-Dist: gradio>=4.0.0; extra == "all"
|
|
37
|
+
Requires-Dist: datasets>=2.16.0; extra == "all"
|
|
38
|
+
Requires-Dist: flashdeberta>=0.0.7; extra == "all"
|
|
39
|
+
Requires-Dist: gliner2[local]>=1.1.2; extra == "all"
|
|
40
|
+
Requires-Dist: pdfplumber>=0.10.0; extra == "all"
|
|
41
|
+
Requires-Dist: pymupdf>=1.23.0; extra == "all"
|
|
42
|
+
Requires-Dist: pytest>=8.0.0; extra == "all"
|
|
43
|
+
Requires-Dist: sentence-transformers>=2.2.0; extra == "all"
|
|
44
|
+
Requires-Dist: ruff>=0.4.0; extra == "all"
|
|
45
|
+
Requires-Dist: torchaudio>=2.0.0; extra == "all"
|
|
46
|
+
Requires-Dist: torchvision>=0.15.0; extra == "all"
|
|
47
|
+
|
|
48
|
+
<div align="center">
|
|
49
|
+
|
|
50
|
+
# GLiFormer: Multi-Task Information Extraction
|
|
51
|
+
|
|
52
|
+
**Named Entity Recognition | Text Classification | Relation Extraction | Multi-Level Structuring | Embeddings**
|
|
53
|
+
|
|
54
|
+
**Text · Documents · Images · Audio**
|
|
55
|
+
|
|
56
|
+
[](pyproject.toml)
|
|
57
|
+
[](pyproject.toml)
|
|
58
|
+
[](https://github.com/urchade/GLiNER)
|
|
59
|
+
|
|
60
|
+
[Quick Start](#quick-start) • [Structuring](#structured-extraction) • [Usage](#usage) • [Training](#training) • [Architectures](#architectures) • [Evaluation](#evaluation)
|
|
61
|
+
|
|
62
|
+
</div>
|
|
63
|
+
|
|
64
|
+
GLiFormer is a framework for training and running models that turn unstructured inputs into labeled spans, relations, classifications, and structured records. Built on [GLiNER](https://github.com/urchade/GLiNER), it combines a shared encoder with configurable task heads and lets you specify entity types, class labels, relation types, and extraction schemas at inference time.
|
|
65
|
+
|
|
66
|
+
Alongside text extraction, the codebase includes model variants for document layout, vision, audio, and combined modalities. Available tasks depend on the heads and modalities configured and trained in your checkpoint.
|
|
67
|
+
|
|
68
|
+
## Why GLiFormer?
|
|
69
|
+
|
|
70
|
+
<table>
|
|
71
|
+
<tr>
|
|
72
|
+
<td width="33%"><strong>Multiple Tasks, One Encoder</strong><br>Run entity recognition, classification, relations, and structuring together through a shared backbone.</td>
|
|
73
|
+
<td width="33%"><strong>Labels at Inference Time</strong><br>Describe the entities, classes, and relations you need with readable labels.</td>
|
|
74
|
+
<td width="33%"><strong>Multi-Level Structuring</strong><br>Extract nested records and connect children to their parents: companies → departments → employees, all in one schema.</td>
|
|
75
|
+
</tr>
|
|
76
|
+
<tr>
|
|
77
|
+
<td><strong>Beyond Plain Text</strong><br>Use dedicated text, layout, vision, audio, and omni model variants.</td>
|
|
78
|
+
<td><strong>Flexible Task Heads</strong><br>Configure individual tasks, shared components, and anchor representations for your workload.</td>
|
|
79
|
+
<td><strong>Fine-Tune on Your Data</strong><br>Train with task-specific annotations, freeze selected components, or update only task heads.</td>
|
|
80
|
+
</tr>
|
|
81
|
+
</table>
|
|
82
|
+
|
|
83
|
+
## Quick Start
|
|
84
|
+
|
|
85
|
+
### Installation
|
|
86
|
+
|
|
87
|
+
Requires **Python 3.10 or newer**. Install from the repository:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
git clone https://github.com/Bionity/GLiFormer.git
|
|
91
|
+
cd GLiFormer
|
|
92
|
+
pip install -e .
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
With uv:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
uv pip install -e .
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Optional dependencies can be installed for the features you use:
|
|
102
|
+
|
|
103
|
+
| Extra | Purpose |
|
|
104
|
+
| --- | --- |
|
|
105
|
+
| `demo` | Gradio demos and comparison models |
|
|
106
|
+
| `data` | Dataset loading with Hugging Face Datasets |
|
|
107
|
+
| `pdf` | PDF processing with PyMuPDF and pdfplumber |
|
|
108
|
+
| `vision` | Torchvision utilities |
|
|
109
|
+
| `audio` | Torchaudio utilities |
|
|
110
|
+
| `flash` | FlashDeBERTa support |
|
|
111
|
+
| `dev` | pytest and Ruff |
|
|
112
|
+
| `all` | All extras listed above |
|
|
113
|
+
|
|
114
|
+
For example:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
pip install -e ".[demo,pdf]"
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Basic Usage
|
|
121
|
+
|
|
122
|
+
Load a **trained GLiFormer checkpoint** containing an NER head. Replace `path/to/checkpoint` throughout these examples with your checkpoint directory. `from_pretrained` also accepts a Hugging Face repository ID for a compatible GLiFormer checkpoint.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from gliformer import GLiFormer
|
|
126
|
+
|
|
127
|
+
model = GLiFormer.from_pretrained("path/to/checkpoint", load_tokenizer=True)
|
|
128
|
+
|
|
129
|
+
text = "Marie Curie worked at the University of Paris in France."
|
|
130
|
+
labels = ["person", "organization", "location"]
|
|
131
|
+
|
|
132
|
+
entities = model.predict_entities(text, labels, threshold=0.5)
|
|
133
|
+
|
|
134
|
+
for entity in entities:
|
|
135
|
+
print(entity["text"], "=>", entity["label"])
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Entity results contain `text`, `label`, `start`, `end`, and `score`. Character offsets use an exclusive `end`, so `text[start:end]` gives the extracted mention.
|
|
139
|
+
|
|
140
|
+
Pass a list of texts for batched inference:
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
entities = model.predict_entities(
|
|
144
|
+
["Alice works at Acme.", "Bob lives in Berlin."],
|
|
145
|
+
["person", "organization", "location"],
|
|
146
|
+
batch_size=8,
|
|
147
|
+
)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Per-task methods return one result for a string input and a list of results for a batch. The examples below reuse `model`; each requires a checkpoint trained for the corresponding task.
|
|
151
|
+
|
|
152
|
+
## Usage
|
|
153
|
+
|
|
154
|
+
### Text Classification
|
|
155
|
+
|
|
156
|
+
Provide candidate labels to classify text:
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
predictions = model.classify(
|
|
160
|
+
"The new search feature is fast and easy to use.",
|
|
161
|
+
["positive", "negative", "neutral"],
|
|
162
|
+
threshold=0.5,
|
|
163
|
+
)
|
|
164
|
+
print(predictions)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Use a dictionary of label lists to request named classification groups, such as `{"sentiment": ["positive", "negative"], "topic": ["product", "support"]}`.
|
|
168
|
+
|
|
169
|
+
### Relation Extraction
|
|
170
|
+
|
|
171
|
+
The open relation head extracts relation endpoints directly from text using the supplied relation labels:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
relations = model.predict_relations(
|
|
175
|
+
"Alice works at Acme and lives in London.",
|
|
176
|
+
["works_at", "lives_in"],
|
|
177
|
+
threshold=0.5,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
for relation in relations:
|
|
181
|
+
print(
|
|
182
|
+
relation["head"]["text"],
|
|
183
|
+
"=>", relation["relation"], "=>",
|
|
184
|
+
relation["tail"]["text"],
|
|
185
|
+
)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
For a checkpoint with a **joint relation head**, supply both entity and relation types through `inference`:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
results = model.inference(
|
|
192
|
+
"Alice works at Acme.",
|
|
193
|
+
joint_relations={
|
|
194
|
+
"employment": {
|
|
195
|
+
"entities": ["person", "organization"],
|
|
196
|
+
"relations": ["works_at"],
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Structured Extraction
|
|
203
|
+
|
|
204
|
+
Define nested Pydantic models to extract **company → departments → employees** in one call. This example requires a checkpoint trained with multi-level structuring enabled (`structuring_config.multi_level: true`).
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
import json
|
|
208
|
+
from pydantic import BaseModel
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
class Employee(BaseModel):
|
|
212
|
+
name: str
|
|
213
|
+
role: str
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
class Department(BaseModel):
|
|
217
|
+
name: str
|
|
218
|
+
employees: list[Employee]
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class Company(BaseModel):
|
|
222
|
+
name: str
|
|
223
|
+
departments: list[Department]
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
text = (
|
|
227
|
+
"At Acme, Engineering includes Alice, a software engineer, and Bob, "
|
|
228
|
+
"a designer. Sales includes Carol, an account manager."
|
|
229
|
+
)
|
|
230
|
+
records = model.structure(text, {"company": Company}, validate_output=True)
|
|
231
|
+
print(json.dumps(records, indent=2))
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Illustrative output:
|
|
235
|
+
|
|
236
|
+
```json
|
|
237
|
+
{
|
|
238
|
+
"company": [{
|
|
239
|
+
"name": "Acme",
|
|
240
|
+
"departments": [
|
|
241
|
+
{
|
|
242
|
+
"name": "Engineering",
|
|
243
|
+
"employees": [
|
|
244
|
+
{"name": "Alice", "role": "software engineer"},
|
|
245
|
+
{"name": "Bob", "role": "designer"}
|
|
246
|
+
]
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
"name": "Sales",
|
|
250
|
+
"employees": [{"name": "Carol", "role": "account manager"}]
|
|
251
|
+
}
|
|
252
|
+
]
|
|
253
|
+
}]
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Each employee stays under its department. Results are dictionaries and lists validated against the Pydantic schema; actual predictions depend on the checkpoint and threshold.
|
|
258
|
+
|
|
259
|
+
### Multi-Task Inference
|
|
260
|
+
|
|
261
|
+
Construct a reusable schema independently of the model:
|
|
262
|
+
|
|
263
|
+
```python
|
|
264
|
+
from gliformer import GLiFormerSchema
|
|
265
|
+
|
|
266
|
+
schema = GLiFormerSchema(
|
|
267
|
+
entities=["person", "organization"],
|
|
268
|
+
classes=["business", "sports", "technology"],
|
|
269
|
+
structures={"employee": ["name", "company"]},
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
results = model.inference_from_schema(
|
|
273
|
+
["Alice joined Acme as a software engineer."],
|
|
274
|
+
schema,
|
|
275
|
+
threshold=0.5,
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
print(results["ner"][0])
|
|
279
|
+
print(results["classification"][0])
|
|
280
|
+
print(results["structuring"][0])
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
`inference` and `inference_from_schema` return a dictionary keyed by task, with one entry per input text under each key. You can also pass `entities`, `classes`, `relations`, and `structures` directly to `model.inference(...)`.
|
|
284
|
+
|
|
285
|
+
### Text Embeddings
|
|
286
|
+
|
|
287
|
+
A checkpoint with an embedding head can produce vectors for similarity and retrieval:
|
|
288
|
+
|
|
289
|
+
```python
|
|
290
|
+
import torch.nn.functional as F
|
|
291
|
+
|
|
292
|
+
embeddings = model.embed_text([
|
|
293
|
+
"A scientist is working in a laboratory.",
|
|
294
|
+
"A researcher is conducting an experiment.",
|
|
295
|
+
])
|
|
296
|
+
similarity = F.cosine_similarity(embeddings[0:1], embeddings[1:2])
|
|
297
|
+
print(similarity.item())
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
`embed_text` returns a CPU tensor of shape `(number_of_texts, embedding_dimension)`. Bi-encoder configurations also expose `model.embed_labels(labels)`.
|
|
301
|
+
|
|
302
|
+
## Training
|
|
303
|
+
|
|
304
|
+
Fine-tune a trained checkpoint with annotated examples using `train_model`. For NER, records use an `extraction` list containing entity mentions and their labels:
|
|
305
|
+
|
|
306
|
+
```python
|
|
307
|
+
from gliformer import GLiFormer
|
|
308
|
+
|
|
309
|
+
model = GLiFormer.from_pretrained("path/to/checkpoint", load_tokenizer=True)
|
|
310
|
+
|
|
311
|
+
train_data = [
|
|
312
|
+
{
|
|
313
|
+
"text": "Alice works at Acme.",
|
|
314
|
+
"extraction": [
|
|
315
|
+
{
|
|
316
|
+
"name": "entities",
|
|
317
|
+
"all_labels": ["person", "organization", "location"],
|
|
318
|
+
"ner": [["Alice", "person"], ["Acme", "organization"]],
|
|
319
|
+
}
|
|
320
|
+
],
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
"text": "Bob lives in Berlin.",
|
|
324
|
+
"extraction": [
|
|
325
|
+
{
|
|
326
|
+
"name": "entities",
|
|
327
|
+
"all_labels": ["person", "organization", "location"],
|
|
328
|
+
"ner": [["Bob", "person"], ["Berlin", "location"]],
|
|
329
|
+
}
|
|
330
|
+
],
|
|
331
|
+
},
|
|
332
|
+
]
|
|
333
|
+
|
|
334
|
+
trainer = model.train_model(
|
|
335
|
+
train_dataset=train_data,
|
|
336
|
+
output_dir="outputs/ner",
|
|
337
|
+
max_steps=100,
|
|
338
|
+
per_device_train_batch_size=2,
|
|
339
|
+
learning_rate=1e-5,
|
|
340
|
+
)
|
|
341
|
+
model.save_pretrained("outputs/ner/final")
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
This small dataset illustrates the format; use a representative training set and pass held-out examples as `eval_dataset` for your own task. Training records can also carry `classification`, `open_relex`, `structuring`, and `embedding` annotations for the enabled heads. See the [task processors](gliformer/tasks) and [sample records in the test fixtures](tests/conftest.py) for their formats.
|
|
345
|
+
|
|
346
|
+
Useful training options include `freeze_components=["text_encoder"]`, `train_head_only=True`, and `resume_from_checkpoint="path/to/training-checkpoint"`.
|
|
347
|
+
|
|
348
|
+
To initialize a new model with a pretrained backbone and new task heads, use `GLiFormer.load_from_config(...)`. It accepts a configuration object, a model configuration dictionary, or a JSON configuration path. The [YAML configurations](configs) contain separate `model`, `data`, and `training` sections; when using them programmatically, parse the YAML and pass its `model` section to `load_from_config`. Newly initialized heads require training before extraction.
|
|
349
|
+
|
|
350
|
+
## Architectures
|
|
351
|
+
|
|
352
|
+
`GLiFormer` selects the appropriate wrapper from the checkpoint configuration:
|
|
353
|
+
|
|
354
|
+
| Variant | Wrapper | Inputs | Example configuration |
|
|
355
|
+
| --- | --- | --- | --- |
|
|
356
|
+
| Text | `GLiFormerText` | Text and task labels | [NER](configs/ner.yaml) |
|
|
357
|
+
| Layout | `GLiFormerLayout` | Text with document geometry | [Layout](configs/layout.yaml) |
|
|
358
|
+
| Vision | `GLiFormerVision` | Images with label prompts | [Vision](configs/vision_only.yaml) |
|
|
359
|
+
| Audio | `GLiFormerAudio` | Audio with label prompts | [Audio](configs/audio_only.yaml) |
|
|
360
|
+
| Omni | `GLiFormerOmni` | Combined text, image, and audio features | [Omni](configs/omni.yaml) |
|
|
361
|
+
|
|
362
|
+
Text models support joint text/label encoding and separate label encoders. Task heads share encoder representations, while each task has its own processor, neural head, and decoder. Configurable anchors represent entities, relation pairs, or record instances for the corresponding prediction tasks.
|
|
363
|
+
|
|
364
|
+
The framework also includes instance counting, image and audio classification, object detection, image segmentation, and audio segmentation heads. Use configurations and checkpoints trained for the required modality and task; the text examples above do not activate additional heads in an existing checkpoint.
|
|
365
|
+
|
|
366
|
+
For implementation details, see the [configuration classes](gliformer/config.py), [model variants](gliformer/model.py), [task modules](gliformer/tasks), and [shared layers](gliformer/layers).
|
|
367
|
+
|
|
368
|
+
## Demo
|
|
369
|
+
|
|
370
|
+
Launch the Gradio interface with your checkpoint:
|
|
371
|
+
|
|
372
|
+
```bash
|
|
373
|
+
pip install -e ".[demo,pdf]"
|
|
374
|
+
GLIFORMER_MODEL_ID=path/to/checkpoint python demo.py
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
The demo includes annotated examples for NER, classification, relations, structured extraction, and embeddings. Select a checkpoint with the heads needed by the tabs you want to use.
|
|
378
|
+
|
|
379
|
+
## Evaluation
|
|
380
|
+
|
|
381
|
+
Run task-specific evaluators from the repository root. For example, evaluate NER on a prepared CrossNER dataset:
|
|
382
|
+
|
|
383
|
+
```bash
|
|
384
|
+
python gliformer_eval/eval_ner.py \
|
|
385
|
+
--model path/to/checkpoint \
|
|
386
|
+
--data path/to/NER \
|
|
387
|
+
--datasets CrossNER_AI CrossNER_literature CrossNER_music CrossNER_politics CrossNER_science \
|
|
388
|
+
--output eval_results/ner.json
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
The NER data directory must contain one subdirectory per dataset with `labels.json` and `test.json`. The evaluator reports strict entity-level precision, recall, and F1 using character spans and entity types.
|
|
392
|
+
|
|
393
|
+
See the [evaluation guide](gliformer_eval/README.md) for classification, relation extraction, structuring, and similarity. The [benchmark guide](benchmarks/README.md) documents classification and structuring latency, throughput, memory, and profiling measurements.
|
|
394
|
+
|
|
395
|
+
## Contributing
|
|
396
|
+
|
|
397
|
+
Bug reports, task examples, and contributions are welcome. For development, install the test and lint tools and run the checks relevant to your changes:
|
|
398
|
+
|
|
399
|
+
```bash
|
|
400
|
+
pip install -e ".[dev]"
|
|
401
|
+
python -m pytest tests/processing/test_schema.py tests/processing/test_formatting.py
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
New task implementations follow the processor → head → decoder organization in [gliformer/tasks](gliformer/tasks).
|
|
405
|
+
|
|
406
|
+
## Acknowledgements
|
|
407
|
+
|
|
408
|
+
GLiFormer builds on [GLiNER](https://github.com/urchade/GLiNER). Its task implementations also draw on ideas from [GLiNER2](https://github.com/fastino-ai/GLiNER2) and [GLiClass](https://github.com/Knowledgator/GLiClass).
|