sharada 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.
- sharada-0.1.0/.github/workflows/ci.yml +27 -0
- sharada-0.1.0/.github/workflows/release.yml +69 -0
- sharada-0.1.0/.gitignore +23 -0
- sharada-0.1.0/LICENSE +201 -0
- sharada-0.1.0/PKG-INFO +234 -0
- sharada-0.1.0/README.md +210 -0
- sharada-0.1.0/examples/finetune_your_own.py +90 -0
- sharada-0.1.0/examples/quickstart.py +49 -0
- sharada-0.1.0/examples/tickets.csv +25 -0
- sharada-0.1.0/pyproject.toml +51 -0
- sharada-0.1.0/sharada/__init__.py +31 -0
- sharada-0.1.0/sharada/calibrate.py +115 -0
- sharada-0.1.0/sharada/data.py +105 -0
- sharada-0.1.0/sharada/evaluate.py +105 -0
- sharada-0.1.0/sharada/layout.py +78 -0
- sharada-0.1.0/sharada/masking.py +48 -0
- sharada-0.1.0/sharada/model.py +167 -0
- sharada-0.1.0/sharada/policy.py +100 -0
- sharada-0.1.0/sharada/train.py +125 -0
- sharada-0.1.0/tests/test_calibration.py +74 -0
- sharada-0.1.0/tests/test_model.py +76 -0
- sharada-0.1.0/tests/test_policy.py +38 -0
- sharada-0.1.0/training/README.md +82 -0
- sharada-0.1.0/training/kaggle.ipynb +303 -0
- sharada-0.1.0/training/publish.py +242 -0
- sharada-0.1.0/training/run.py +313 -0
- sharada-0.1.0/training/sources.py +462 -0
- sharada-0.1.0/uv.lock +2901 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
workflow_call:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
fail-fast: false
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v7
|
|
18
|
+
- uses: astral-sh/setup-uv@v7
|
|
19
|
+
with:
|
|
20
|
+
python-version: ${{ matrix.python-version }}
|
|
21
|
+
# The architecture tests run a real encoder, so its weights are worth keeping between runs.
|
|
22
|
+
- uses: actions/cache@v4
|
|
23
|
+
with:
|
|
24
|
+
path: ~/.cache/huggingface
|
|
25
|
+
key: hf-${{ hashFiles('pyproject.toml') }}
|
|
26
|
+
- run: uv sync
|
|
27
|
+
- run: uv run pytest -q
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Publishing, with no tokens (PyPI "trusted publishing"):
|
|
2
|
+
# - Run this workflow by hand (Actions -> Release -> Run workflow) to publish to TestPyPI.
|
|
3
|
+
# - Publish a GitHub release with tag vX.Y.Z to publish to PyPI.
|
|
4
|
+
# One-time setup on pypi.org and test.pypi.org: add a trusted publisher with
|
|
5
|
+
# owner LenaBarretta, repository sharada, workflow release.yml and environment
|
|
6
|
+
# `pypi` (on pypi.org) or `testpypi` (on test.pypi.org).
|
|
7
|
+
name: Release
|
|
8
|
+
|
|
9
|
+
on:
|
|
10
|
+
release:
|
|
11
|
+
types: [published]
|
|
12
|
+
workflow_dispatch:
|
|
13
|
+
|
|
14
|
+
jobs:
|
|
15
|
+
test:
|
|
16
|
+
uses: ./.github/workflows/ci.yml
|
|
17
|
+
|
|
18
|
+
build:
|
|
19
|
+
needs: test
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
steps:
|
|
22
|
+
- uses: actions/checkout@v7
|
|
23
|
+
- uses: astral-sh/setup-uv@v7
|
|
24
|
+
- run: uv build
|
|
25
|
+
- name: Check that the release tag matches the package version
|
|
26
|
+
if: github.event_name == 'release'
|
|
27
|
+
run: |
|
|
28
|
+
version="${GITHUB_REF_NAME#v}"
|
|
29
|
+
test -f "dist/sharada-${version}-py3-none-any.whl" || {
|
|
30
|
+
echo "Tag $GITHUB_REF_NAME does not match the version in sharada/__init__.py"
|
|
31
|
+
ls dist
|
|
32
|
+
exit 1
|
|
33
|
+
}
|
|
34
|
+
- uses: actions/upload-artifact@v7
|
|
35
|
+
with:
|
|
36
|
+
name: dist
|
|
37
|
+
path: dist/
|
|
38
|
+
|
|
39
|
+
testpypi:
|
|
40
|
+
if: github.event_name == 'workflow_dispatch'
|
|
41
|
+
needs: build
|
|
42
|
+
runs-on: ubuntu-latest
|
|
43
|
+
environment: testpypi
|
|
44
|
+
permissions:
|
|
45
|
+
id-token: write
|
|
46
|
+
steps:
|
|
47
|
+
- uses: actions/download-artifact@v8
|
|
48
|
+
with:
|
|
49
|
+
name: dist
|
|
50
|
+
path: dist/
|
|
51
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
52
|
+
with:
|
|
53
|
+
repository-url: https://test.pypi.org/legacy/
|
|
54
|
+
# A version can be uploaded only once; reruns just re-check the setup.
|
|
55
|
+
skip-existing: true
|
|
56
|
+
|
|
57
|
+
pypi:
|
|
58
|
+
if: github.event_name == 'release'
|
|
59
|
+
needs: build
|
|
60
|
+
runs-on: ubuntu-latest
|
|
61
|
+
environment: pypi
|
|
62
|
+
permissions:
|
|
63
|
+
id-token: write
|
|
64
|
+
steps:
|
|
65
|
+
- uses: actions/download-artifact@v8
|
|
66
|
+
with:
|
|
67
|
+
name: dist
|
|
68
|
+
path: dist/
|
|
69
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
sharada-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
*.egg-info/
|
|
4
|
+
build/
|
|
5
|
+
dist/
|
|
6
|
+
.venv/
|
|
7
|
+
.pytest_cache/
|
|
8
|
+
.ipynb_checkpoints/
|
|
9
|
+
|
|
10
|
+
# weights, checkpoints, runs
|
|
11
|
+
*.pt
|
|
12
|
+
*.bin
|
|
13
|
+
*.safetensors
|
|
14
|
+
checkpoints/
|
|
15
|
+
runs/
|
|
16
|
+
out/
|
|
17
|
+
wandb/
|
|
18
|
+
|
|
19
|
+
# datasets cache
|
|
20
|
+
data/
|
|
21
|
+
.cache/
|
|
22
|
+
|
|
23
|
+
.DS_Store
|
sharada-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
sharada-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sharada
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Typed decisions from text in one forward pass — options in the request, calibrated probabilities, easy to fine-tune.
|
|
5
|
+
Project-URL: Homepage, https://github.com/LenaBarretta/sharada
|
|
6
|
+
Project-URL: Article, https://lenatriestounderstand.com/notes/llm/024-rlcr/
|
|
7
|
+
Author: Lena Barretta
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: calibration,decision-making,text-classification,uncertainty,zero-shot-classification
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Requires-Dist: huggingface-hub>=0.26
|
|
18
|
+
Requires-Dist: safetensors>=0.4
|
|
19
|
+
Requires-Dist: torch>=2.2
|
|
20
|
+
Requires-Dist: transformers==5.12.1
|
|
21
|
+
Provides-Extra: train
|
|
22
|
+
Requires-Dist: datasets>=4; extra == 'train'
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
<p align="center">
|
|
26
|
+
<img src="https://raw.githubusercontent.com/LenaBarretta/sharada/main/docs/logo.png" alt="Sharada" width="200">
|
|
27
|
+
</p>
|
|
28
|
+
|
|
29
|
+
<h1 align="center">Sharada</h1>
|
|
30
|
+
|
|
31
|
+
<p align="center">
|
|
32
|
+
<em>Typed decisions about text in one forward pass — options in the request, calibrated probabilities out.</em>
|
|
33
|
+
</p>
|
|
34
|
+
|
|
35
|
+
<p align="center">
|
|
36
|
+
<a href="https://github.com/LenaBarretta/sharada/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-Apache%202.0-blue.svg" alt="Apache 2.0"></a>
|
|
37
|
+
<a href="https://huggingface.co/lenabarretta/sharada-base"><img src="https://img.shields.io/badge/%F0%9F%A4%97-sharada--base-yellow" alt="sharada-base on the Hub"></a>
|
|
38
|
+
<a href="https://lenatriestounderstand.com/notes/llm/024-rlcr/"><img src="https://img.shields.io/badge/write--up-lenatriestounderstand-ff69b4" alt="the write-up"></a>
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
A small encoder that makes **typed decisions about text in one forward pass**: the options come with the
|
|
42
|
+
request, and what comes back is a probability for each of them. Nothing is generated, nothing is parsed,
|
|
43
|
+
and the same model answers a question it has never seen because the labels are part of the input rather
|
|
44
|
+
than part of the weights.
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from sharada import DecisionModel
|
|
48
|
+
|
|
49
|
+
model = DecisionModel.from_pretrained("lenabarretta/sharada-base")
|
|
50
|
+
|
|
51
|
+
d = model.decide(
|
|
52
|
+
text="My card still hasn't arrived and I ordered it two weeks ago.",
|
|
53
|
+
question="Which team should handle this?",
|
|
54
|
+
options=["billing", "card delivery", "technical support", "account closure"],
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
d.answer # 'card delivery'
|
|
58
|
+
d.confidence # 0.86
|
|
59
|
+
d.probabilities # {'billing': 0.07, 'card delivery': 0.86, ...}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Three kinds of question are typed, so the model knows what the options mean:
|
|
63
|
+
|
|
64
|
+
| kind | the options are | example |
|
|
65
|
+
| --- | --- | --- |
|
|
66
|
+
| `choice` | unordered labels | which team, which topic, which intent |
|
|
67
|
+
| `scale` | ordered steps | severity 1–5, how positive, how urgent |
|
|
68
|
+
| `binary` | yes or no | is this spam, does this need a human |
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from sharada import Request
|
|
72
|
+
|
|
73
|
+
model.decide_many([
|
|
74
|
+
Request(text=review, question="How positive is this review?",
|
|
75
|
+
options=["very negative", "negative", "neutral", "positive", "very positive"],
|
|
76
|
+
kind="scale", task="review-tone"),
|
|
77
|
+
Request(text=review, question="Does this mention a refund?",
|
|
78
|
+
options=["no", "yes"], kind="binary", task="refund-flag"),
|
|
79
|
+
])
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Install
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
pip install git+https://github.com/LenaBarretta/sharada
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Python 3.10+, `torch` and `transformers`; CPU is enough to run it.
|
|
89
|
+
|
|
90
|
+
## Models
|
|
91
|
+
|
|
92
|
+
| model | parameters | accuracy | ECE | one decision | |
|
|
93
|
+
| --- | --- | --- | --- | --- | --- |
|
|
94
|
+
| `lenabarretta/sharada-base` | 150M | 0.813 | 0.008 | 19.8 ms | the default; fine-tune this one |
|
|
95
|
+
| `lenabarretta/sharada-large` | 397M | 0.834 | 0.009 | 26.6 ms | better, and better still on label sets it has never seen |
|
|
96
|
+
| `lenabarretta/sharada-multilingual` *(in training)* | 300M | | | | the same architecture over 1800+ languages, on mmBERT |
|
|
97
|
+
|
|
98
|
+
Accuracy is over 38 label sets with every label offered at once — banking on all 77 intents, clinc on
|
|
99
|
+
all 151 — and **ECE** is how far the stated probability lands from how often it turns out right. The
|
|
100
|
+
gap between the two models is widest where it matters most: on label sets neither was trained on,
|
|
101
|
+
`large` reads arXiv categories at 0.456 against `base`'s 0.317.
|
|
102
|
+
|
|
103
|
+
A checkpoint carries its own encoder, limits and temperatures in `config.json`, so a bigger model — or a
|
|
104
|
+
multilingual one, built on a multilingual encoder — is another repository rather than another version of
|
|
105
|
+
the library. It also means a multilingual model cannot be a flag on an English one: this encoder is
|
|
106
|
+
English down to its tokenizer, and another language means other weights.
|
|
107
|
+
|
|
108
|
+
Accuracy and calibration per label set are in each model card, measured on label sets the model was
|
|
109
|
+
**not** trained on as well as on the ones it was.
|
|
110
|
+
|
|
111
|
+
## Three things the architecture guarantees
|
|
112
|
+
|
|
113
|
+
The request is laid out as one sequence — text, question, then every option as a parallel branch — and a
|
|
114
|
+
mask decides who may read whom. Both are in [`layout.py`](https://github.com/LenaBarretta/sharada/blob/main/sharada/layout.py) and
|
|
115
|
+
[`masking.py`](https://github.com/LenaBarretta/sharada/blob/main/sharada/masking.py), and they buy three properties that hold by construction, not because
|
|
116
|
+
training got them approximately right:
|
|
117
|
+
|
|
118
|
+
1. **The order of the options cannot matter.** Every option branch starts at the same position id, and
|
|
119
|
+
the encoder's positions are rotary, so no option is earlier or later than another. Reshuffle them and
|
|
120
|
+
the probabilities follow their options exactly.
|
|
121
|
+
2. **An option's score does not depend on which other options are offered.** An option reads the text,
|
|
122
|
+
the question and itself, nothing else. Drop two options from a list of four and the remaining two keep
|
|
123
|
+
their scores to the last bit — so the probabilities are a renormalisation, and a long list of options
|
|
124
|
+
does not make each one noisier.
|
|
125
|
+
3. **The text is read once.** Text tokens read only text tokens, so their states do not depend on the
|
|
126
|
+
question. Ten questions about one document are ten cheap read-outs over one encoding of it.
|
|
127
|
+
|
|
128
|
+
These are the tests in [`tests/test_model.py`](https://github.com/LenaBarretta/sharada/blob/main/tests/test_model.py), checked on an untrained model.
|
|
129
|
+
|
|
130
|
+
## Fine-tune it on your own labels
|
|
131
|
+
|
|
132
|
+
This is what the package is built around: a few hundred labelled examples and a few minutes.
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
from sharada import DecisionModel, Example
|
|
136
|
+
|
|
137
|
+
examples = [
|
|
138
|
+
Example(text="the invoice is wrong again", question="Which team should handle this?",
|
|
139
|
+
options=["billing", "technical", "sales"], label=0, task="routing"),
|
|
140
|
+
...
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
model = DecisionModel.from_pretrained("lenabarretta/sharada-base")
|
|
144
|
+
report = model.fit(examples) # holds out 20%, stops when held-out log loss stops improving
|
|
145
|
+
model.save("my-router")
|
|
146
|
+
|
|
147
|
+
report # Report(820 examples, accuracy 0.914, log loss 0.287, calibration error 0.031)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
`fit` keeps a part of the examples out, trains on the rest, early-stops, then fits one temperature per
|
|
151
|
+
task on the held-out part and writes a calibration passport. Useful arguments:
|
|
152
|
+
|
|
153
|
+
| argument | default | |
|
|
154
|
+
| --- | --- | --- |
|
|
155
|
+
| `loss` | `"cross_entropy"` | `"brier"` scores the whole distribution, not just the right option |
|
|
156
|
+
| `freeze_encoder` | `False` | train the read-out only: seconds, and enough for a few hundred examples |
|
|
157
|
+
| `batch_size`, `lr`, `max_epochs`, `patience` | `16`, `2e-5`, `10`, `2` | |
|
|
158
|
+
|
|
159
|
+
Mixing several tasks in one `fit` is the normal case — give each one its own `task` name and each gets
|
|
160
|
+
its own temperature. See [`examples/finetune_your_own.py`](https://github.com/LenaBarretta/sharada/blob/main/examples/finetune_your_own.py), which trains
|
|
161
|
+
on a CSV.
|
|
162
|
+
|
|
163
|
+
## The number next to the answer is supposed to be true
|
|
164
|
+
|
|
165
|
+
`0.86` should mean right about 86% of the time. That is a property of a distribution, not of a model, so
|
|
166
|
+
it is fitted and it expires:
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
from sharada import check_passport, save_passport
|
|
170
|
+
|
|
171
|
+
passport = model.calibrate(recent_examples) # one temperature per task
|
|
172
|
+
save_passport(passport, "passport.json")
|
|
173
|
+
|
|
174
|
+
check_passport(passport, recent_examples, model)
|
|
175
|
+
# ['the calibration expired on 2026-04-01; fit it again on recent answers',
|
|
176
|
+
# 'routing: the options have changed since the calibration']
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`check_passport` returns an empty list when it finds nothing wrong — run it in the deployment pipeline
|
|
180
|
+
and fail the build on anything it returns. `evaluate` gives accuracy, log loss, Brier, expected
|
|
181
|
+
calibration error with a bootstrap interval, a reliability curve and a risk–coverage curve.
|
|
182
|
+
|
|
183
|
+
## What to do with 0.86
|
|
184
|
+
|
|
185
|
+
A probability is not a decision. `Policy` turns one into the action that costs the least, including
|
|
186
|
+
handing the request to a person:
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
from sharada import Policy, escalation_budget
|
|
190
|
+
|
|
191
|
+
policy = Policy(options=["approve", "reject"],
|
|
192
|
+
costs={("fraud", "approve"): 10_000, ("clean", "reject"): 100},
|
|
193
|
+
escalate=30)
|
|
194
|
+
|
|
195
|
+
policy.act({"fraud": 0.1, "clean": 0.9}) # Action('answer', 'reject', expected_loss=90.0)
|
|
196
|
+
policy.act({"fraud": 0.5, "clean": 0.5}) # Action('escalate', ...)
|
|
197
|
+
|
|
198
|
+
# a person can look at 5% of the traffic, no more:
|
|
199
|
+
policy = escalation_budget(policy, probabilities, budget=0.05)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Train the base models yourself
|
|
203
|
+
|
|
204
|
+
[`training/`](https://github.com/LenaBarretta/sharada/tree/main/training) has the whole run: a mix of public label sets in
|
|
205
|
+
[`sources.py`](https://github.com/LenaBarretta/sharada/blob/main/training/sources.py) — intents, topics, sentiment, toxicity, spam, entailment, review
|
|
206
|
+
scores — each one presented with several question phrasings, shuffled options and sampled option
|
|
207
|
+
subsets, so the model learns to read the options rather than their positions. Some label sets are held
|
|
208
|
+
out of training entirely and only measured, which is where the zero-shot numbers come from.
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
python training/run.py --encoder answerdotai/ModernBERT-base --out runs/base
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
It checkpoints every few hundred steps and resumes from the checkpoint if it finds one, which is what
|
|
215
|
+
makes it survive a Kaggle session; [`training/kaggle.ipynb`](https://github.com/LenaBarretta/sharada/blob/main/training/kaggle.ipynb) is the notebook
|
|
216
|
+
wrapper. One free T4: about an hour for base, about three for large.
|
|
217
|
+
|
|
218
|
+
## What it will not do
|
|
219
|
+
|
|
220
|
+
- **It does not generate.** No free-form answers, no extraction, no reasoning out loud. Options or
|
|
221
|
+
nothing.
|
|
222
|
+
- **256 tokens of text** by default (`Limits`), with 48 for the question and 12 per option. Longer
|
|
223
|
+
documents need chunking or a larger limit, and the limit costs quadratic attention.
|
|
224
|
+
- **English.** The encoder is English-only; a multilingual encoder drops in, but the published
|
|
225
|
+
checkpoints are not trained for it.
|
|
226
|
+
- **It is small.** Where a frontier model knows a fact that is not in the text, it wins. This answers
|
|
227
|
+
questions about the text in front of it.
|
|
228
|
+
|
|
229
|
+
## Where it came from
|
|
230
|
+
|
|
231
|
+
The design, the experiments behind it and what each training signal did are written up in
|
|
232
|
+
[RLCR from Scratch](https://lenatriestounderstand.com/notes/llm/024-rlcr/), with a runnable lab.
|
|
233
|
+
|
|
234
|
+
Apache 2.0.
|