fliprate 0.2.1__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.
- fliprate-0.2.1/LICENSE +21 -0
- fliprate-0.2.1/PKG-INFO +331 -0
- fliprate-0.2.1/README.md +286 -0
- fliprate-0.2.1/pyproject.toml +48 -0
- fliprate-0.2.1/setup.cfg +4 -0
- fliprate-0.2.1/src/fliprate/__init__.py +132 -0
- fliprate-0.2.1/src/fliprate/adapters.py +143 -0
- fliprate-0.2.1/src/fliprate/aksharantar.py +297 -0
- fliprate-0.2.1/src/fliprate/api.py +135 -0
- fliprate-0.2.1/src/fliprate/baselines.py +28 -0
- fliprate-0.2.1/src/fliprate/cli.py +137 -0
- fliprate-0.2.1/src/fliprate/dakshina.py +341 -0
- fliprate-0.2.1/src/fliprate/data/sample_support.csv +41 -0
- fliprate-0.2.1/src/fliprate/dataset.py +320 -0
- fliprate-0.2.1/src/fliprate/demo.py +51 -0
- fliprate-0.2.1/src/fliprate/evaluation.py +344 -0
- fliprate-0.2.1/src/fliprate/events.py +28 -0
- fliprate-0.2.1/src/fliprate/experiment.py +73 -0
- fliprate-0.2.1/src/fliprate/gating.py +148 -0
- fliprate-0.2.1/src/fliprate/hf_models.py +13 -0
- fliprate-0.2.1/src/fliprate/models.py +82 -0
- fliprate-0.2.1/src/fliprate/normalization.py +94 -0
- fliprate-0.2.1/src/fliprate/perturbation.py +101 -0
- fliprate-0.2.1/src/fliprate/py.typed +0 -0
- fliprate-0.2.1/src/fliprate/rules.py +1159 -0
- fliprate-0.2.1/src/fliprate/service.py +154 -0
- fliprate-0.2.1/src/fliprate/web/index.html +564 -0
- fliprate-0.2.1/src/fliprate.egg-info/PKG-INFO +331 -0
- fliprate-0.2.1/src/fliprate.egg-info/SOURCES.txt +38 -0
- fliprate-0.2.1/src/fliprate.egg-info/dependency_links.txt +1 -0
- fliprate-0.2.1/src/fliprate.egg-info/entry_points.txt +2 -0
- fliprate-0.2.1/src/fliprate.egg-info/requires.txt +25 -0
- fliprate-0.2.1/src/fliprate.egg-info/top_level.txt +1 -0
- fliprate-0.2.1/tests/test_api.py +113 -0
- fliprate-0.2.1/tests/test_dataset_rules.py +147 -0
- fliprate-0.2.1/tests/test_evaluation.py +77 -0
- fliprate-0.2.1/tests/test_gate.py +54 -0
- fliprate-0.2.1/tests/test_models_adapters.py +155 -0
- fliprate-0.2.1/tests/test_perturb_rules.py +74 -0
- fliprate-0.2.1/tests/test_service_gate_experiment.py +169 -0
fliprate-0.2.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shiv Supane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
fliprate-0.2.1/PKG-INFO
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fliprate
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Measure and reduce how often an NLP model changes its answer on messy romanized Hindi/Marathi and other Indian-language text.
|
|
5
|
+
Author-email: Shiv Supane <shiv.supane777@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/ShivSupane/fliprate
|
|
8
|
+
Project-URL: Issues, https://github.com/ShivSupane/fliprate/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/ShivSupane/fliprate/blob/main/CHANGELOG.md
|
|
10
|
+
Keywords: nlp,robustness,hinglish,marathi,indic,romanized,testing,transliteration,code-mixed
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: api
|
|
25
|
+
Requires-Dist: fastapi>=0.100; extra == "api"
|
|
26
|
+
Requires-Dist: uvicorn>=0.23; extra == "api"
|
|
27
|
+
Provides-Extra: sklearn
|
|
28
|
+
Requires-Dist: scikit-learn>=1.3; extra == "sklearn"
|
|
29
|
+
Provides-Extra: hf
|
|
30
|
+
Requires-Dist: transformers>=4.36; extra == "hf"
|
|
31
|
+
Requires-Dist: torch>=2.1; extra == "hf"
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
34
|
+
Requires-Dist: scikit-learn>=1.3; extra == "dev"
|
|
35
|
+
Requires-Dist: build; extra == "dev"
|
|
36
|
+
Requires-Dist: twine; extra == "dev"
|
|
37
|
+
Requires-Dist: httpx; extra == "dev"
|
|
38
|
+
Provides-Extra: all
|
|
39
|
+
Requires-Dist: fastapi>=0.100; extra == "all"
|
|
40
|
+
Requires-Dist: uvicorn>=0.23; extra == "all"
|
|
41
|
+
Requires-Dist: scikit-learn>=1.3; extra == "all"
|
|
42
|
+
Requires-Dist: transformers>=4.36; extra == "all"
|
|
43
|
+
Requires-Dist: torch>=2.1; extra == "all"
|
|
44
|
+
Dynamic: license-file
|
|
45
|
+
|
|
46
|
+
# FlipRate
|
|
47
|
+
|
|
48
|
+
**Does your NLP model change its answer when the same message is typed differently?**
|
|
49
|
+
|
|
50
|
+
[](https://www.python.org/)
|
|
51
|
+
[](LICENSE)
|
|
52
|
+
[](https://github.com/ShivSupane/fliprate/actions/workflows/ci.yml)
|
|
53
|
+
|
|
54
|
+
Built by **Shiv Supane**
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## The problem
|
|
59
|
+
|
|
60
|
+
People who text in Hindi, Marathi, Bengali, Tamil, Telugu, and other Indian languages
|
|
61
|
+
using Roman (English) script have no standard spelling. The same word gets typed dozens of ways:
|
|
62
|
+
|
|
63
|
+
| Word | Romanized variants |
|
|
64
|
+
|---|---|
|
|
65
|
+
| नही (no) | `nahi` · `nhi` · `nahin` · `nahee` |
|
|
66
|
+
| बहुत (very) | `bahut` · `bohot` · `bhut` · `boht` |
|
|
67
|
+
| आवडला (liked) | `avadla` · `avdla` · `avadlaaa` |
|
|
68
|
+
|
|
69
|
+
Most NLP models treat these as completely different inputs and give different answers —
|
|
70
|
+
even though the meaning is identical. **FlipRate measures that problem, quantifies it
|
|
71
|
+
with confidence intervals, and gives you tools to fix it.**
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
User types: "mala product avadlaaa nahi" → model says: product_feedback ✓
|
|
75
|
+
Same meaning: "mala product avdla nhi" → model says: human_support ✗ ← FLIP
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Install
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pip install fliprate # core library (zero dependencies)
|
|
84
|
+
pip install "fliprate[api]" # + FastAPI web console
|
|
85
|
+
pip install "fliprate[api,hf]" # + Hugging Face model support
|
|
86
|
+
pip install "fliprate[api,hf,sklearn]" # + scikit-learn baselines
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
From source:
|
|
90
|
+
```bash
|
|
91
|
+
git clone https://github.com/ShivSupane/fliprate.git
|
|
92
|
+
cd fliprate
|
|
93
|
+
pip install -e ".[dev,api,hf,sklearn]"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Quick start
|
|
99
|
+
|
|
100
|
+
### Test your model in 3 lines
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
import fliprate as fr
|
|
104
|
+
|
|
105
|
+
report = fr.test(my_model, "data/test.csv") # my_model: any function text -> label
|
|
106
|
+
print(report.summary())
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Example output *(illustrative — your numbers will differ)*:
|
|
110
|
+
```
|
|
111
|
+
Messages: 500 | safe variants tested: 2500
|
|
112
|
+
Accuracy on originals: 68.4% (macro-F1 0.672)
|
|
113
|
+
Accuracy on safe variants: 67.1% (macro-F1 0.658)
|
|
114
|
+
Accuracy drop: -1.3% 95% CI [-2.1%, -0.5%]
|
|
115
|
+
Flip rate: 6.2% 95% CI [5.1%, 7.4%]
|
|
116
|
+
Most fragile change type: repetition (flip rate 9.8%)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Protect your model
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
safe_model = fr.protect(my_model) # normalizer in front
|
|
123
|
+
report2 = fr.test(safe_model, "data/test.csv")
|
|
124
|
+
|
|
125
|
+
diff = fr.paired_difference(report, report2)
|
|
126
|
+
print(f"Flip rate change: {diff['difference']:+.1%}")
|
|
127
|
+
print(f"95% CI: {diff['ci']}")
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Real-time gate
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
result = fr.gate(my_model, "mala product avadlaaa nahi")
|
|
134
|
+
print(result.decision) # PASS / TRANSFORM / REVIEW
|
|
135
|
+
print(result.prediction) # model's answer on the (possibly normalized) text
|
|
136
|
+
print(result.reason) # human-readable explanation
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## Web Console
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
uvicorn fliprate.api:app --reload # toy model, instant start
|
|
145
|
+
# then open http://localhost:8000
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Two tabs:
|
|
149
|
+
- **Playground** — paste a message, watch the gate decision stream live
|
|
150
|
+
- **Robustness Lab** — upload a labeled CSV, get a live flip-rate chart
|
|
151
|
+
|
|
152
|
+
Zero-dependency vanilla HTML/JS. No Streamlit, no npm, no build step.
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## CLI
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
fliprate gate "mala product avadlaaa nahi"
|
|
160
|
+
fliprate perturb "delivery nahi aaya"
|
|
161
|
+
fliprate normalize "mala product avadlaaa nhi"
|
|
162
|
+
fliprate test --data mydata.csv
|
|
163
|
+
fliprate test --data mydata.csv -n 10 --protect --save report.json
|
|
164
|
+
fliprate check-data testdata.csv --against traindata.csv
|
|
165
|
+
fliprate split mydata.csv --out-dir data/
|
|
166
|
+
fliprate mine mydata.csv --out candidates.csv
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Plug in any model
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
# Hugging Face (lazy-loaded, batched)
|
|
175
|
+
clf = fr.HFTextClassifier(
|
|
176
|
+
"cardiffnlp/twitter-xlm-roberta-base-sentiment",
|
|
177
|
+
lowercase_labels=True,
|
|
178
|
+
preprocess=fr.twitter_preprocess,
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
# scikit-learn
|
|
182
|
+
from fliprate.baselines import train_tfidf
|
|
183
|
+
clf = train_tfidf(train_rows, analyzer="word") # or "char_wb"
|
|
184
|
+
|
|
185
|
+
# Any LLM
|
|
186
|
+
llm = fr.with_retries(fr.llm_classifier(
|
|
187
|
+
my_openai_fn, labels=["positive", "neutral", "negative"]
|
|
188
|
+
))
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Multi-language rules
|
|
194
|
+
|
|
195
|
+
FlipRate ships with 73 core HIGH-confidence groups for immediate use.
|
|
196
|
+
For broader coverage, load from two research datasets:
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
# Dakshina (Google Research, CC BY-SA 4.0) — 225K groups, 12 languages
|
|
200
|
+
# Codes: hi ur mr bn ta te kn ml gu pa sd si
|
|
201
|
+
rules = fr.dakshina_rules("ta") # Tamil
|
|
202
|
+
rules = fr.dakshina_rules("hi", "mr") # Hindi + Marathi
|
|
203
|
+
|
|
204
|
+
# Aksharantar (AI4Bharat, CC BY 4.0) — 428K groups, 20 languages
|
|
205
|
+
# Codes: hin guj tam tel kan mal mar pan ben urd ori asm kas kok mai mni nep san brx sid
|
|
206
|
+
rules = fr.aksharantar_rules("hin") # Hindi: 58K groups
|
|
207
|
+
rules = fr.aksharantar_rules("tam", "tel", "kan") # South India
|
|
208
|
+
|
|
209
|
+
# Use in any call
|
|
210
|
+
report = fr.test(my_model, "data/test.csv", rules=rules)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## How it works
|
|
216
|
+
|
|
217
|
+
**Safe variant:** a rewriting of a message that keeps the same canonical words
|
|
218
|
+
(up to reviewed equivalent spellings) and the same negation count. Adding or
|
|
219
|
+
removing a negation word (`nahi`, `nako`, `illa`, …) is never allowed.
|
|
220
|
+
|
|
221
|
+
**Flip rate:** `variants_where_answer_changed / total_safe_variants`.
|
|
222
|
+
Bootstrap 95% CIs are computed over messages, not individual variants.
|
|
223
|
+
|
|
224
|
+
**Gate decisions:**
|
|
225
|
+
|
|
226
|
+
| Decision | When |
|
|
227
|
+
|---|---|
|
|
228
|
+
| **PASS** | ≥ 80% of safe variants get the same answer. |
|
|
229
|
+
| **TRANSFORM** | Unstable, but the built-in normalizer fixes it. Sends cleaned text to model; keeps original. |
|
|
230
|
+
| **REVIEW** | Unstable and normalization does not help. Route to a human. |
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Experiment results (v0.2)
|
|
235
|
+
|
|
236
|
+
Tested on a processed copy of SentiMix Hinglish sentiment (2,997 held-out messages,
|
|
237
|
+
5 rule-generated variants per message). **Read the caveats below before citing these numbers.**
|
|
238
|
+
|
|
239
|
+
| Model | Mode | Accuracy | Flip rate (95% CI) |
|
|
240
|
+
|---|---|---|---|
|
|
241
|
+
| xlm-roberta-base-sentiment | raw | 59.2% | 7.3% (6.7–7.9%) |
|
|
242
|
+
| xlm-roberta-base-sentiment | **protected** | 59.1% | **3.5%** (3.1–3.9%) |
|
|
243
|
+
| TF-IDF word n-gram | raw | 65.9% | 4.2% (3.8–4.6%) |
|
|
244
|
+
| TF-IDF word n-gram | **protected** | 65.6% | **3.2%** (2.9–3.6%) |
|
|
245
|
+
|
|
246
|
+
Paired flip-rate change: **−3.80 pts** [−4.22, −3.39]. Accuracy: −0.03 pts [−0.23, +0.17].
|
|
247
|
+
|
|
248
|
+
> **Important caveats (from [`evidence/EVIDENCE.md`](evidence/EVIDENCE.md)):**
|
|
249
|
+
> - The SentiMix copy used here is lowercased, stopword-stripped and Porter-stemmed — not natural user typing.
|
|
250
|
+
> - The spelling-swap flip rate drops to 0 % after protection **by construction**: the variants and the normalizer use the same rule list, so normalization simply undoes its own perturbations. This shows the normalizer is internally consistent, not that it fixes real-world spelling noise.
|
|
251
|
+
> - The spelling rules have not been reviewed by a fluent speaker.
|
|
252
|
+
> - Only one transformer was tested, zero-shot.
|
|
253
|
+
>
|
|
254
|
+
> These numbers are a proof-of-concept baseline. Treat them as lower bounds on what a fair evaluation on natural, unprocessed text would show.
|
|
255
|
+
|
|
256
|
+
Full evidence and raw result files: [`evidence/`](evidence/)
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## Production deployment
|
|
261
|
+
|
|
262
|
+
```bash
|
|
263
|
+
# With API key and restricted CORS
|
|
264
|
+
FLIPRATE_API_KEY=secret FLIPRATE_CORS=https://yoursite.com \
|
|
265
|
+
uvicorn fliprate.api:app --host 0.0.0.0 --port 8000 \
|
|
266
|
+
--proxy-headers --forwarded-allow-ips="*"
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
**Environment variables:**
|
|
270
|
+
|
|
271
|
+
| Variable | Default | Effect |
|
|
272
|
+
|---|---|---|
|
|
273
|
+
| `FLIPRATE_MODEL` | toy model | `module:function` path to your classifier |
|
|
274
|
+
| `FLIPRATE_API_KEY` | none (open) | Require key on POST endpoints |
|
|
275
|
+
| `FLIPRATE_CORS` | `*` | Allowed origins — always restrict in production |
|
|
276
|
+
| `FLIPRATE_RATE_LIMIT` | `60` | Requests per minute per IP/key |
|
|
277
|
+
| `FLIPRATE_MAX_CONCURRENT` | `2` | Max simultaneous jobs |
|
|
278
|
+
| `FLIPRATE_MAX_TEXT_CHARS` | `2000` | Max chars per analyze request |
|
|
279
|
+
| `FLIPRATE_MAX_ROWS` | `2000` | Max rows per test request |
|
|
280
|
+
|
|
281
|
+
Docker:
|
|
282
|
+
```bash
|
|
283
|
+
docker build -t fliprate .
|
|
284
|
+
docker run -p 7860:7860 \
|
|
285
|
+
-e FLIPRATE_MODEL=fliprate.hf_models:xlmt_sentiment \
|
|
286
|
+
-e FLIPRATE_API_KEY=secret \
|
|
287
|
+
fliprate
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
## Documentation
|
|
293
|
+
|
|
294
|
+
| File | Contents |
|
|
295
|
+
|---|---|
|
|
296
|
+
| [`docs/QUICKSTART.md`](docs/QUICKSTART.md) | Every command, env var, and code example |
|
|
297
|
+
| [`docs/API_REFERENCE.md`](docs/API_REFERENCE.md) | Full function reference with parameters |
|
|
298
|
+
| [`docs/PROJECT_OVERVIEW.md`](docs/PROJECT_OVERVIEW.md) | Architecture, design decisions, module map |
|
|
299
|
+
| [`docs/EXPERIMENT.md`](docs/EXPERIMENT.md) | Experiment protocol and caveats |
|
|
300
|
+
| [`docs/RELEASE_AND_DEPLOY.md`](docs/RELEASE_AND_DEPLOY.md) | PyPI and deployment guide |
|
|
301
|
+
| [`evidence/`](evidence/) | Raw result files and EVIDENCE.md |
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
## Limitations
|
|
306
|
+
|
|
307
|
+
- Romanized Latin-script text only. Devanagari and other native scripts go to REVIEW.
|
|
308
|
+
- Rule-generated variants only — real typing noise is wider than 4 change types.
|
|
309
|
+
- Spelling rules have not been reviewed by a fluent speaker for any language.
|
|
310
|
+
- Customer-support domain lexicon; use `--no-lexicon` for other domains.
|
|
311
|
+
- Dakshina data is from Wikipedia. Aksharantar draws on Wikipedia, IndicCorp, Samanantar, and other corpora — but not all pairs are manually annotated.
|
|
312
|
+
- Do not publish numbers from the bundled 40-message demo sample.
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
## Datasets
|
|
317
|
+
|
|
318
|
+
If you use the multi-language rules in published work, cite:
|
|
319
|
+
|
|
320
|
+
> Roark et al. (2020). "Processing South Asian Languages Written in the Latin Script: the Dakshina Dataset." LREC 2020. https://aclanthology.org/2020.lrec-1.294
|
|
321
|
+
|
|
322
|
+
> Mittal et al. (2022). "Aksharantar: Towards Building Open Transliteration Tools for the Next Billion Users." https://arxiv.org/abs/2205.03018
|
|
323
|
+
|
|
324
|
+
---
|
|
325
|
+
|
|
326
|
+
## License
|
|
327
|
+
|
|
328
|
+
MIT — see [LICENSE](LICENSE). © 2025 Shiv Supane
|
|
329
|
+
|
|
330
|
+
The Dakshina dataset is CC BY-SA 4.0. The Aksharantar dataset is CC BY 4.0.
|
|
331
|
+
Neither is included in this package — download separately.
|
fliprate-0.2.1/README.md
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# FlipRate
|
|
2
|
+
|
|
3
|
+
**Does your NLP model change its answer when the same message is typed differently?**
|
|
4
|
+
|
|
5
|
+
[](https://www.python.org/)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
[](https://github.com/ShivSupane/fliprate/actions/workflows/ci.yml)
|
|
8
|
+
|
|
9
|
+
Built by **Shiv Supane**
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## The problem
|
|
14
|
+
|
|
15
|
+
People who text in Hindi, Marathi, Bengali, Tamil, Telugu, and other Indian languages
|
|
16
|
+
using Roman (English) script have no standard spelling. The same word gets typed dozens of ways:
|
|
17
|
+
|
|
18
|
+
| Word | Romanized variants |
|
|
19
|
+
|---|---|
|
|
20
|
+
| नही (no) | `nahi` · `nhi` · `nahin` · `nahee` |
|
|
21
|
+
| बहुत (very) | `bahut` · `bohot` · `bhut` · `boht` |
|
|
22
|
+
| आवडला (liked) | `avadla` · `avdla` · `avadlaaa` |
|
|
23
|
+
|
|
24
|
+
Most NLP models treat these as completely different inputs and give different answers —
|
|
25
|
+
even though the meaning is identical. **FlipRate measures that problem, quantifies it
|
|
26
|
+
with confidence intervals, and gives you tools to fix it.**
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
User types: "mala product avadlaaa nahi" → model says: product_feedback ✓
|
|
30
|
+
Same meaning: "mala product avdla nhi" → model says: human_support ✗ ← FLIP
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install fliprate # core library (zero dependencies)
|
|
39
|
+
pip install "fliprate[api]" # + FastAPI web console
|
|
40
|
+
pip install "fliprate[api,hf]" # + Hugging Face model support
|
|
41
|
+
pip install "fliprate[api,hf,sklearn]" # + scikit-learn baselines
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
From source:
|
|
45
|
+
```bash
|
|
46
|
+
git clone https://github.com/ShivSupane/fliprate.git
|
|
47
|
+
cd fliprate
|
|
48
|
+
pip install -e ".[dev,api,hf,sklearn]"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Quick start
|
|
54
|
+
|
|
55
|
+
### Test your model in 3 lines
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import fliprate as fr
|
|
59
|
+
|
|
60
|
+
report = fr.test(my_model, "data/test.csv") # my_model: any function text -> label
|
|
61
|
+
print(report.summary())
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Example output *(illustrative — your numbers will differ)*:
|
|
65
|
+
```
|
|
66
|
+
Messages: 500 | safe variants tested: 2500
|
|
67
|
+
Accuracy on originals: 68.4% (macro-F1 0.672)
|
|
68
|
+
Accuracy on safe variants: 67.1% (macro-F1 0.658)
|
|
69
|
+
Accuracy drop: -1.3% 95% CI [-2.1%, -0.5%]
|
|
70
|
+
Flip rate: 6.2% 95% CI [5.1%, 7.4%]
|
|
71
|
+
Most fragile change type: repetition (flip rate 9.8%)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Protect your model
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
safe_model = fr.protect(my_model) # normalizer in front
|
|
78
|
+
report2 = fr.test(safe_model, "data/test.csv")
|
|
79
|
+
|
|
80
|
+
diff = fr.paired_difference(report, report2)
|
|
81
|
+
print(f"Flip rate change: {diff['difference']:+.1%}")
|
|
82
|
+
print(f"95% CI: {diff['ci']}")
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Real-time gate
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
result = fr.gate(my_model, "mala product avadlaaa nahi")
|
|
89
|
+
print(result.decision) # PASS / TRANSFORM / REVIEW
|
|
90
|
+
print(result.prediction) # model's answer on the (possibly normalized) text
|
|
91
|
+
print(result.reason) # human-readable explanation
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Web Console
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
uvicorn fliprate.api:app --reload # toy model, instant start
|
|
100
|
+
# then open http://localhost:8000
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Two tabs:
|
|
104
|
+
- **Playground** — paste a message, watch the gate decision stream live
|
|
105
|
+
- **Robustness Lab** — upload a labeled CSV, get a live flip-rate chart
|
|
106
|
+
|
|
107
|
+
Zero-dependency vanilla HTML/JS. No Streamlit, no npm, no build step.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## CLI
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
fliprate gate "mala product avadlaaa nahi"
|
|
115
|
+
fliprate perturb "delivery nahi aaya"
|
|
116
|
+
fliprate normalize "mala product avadlaaa nhi"
|
|
117
|
+
fliprate test --data mydata.csv
|
|
118
|
+
fliprate test --data mydata.csv -n 10 --protect --save report.json
|
|
119
|
+
fliprate check-data testdata.csv --against traindata.csv
|
|
120
|
+
fliprate split mydata.csv --out-dir data/
|
|
121
|
+
fliprate mine mydata.csv --out candidates.csv
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Plug in any model
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
# Hugging Face (lazy-loaded, batched)
|
|
130
|
+
clf = fr.HFTextClassifier(
|
|
131
|
+
"cardiffnlp/twitter-xlm-roberta-base-sentiment",
|
|
132
|
+
lowercase_labels=True,
|
|
133
|
+
preprocess=fr.twitter_preprocess,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# scikit-learn
|
|
137
|
+
from fliprate.baselines import train_tfidf
|
|
138
|
+
clf = train_tfidf(train_rows, analyzer="word") # or "char_wb"
|
|
139
|
+
|
|
140
|
+
# Any LLM
|
|
141
|
+
llm = fr.with_retries(fr.llm_classifier(
|
|
142
|
+
my_openai_fn, labels=["positive", "neutral", "negative"]
|
|
143
|
+
))
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## Multi-language rules
|
|
149
|
+
|
|
150
|
+
FlipRate ships with 73 core HIGH-confidence groups for immediate use.
|
|
151
|
+
For broader coverage, load from two research datasets:
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
# Dakshina (Google Research, CC BY-SA 4.0) — 225K groups, 12 languages
|
|
155
|
+
# Codes: hi ur mr bn ta te kn ml gu pa sd si
|
|
156
|
+
rules = fr.dakshina_rules("ta") # Tamil
|
|
157
|
+
rules = fr.dakshina_rules("hi", "mr") # Hindi + Marathi
|
|
158
|
+
|
|
159
|
+
# Aksharantar (AI4Bharat, CC BY 4.0) — 428K groups, 20 languages
|
|
160
|
+
# Codes: hin guj tam tel kan mal mar pan ben urd ori asm kas kok mai mni nep san brx sid
|
|
161
|
+
rules = fr.aksharantar_rules("hin") # Hindi: 58K groups
|
|
162
|
+
rules = fr.aksharantar_rules("tam", "tel", "kan") # South India
|
|
163
|
+
|
|
164
|
+
# Use in any call
|
|
165
|
+
report = fr.test(my_model, "data/test.csv", rules=rules)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## How it works
|
|
171
|
+
|
|
172
|
+
**Safe variant:** a rewriting of a message that keeps the same canonical words
|
|
173
|
+
(up to reviewed equivalent spellings) and the same negation count. Adding or
|
|
174
|
+
removing a negation word (`nahi`, `nako`, `illa`, …) is never allowed.
|
|
175
|
+
|
|
176
|
+
**Flip rate:** `variants_where_answer_changed / total_safe_variants`.
|
|
177
|
+
Bootstrap 95% CIs are computed over messages, not individual variants.
|
|
178
|
+
|
|
179
|
+
**Gate decisions:**
|
|
180
|
+
|
|
181
|
+
| Decision | When |
|
|
182
|
+
|---|---|
|
|
183
|
+
| **PASS** | ≥ 80% of safe variants get the same answer. |
|
|
184
|
+
| **TRANSFORM** | Unstable, but the built-in normalizer fixes it. Sends cleaned text to model; keeps original. |
|
|
185
|
+
| **REVIEW** | Unstable and normalization does not help. Route to a human. |
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Experiment results (v0.2)
|
|
190
|
+
|
|
191
|
+
Tested on a processed copy of SentiMix Hinglish sentiment (2,997 held-out messages,
|
|
192
|
+
5 rule-generated variants per message). **Read the caveats below before citing these numbers.**
|
|
193
|
+
|
|
194
|
+
| Model | Mode | Accuracy | Flip rate (95% CI) |
|
|
195
|
+
|---|---|---|---|
|
|
196
|
+
| xlm-roberta-base-sentiment | raw | 59.2% | 7.3% (6.7–7.9%) |
|
|
197
|
+
| xlm-roberta-base-sentiment | **protected** | 59.1% | **3.5%** (3.1–3.9%) |
|
|
198
|
+
| TF-IDF word n-gram | raw | 65.9% | 4.2% (3.8–4.6%) |
|
|
199
|
+
| TF-IDF word n-gram | **protected** | 65.6% | **3.2%** (2.9–3.6%) |
|
|
200
|
+
|
|
201
|
+
Paired flip-rate change: **−3.80 pts** [−4.22, −3.39]. Accuracy: −0.03 pts [−0.23, +0.17].
|
|
202
|
+
|
|
203
|
+
> **Important caveats (from [`evidence/EVIDENCE.md`](evidence/EVIDENCE.md)):**
|
|
204
|
+
> - The SentiMix copy used here is lowercased, stopword-stripped and Porter-stemmed — not natural user typing.
|
|
205
|
+
> - The spelling-swap flip rate drops to 0 % after protection **by construction**: the variants and the normalizer use the same rule list, so normalization simply undoes its own perturbations. This shows the normalizer is internally consistent, not that it fixes real-world spelling noise.
|
|
206
|
+
> - The spelling rules have not been reviewed by a fluent speaker.
|
|
207
|
+
> - Only one transformer was tested, zero-shot.
|
|
208
|
+
>
|
|
209
|
+
> These numbers are a proof-of-concept baseline. Treat them as lower bounds on what a fair evaluation on natural, unprocessed text would show.
|
|
210
|
+
|
|
211
|
+
Full evidence and raw result files: [`evidence/`](evidence/)
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## Production deployment
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
# With API key and restricted CORS
|
|
219
|
+
FLIPRATE_API_KEY=secret FLIPRATE_CORS=https://yoursite.com \
|
|
220
|
+
uvicorn fliprate.api:app --host 0.0.0.0 --port 8000 \
|
|
221
|
+
--proxy-headers --forwarded-allow-ips="*"
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Environment variables:**
|
|
225
|
+
|
|
226
|
+
| Variable | Default | Effect |
|
|
227
|
+
|---|---|---|
|
|
228
|
+
| `FLIPRATE_MODEL` | toy model | `module:function` path to your classifier |
|
|
229
|
+
| `FLIPRATE_API_KEY` | none (open) | Require key on POST endpoints |
|
|
230
|
+
| `FLIPRATE_CORS` | `*` | Allowed origins — always restrict in production |
|
|
231
|
+
| `FLIPRATE_RATE_LIMIT` | `60` | Requests per minute per IP/key |
|
|
232
|
+
| `FLIPRATE_MAX_CONCURRENT` | `2` | Max simultaneous jobs |
|
|
233
|
+
| `FLIPRATE_MAX_TEXT_CHARS` | `2000` | Max chars per analyze request |
|
|
234
|
+
| `FLIPRATE_MAX_ROWS` | `2000` | Max rows per test request |
|
|
235
|
+
|
|
236
|
+
Docker:
|
|
237
|
+
```bash
|
|
238
|
+
docker build -t fliprate .
|
|
239
|
+
docker run -p 7860:7860 \
|
|
240
|
+
-e FLIPRATE_MODEL=fliprate.hf_models:xlmt_sentiment \
|
|
241
|
+
-e FLIPRATE_API_KEY=secret \
|
|
242
|
+
fliprate
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## Documentation
|
|
248
|
+
|
|
249
|
+
| File | Contents |
|
|
250
|
+
|---|---|
|
|
251
|
+
| [`docs/QUICKSTART.md`](docs/QUICKSTART.md) | Every command, env var, and code example |
|
|
252
|
+
| [`docs/API_REFERENCE.md`](docs/API_REFERENCE.md) | Full function reference with parameters |
|
|
253
|
+
| [`docs/PROJECT_OVERVIEW.md`](docs/PROJECT_OVERVIEW.md) | Architecture, design decisions, module map |
|
|
254
|
+
| [`docs/EXPERIMENT.md`](docs/EXPERIMENT.md) | Experiment protocol and caveats |
|
|
255
|
+
| [`docs/RELEASE_AND_DEPLOY.md`](docs/RELEASE_AND_DEPLOY.md) | PyPI and deployment guide |
|
|
256
|
+
| [`evidence/`](evidence/) | Raw result files and EVIDENCE.md |
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## Limitations
|
|
261
|
+
|
|
262
|
+
- Romanized Latin-script text only. Devanagari and other native scripts go to REVIEW.
|
|
263
|
+
- Rule-generated variants only — real typing noise is wider than 4 change types.
|
|
264
|
+
- Spelling rules have not been reviewed by a fluent speaker for any language.
|
|
265
|
+
- Customer-support domain lexicon; use `--no-lexicon` for other domains.
|
|
266
|
+
- Dakshina data is from Wikipedia. Aksharantar draws on Wikipedia, IndicCorp, Samanantar, and other corpora — but not all pairs are manually annotated.
|
|
267
|
+
- Do not publish numbers from the bundled 40-message demo sample.
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
## Datasets
|
|
272
|
+
|
|
273
|
+
If you use the multi-language rules in published work, cite:
|
|
274
|
+
|
|
275
|
+
> Roark et al. (2020). "Processing South Asian Languages Written in the Latin Script: the Dakshina Dataset." LREC 2020. https://aclanthology.org/2020.lrec-1.294
|
|
276
|
+
|
|
277
|
+
> Mittal et al. (2022). "Aksharantar: Towards Building Open Transliteration Tools for the Next Billion Users." https://arxiv.org/abs/2205.03018
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
## License
|
|
282
|
+
|
|
283
|
+
MIT — see [LICENSE](LICENSE). © 2025 Shiv Supane
|
|
284
|
+
|
|
285
|
+
The Dakshina dataset is CC BY-SA 4.0. The Aksharantar dataset is CC BY 4.0.
|
|
286
|
+
Neither is included in this package — download separately.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fliprate"
|
|
7
|
+
version = "0.2.1"
|
|
8
|
+
description = "Measure and reduce how often an NLP model changes its answer on messy romanized Hindi/Marathi and other Indian-language text."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Shiv Supane", email = "shiv.supane777@gmail.com" }]
|
|
14
|
+
keywords = ["nlp", "robustness", "hinglish", "marathi", "indic", "romanized", "testing", "transliteration", "code-mixed"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.9",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
25
|
+
"Topic :: Text Processing :: Linguistic",
|
|
26
|
+
]
|
|
27
|
+
dependencies = []
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
api = ["fastapi>=0.100", "uvicorn>=0.23"]
|
|
31
|
+
sklearn = ["scikit-learn>=1.3"]
|
|
32
|
+
hf = ["transformers>=4.36", "torch>=2.1"]
|
|
33
|
+
dev = ["pytest>=7", "scikit-learn>=1.3", "build", "twine", "httpx"]
|
|
34
|
+
all = ["fastapi>=0.100", "uvicorn>=0.23", "scikit-learn>=1.3", "transformers>=4.36", "torch>=2.1"]
|
|
35
|
+
|
|
36
|
+
[project.scripts]
|
|
37
|
+
fliprate = "fliprate.cli:main"
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://github.com/ShivSupane/fliprate"
|
|
41
|
+
Issues = "https://github.com/ShivSupane/fliprate/issues"
|
|
42
|
+
Changelog = "https://github.com/ShivSupane/fliprate/blob/main/CHANGELOG.md"
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.packages.find]
|
|
45
|
+
where = ["src"]
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.package-data]
|
|
48
|
+
fliprate = ["data/*.csv", "web/*.html", "py.typed"]
|
fliprate-0.2.1/setup.cfg
ADDED