pyhighlights 0.2.0__tar.gz → 0.3.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.
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/PKG-INFO +2 -2
- pyhighlights-0.3.0/pyhighlights/__init__.py +1 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/analyzers.py +20 -4
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/faithfulness.py +1 -8
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/base.py +19 -6
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/data.py +9 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/genspp.py +1 -11
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/grat.py +3 -5
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/mcd.py +14 -5
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/tasks.py +11 -13
- pyhighlights-0.3.0/pyhighlights/configurations/backbones.py +57 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/benchmarks.py +40 -56
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/datasets.py +33 -53
- pyhighlights-0.3.0/pyhighlights/configurations/fr.py +27 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/genspp.py +52 -81
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/grat.py +25 -41
- pyhighlights-0.3.0/pyhighlights/configurations/losses.py +175 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/mcd.py +12 -21
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/metrics.py +98 -159
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/mgr.py +12 -17
- pyhighlights-0.3.0/pyhighlights/configurations/optimizers.py +14 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/preprocessors.py +39 -61
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/tasks.py +22 -34
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights.egg-info/PKG-INFO +2 -2
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights.egg-info/requires.txt +1 -1
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights_benchmarks/genspp2025/configurations/hatexplain.py +152 -237
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights_benchmarks/genspp2025/configurations/toy.py +112 -177
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyproject.toml +1 -1
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_benchmarks.py +43 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_losses.py +19 -0
- pyhighlights-0.2.0/pyhighlights/__init__.py +0 -1
- pyhighlights-0.2.0/pyhighlights/configurations/backbones.py +0 -73
- pyhighlights-0.2.0/pyhighlights/configurations/fr.py +0 -36
- pyhighlights-0.2.0/pyhighlights/configurations/losses.py +0 -244
- pyhighlights-0.2.0/pyhighlights/configurations/optimizers.py +0 -21
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/LICENSE +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/README.md +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/__init__.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/benchmarks.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/data.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/leakage.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/loaders.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/__init__.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/__init__.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/base.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/data.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/fr.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/implementations.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/models/spp/mgr.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/components/preprocessors.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/__init__.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/base.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/configurations/keys.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/utility/__init__.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/utility/embeddings.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/utility/io.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/utility/losses.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/utility/manifest.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/utility/metrics.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights/utility/statistics.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights.egg-info/SOURCES.txt +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights.egg-info/dependency_links.txt +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights.egg-info/top_level.txt +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights_benchmarks/__init__.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights_benchmarks/genspp2025/__init__.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights_benchmarks/genspp2025/configurations/__init__.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights_benchmarks/genspp2025/configurations/common.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights_benchmarks/genspp2025/configurations/keys.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/pyhighlights_benchmarks/genspp2025/corpora.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/setup.cfg +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_configurations.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_data.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_documentation.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_embeddings.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_faithfulness.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_fr.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_genspp.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_genspp2025.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_grat.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_imports.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_io.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_leakage.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_loaders.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_manifest.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_metrics.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_preprocessors.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_statistics.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_tasks.py +0 -0
- {pyhighlights-0.2.0 → pyhighlights-0.3.0}/tests/test_transformer_configurations.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyhighlights
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A simple library implementing Select-Then-Predict (SPP) models.
|
|
5
5
|
Author-email: Federico Ruggeri <federico.ruggeri6@unibo.it>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -20,7 +20,7 @@ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
|
20
20
|
Requires-Python: >=3.10
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
22
22
|
License-File: LICENSE
|
|
23
|
-
Requires-Dist: cinnamon-core<3,>=2.0
|
|
23
|
+
Requires-Dist: cinnamon-core<3,>=2.1.0
|
|
24
24
|
Requires-Dist: pandas>=2.0.0
|
|
25
25
|
Requires-Dist: lightning>=2.0.0
|
|
26
26
|
Requires-Dist: torch>=2.0.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.3.0"
|
|
@@ -470,9 +470,18 @@ class LabelStudioExporter(PredictionAnalyzer):
|
|
|
470
470
|
per run, pre-annotated with what the model selected, so the reading is a
|
|
471
471
|
review rather than a fresh annotation.
|
|
472
472
|
|
|
473
|
-
``only`` narrows the export to samples of one
|
|
474
|
-
|
|
475
|
-
|
|
473
|
+
``only`` narrows the export to samples of one class, which is what a corpus
|
|
474
|
+
annotated for a rare one needs: the negatives are 97% of it and the
|
|
475
|
+
interesting highlights are all on the positives.
|
|
476
|
+
|
|
477
|
+
``column`` decides *which* class that is, and the two answers are different
|
|
478
|
+
questions. ``"label"`` selects the samples that carry the class, and asks
|
|
479
|
+
whether the model found the right words in them. ``"predicted"`` selects
|
|
480
|
+
the samples the model *called* that class, and asks whether the words it
|
|
481
|
+
kept justify the call -- which is the only one of the two available on a
|
|
482
|
+
corpus with no annotation to select by, and the one that surfaces a
|
|
483
|
+
confident mistake. Any column
|
|
484
|
+
:class:`PredictionAnalyzer` reports may be named.
|
|
476
485
|
|
|
477
486
|
One file per seed, beside the predictions it came from. A run's seeds are
|
|
478
487
|
separate predictions of the same samples, so merging them would show the
|
|
@@ -488,19 +497,26 @@ class LabelStudioExporter(PredictionAnalyzer):
|
|
|
488
497
|
model_version: str = "pyhighlights",
|
|
489
498
|
labels: Sequence[str] = ("highlight",),
|
|
490
499
|
only: int | None = None,
|
|
500
|
+
column: str = "label",
|
|
491
501
|
stem: str = "label-studio",
|
|
492
502
|
):
|
|
493
503
|
super().__init__(directory=directory, pattern=pattern, split=split)
|
|
494
504
|
self.model_version = model_version
|
|
495
505
|
self.labels = list(labels)
|
|
496
506
|
self.only = only
|
|
507
|
+
self.column = column
|
|
497
508
|
self.stem = stem
|
|
498
509
|
|
|
499
510
|
def selected(self, frame: pd.DataFrame) -> pd.DataFrame:
|
|
500
511
|
"""The rows this export is about."""
|
|
501
512
|
if self.only is None or frame.empty:
|
|
502
513
|
return frame
|
|
503
|
-
|
|
514
|
+
if self.column not in frame.columns:
|
|
515
|
+
raise KeyError(
|
|
516
|
+
f"cannot narrow the export by {self.column!r}: a prediction row "
|
|
517
|
+
f"carries {sorted(frame.columns)}"
|
|
518
|
+
)
|
|
519
|
+
return frame[frame[self.column] == self.only].reset_index(drop=True)
|
|
504
520
|
|
|
505
521
|
def analyze(self) -> pd.DataFrame:
|
|
506
522
|
return self.selected(super().analyze())
|
|
@@ -81,14 +81,7 @@ def evaluate(model: Model, loader: DataLoader) -> Dict[str, float]:
|
|
|
81
81
|
try:
|
|
82
82
|
with th.no_grad():
|
|
83
83
|
for batch in loader:
|
|
84
|
-
batch =
|
|
85
|
-
**{
|
|
86
|
-
name: value.to(model.device)
|
|
87
|
-
if isinstance(value, th.Tensor)
|
|
88
|
-
else value
|
|
89
|
-
for name, value in batch.as_dict().items()
|
|
90
|
-
}
|
|
91
|
-
)
|
|
84
|
+
batch = batch.to(model.device)
|
|
92
85
|
terms = model.faithfulness(batch, model.test_forward(batch))
|
|
93
86
|
for name, value in terms.items():
|
|
94
87
|
totals[name] = totals.get(name, 0.0) + float(value.sum())
|
|
@@ -137,12 +137,20 @@ class Model(L.LightningModule, abc.ABC, Generic[OutputT]):
|
|
|
137
137
|
def test_forward(self, batch: InputData) -> OutputT:
|
|
138
138
|
return self.training_forward(batch=batch)
|
|
139
139
|
|
|
140
|
-
def
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
140
|
+
def record(
|
|
141
|
+
self,
|
|
142
|
+
split: Split,
|
|
143
|
+
batch: InputData,
|
|
144
|
+
output_data: OutputT,
|
|
145
|
+
total_loss: th.Tensor,
|
|
146
|
+
losses: Dict[str, th.Tensor],
|
|
147
|
+
) -> None:
|
|
148
|
+
"""Log the losses, update the metrics, keep the predictions if asked.
|
|
145
149
|
|
|
150
|
+
What every step does once its loss is known, however it got there: a
|
|
151
|
+
model driving its own optimizers computes that loss in phases, but has
|
|
152
|
+
the same record to write afterwards.
|
|
153
|
+
"""
|
|
146
154
|
self.log_metrics(
|
|
147
155
|
split=split,
|
|
148
156
|
total_loss=total_loss,
|
|
@@ -150,10 +158,15 @@ class Model(L.LightningModule, abc.ABC, Generic[OutputT]):
|
|
|
150
158
|
batch_size=batch.y_true.shape[0],
|
|
151
159
|
)
|
|
152
160
|
self.update_metrics(split=split, input_data=batch, output_data=output_data)
|
|
153
|
-
|
|
154
161
|
if self.store_predictions:
|
|
155
162
|
self.predictions.append({**batch.as_numpy(), **output_data.as_numpy()})
|
|
156
163
|
|
|
164
|
+
def _step(self, batch: InputData, batch_idx: int, split: Split) -> th.Tensor:
|
|
165
|
+
output_data = self.forward_mapping[split](batch)
|
|
166
|
+
total_loss, losses = self.compute_loss(
|
|
167
|
+
input_data=batch, output_data=output_data
|
|
168
|
+
)
|
|
169
|
+
self.record(split, batch, output_data, total_loss, losses)
|
|
157
170
|
return total_loss
|
|
158
171
|
|
|
159
172
|
def training_step(self, batch: InputData, batch_idx: int):
|
|
@@ -20,6 +20,15 @@ class ModelData:
|
|
|
20
20
|
if isinstance((value := getattr(self, field.name)), th.Tensor)
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
def to(self: D, device: th.device | str) -> D:
|
|
24
|
+
"""The same record with every tensor on ``device``, non-tensors as they are."""
|
|
25
|
+
return type(self)(
|
|
26
|
+
**{
|
|
27
|
+
name: value.to(device) if isinstance(value, th.Tensor) else value
|
|
28
|
+
for name, value in self.as_dict().items()
|
|
29
|
+
}
|
|
30
|
+
)
|
|
31
|
+
|
|
23
32
|
def unbind(self: D, dim: int = 0) -> Generator[D, None, None]:
|
|
24
33
|
tensor_fields = {
|
|
25
34
|
field.name: th.unbind(value, dim=dim)
|
|
@@ -395,7 +395,7 @@ class GenSPPTrainer:
|
|
|
395
395
|
|
|
396
396
|
with th.no_grad():
|
|
397
397
|
for batch in val_loader:
|
|
398
|
-
batch = self.
|
|
398
|
+
batch = batch.to(self.device)
|
|
399
399
|
output = model(batch)
|
|
400
400
|
batch_size = batch.y_true.shape[0]
|
|
401
401
|
loss, _ = model.compute_loss(batch, output)
|
|
@@ -473,13 +473,3 @@ class GenSPPTrainer:
|
|
|
473
473
|
@staticmethod
|
|
474
474
|
def _chromosome(model: GenSPP) -> th.Tensor:
|
|
475
475
|
return th.nn.utils.parameters_to_vector(model.generator_parameters()).detach()
|
|
476
|
-
|
|
477
|
-
def _move_batch(self, batch: InputData) -> InputData:
|
|
478
|
-
if self.device.type == "cpu":
|
|
479
|
-
return batch
|
|
480
|
-
return InputData(
|
|
481
|
-
**{
|
|
482
|
-
name: value.to(self.device) if isinstance(value, th.Tensor) else value
|
|
483
|
-
for name, value in batch.as_dict().items()
|
|
484
|
-
}
|
|
485
|
-
)
|
|
@@ -210,16 +210,14 @@ class GRAT(SPP):
|
|
|
210
210
|
self._model_steps.add_(1)
|
|
211
211
|
|
|
212
212
|
total = model_total.detach()
|
|
213
|
-
self.
|
|
213
|
+
self.record(
|
|
214
214
|
split="train",
|
|
215
|
+
batch=batch,
|
|
216
|
+
output_data=output,
|
|
215
217
|
total_loss=total,
|
|
216
218
|
losses={
|
|
217
219
|
**{f"guider_{name}": value for name, value in guider_losses.items()},
|
|
218
220
|
**model_losses,
|
|
219
221
|
},
|
|
220
|
-
batch_size=batch.y_true.shape[0],
|
|
221
222
|
)
|
|
222
|
-
self.update_metrics(split="train", input_data=batch, output_data=output)
|
|
223
|
-
if self.store_predictions:
|
|
224
|
-
self.predictions.append({**batch.as_numpy(), **output.as_numpy()})
|
|
225
223
|
return total
|
|
@@ -46,6 +46,17 @@ class MCD(SPP):
|
|
|
46
46
|
):
|
|
47
47
|
if predictor_backbone is None:
|
|
48
48
|
raise ValueError("MCD requires a separate predictor backbone")
|
|
49
|
+
# MCD groups its losses per training phase and replaces the flat list
|
|
50
|
+
# SPP builds, so a supervision loss appended to that list would be
|
|
51
|
+
# dropped before the first batch. Refused rather than silently ignored:
|
|
52
|
+
# a run that reports itself as the supervised ceiling and trains
|
|
53
|
+
# unsupervised is a number nobody can read as wrong.
|
|
54
|
+
if kwargs.get("supervise_highlights"):
|
|
55
|
+
raise ValueError(
|
|
56
|
+
"MCD scores its losses per training phase, so highlight "
|
|
57
|
+
"supervision has to name the phase it belongs to; put the "
|
|
58
|
+
"highlight loss in rationale_losses instead"
|
|
59
|
+
)
|
|
49
60
|
super().__init__(
|
|
50
61
|
selector_backbones=selector_backbones,
|
|
51
62
|
selectors=selectors,
|
|
@@ -155,8 +166,10 @@ class MCD(SPP):
|
|
|
155
166
|
generator_optimizer.zero_grad()
|
|
156
167
|
|
|
157
168
|
total = classifier_total.detach() + generator_total.detach()
|
|
158
|
-
self.
|
|
169
|
+
self.record(
|
|
159
170
|
split="train",
|
|
171
|
+
batch=batch,
|
|
172
|
+
output_data=output,
|
|
160
173
|
total_loss=total,
|
|
161
174
|
losses={
|
|
162
175
|
**{
|
|
@@ -168,9 +181,5 @@ class MCD(SPP):
|
|
|
168
181
|
for name, value in generator_losses.items()
|
|
169
182
|
},
|
|
170
183
|
},
|
|
171
|
-
batch_size=batch.y_true.shape[0],
|
|
172
184
|
)
|
|
173
|
-
self.update_metrics(split="train", input_data=batch, output_data=output)
|
|
174
|
-
if self.store_predictions:
|
|
175
|
-
self.predictions.append({**batch.as_numpy(), **output.as_numpy()})
|
|
176
185
|
return total
|
|
@@ -102,13 +102,14 @@ def load_splits(
|
|
|
102
102
|
def summarize(runs: Sequence[Mapping[str, float]]) -> Dict[str, Dict[str, float]]:
|
|
103
103
|
"""Mean and standard deviation of each metric across seeds."""
|
|
104
104
|
names = sorted({name for run in runs for name in run})
|
|
105
|
+
found = {name: [float(run[name]) for run in runs if name in run] for name in names}
|
|
105
106
|
return {
|
|
106
107
|
name: {
|
|
107
|
-
"mean": float(np.mean(
|
|
108
|
-
"std": float(np.std(
|
|
109
|
-
"values":
|
|
108
|
+
"mean": float(np.mean(values)),
|
|
109
|
+
"std": float(np.std(values)),
|
|
110
|
+
"values": values,
|
|
110
111
|
}
|
|
111
|
-
for name in
|
|
112
|
+
for name, values in found.items()
|
|
112
113
|
}
|
|
113
114
|
|
|
114
115
|
|
|
@@ -267,12 +268,14 @@ class SPPTask(Task):
|
|
|
267
268
|
"""
|
|
268
269
|
if self.pretrained_model_card is not None:
|
|
269
270
|
return HuggingFaceTokenizer(self.pretrained_model_card)
|
|
271
|
+
# Fitted on the training split alone, and on nothing at all when a
|
|
272
|
+
# corpus has none: a vocabulary that saw the evaluation text leaks it.
|
|
273
|
+
training = [splits["train"]] if "train" in splits else []
|
|
270
274
|
if self.embeddings is not None:
|
|
271
275
|
words = {
|
|
272
276
|
token
|
|
273
|
-
for
|
|
274
|
-
|
|
275
|
-
for tokens in splits[name]["tokens"]
|
|
277
|
+
for frame in training
|
|
278
|
+
for tokens in frame["tokens"]
|
|
276
279
|
for token in tokens
|
|
277
280
|
}
|
|
278
281
|
table, self._embedding_matrix = load_vectors(
|
|
@@ -281,12 +284,7 @@ class SPPTask(Task):
|
|
|
281
284
|
pretrained_only=self.pretrained_tokens_only,
|
|
282
285
|
)
|
|
283
286
|
return VocabularyTokenizer(table)
|
|
284
|
-
return VocabularyTokenizer(
|
|
285
|
-
vocabulary(
|
|
286
|
-
[splits[name] for name in ("train",) if name in splits],
|
|
287
|
-
size=self.vocabulary_size,
|
|
288
|
-
)
|
|
289
|
-
)
|
|
287
|
+
return VocabularyTokenizer(vocabulary(training, size=self.vocabulary_size))
|
|
290
288
|
|
|
291
289
|
def loaders(self, splits: Mapping[str, pd.DataFrame]) -> Dict[str, DataLoader]:
|
|
292
290
|
if self.highlight_supervision:
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Backbone, selector and predictor registrations shared by the SPP models."""
|
|
2
|
+
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from cinnamon.configuration import Configuration, Param
|
|
6
|
+
from cinnamon.registry import register_class
|
|
7
|
+
|
|
8
|
+
from pyhighlights.configurations.keys import NAMESPACE
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@register_class(
|
|
12
|
+
name="backbone",
|
|
13
|
+
tags={"gru"},
|
|
14
|
+
namespace=NAMESPACE,
|
|
15
|
+
component="pyhighlights.components.models.spp.implementations.GRUBackbone",
|
|
16
|
+
)
|
|
17
|
+
class GRUBackboneConfig(Configuration):
|
|
18
|
+
vocab_size: int = Param(10_000, ge=1)
|
|
19
|
+
embedding_dim: int = Param(128, ge=1)
|
|
20
|
+
hidden_size: int = Param(128, ge=1)
|
|
21
|
+
freeze_embeddings: bool = Param(False)
|
|
22
|
+
num_layers: int = Param(1, ge=1)
|
|
23
|
+
bidirectional: bool = Param(True)
|
|
24
|
+
dropout_rate: float = Param(0.0, ge=0.0, lt=1.0)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@register_class(
|
|
28
|
+
name="backbone",
|
|
29
|
+
tags={"transformer"},
|
|
30
|
+
namespace=NAMESPACE,
|
|
31
|
+
component="pyhighlights.components.models.spp.implementations.TransformerBackbone",
|
|
32
|
+
)
|
|
33
|
+
class TransformerBackboneConfig(Configuration):
|
|
34
|
+
pretrained_model_card: str = Param("distilbert-base-uncased")
|
|
35
|
+
num_features: int | None = Param(None, ge=1)
|
|
36
|
+
freeze_transformer: bool = Param(False)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@register_class(
|
|
40
|
+
name="selector",
|
|
41
|
+
tags={"mlp"},
|
|
42
|
+
namespace=NAMESPACE,
|
|
43
|
+
component="pyhighlights.components.models.spp.implementations.MLPSelector",
|
|
44
|
+
)
|
|
45
|
+
class MLPSelectorConfig(Configuration):
|
|
46
|
+
hidden_sizes: List[int] = Param([])
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@register_class(
|
|
50
|
+
name="predictor",
|
|
51
|
+
tags={"mlp"},
|
|
52
|
+
namespace=NAMESPACE,
|
|
53
|
+
component="pyhighlights.components.models.spp.implementations.MLPPredictor",
|
|
54
|
+
)
|
|
55
|
+
class MLPPredictorConfig(Configuration):
|
|
56
|
+
hidden_sizes: List[int] = Param([])
|
|
57
|
+
num_classes: int = Param(2, ge=2)
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
from typing import List, Sequence
|
|
4
4
|
|
|
5
5
|
from cinnamon.configuration import Configuration, Param
|
|
6
|
-
from cinnamon.registry import RegistrationKey,
|
|
6
|
+
from cinnamon.registry import RegistrationKey, register_class
|
|
7
7
|
|
|
8
8
|
from pyhighlights.components.analyzers import PREDICTIONS
|
|
9
9
|
from pyhighlights.components.tasks import Task
|
|
@@ -19,23 +19,26 @@ class BenchmarkConfig(Configuration):
|
|
|
19
19
|
strict: bool = Param(False)
|
|
20
20
|
|
|
21
21
|
|
|
22
|
+
@register_class(
|
|
23
|
+
name="benchmark",
|
|
24
|
+
tags={"toy"},
|
|
25
|
+
namespace=NAMESPACE,
|
|
26
|
+
component="pyhighlights.components.benchmarks.Benchmark",
|
|
27
|
+
run_method="run",
|
|
28
|
+
)
|
|
22
29
|
class ToyBenchmarkConfig(BenchmarkConfig):
|
|
23
30
|
"""The synthetic corpus, as a benchmark of one: the end-to-end smoke test."""
|
|
24
31
|
|
|
25
32
|
name: str = Param("toy-benchmark")
|
|
26
33
|
|
|
27
|
-
@classmethod
|
|
28
|
-
@register_method(
|
|
29
|
-
name="benchmark",
|
|
30
|
-
tags={"toy"},
|
|
31
|
-
namespace=NAMESPACE,
|
|
32
|
-
component="pyhighlights.components.benchmarks.Benchmark",
|
|
33
|
-
run_method="run",
|
|
34
|
-
)
|
|
35
|
-
def default(cls):
|
|
36
|
-
return super().default()
|
|
37
|
-
|
|
38
34
|
|
|
35
|
+
@register_class(
|
|
36
|
+
name="analyzer",
|
|
37
|
+
tags={"metrics"},
|
|
38
|
+
namespace=NAMESPACE,
|
|
39
|
+
component="pyhighlights.components.analyzers.MetricsAnalyzer",
|
|
40
|
+
run_method="run",
|
|
41
|
+
)
|
|
39
42
|
class MetricsAnalyzerConfig(Configuration):
|
|
40
43
|
"""One row per task, ``mean +/- std`` across seeds."""
|
|
41
44
|
|
|
@@ -43,19 +46,17 @@ class MetricsAnalyzerConfig(Configuration):
|
|
|
43
46
|
metrics: Sequence[str] = Param([])
|
|
44
47
|
split: str = Param("test")
|
|
45
48
|
pairs: bool = Param(False)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
@register_method(
|
|
49
|
-
name="analyzer",
|
|
50
|
-
tags={"metrics"},
|
|
51
|
-
namespace=NAMESPACE,
|
|
52
|
-
component="pyhighlights.components.analyzers.MetricsAnalyzer",
|
|
53
|
-
run_method="run",
|
|
54
|
-
)
|
|
55
|
-
def default(cls):
|
|
56
|
-
return super().default()
|
|
49
|
+
#: The most recent run of each task, or every run a task has ever done.
|
|
50
|
+
latest: bool = Param(True)
|
|
57
51
|
|
|
58
52
|
|
|
53
|
+
@register_class(
|
|
54
|
+
name="analyzer",
|
|
55
|
+
tags={"highlight", "position"},
|
|
56
|
+
namespace=NAMESPACE,
|
|
57
|
+
component="pyhighlights.components.analyzers.HighlightPositionAnalyzer",
|
|
58
|
+
run_method="run",
|
|
59
|
+
)
|
|
59
60
|
class HighlightPositionAnalyzerConfig(Configuration):
|
|
60
61
|
"""Where in the document the selector looked."""
|
|
61
62
|
|
|
@@ -64,18 +65,14 @@ class HighlightPositionAnalyzerConfig(Configuration):
|
|
|
64
65
|
bins: int = Param(10, ge=1)
|
|
65
66
|
absolute: bool = Param(False)
|
|
66
67
|
|
|
67
|
-
@classmethod
|
|
68
|
-
@register_method(
|
|
69
|
-
name="analyzer",
|
|
70
|
-
tags={"position", "highlight"},
|
|
71
|
-
namespace=NAMESPACE,
|
|
72
|
-
component="pyhighlights.components.analyzers.HighlightPositionAnalyzer",
|
|
73
|
-
run_method="run",
|
|
74
|
-
)
|
|
75
|
-
def default(cls):
|
|
76
|
-
return super().default()
|
|
77
|
-
|
|
78
68
|
|
|
69
|
+
@register_class(
|
|
70
|
+
name="analyzer",
|
|
71
|
+
tags={"prediction"},
|
|
72
|
+
namespace=NAMESPACE,
|
|
73
|
+
component="pyhighlights.components.analyzers.PredictionAnalyzer",
|
|
74
|
+
run_method="run",
|
|
75
|
+
)
|
|
79
76
|
class PredictionAnalyzerConfig(Configuration):
|
|
80
77
|
"""What the selector kept, in words, one row per sample."""
|
|
81
78
|
|
|
@@ -83,37 +80,24 @@ class PredictionAnalyzerConfig(Configuration):
|
|
|
83
80
|
pattern: str = Param(PREDICTIONS)
|
|
84
81
|
split: str = Param("test")
|
|
85
82
|
|
|
86
|
-
@classmethod
|
|
87
|
-
@register_method(
|
|
88
|
-
name="analyzer",
|
|
89
|
-
tags={"prediction"},
|
|
90
|
-
namespace=NAMESPACE,
|
|
91
|
-
component="pyhighlights.components.analyzers.PredictionAnalyzer",
|
|
92
|
-
run_method="run",
|
|
93
|
-
)
|
|
94
|
-
def default(cls):
|
|
95
|
-
return super().default()
|
|
96
|
-
|
|
97
83
|
|
|
84
|
+
@register_class(
|
|
85
|
+
name="analyzer",
|
|
86
|
+
tags={"label-studio"},
|
|
87
|
+
namespace=NAMESPACE,
|
|
88
|
+
component="pyhighlights.components.analyzers.LabelStudioExporter",
|
|
89
|
+
run_method="run",
|
|
90
|
+
)
|
|
98
91
|
class LabelStudioExporterConfig(PredictionAnalyzerConfig):
|
|
99
92
|
"""Predicted highlights, written where a domain expert can read them."""
|
|
100
93
|
|
|
101
94
|
model_version: str = Param("pyhighlights")
|
|
102
95
|
labels: Sequence[str] = Param(["highlight"])
|
|
103
96
|
only: int | None = Param(None)
|
|
97
|
+
#: Which class ``only`` names: the annotated one, or the predicted one.
|
|
98
|
+
column: str = Param("label")
|
|
104
99
|
stem: str = Param("label-studio")
|
|
105
100
|
|
|
106
|
-
@classmethod
|
|
107
|
-
@register_method(
|
|
108
|
-
name="analyzer",
|
|
109
|
-
tags={"label-studio"},
|
|
110
|
-
namespace=NAMESPACE,
|
|
111
|
-
component="pyhighlights.components.analyzers.LabelStudioExporter",
|
|
112
|
-
run_method="run",
|
|
113
|
-
)
|
|
114
|
-
def default(cls):
|
|
115
|
-
return super().default()
|
|
116
|
-
|
|
117
101
|
|
|
118
102
|
__all__: List[str] = [
|
|
119
103
|
"BenchmarkConfig",
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
from typing import Dict, List
|
|
4
4
|
|
|
5
5
|
from cinnamon.configuration import Configuration, Param
|
|
6
|
-
from cinnamon.registry import
|
|
6
|
+
from cinnamon.registry import register_class
|
|
7
7
|
|
|
8
8
|
from pyhighlights.components.loaders import (
|
|
9
9
|
BEER_TASKS,
|
|
@@ -29,56 +29,50 @@ class R2AConfig(LoaderConfig):
|
|
|
29
29
|
sha256: str | None = Param(None)
|
|
30
30
|
|
|
31
31
|
|
|
32
|
+
@register_class(
|
|
33
|
+
name="dataset",
|
|
34
|
+
tags={"beer"},
|
|
35
|
+
namespace=NAMESPACE,
|
|
36
|
+
component="pyhighlights.components.loaders.BeerLoader",
|
|
37
|
+
)
|
|
32
38
|
class BeerConfig(R2AConfig):
|
|
33
39
|
"""Beer aspects; one variant per aspect."""
|
|
34
40
|
|
|
35
41
|
# Cinnamon rejects a default that also appears in the variant list.
|
|
36
|
-
task: str = Param(BEER_TASKS[0], variants=
|
|
37
|
-
|
|
38
|
-
@classmethod
|
|
39
|
-
@register_method(
|
|
40
|
-
name="dataset",
|
|
41
|
-
tags={"beer"},
|
|
42
|
-
namespace=NAMESPACE,
|
|
43
|
-
component="pyhighlights.components.loaders.BeerLoader",
|
|
44
|
-
)
|
|
45
|
-
def default(cls):
|
|
46
|
-
return super().default()
|
|
42
|
+
task: str = Param(BEER_TASKS[0], variants=list(BEER_TASKS[1:]))
|
|
47
43
|
|
|
48
44
|
|
|
45
|
+
@register_class(
|
|
46
|
+
name="dataset",
|
|
47
|
+
tags={"hotel"},
|
|
48
|
+
namespace=NAMESPACE,
|
|
49
|
+
component="pyhighlights.components.loaders.HotelLoader",
|
|
50
|
+
)
|
|
49
51
|
class HotelConfig(R2AConfig):
|
|
50
52
|
"""Hotel aspects; one variant per aspect."""
|
|
51
53
|
|
|
52
|
-
task: str = Param(HOTEL_TASKS[0], variants=
|
|
53
|
-
|
|
54
|
-
@classmethod
|
|
55
|
-
@register_method(
|
|
56
|
-
name="dataset",
|
|
57
|
-
tags={"hotel"},
|
|
58
|
-
namespace=NAMESPACE,
|
|
59
|
-
component="pyhighlights.components.loaders.HotelLoader",
|
|
60
|
-
)
|
|
61
|
-
def default(cls):
|
|
62
|
-
return super().default()
|
|
54
|
+
task: str = Param(HOTEL_TASKS[0], variants=list(HOTEL_TASKS[1:]))
|
|
63
55
|
|
|
64
56
|
|
|
57
|
+
@register_class(
|
|
58
|
+
name="dataset",
|
|
59
|
+
tags={"hatexplain"},
|
|
60
|
+
namespace=NAMESPACE,
|
|
61
|
+
component="pyhighlights.components.loaders.HateXplainLoader",
|
|
62
|
+
)
|
|
65
63
|
class HateXplainConfig(LoaderConfig):
|
|
66
64
|
"""Hate-speech posts, every annotator judgement kept."""
|
|
67
65
|
|
|
68
66
|
url: str = Param(HateXplainLoader.URL)
|
|
69
67
|
divisions_url: str = Param(HateXplainLoader.DIVISIONS_URL)
|
|
70
68
|
|
|
71
|
-
@classmethod
|
|
72
|
-
@register_method(
|
|
73
|
-
name="dataset",
|
|
74
|
-
tags={"hatexplain"},
|
|
75
|
-
namespace=NAMESPACE,
|
|
76
|
-
component="pyhighlights.components.loaders.HateXplainLoader",
|
|
77
|
-
)
|
|
78
|
-
def default(cls):
|
|
79
|
-
return super().default()
|
|
80
|
-
|
|
81
69
|
|
|
70
|
+
@register_class(
|
|
71
|
+
name="dataset",
|
|
72
|
+
tags={"movies"},
|
|
73
|
+
namespace=NAMESPACE,
|
|
74
|
+
component="pyhighlights.components.loaders.MoviesLoader",
|
|
75
|
+
)
|
|
82
76
|
class MoviesConfig(LoaderConfig):
|
|
83
77
|
"""ERASER movies: evidence spans become highlights."""
|
|
84
78
|
|
|
@@ -87,17 +81,13 @@ class MoviesConfig(LoaderConfig):
|
|
|
87
81
|
url: str = Param(ERASERLoader.URL)
|
|
88
82
|
sha256: str | None = Param(None)
|
|
89
83
|
|
|
90
|
-
@classmethod
|
|
91
|
-
@register_method(
|
|
92
|
-
name="dataset",
|
|
93
|
-
tags={"movies"},
|
|
94
|
-
namespace=NAMESPACE,
|
|
95
|
-
component="pyhighlights.components.loaders.MoviesLoader",
|
|
96
|
-
)
|
|
97
|
-
def default(cls):
|
|
98
|
-
return super().default()
|
|
99
|
-
|
|
100
84
|
|
|
85
|
+
@register_class(
|
|
86
|
+
name="dataset",
|
|
87
|
+
tags={"toy"},
|
|
88
|
+
namespace=NAMESPACE,
|
|
89
|
+
component="pyhighlights.components.loaders.ToyLoader",
|
|
90
|
+
)
|
|
101
91
|
class ToyConfig(LoaderConfig):
|
|
102
92
|
"""Synthetic corpus for smoke tests and demos; no download."""
|
|
103
93
|
|
|
@@ -107,16 +97,6 @@ class ToyConfig(LoaderConfig):
|
|
|
107
97
|
vocabulary_size: int = Param(32, ge=1)
|
|
108
98
|
seed: int = Param(0)
|
|
109
99
|
|
|
110
|
-
@classmethod
|
|
111
|
-
@register_method(
|
|
112
|
-
name="dataset",
|
|
113
|
-
tags={"toy"},
|
|
114
|
-
namespace=NAMESPACE,
|
|
115
|
-
component="pyhighlights.components.loaders.ToyLoader",
|
|
116
|
-
)
|
|
117
|
-
def default(cls):
|
|
118
|
-
return super().default()
|
|
119
|
-
|
|
120
100
|
|
|
121
101
|
__all__: List[str] = [
|
|
122
102
|
"BeerConfig",
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""FR: folded rationalization, one selector sharing the predictor backbone."""
|
|
2
|
+
|
|
3
|
+
from cinnamon.configuration import Param
|
|
4
|
+
from cinnamon.registry import RegistrationKey, register_class
|
|
5
|
+
|
|
6
|
+
from pyhighlights.components.models.spp.base import SPPBackbone
|
|
7
|
+
from pyhighlights.configurations.base import SPPModelConfig
|
|
8
|
+
from pyhighlights.configurations.keys import NAMESPACE, TRANSFORMER_BACKBONE
|
|
9
|
+
|
|
10
|
+
FR_COMPONENT = "pyhighlights.components.models.spp.fr.FR"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@register_class(
|
|
14
|
+
name="model", tags={"fr", "gru"}, namespace=NAMESPACE, component=FR_COMPONENT
|
|
15
|
+
)
|
|
16
|
+
class GRUFRConfig(SPPModelConfig):
|
|
17
|
+
name: str = Param("fr")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@register_class(
|
|
21
|
+
name="model",
|
|
22
|
+
tags={"fr", "transformer"},
|
|
23
|
+
namespace=NAMESPACE,
|
|
24
|
+
component=FR_COMPONENT,
|
|
25
|
+
)
|
|
26
|
+
class TransformerFRConfig(GRUFRConfig):
|
|
27
|
+
selector_backbones: RegistrationKey[SPPBackbone] = Param(TRANSFORMER_BACKBONE)
|