borg-nlp 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.
- borg_nlp-0.1.0/PKG-INFO +20 -0
- borg_nlp-0.1.0/README.md +154 -0
- borg_nlp-0.1.0/borg/__init__.py +150 -0
- borg_nlp-0.1.0/borg/cli.py +215 -0
- borg_nlp-0.1.0/borg_nlp.egg-info/PKG-INFO +20 -0
- borg_nlp-0.1.0/borg_nlp.egg-info/SOURCES.txt +51 -0
- borg_nlp-0.1.0/borg_nlp.egg-info/dependency_links.txt +1 -0
- borg_nlp-0.1.0/borg_nlp.egg-info/entry_points.txt +2 -0
- borg_nlp-0.1.0/borg_nlp.egg-info/requires.txt +8 -0
- borg_nlp-0.1.0/borg_nlp.egg-info/top_level.txt +3 -0
- borg_nlp-0.1.0/eval/__init__.py +0 -0
- borg_nlp-0.1.0/eval/conll18_ud_eval.py +432 -0
- borg_nlp-0.1.0/setup.cfg +4 -0
- borg_nlp-0.1.0/setup.py +22 -0
- borg_nlp-0.1.0/src/__init__.py +1 -0
- borg_nlp-0.1.0/src/config.py +117 -0
- borg_nlp-0.1.0/src/data/__init__.py +1 -0
- borg_nlp-0.1.0/src/data/conllu.py +220 -0
- borg_nlp-0.1.0/src/data/dataset.py +610 -0
- borg_nlp-0.1.0/src/models/__init__.py +1 -0
- borg_nlp-0.1.0/src/models/base.py +100 -0
- borg_nlp-0.1.0/src/models/checkpoints.py +131 -0
- borg_nlp-0.1.0/src/models/evaluation.py +52 -0
- borg_nlp-0.1.0/src/models/hub.py +63 -0
- borg_nlp-0.1.0/src/models/inference.py +129 -0
- borg_nlp-0.1.0/src/models/lemmatizer.py +515 -0
- borg_nlp-0.1.0/src/models/mwt.py +424 -0
- borg_nlp-0.1.0/src/models/parser.py +692 -0
- borg_nlp-0.1.0/src/models/tagger.py +314 -0
- borg_nlp-0.1.0/src/models/tokenizer.py +414 -0
- borg_nlp-0.1.0/src/pipeline/__init__.py +1 -0
- borg_nlp-0.1.0/src/pipeline/hf/__init__.py +5 -0
- borg_nlp-0.1.0/src/pipeline/hf/config.json +22 -0
- borg_nlp-0.1.0/src/pipeline/hf/configuration_borg.py +22 -0
- borg_nlp-0.1.0/src/pipeline/hf/export.py +114 -0
- borg_nlp-0.1.0/src/pipeline/hf/modeling_borg.py +28 -0
- borg_nlp-0.1.0/src/pipeline/hf/pipeline.py +217 -0
- borg_nlp-0.1.0/src/pipeline/pipeline.py +163 -0
- borg_nlp-0.1.0/tests/test_checkpoint_config.py +150 -0
- borg_nlp-0.1.0/tests/test_cli_upload.py +68 -0
- borg_nlp-0.1.0/tests/test_dataset_collate.py +168 -0
- borg_nlp-0.1.0/tests/test_hf_pipeline.py +209 -0
- borg_nlp-0.1.0/tests/test_mwt.py +207 -0
- borg_nlp-0.1.0/tests/test_parser_dataset_alignment.py +108 -0
- borg_nlp-0.1.0/tests/test_parser_greedy_decode.py +131 -0
- borg_nlp-0.1.0/tests/test_pipeline.py +707 -0
- borg_nlp-0.1.0/tests/test_prediction_batching.py +423 -0
- borg_nlp-0.1.0/tests/test_tokenizer_dataset_alignment.py +91 -0
- borg_nlp-0.1.0/tests/test_tokenizer_load_classifier.py +73 -0
- borg_nlp-0.1.0/tests/test_tokenizer_whitespace.py +333 -0
- borg_nlp-0.1.0/tests/test_training_checkpoints.py +110 -0
- borg_nlp-0.1.0/tests/test_training_evaluation.py +101 -0
- borg_nlp-0.1.0/tests/test_training_progress.py +78 -0
borg_nlp-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: borg-nlp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A complete NLP processing pipeline using DeBERTa-v3 and adapters
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Requires-Dist: torch>=2.0.0
|
|
10
|
+
Requires-Dist: transformers>=4.35.0
|
|
11
|
+
Requires-Dist: huggingface_hub>=0.19.0
|
|
12
|
+
Requires-Dist: adapters>=0.1.1
|
|
13
|
+
Requires-Dist: numpy>=1.24.0
|
|
14
|
+
Requires-Dist: tqdm>=4.65.0
|
|
15
|
+
Requires-Dist: sentencepiece>=0.1.99
|
|
16
|
+
Requires-Dist: protobuf>=3.20.0
|
|
17
|
+
Dynamic: classifier
|
|
18
|
+
Dynamic: requires-dist
|
|
19
|
+
Dynamic: requires-python
|
|
20
|
+
Dynamic: summary
|
borg_nlp-0.1.0/README.md
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# borg-cube
|
|
2
|
+
|
|
3
|
+
## 1. Introduction
|
|
4
|
+
|
|
5
|
+
`borg-cube` is a Python NLP pipeline for tokenization, multiword-token
|
|
6
|
+
expansion, part-of-speech tagging, dependency parsing, and lemmatization.
|
|
7
|
+
Components can be used from a local model directory or loaded as a
|
|
8
|
+
Hugging Face pipeline.
|
|
9
|
+
|
|
10
|
+
## 2. Quick start guide
|
|
11
|
+
|
|
12
|
+
Install the package and its runtime dependencies:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
python -m pip install borg-cube
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Local models
|
|
19
|
+
|
|
20
|
+
Load component folders from a local directory:
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from borg import cube
|
|
24
|
+
|
|
25
|
+
nlp = cube("en", model_path="/path/to/models/en")
|
|
26
|
+
sentences = nlp("The cat sat on the mat.")
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The model directory contains one folder per component. Without `model_path`,
|
|
30
|
+
`cube("en")` looks under `~/.borg_cube/en/`.
|
|
31
|
+
|
|
32
|
+
### Download adapters directly with `borg.cube`
|
|
33
|
+
|
|
34
|
+
The standard `borg.cube` API can download your component repos directly. The
|
|
35
|
+
base ID here is only a naming prefix; it does not need to be an existing Hub
|
|
36
|
+
repo:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from borg import cube
|
|
40
|
+
|
|
41
|
+
nlp = cube("racai/borg", adapter_variant="combined")
|
|
42
|
+
sentences = nlp("The cat sat on the mat.")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Omit `adapter_variant` for default repo names such as `racai/borg-tokenizer`.
|
|
46
|
+
The Hugging Face Hub caches downloaded adapters locally. This API does not
|
|
47
|
+
require a separate pipeline repo or `trust_remote_code`.
|
|
48
|
+
|
|
49
|
+
### Hugging Face models
|
|
50
|
+
|
|
51
|
+
If you prefer the Transformers `pipeline()` API, publish a base pipeline repo
|
|
52
|
+
as described below. By default it loads unsuffixed adapter repos such as
|
|
53
|
+
`racai/borg-tokenizer`:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from transformers import pipeline
|
|
57
|
+
|
|
58
|
+
nlp = pipeline(model="racai/borg", trust_remote_code=True)
|
|
59
|
+
result = nlp("The cat sat on the mat.")
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
For a named variant, pass `adapter_variant`. For example, `"combined"` loads
|
|
63
|
+
`racai/borg-tokenizer-combined`, `racai/borg-parser-combined`, and the other
|
|
64
|
+
listed component repos with the same suffix:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
nlp = pipeline(
|
|
68
|
+
model="racai/borg",
|
|
69
|
+
trust_remote_code=True,
|
|
70
|
+
adapter_variant="combined",
|
|
71
|
+
)
|
|
72
|
+
result = nlp("The cat sat on the mat.")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Install `borg-cube` in the inference environment before loading the pipeline.
|
|
76
|
+
`trust_remote_code=True` runs code from the Hub repository; review it before
|
|
77
|
+
enabling this option. Runtime device selection uses the local hardware by
|
|
78
|
+
default; pass `device=-1` to force CPU.
|
|
79
|
+
|
|
80
|
+
Predictions use batches of up to `eval_batch_size` sentences (default: 32).
|
|
81
|
+
Reduce it if inference exceeds available memory. Inputs are truncated at
|
|
82
|
+
`max_seq_length`. Component `predict()` methods show progress bars by default;
|
|
83
|
+
pass `show_progress=False` to disable them.
|
|
84
|
+
|
|
85
|
+
## 3. Training your own model
|
|
86
|
+
|
|
87
|
+
### Install from GitHub
|
|
88
|
+
|
|
89
|
+
Install the latest source directly:
|
|
90
|
+
|
|
91
|
+
```sh
|
|
92
|
+
python -m pip install "git+https://github.com/tiberiu44/borg-cube.git"
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
For development, clone the repository and install it in editable mode:
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
git clone https://github.com/tiberiu44/borg-cube.git
|
|
99
|
+
cd borg-cube
|
|
100
|
+
python -m pip install -e .
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Train components
|
|
104
|
+
|
|
105
|
+
Train one component using CoNLL-U training and development files:
|
|
106
|
+
|
|
107
|
+
```sh
|
|
108
|
+
borg train --component tokenizer train.conllu dev.conllu data/models/mixed
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Repeat with `mwt`, `tagger`, `parser`, or `lemmatizer` as needed. Training
|
|
112
|
+
saves component checkpoints under
|
|
113
|
+
`<model-directory>/<component>/{best,last}`. The CLI also accepts options such
|
|
114
|
+
as `--epochs`, `--batch-size`, `--device`, and `--lang`.
|
|
115
|
+
|
|
116
|
+
### Publish Hugging Face adapters and pipeline
|
|
117
|
+
|
|
118
|
+
Log in to Hugging Face first:
|
|
119
|
+
|
|
120
|
+
```sh
|
|
121
|
+
huggingface-cli login
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Upload each component directory using the base repo name, component name, and
|
|
125
|
+
optional variant suffix. For example:
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
borg upload data/models/mixed/tokenizer/ racai/borg-tokenizer-combined
|
|
129
|
+
borg upload data/models/mixed/mwt/ racai/borg-mwt-combined
|
|
130
|
+
borg upload data/models/mixed/tagger/ racai/borg-tagger-combined
|
|
131
|
+
borg upload data/models/mixed/parser/ racai/borg-parser-combined
|
|
132
|
+
borg upload data/models/mixed/lemmatizer/ racai/borg-lemmatizer-combined
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
These component repos are sufficient for `borg.cube`; a separate base pipeline
|
|
136
|
+
repo is needed only for Transformers `pipeline()`:
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
borg upload racai/borg --pipeline
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
This publishes the pipeline configuration and remote code, not adapter
|
|
143
|
+
weights. By default it lists all supported components; use
|
|
144
|
+
`--components tokenizer parser` to list only selected ones. Adapters may have
|
|
145
|
+
`borg_config.json` at the repository root; `best/` or `last/` subfolders are
|
|
146
|
+
not required.
|
|
147
|
+
|
|
148
|
+
Runtime configuration is loaded from the adapters. Training checkpoints save
|
|
149
|
+
`BorgConfig.dtype` as a string and restore it as a PyTorch dtype; if the local
|
|
150
|
+
device does not support BF16 autocast, inference warns and uses float32.
|
|
151
|
+
|
|
152
|
+
## 4. Paper to cite
|
|
153
|
+
|
|
154
|
+
Citation information will be added here.
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""Public Python API for borg-cube.
|
|
2
|
+
|
|
3
|
+
Usage::
|
|
4
|
+
|
|
5
|
+
from borg import cube
|
|
6
|
+
nlp = cube('en')
|
|
7
|
+
sentences = nlp("The cat sat on the mat.")
|
|
8
|
+
print(sentences)
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
from typing import List, Mapping, Optional
|
|
14
|
+
from urllib.parse import unquote
|
|
15
|
+
|
|
16
|
+
from src.config import BorgConfig
|
|
17
|
+
from src.data.conllu import Sentence
|
|
18
|
+
from src.pipeline.pipeline import BorgPipeline
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Cube:
|
|
22
|
+
"""High-level interface to the borg-cube NLP pipeline.
|
|
23
|
+
|
|
24
|
+
Parameters
|
|
25
|
+
----------
|
|
26
|
+
lang:
|
|
27
|
+
Language code (used for locating default model files).
|
|
28
|
+
model_path:
|
|
29
|
+
Base directory containing sub-directories for each component
|
|
30
|
+
(``tokenizer/``, ``mwt/``, ``tagger/``, ``parser/``, ``lemmatizer/``).
|
|
31
|
+
If *None*, defaults to ``~/.borg_cube/<lang>/``.
|
|
32
|
+
components:
|
|
33
|
+
List of components to load. Defaults to all pipeline components.
|
|
34
|
+
config:
|
|
35
|
+
Optional :class:`~src.config.BorgConfig` instance.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
DEFAULT_COMPONENTS = ["tokenizer", "mwt", "tagger", "parser", "lemmatizer"]
|
|
39
|
+
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
lang: str = "en",
|
|
43
|
+
model_path: Optional[str] = None,
|
|
44
|
+
components: Optional[List[str]] = None,
|
|
45
|
+
config: Optional[BorgConfig] = None,
|
|
46
|
+
_component_paths: Optional[Mapping[str, str]] = None,
|
|
47
|
+
):
|
|
48
|
+
self.lang = lang
|
|
49
|
+
if model_path is None and _component_paths is None:
|
|
50
|
+
model_path = os.path.join(os.path.expanduser("~"), ".borg_cube", lang)
|
|
51
|
+
self.model_path = model_path
|
|
52
|
+
|
|
53
|
+
if config is None:
|
|
54
|
+
config = BorgConfig(lang=lang)
|
|
55
|
+
self.config = config
|
|
56
|
+
|
|
57
|
+
components = self.DEFAULT_COMPONENTS if components is None else components
|
|
58
|
+
|
|
59
|
+
self.pipeline = BorgPipeline(config)
|
|
60
|
+
for component in components:
|
|
61
|
+
if _component_paths is not None:
|
|
62
|
+
self.pipeline.load_component(
|
|
63
|
+
component,
|
|
64
|
+
_component_paths[component],
|
|
65
|
+
)
|
|
66
|
+
else:
|
|
67
|
+
component_dir = os.path.join(model_path, component)
|
|
68
|
+
if os.path.isdir(component_dir):
|
|
69
|
+
self.pipeline.load_component(component, component_dir)
|
|
70
|
+
|
|
71
|
+
# ------------------------------------------------------------------
|
|
72
|
+
def __call__(self, text: str) -> List[Sentence]:
|
|
73
|
+
"""Process *text* and return a list of annotated :class:`Sentence` objects."""
|
|
74
|
+
return self.pipeline.process(text)
|
|
75
|
+
|
|
76
|
+
def __repr__(self) -> str:
|
|
77
|
+
loaded = [
|
|
78
|
+
c for c in self.DEFAULT_COMPONENTS
|
|
79
|
+
if getattr(self.pipeline, f"{c}_model") is not None
|
|
80
|
+
]
|
|
81
|
+
return f"Cube(lang={self.lang!r}, loaded={loaded})"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def cube(
|
|
85
|
+
model: str,
|
|
86
|
+
components: Optional[List[str]] = None,
|
|
87
|
+
config: Optional[BorgConfig] = None,
|
|
88
|
+
*,
|
|
89
|
+
adapter_variant: Optional[str] = None,
|
|
90
|
+
checkpoint: str = "best",
|
|
91
|
+
) -> Cube:
|
|
92
|
+
"""Load a local model or download component adapters from the Hub.
|
|
93
|
+
|
|
94
|
+
``cube("en")`` loads local components from ``~/.borg_cube/en/``.
|
|
95
|
+
``cube("file:///path/to/models")`` loads from a local folder.
|
|
96
|
+
A Hub base ID such as ``"racai/borg"`` downloads component repos named
|
|
97
|
+
``racai/borg-<component>``; ``adapter_variant="combined"`` appends
|
|
98
|
+
``-combined`` to each component repo name. No base pipeline repo is needed.
|
|
99
|
+
"""
|
|
100
|
+
if model.startswith("file://"):
|
|
101
|
+
model_path = unquote(model[len("file://"):])
|
|
102
|
+
if not model_path:
|
|
103
|
+
raise ValueError("A file:// model URI must include a folder path")
|
|
104
|
+
lang = os.path.basename(os.path.normpath(model_path)) or "en"
|
|
105
|
+
return Cube(lang=lang, model_path=model_path, components=components, config=config)
|
|
106
|
+
|
|
107
|
+
if model.count("/") == 1 and not model.startswith((".", "/")):
|
|
108
|
+
selected_components = (
|
|
109
|
+
Cube.DEFAULT_COMPONENTS if components is None else components
|
|
110
|
+
)
|
|
111
|
+
if not selected_components:
|
|
112
|
+
return Cube(
|
|
113
|
+
lang=config.lang if config is not None else "en",
|
|
114
|
+
components=[],
|
|
115
|
+
config=config,
|
|
116
|
+
_component_paths={},
|
|
117
|
+
)
|
|
118
|
+
unknown = set(selected_components) - set(Cube.DEFAULT_COMPONENTS)
|
|
119
|
+
if unknown:
|
|
120
|
+
raise ValueError(f"Unknown components: {', '.join(sorted(unknown))}")
|
|
121
|
+
|
|
122
|
+
from src.models.hub import download_component_checkpoints
|
|
123
|
+
|
|
124
|
+
component_paths = download_component_checkpoints(
|
|
125
|
+
model,
|
|
126
|
+
selected_components,
|
|
127
|
+
variant=adapter_variant,
|
|
128
|
+
checkpoint=checkpoint,
|
|
129
|
+
)
|
|
130
|
+
if config is None:
|
|
131
|
+
import json
|
|
132
|
+
|
|
133
|
+
first_checkpoint = next(iter(component_paths.values()))
|
|
134
|
+
with (first_checkpoint / "borg_config.json").open(
|
|
135
|
+
encoding="utf-8"
|
|
136
|
+
) as source:
|
|
137
|
+
config = BorgConfig.from_dict(json.load(source))
|
|
138
|
+
config.device = "auto"
|
|
139
|
+
|
|
140
|
+
return Cube(
|
|
141
|
+
lang=config.lang,
|
|
142
|
+
components=selected_components,
|
|
143
|
+
config=config,
|
|
144
|
+
_component_paths={
|
|
145
|
+
component: str(path)
|
|
146
|
+
for component, path in component_paths.items()
|
|
147
|
+
},
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
return Cube(lang=model, components=components, config=config)
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""CLI entry point for borg-cube.
|
|
2
|
+
|
|
3
|
+
Commands
|
|
4
|
+
--------
|
|
5
|
+
borg train --component <component> <train_file> <dev_file> <model_file>
|
|
6
|
+
borg test <model_file> <input_file> <output_file>
|
|
7
|
+
borg upload <component_path> <repo_id>
|
|
8
|
+
borg upload <base_repo_id> --pipeline
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
import sys
|
|
14
|
+
import tempfile
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from huggingface_hub import HfApi
|
|
18
|
+
from src.config import BorgConfig
|
|
19
|
+
from src.pipeline.pipeline import BorgPipeline
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
23
|
+
top = argparse.ArgumentParser(
|
|
24
|
+
prog="borg",
|
|
25
|
+
description="borg-cube NLP pipeline CLI",
|
|
26
|
+
)
|
|
27
|
+
sub = top.add_subparsers(dest="command", required=True)
|
|
28
|
+
|
|
29
|
+
# ---- train ----
|
|
30
|
+
train_p = sub.add_parser("train", help="Train a pipeline component")
|
|
31
|
+
train_p.add_argument(
|
|
32
|
+
"--component",
|
|
33
|
+
choices=["tokenizer", "mwt", "tagger", "parser", "lemmatizer"],
|
|
34
|
+
required=True,
|
|
35
|
+
help="Component to train",
|
|
36
|
+
)
|
|
37
|
+
train_p.add_argument("train_file", help="Path to CoNLL-U training file")
|
|
38
|
+
train_p.add_argument("dev_file", help="Path to CoNLL-U development file")
|
|
39
|
+
train_p.add_argument("model_file", help="Model folder; the component is saved in its subfolder")
|
|
40
|
+
train_p.add_argument("--resume", action="store_true", help="Resume from the component's last checkpoint")
|
|
41
|
+
|
|
42
|
+
# Optional training hyper-parameter overrides
|
|
43
|
+
train_p.add_argument("--epochs", type=int, default=None)
|
|
44
|
+
train_p.add_argument("--batch-size", type=int, default=None)
|
|
45
|
+
train_p.add_argument("--lr", type=float, default=None)
|
|
46
|
+
train_p.add_argument("--device", type=str, default=None)
|
|
47
|
+
train_p.add_argument("--lang", type=str, default="en")
|
|
48
|
+
|
|
49
|
+
# ---- test ----
|
|
50
|
+
test_p = sub.add_parser("test", help="Run inference with a trained model/pipeline")
|
|
51
|
+
test_p.add_argument("model_file", help="Model directory (or pipeline root)")
|
|
52
|
+
test_p.add_argument("input_file", help="Input file (.txt for raw text, .conllu for annotated)")
|
|
53
|
+
test_p.add_argument("output_file", help="Output CoNLL-U file")
|
|
54
|
+
test_p.add_argument(
|
|
55
|
+
"--component",
|
|
56
|
+
choices=["tokenizer", "mwt", "tagger", "parser", "lemmatizer"],
|
|
57
|
+
default=None,
|
|
58
|
+
help="Single component to apply (default: auto-detect from directory contents)",
|
|
59
|
+
)
|
|
60
|
+
test_p.add_argument("--device", type=str, default=None)
|
|
61
|
+
test_p.add_argument("--lang", type=str, default="en")
|
|
62
|
+
|
|
63
|
+
# ---- upload ----
|
|
64
|
+
upload_p = sub.add_parser(
|
|
65
|
+
"upload",
|
|
66
|
+
help="Upload a component or complete pipeline to the Hugging Face Hub",
|
|
67
|
+
)
|
|
68
|
+
upload_p.add_argument(
|
|
69
|
+
"model_path",
|
|
70
|
+
nargs="?",
|
|
71
|
+
help="Component directory for raw upload; ignored with --pipeline",
|
|
72
|
+
)
|
|
73
|
+
upload_p.add_argument(
|
|
74
|
+
"repo_id",
|
|
75
|
+
nargs="?",
|
|
76
|
+
help="Hub repository ID (or the only positional argument with --pipeline)",
|
|
77
|
+
)
|
|
78
|
+
upload_p.add_argument(
|
|
79
|
+
"--pipeline",
|
|
80
|
+
action="store_true",
|
|
81
|
+
help="Upload pipeline metadata/code; adapters use <repo_id>-<component> repos",
|
|
82
|
+
)
|
|
83
|
+
upload_p.add_argument(
|
|
84
|
+
"--components",
|
|
85
|
+
nargs="+",
|
|
86
|
+
choices=["tokenizer", "mwt", "tagger", "parser", "lemmatizer"],
|
|
87
|
+
default=None,
|
|
88
|
+
help="Components to include in the pipeline (default: all)",
|
|
89
|
+
)
|
|
90
|
+
upload_p.add_argument(
|
|
91
|
+
"--checkpoint",
|
|
92
|
+
choices=["best", "last"],
|
|
93
|
+
default="best",
|
|
94
|
+
help=(
|
|
95
|
+
"Use this checkpoint subfolder, or a root-level adapter if absent "
|
|
96
|
+
"(default: best)"
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
return top
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _make_config(args: argparse.Namespace) -> BorgConfig:
|
|
104
|
+
cfg = BorgConfig(lang=getattr(args, "lang", "en"))
|
|
105
|
+
if getattr(args, "epochs", None) is not None:
|
|
106
|
+
cfg.num_epochs = args.epochs
|
|
107
|
+
if getattr(args, "batch_size", None) is not None:
|
|
108
|
+
cfg.batch_size = args.batch_size
|
|
109
|
+
if getattr(args, "lr", None) is not None:
|
|
110
|
+
cfg.learning_rate = args.lr
|
|
111
|
+
if getattr(args, "device", None) is not None:
|
|
112
|
+
cfg.device = args.device
|
|
113
|
+
return cfg
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def cmd_train(args: argparse.Namespace) -> None:
|
|
117
|
+
config = _make_config(args)
|
|
118
|
+
pipeline = BorgPipeline(config)
|
|
119
|
+
pipeline.train_component(
|
|
120
|
+
args.component, args.train_file, args.dev_file, args.model_file,
|
|
121
|
+
resume=args.resume,
|
|
122
|
+
)
|
|
123
|
+
print(
|
|
124
|
+
f"Saved {args.component} last and best models to "
|
|
125
|
+
f"{args.model_file}/{args.component}/{{last,best}}"
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def cmd_test(args: argparse.Namespace) -> None:
|
|
130
|
+
import os
|
|
131
|
+
|
|
132
|
+
config = _make_config(args)
|
|
133
|
+
pipeline = BorgPipeline(config)
|
|
134
|
+
|
|
135
|
+
if args.component is not None:
|
|
136
|
+
# Single component test
|
|
137
|
+
pipeline.load_component(args.component, args.model_file)
|
|
138
|
+
else:
|
|
139
|
+
# Auto-detect: try to load whichever component directories exist
|
|
140
|
+
for comp in ["tokenizer", "mwt", "tagger", "parser", "lemmatizer"]:
|
|
141
|
+
comp_dir = os.path.join(args.model_file, comp)
|
|
142
|
+
if os.path.isdir(comp_dir):
|
|
143
|
+
pipeline.load_component(comp, comp_dir)
|
|
144
|
+
elif os.path.isfile(os.path.join(args.model_file, "borg_config.json")):
|
|
145
|
+
# model_file IS a single component directory
|
|
146
|
+
# figure out which one
|
|
147
|
+
import json
|
|
148
|
+
with open(os.path.join(args.model_file, "borg_config.json")) as f:
|
|
149
|
+
info = json.load(f)
|
|
150
|
+
detected = info.get("component")
|
|
151
|
+
if detected:
|
|
152
|
+
pipeline.load_component(detected, args.model_file)
|
|
153
|
+
break
|
|
154
|
+
|
|
155
|
+
pipeline.test(args.model_file, args.input_file, args.output_file)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _upload_folder(folder: Path, repo_id: str) -> None:
|
|
159
|
+
api = HfApi()
|
|
160
|
+
api.create_repo(repo_id, repo_type="model", exist_ok=True)
|
|
161
|
+
api.upload_folder(
|
|
162
|
+
repo_id=repo_id,
|
|
163
|
+
repo_type="model",
|
|
164
|
+
folder_path=str(folder),
|
|
165
|
+
)
|
|
166
|
+
print(f"Uploaded to https://huggingface.co/{repo_id}")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def cmd_upload(args: argparse.Namespace) -> None:
|
|
170
|
+
if args.pipeline:
|
|
171
|
+
repo_id = args.repo_id or args.model_path
|
|
172
|
+
if not repo_id:
|
|
173
|
+
raise ValueError("A Hub repo ID is required for pipeline upload")
|
|
174
|
+
from src.pipeline.hf.export import export_hf_repo
|
|
175
|
+
|
|
176
|
+
with tempfile.TemporaryDirectory(prefix="borg-hf-upload-") as temp_dir:
|
|
177
|
+
export_path = Path(temp_dir) / "model"
|
|
178
|
+
export_hf_repo(
|
|
179
|
+
export_path,
|
|
180
|
+
components=args.components,
|
|
181
|
+
checkpoint=args.checkpoint,
|
|
182
|
+
)
|
|
183
|
+
_upload_folder(export_path, repo_id)
|
|
184
|
+
return
|
|
185
|
+
|
|
186
|
+
if args.model_path is None or args.repo_id is None:
|
|
187
|
+
raise ValueError("Raw component upload requires a directory and a Hub repo ID")
|
|
188
|
+
if args.components is not None:
|
|
189
|
+
raise ValueError("--components can only be used with --pipeline")
|
|
190
|
+
model_path = Path(args.model_path).expanduser()
|
|
191
|
+
if not model_path.is_dir():
|
|
192
|
+
raise FileNotFoundError(f"Model directory not found: {model_path}")
|
|
193
|
+
|
|
194
|
+
if args.checkpoint != "best":
|
|
195
|
+
raise ValueError("--checkpoint can only be used with --pipeline")
|
|
196
|
+
_upload_folder(model_path, args.repo_id)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def main(argv=None) -> None:
|
|
200
|
+
parser = _build_parser()
|
|
201
|
+
args = parser.parse_args(argv)
|
|
202
|
+
|
|
203
|
+
if args.command == "train":
|
|
204
|
+
cmd_train(args)
|
|
205
|
+
elif args.command == "test":
|
|
206
|
+
cmd_test(args)
|
|
207
|
+
elif args.command == "upload":
|
|
208
|
+
cmd_upload(args)
|
|
209
|
+
else:
|
|
210
|
+
parser.print_help()
|
|
211
|
+
sys.exit(1)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
if __name__ == "__main__":
|
|
215
|
+
main()
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: borg-nlp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A complete NLP processing pipeline using DeBERTa-v3 and adapters
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Requires-Dist: torch>=2.0.0
|
|
10
|
+
Requires-Dist: transformers>=4.35.0
|
|
11
|
+
Requires-Dist: huggingface_hub>=0.19.0
|
|
12
|
+
Requires-Dist: adapters>=0.1.1
|
|
13
|
+
Requires-Dist: numpy>=1.24.0
|
|
14
|
+
Requires-Dist: tqdm>=4.65.0
|
|
15
|
+
Requires-Dist: sentencepiece>=0.1.99
|
|
16
|
+
Requires-Dist: protobuf>=3.20.0
|
|
17
|
+
Dynamic: classifier
|
|
18
|
+
Dynamic: requires-dist
|
|
19
|
+
Dynamic: requires-python
|
|
20
|
+
Dynamic: summary
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
setup.py
|
|
3
|
+
borg/__init__.py
|
|
4
|
+
borg/cli.py
|
|
5
|
+
borg_nlp.egg-info/PKG-INFO
|
|
6
|
+
borg_nlp.egg-info/SOURCES.txt
|
|
7
|
+
borg_nlp.egg-info/dependency_links.txt
|
|
8
|
+
borg_nlp.egg-info/entry_points.txt
|
|
9
|
+
borg_nlp.egg-info/requires.txt
|
|
10
|
+
borg_nlp.egg-info/top_level.txt
|
|
11
|
+
eval/__init__.py
|
|
12
|
+
eval/conll18_ud_eval.py
|
|
13
|
+
src/__init__.py
|
|
14
|
+
src/config.py
|
|
15
|
+
src/data/__init__.py
|
|
16
|
+
src/data/conllu.py
|
|
17
|
+
src/data/dataset.py
|
|
18
|
+
src/models/__init__.py
|
|
19
|
+
src/models/base.py
|
|
20
|
+
src/models/checkpoints.py
|
|
21
|
+
src/models/evaluation.py
|
|
22
|
+
src/models/hub.py
|
|
23
|
+
src/models/inference.py
|
|
24
|
+
src/models/lemmatizer.py
|
|
25
|
+
src/models/mwt.py
|
|
26
|
+
src/models/parser.py
|
|
27
|
+
src/models/tagger.py
|
|
28
|
+
src/models/tokenizer.py
|
|
29
|
+
src/pipeline/__init__.py
|
|
30
|
+
src/pipeline/pipeline.py
|
|
31
|
+
src/pipeline/hf/__init__.py
|
|
32
|
+
src/pipeline/hf/config.json
|
|
33
|
+
src/pipeline/hf/configuration_borg.py
|
|
34
|
+
src/pipeline/hf/export.py
|
|
35
|
+
src/pipeline/hf/modeling_borg.py
|
|
36
|
+
src/pipeline/hf/pipeline.py
|
|
37
|
+
tests/test_checkpoint_config.py
|
|
38
|
+
tests/test_cli_upload.py
|
|
39
|
+
tests/test_dataset_collate.py
|
|
40
|
+
tests/test_hf_pipeline.py
|
|
41
|
+
tests/test_mwt.py
|
|
42
|
+
tests/test_parser_dataset_alignment.py
|
|
43
|
+
tests/test_parser_greedy_decode.py
|
|
44
|
+
tests/test_pipeline.py
|
|
45
|
+
tests/test_prediction_batching.py
|
|
46
|
+
tests/test_tokenizer_dataset_alignment.py
|
|
47
|
+
tests/test_tokenizer_load_classifier.py
|
|
48
|
+
tests/test_tokenizer_whitespace.py
|
|
49
|
+
tests/test_training_checkpoints.py
|
|
50
|
+
tests/test_training_evaluation.py
|
|
51
|
+
tests/test_training_progress.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
File without changes
|