theclassifier 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.
- theclassifier-0.1.0/LICENSE +202 -0
- theclassifier-0.1.0/MANIFEST.in +3 -0
- theclassifier-0.1.0/PKG-INFO +215 -0
- theclassifier-0.1.0/README.md +189 -0
- theclassifier-0.1.0/RELEASE.md +69 -0
- theclassifier-0.1.0/examples/train.jsonl +2 -0
- theclassifier-0.1.0/pyproject.toml +30 -0
- theclassifier-0.1.0/setup.cfg +4 -0
- theclassifier-0.1.0/src/theclassifier/__init__.py +12 -0
- theclassifier-0.1.0/src/theclassifier/__main__.py +2 -0
- theclassifier-0.1.0/src/theclassifier/checkpoints.py +40 -0
- theclassifier-0.1.0/src/theclassifier/cli.py +63 -0
- theclassifier-0.1.0/src/theclassifier/model.py +142 -0
- theclassifier-0.1.0/src/theclassifier/prompt.py +45 -0
- theclassifier-0.1.0/src/theclassifier/schema.py +30 -0
- theclassifier-0.1.0/src/theclassifier/server.py +33 -0
- theclassifier-0.1.0/src/theclassifier/training.py +120 -0
- theclassifier-0.1.0/src/theclassifier.egg-info/PKG-INFO +215 -0
- theclassifier-0.1.0/src/theclassifier.egg-info/SOURCES.txt +22 -0
- theclassifier-0.1.0/src/theclassifier.egg-info/dependency_links.txt +1 -0
- theclassifier-0.1.0/src/theclassifier.egg-info/entry_points.txt +2 -0
- theclassifier-0.1.0/src/theclassifier.egg-info/requires.txt +16 -0
- theclassifier-0.1.0/src/theclassifier.egg-info/top_level.txt +1 -0
- theclassifier-0.1.0/tests/test_package.py +114 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: theclassifier
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Natural language classification on CPU and GPU, with domain adapters and LoRA fine-tuning
|
|
5
|
+
Author: TrueState
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://theclassifier.ai
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: torch<3,>=2.6
|
|
12
|
+
Requires-Dist: transformers<6,>=4.56
|
|
13
|
+
Requires-Dist: peft<1,>=0.17
|
|
14
|
+
Requires-Dist: accelerate<2,>=1
|
|
15
|
+
Requires-Dist: huggingface-hub<2,>=0.34
|
|
16
|
+
Requires-Dist: safetensors>=0.5
|
|
17
|
+
Provides-Extra: serve
|
|
18
|
+
Requires-Dist: fastapi<1,>=0.115; extra == "serve"
|
|
19
|
+
Requires-Dist: uvicorn<1,>=0.30; extra == "serve"
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
22
|
+
Requires-Dist: httpx; extra == "dev"
|
|
23
|
+
Requires-Dist: build; extra == "dev"
|
|
24
|
+
Requires-Dist: twine; extra == "dev"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# The Classifier
|
|
28
|
+
|
|
29
|
+
A general-purpose natural language classifier with domain-specific LoRA adapters.
|
|
30
|
+
Run locally on CPU or NVIDIA GPU, serve the same `/v1/classify` request format as
|
|
31
|
+
the hosted service, or fine-tune an adapter on your own labelled examples.
|
|
32
|
+
|
|
33
|
+
## Release status
|
|
34
|
+
|
|
35
|
+
This is the initial 0.1.0 package. The configured model repository is
|
|
36
|
+
`TrueState/theclassifier`. The checkpoints are staged privately; authenticate with
|
|
37
|
+
Hugging Face and obtain repository access before using named-version downloads.
|
|
38
|
+
A local checkpoint also works.
|
|
39
|
+
Model weights are not included in the Python package. This code is Apache-2.0;
|
|
40
|
+
checkpoint and dataset licences are separate.
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
From this checkout, until the PyPI release is published:
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
pip install './python[serve]'
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
After release:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install 'theclassifier[serve]'
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Python 3.10+ is required. Install a PyTorch build suitable for your platform first
|
|
57
|
+
if needed; CUDA support depends on that build and your NVIDIA driver. CPU uses
|
|
58
|
+
float32; CUDA uses bfloat16 when supported, otherwise float32. No FlashAttention
|
|
59
|
+
extension is required. `auto` selects CUDA when available and CPU otherwise.
|
|
60
|
+
Apple MPS is not currently supported.
|
|
61
|
+
|
|
62
|
+
## Classify locally
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from theclassifier import Classifier
|
|
66
|
+
|
|
67
|
+
classifier = Classifier() # general; downloads once, then uses the local cache
|
|
68
|
+
result = classifier.classify(
|
|
69
|
+
'Please close my account.',
|
|
70
|
+
choices=['billing', 'cancellation', 'none'],
|
|
71
|
+
question='What is the customer asking for?',
|
|
72
|
+
)
|
|
73
|
+
print(result['results'][0]['selected'])
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Use `Classifier(checkpoint='./checkpoints/general', device='cpu')` to load an
|
|
77
|
+
existing local checkpoint. A checkpoint contains `adapter/`, `tokenizer/` and
|
|
78
|
+
`head.safetensors` (or the existing `head.pt` format). The pretrained backbone is
|
|
79
|
+
resolved from the adapter configuration. A local adapter can still require a
|
|
80
|
+
backbone download if the backbone is not already cached.
|
|
81
|
+
|
|
82
|
+
The full question format supports multiple decisions and optional criteria:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
result = classifier.classify(
|
|
86
|
+
'Please close my account.',
|
|
87
|
+
questions=[{
|
|
88
|
+
'question': 'What is the customer asking for?',
|
|
89
|
+
'choices': [
|
|
90
|
+
{'choice': 'cancellation', 'criteria': 'Requests to close an account.'},
|
|
91
|
+
{'choice': 'none', 'criteria': 'None of the other choices apply.'},
|
|
92
|
+
],
|
|
93
|
+
}],
|
|
94
|
+
version='intent',
|
|
95
|
+
)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Versions: `general`, `intent`, `sentiment`, `triage`, `cause`, `codify`,
|
|
99
|
+
`complaints`, `stars`. Selecting a new version checks the Hugging Face cache,
|
|
100
|
+
downloads missing files, then loads the adapter and its scoring head onto the
|
|
101
|
+
shared backbone. Selection and inference are serialized to keep the adapter and
|
|
102
|
+
head paired. A failed download raises an error; it never silently falls back to
|
|
103
|
+
another model.
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
classifier.load_adapter('custom', checkpoint='./checkpoints/my-adapter')
|
|
107
|
+
result = classifier.classify('example', choices=['yes', 'no'], version='custom')
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Use `model_repo=`, `revision=` and `cache_dir=` or the
|
|
111
|
+
`THECLASSIFIER_MODEL_REPO` environment variable to override download settings.
|
|
112
|
+
Pin `revision` to a commit for reproducibility. `offline=True` disables network
|
|
113
|
+
loading, including the base model. Pre-download adapters with:
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
theclassifier download --version general
|
|
117
|
+
theclassifier download --version intent
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
These commands download adapters and tokenizers; initialize the classifier once
|
|
121
|
+
online to cache the backbone as well. The repository layout is
|
|
122
|
+
`<version>/adapter/`, `<version>/tokenizer/`, `<version>/head.safetensors`.
|
|
123
|
+
|
|
124
|
+
Default maximum input length is 2,048 tokens per candidate; longer inputs retain
|
|
125
|
+
the first token and tail, matching the existing prompt format. Set `max_length`
|
|
126
|
+
explicitly for longer records. `batch_size` controls inference candidate chunks.
|
|
127
|
+
This initial portable runtime uses direct candidate scoring, not shared-prefix
|
|
128
|
+
KV caching. It has not been benchmarked against the optimized production server.
|
|
129
|
+
|
|
130
|
+
## Serve on CPU or GPU
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
theclassifier serve --checkpoint ./checkpoints/general --device cpu
|
|
134
|
+
# On a CUDA machine:
|
|
135
|
+
API_KEY=your-serving-key theclassifier serve --device cuda --host 0.0.0.0
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Default binding is `127.0.0.1:8090`. Binding outside localhost requires `API_KEY`.
|
|
139
|
+
Use one process per GPU; run behind a TLS proxy for remote clients. This standalone
|
|
140
|
+
server provides inference and authentication, not the hosted service's billing,
|
|
141
|
+
quotas or rate limiting. Set resource limits in your deployment.
|
|
142
|
+
|
|
143
|
+
```sh
|
|
144
|
+
curl http://127.0.0.1:8090/v1/classify \
|
|
145
|
+
-H 'Content-Type: application/json' \
|
|
146
|
+
-d '{"state":"Close my account","questions":[{"question":"Intent?","choices":[{"choice":"cancel"},{"choice":"none"}]}]}'
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Include `Authorization: Bearer YOUR_KEY` when `API_KEY` is configured.
|
|
150
|
+
Responses contain `results` (with `question`, `selected`, `detailed_results`),
|
|
151
|
+
`version`, `input_tokens` and `latency_ms`.
|
|
152
|
+
|
|
153
|
+
## Fine-tune with LoRA
|
|
154
|
+
|
|
155
|
+
Each JSONL line is one complete choice menu, with an exact matching answer label:
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{"state":"Close my account","question":"Intent?","choices":[{"choice":"cancel","criteria":"Requests to close an account"},{"choice":"none"}],"answer":"cancel"}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```sh
|
|
162
|
+
# Continue the general checkpoint to create a specialist:
|
|
163
|
+
theclassifier finetune --version general --data train.jsonl \
|
|
164
|
+
--output checkpoints/my-adapter --device cuda
|
|
165
|
+
|
|
166
|
+
# Train an adapter and scoring head from the base model:
|
|
167
|
+
theclassifier finetune --base-model Qwen/Qwen3-1.7B-Base \
|
|
168
|
+
--data train.jsonl --output checkpoints/from-base --rank 16 --alpha 16
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Or in Python:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from theclassifier import finetune
|
|
175
|
+
|
|
176
|
+
report = finetune(
|
|
177
|
+
'train.jsonl', 'checkpoints/my-adapter',
|
|
178
|
+
checkpoint='checkpoints/general', device='auto', epochs=1,
|
|
179
|
+
learning_rate=2e-4, grad_accum=16,
|
|
180
|
+
)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Training freezes the backbone, learns LoRA updates to `q_proj` and `v_proj`, and
|
|
184
|
+
trains a float32 scalar head using cross-entropy across each menu. Whole menus
|
|
185
|
+
are processed together, gradients accumulate between menus, and gradient
|
|
186
|
+
checkpointing reduces activation memory. Large choice menus may still require
|
|
187
|
+
smaller input lengths or more memory. CPU training is supported but slow.
|
|
188
|
+
|
|
189
|
+
New adapters default to rank 16, alpha 16 and dropout 0.05. Continuing an existing
|
|
190
|
+
adapter preserves its LoRA configuration. A new AdamW optimizer is created with
|
|
191
|
+
a constant learning rate; this is fine-tuning, not exact training-state resume.
|
|
192
|
+
The optimizer configuration differs from the original research runs. Output
|
|
193
|
+
folders must be empty. Saved files include the adapter, head, tokenizer,
|
|
194
|
+
`classifier.json` and `training.json`. Keep evaluation data separate and evaluate
|
|
195
|
+
before using a new checkpoint; the training loss is not a quality benchmark.
|
|
196
|
+
|
|
197
|
+
## Development and release
|
|
198
|
+
|
|
199
|
+
```sh
|
|
200
|
+
cd python
|
|
201
|
+
pip install -e '.[serve,dev]'
|
|
202
|
+
pytest
|
|
203
|
+
python -m build
|
|
204
|
+
python -m twine check dist/*
|
|
205
|
+
# With PyPI credentials configured locally:
|
|
206
|
+
python -m twine upload dist/*
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
The tests build a tiny random Qwen model locally: no production weights, private
|
|
210
|
+
data or external inference service is needed. All six tests passed on the NVIDIA
|
|
211
|
+
workstation, including CUDA inference. Real v1.1 checkpoints were also smoke-tested
|
|
212
|
+
on CUDA, and the general model was checked on CPU against the original scoring
|
|
213
|
+
implementation. These checks establish execution compatibility, not accuracy.
|
|
214
|
+
Publishing the package does not upload model weights. See `RELEASE.md` for the
|
|
215
|
+
checkpoint and publication steps.
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# The Classifier
|
|
2
|
+
|
|
3
|
+
A general-purpose natural language classifier with domain-specific LoRA adapters.
|
|
4
|
+
Run locally on CPU or NVIDIA GPU, serve the same `/v1/classify` request format as
|
|
5
|
+
the hosted service, or fine-tune an adapter on your own labelled examples.
|
|
6
|
+
|
|
7
|
+
## Release status
|
|
8
|
+
|
|
9
|
+
This is the initial 0.1.0 package. The configured model repository is
|
|
10
|
+
`TrueState/theclassifier`. The checkpoints are staged privately; authenticate with
|
|
11
|
+
Hugging Face and obtain repository access before using named-version downloads.
|
|
12
|
+
A local checkpoint also works.
|
|
13
|
+
Model weights are not included in the Python package. This code is Apache-2.0;
|
|
14
|
+
checkpoint and dataset licences are separate.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
From this checkout, until the PyPI release is published:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
pip install './python[serve]'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
After release:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
pip install 'theclassifier[serve]'
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Python 3.10+ is required. Install a PyTorch build suitable for your platform first
|
|
31
|
+
if needed; CUDA support depends on that build and your NVIDIA driver. CPU uses
|
|
32
|
+
float32; CUDA uses bfloat16 when supported, otherwise float32. No FlashAttention
|
|
33
|
+
extension is required. `auto` selects CUDA when available and CPU otherwise.
|
|
34
|
+
Apple MPS is not currently supported.
|
|
35
|
+
|
|
36
|
+
## Classify locally
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from theclassifier import Classifier
|
|
40
|
+
|
|
41
|
+
classifier = Classifier() # general; downloads once, then uses the local cache
|
|
42
|
+
result = classifier.classify(
|
|
43
|
+
'Please close my account.',
|
|
44
|
+
choices=['billing', 'cancellation', 'none'],
|
|
45
|
+
question='What is the customer asking for?',
|
|
46
|
+
)
|
|
47
|
+
print(result['results'][0]['selected'])
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Use `Classifier(checkpoint='./checkpoints/general', device='cpu')` to load an
|
|
51
|
+
existing local checkpoint. A checkpoint contains `adapter/`, `tokenizer/` and
|
|
52
|
+
`head.safetensors` (or the existing `head.pt` format). The pretrained backbone is
|
|
53
|
+
resolved from the adapter configuration. A local adapter can still require a
|
|
54
|
+
backbone download if the backbone is not already cached.
|
|
55
|
+
|
|
56
|
+
The full question format supports multiple decisions and optional criteria:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
result = classifier.classify(
|
|
60
|
+
'Please close my account.',
|
|
61
|
+
questions=[{
|
|
62
|
+
'question': 'What is the customer asking for?',
|
|
63
|
+
'choices': [
|
|
64
|
+
{'choice': 'cancellation', 'criteria': 'Requests to close an account.'},
|
|
65
|
+
{'choice': 'none', 'criteria': 'None of the other choices apply.'},
|
|
66
|
+
],
|
|
67
|
+
}],
|
|
68
|
+
version='intent',
|
|
69
|
+
)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Versions: `general`, `intent`, `sentiment`, `triage`, `cause`, `codify`,
|
|
73
|
+
`complaints`, `stars`. Selecting a new version checks the Hugging Face cache,
|
|
74
|
+
downloads missing files, then loads the adapter and its scoring head onto the
|
|
75
|
+
shared backbone. Selection and inference are serialized to keep the adapter and
|
|
76
|
+
head paired. A failed download raises an error; it never silently falls back to
|
|
77
|
+
another model.
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
classifier.load_adapter('custom', checkpoint='./checkpoints/my-adapter')
|
|
81
|
+
result = classifier.classify('example', choices=['yes', 'no'], version='custom')
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Use `model_repo=`, `revision=` and `cache_dir=` or the
|
|
85
|
+
`THECLASSIFIER_MODEL_REPO` environment variable to override download settings.
|
|
86
|
+
Pin `revision` to a commit for reproducibility. `offline=True` disables network
|
|
87
|
+
loading, including the base model. Pre-download adapters with:
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
theclassifier download --version general
|
|
91
|
+
theclassifier download --version intent
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
These commands download adapters and tokenizers; initialize the classifier once
|
|
95
|
+
online to cache the backbone as well. The repository layout is
|
|
96
|
+
`<version>/adapter/`, `<version>/tokenizer/`, `<version>/head.safetensors`.
|
|
97
|
+
|
|
98
|
+
Default maximum input length is 2,048 tokens per candidate; longer inputs retain
|
|
99
|
+
the first token and tail, matching the existing prompt format. Set `max_length`
|
|
100
|
+
explicitly for longer records. `batch_size` controls inference candidate chunks.
|
|
101
|
+
This initial portable runtime uses direct candidate scoring, not shared-prefix
|
|
102
|
+
KV caching. It has not been benchmarked against the optimized production server.
|
|
103
|
+
|
|
104
|
+
## Serve on CPU or GPU
|
|
105
|
+
|
|
106
|
+
```sh
|
|
107
|
+
theclassifier serve --checkpoint ./checkpoints/general --device cpu
|
|
108
|
+
# On a CUDA machine:
|
|
109
|
+
API_KEY=your-serving-key theclassifier serve --device cuda --host 0.0.0.0
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Default binding is `127.0.0.1:8090`. Binding outside localhost requires `API_KEY`.
|
|
113
|
+
Use one process per GPU; run behind a TLS proxy for remote clients. This standalone
|
|
114
|
+
server provides inference and authentication, not the hosted service's billing,
|
|
115
|
+
quotas or rate limiting. Set resource limits in your deployment.
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
curl http://127.0.0.1:8090/v1/classify \
|
|
119
|
+
-H 'Content-Type: application/json' \
|
|
120
|
+
-d '{"state":"Close my account","questions":[{"question":"Intent?","choices":[{"choice":"cancel"},{"choice":"none"}]}]}'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Include `Authorization: Bearer YOUR_KEY` when `API_KEY` is configured.
|
|
124
|
+
Responses contain `results` (with `question`, `selected`, `detailed_results`),
|
|
125
|
+
`version`, `input_tokens` and `latency_ms`.
|
|
126
|
+
|
|
127
|
+
## Fine-tune with LoRA
|
|
128
|
+
|
|
129
|
+
Each JSONL line is one complete choice menu, with an exact matching answer label:
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
{"state":"Close my account","question":"Intent?","choices":[{"choice":"cancel","criteria":"Requests to close an account"},{"choice":"none"}],"answer":"cancel"}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```sh
|
|
136
|
+
# Continue the general checkpoint to create a specialist:
|
|
137
|
+
theclassifier finetune --version general --data train.jsonl \
|
|
138
|
+
--output checkpoints/my-adapter --device cuda
|
|
139
|
+
|
|
140
|
+
# Train an adapter and scoring head from the base model:
|
|
141
|
+
theclassifier finetune --base-model Qwen/Qwen3-1.7B-Base \
|
|
142
|
+
--data train.jsonl --output checkpoints/from-base --rank 16 --alpha 16
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Or in Python:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from theclassifier import finetune
|
|
149
|
+
|
|
150
|
+
report = finetune(
|
|
151
|
+
'train.jsonl', 'checkpoints/my-adapter',
|
|
152
|
+
checkpoint='checkpoints/general', device='auto', epochs=1,
|
|
153
|
+
learning_rate=2e-4, grad_accum=16,
|
|
154
|
+
)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Training freezes the backbone, learns LoRA updates to `q_proj` and `v_proj`, and
|
|
158
|
+
trains a float32 scalar head using cross-entropy across each menu. Whole menus
|
|
159
|
+
are processed together, gradients accumulate between menus, and gradient
|
|
160
|
+
checkpointing reduces activation memory. Large choice menus may still require
|
|
161
|
+
smaller input lengths or more memory. CPU training is supported but slow.
|
|
162
|
+
|
|
163
|
+
New adapters default to rank 16, alpha 16 and dropout 0.05. Continuing an existing
|
|
164
|
+
adapter preserves its LoRA configuration. A new AdamW optimizer is created with
|
|
165
|
+
a constant learning rate; this is fine-tuning, not exact training-state resume.
|
|
166
|
+
The optimizer configuration differs from the original research runs. Output
|
|
167
|
+
folders must be empty. Saved files include the adapter, head, tokenizer,
|
|
168
|
+
`classifier.json` and `training.json`. Keep evaluation data separate and evaluate
|
|
169
|
+
before using a new checkpoint; the training loss is not a quality benchmark.
|
|
170
|
+
|
|
171
|
+
## Development and release
|
|
172
|
+
|
|
173
|
+
```sh
|
|
174
|
+
cd python
|
|
175
|
+
pip install -e '.[serve,dev]'
|
|
176
|
+
pytest
|
|
177
|
+
python -m build
|
|
178
|
+
python -m twine check dist/*
|
|
179
|
+
# With PyPI credentials configured locally:
|
|
180
|
+
python -m twine upload dist/*
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The tests build a tiny random Qwen model locally: no production weights, private
|
|
184
|
+
data or external inference service is needed. All six tests passed on the NVIDIA
|
|
185
|
+
workstation, including CUDA inference. Real v1.1 checkpoints were also smoke-tested
|
|
186
|
+
on CUDA, and the general model was checked on CPU against the original scoring
|
|
187
|
+
implementation. These checks establish execution compatibility, not accuracy.
|
|
188
|
+
Publishing the package does not upload model weights. See `RELEASE.md` for the
|
|
189
|
+
checkpoint and publication steps.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Release checklist
|
|
2
|
+
|
|
3
|
+
## Package
|
|
4
|
+
|
|
5
|
+
The initial package is `theclassifier==0.1.0`, under Apache-2.0. Build and inspect
|
|
6
|
+
only this directory, not the parent application repository:
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
python -m pytest
|
|
10
|
+
python -m build
|
|
11
|
+
python -m twine check dist/*
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Inspect the wheel and sdist file lists. They should contain package source,
|
|
15
|
+
documentation, licence and tests, with no credentials, training data or model
|
|
16
|
+
weights. Confirm ownership of the PyPI name at upload time: an earlier 404 is not
|
|
17
|
+
a reservation. Authenticate locally with a PyPI API token or configure a trusted
|
|
18
|
+
publisher; do not put credentials in source files or chat.
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
python -m twine upload dist/*
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
No PyPI upload has been performed by the build process. Hugging Face authentication
|
|
25
|
+
has been verified, and the release checkpoints are staged privately in
|
|
26
|
+
TrueState/theclassifier pending public weight-licence confirmation.
|
|
27
|
+
|
|
28
|
+
## Checkpoints
|
|
29
|
+
|
|
30
|
+
The default repository is `TrueState/theclassifier` with one directory per version:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
general/adapter/adapter_config.json
|
|
34
|
+
general/adapter/adapter_model.safetensors
|
|
35
|
+
general/tokenizer/...
|
|
36
|
+
general/head.safetensors
|
|
37
|
+
intent/adapter/...
|
|
38
|
+
intent/tokenizer/...
|
|
39
|
+
intent/head.safetensors
|
|
40
|
+
# likewise sentiment, triage, cause, codify, complaints, stars
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Existing `head.pt` checkpoints work; prefer safetensors for new public releases.
|
|
44
|
+
Only upload the intended release adapters, heads and tokenizers. Do not upload
|
|
45
|
+
logs, datasets, local cache folders or credentials. Verify that every adapter's
|
|
46
|
+
`base_model_name_or_path` points to `Qwen/Qwen3-1.7B-Base` (or the actual public
|
|
47
|
+
backbone used), not a private workstation path. Include model cards describing
|
|
48
|
+
training, evaluation and limitations. The user selected Apache-2.0 for package
|
|
49
|
+
code; verify and choose weight licensing separately before publishing weights.
|
|
50
|
+
|
|
51
|
+
After authentication via `hf auth login`, create the model repository and upload
|
|
52
|
+
an explicitly staged release folder using `hf upload`. Add the model card, then
|
|
53
|
+
verify all versions from a fresh cache and rerun the CUDA tests. Pin a tested Hub
|
|
54
|
+
commit with `revision=` for reproducible deployments. The package's default
|
|
55
|
+
`main` uses the existing local cached revision when available, without checking
|
|
56
|
+
for upstream changes on each classification.
|
|
57
|
+
|
|
58
|
+
## Validation status
|
|
59
|
+
|
|
60
|
+
The automated tests use a tiny local random Qwen3 model to exercise real CPU
|
|
61
|
+
inference, LoRA training, checkpoint reload and HTTP serving. They are functional
|
|
62
|
+
tests, not evidence of production classification quality or latency. All six tests passed on the CUDA workstation. The actual general model also
|
|
63
|
+
passed CPU parity against the original scorer, all seven specialists loaded
|
|
64
|
+
and ran on CUDA, and one real-checkpoint LoRA update saved and reloaded on CUDA.
|
|
65
|
+
All eight versions were downloaded from the private Hugging Face repository into
|
|
66
|
+
a fresh cache, matched against the staged files using SHA-256, and resolved
|
|
67
|
+
successfully offline. Verified Hub revision:
|
|
68
|
+
`b5c3072a4b83d14d7dbaf6a358a2ece01b173596`.
|
|
69
|
+
These are functional smoke tests, not a new quality or throughput benchmark.
|