formless 0.7.0__tar.gz → 0.9.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.

Potentially problematic release.


This version of formless might be problematic. Click here for more details.

Files changed (43) hide show
  1. {formless-0.7.0 → formless-0.9.0}/.gitignore +4 -0
  2. formless-0.9.0/Makefile +37 -0
  3. formless-0.9.0/PKG-INFO +478 -0
  4. formless-0.9.0/README.md +417 -0
  5. formless-0.9.0/api/app.py +291 -0
  6. {formless-0.7.0 → formless-0.9.0}/db/migrations/env.py +9 -3
  7. {formless-0.7.0 → formless-0.9.0}/db/models.py +1 -1
  8. formless-0.9.0/eg/0.png +0 -0
  9. formless-0.9.0/eg/1.png +0 -0
  10. formless-0.9.0/eg/2.png +0 -0
  11. formless-0.9.0/eg/3.png +0 -0
  12. {formless-0.7.0 → formless-0.9.0}/frontend/app.py +379 -423
  13. {formless-0.7.0 → formless-0.9.0}/pyproject.toml +47 -13
  14. {formless-0.7.0 → formless-0.9.0}/src/formless/__init__.py +3 -3
  15. formless-0.9.0/training/etl.py +881 -0
  16. formless-0.9.0/training/eval.py +198 -0
  17. formless-0.9.0/training/pyproject.toml +34 -0
  18. formless-0.9.0/training/quantize.py +274 -0
  19. formless-0.9.0/training/train.py +1396 -0
  20. {formless-0.7.0 → formless-0.9.0/training}/uv.lock +591 -474
  21. formless-0.9.0/utils.py +394 -0
  22. formless-0.9.0/uv.lock +3896 -0
  23. formless-0.7.0/Makefile +0 -16
  24. formless-0.7.0/PKG-INFO +0 -202
  25. formless-0.7.0/README.md +0 -178
  26. formless-0.7.0/api/app.py +0 -398
  27. formless-0.7.0/api/eg.png +0 -0
  28. formless-0.7.0/training/dataset_info.json +0 -17
  29. formless-0.7.0/training/ds_z3_config.json +0 -30
  30. formless-0.7.0/training/etl.py +0 -388
  31. formless-0.7.0/training/model.py +0 -1831
  32. formless-0.7.0/training/quantize.py +0 -185
  33. formless-0.7.0/training/qwen2vl_full_sft.yaml +0 -43
  34. formless-0.7.0/training/train.py +0 -174
  35. formless-0.7.0/utils.py +0 -93
  36. {formless-0.7.0 → formless-0.9.0}/.github/workflows/pre-commit.yml +0 -0
  37. {formless-0.7.0 → formless-0.9.0}/.pre-commit-config.yaml +0 -0
  38. {formless-0.7.0 → formless-0.9.0}/LICENSE +0 -0
  39. {formless-0.7.0 → formless-0.9.0}/db/migrations/README +0 -0
  40. {formless-0.7.0 → formless-0.9.0}/db/migrations/alembic.ini +0 -0
  41. {formless-0.7.0 → formless-0.9.0}/db/migrations/script.py.mako +0 -0
  42. {formless-0.7.0 → formless-0.9.0}/frontend/favicon.ico +0 -0
  43. {formless-0.7.0 → formless-0.9.0}/training/__init__.py +0 -0
@@ -203,13 +203,17 @@ cython_debug/
203
203
 
204
204
  # FastHTML
205
205
  .sesskey
206
+ Python-Antivirus
206
207
 
207
208
  # DB
208
209
  *.db
209
210
  **/versions/
211
+ **/local_db/
210
212
 
211
213
  # Training
212
214
  **/artifacts/
215
+ **/wandb/
216
+ **/LLaMA-Factory/
213
217
 
214
218
  # windsurf rules
215
219
  .windsurfrules
@@ -0,0 +1,37 @@
1
+ SHELL := /bin/bash
2
+
3
+ setup:
4
+ sudo apt install libpq-dev libcairo2-dev libjpeg-dev libgif-dev openjdk-11-jdk aria2 pigz s3fs
5
+ uv sync --all-extras --dev
6
+ uv run pre-commit install
7
+ modal setup
8
+ modal config set-environment dev
9
+ git clone git@github.com:Len-Stevens/Python-Antivirus.git
10
+ git clone --depth 1 training/https://github.com/hiyouga/LLaMA-Factory.git
11
+ aws configure
12
+ echo "alias modal='uv run modal'" >> ~/.bashrc
13
+ echo "export PYTHONPATH=.:$PYTHONPATH" >> ~/.bashrc
14
+ echo "export TOKENIZERS_PARALLELISM=false" >> ~/.bashrc
15
+ echo "export HF_HUB_ENABLE_HF_TRANSFER=1" >> ~/.bashrc
16
+ source ~/.bashrc
17
+
18
+
19
+ # migrate database
20
+ # Usage: make migrate ENV=<env> [MSG=<message>]
21
+ ENV ?= dev
22
+ MSG ?=
23
+
24
+ migrate:
25
+ uv run alembic -x env=$(ENV) -c db/migrations/alembic.ini stamp head
26
+ uv run alembic -x env=$(ENV) -c db/migrations/alembic.ini revision --autogenerate -m "$(MSG)" --version-path db/migrations/versions/$(ENV)
27
+ uv run alembic -x env=$(ENV) -c db/migrations/alembic.ini upgrade head
28
+
29
+ data:
30
+ mkdir training/artifacts/mathwriting-2024-excerpt
31
+ aria2c -x 16 -s 16 -j 1 -d training/artifacts -o mathwriting-2024-excerpt.tgz https://storage.googleapis.com/mathwriting_data/mathwriting-2024-excerpt.tgz
32
+ tar --use-compress-program="pigz -p 8" -xvf training/artifacts/mathwriting-2024-excerpt.tgz -C training/artifacts
33
+ rm training/artifacts/mathwriting-2024-excerpt.tgz
34
+
35
+ upload:
36
+ modal volume create formless-data
37
+ modal volume put formless-data training/artifacts/mathwriting-2024-excerpt
@@ -0,0 +1,478 @@
1
+ Metadata-Version: 2.4
2
+ Name: formless
3
+ Version: 0.9.0
4
+ Summary: Handwritten + image OCR.
5
+ License-File: LICENSE
6
+ Requires-Python: >=3.12
7
+ Requires-Dist: accelerate>=1.2.1
8
+ Requires-Dist: alembic>=1.14.0
9
+ Requires-Dist: autoawq>=0.2.7.post3
10
+ Requires-Dist: bitsandbytes>=0.45.1
11
+ Requires-Dist: blobfile>=3.0.0
12
+ Requires-Dist: boto>=2.49.0
13
+ Requires-Dist: datasketch>=1.6.5
14
+ Requires-Dist: deepspeed>=0.16.3
15
+ Requires-Dist: editables>=0.5
16
+ Requires-Dist: fastapi>=0.115.6
17
+ Requires-Dist: flash-attn>=2.7.4.post1
18
+ Requires-Dist: hatchling>=1.22.5
19
+ Requires-Dist: hf-transfer>=0.1.8
20
+ Requires-Dist: huggingface-hub[hf-transfer]>=0.27.1
21
+ Requires-Dist: imagehash>=4.3.2
22
+ Requires-Dist: jiwer>=3.1.0
23
+ Requires-Dist: lark-parser>=0.12.0
24
+ Requires-Dist: modal>=0.64.178
25
+ Requires-Dist: more-itertools>=10.6.0
26
+ Requires-Dist: ninja>=1.11.1
27
+ Requires-Dist: outlines>=0.1.11
28
+ Requires-Dist: packaging>=23.1
29
+ Requires-Dist: pandas>=2.2.3
30
+ Requires-Dist: pillow>=10.4.0
31
+ Requires-Dist: pre-commit>=4.0.1
32
+ Requires-Dist: psycopg2-binary>=2.9.10
33
+ Requires-Dist: psycopg2>=2.9.10
34
+ Requires-Dist: pycairo>=1.27.0
35
+ Requires-Dist: pydantic>=2.10.4
36
+ Requires-Dist: python-dotenv>=1.0.1
37
+ Requires-Dist: python-fasthtml>=0.12.1
38
+ Requires-Dist: python-magic>=0.4.27
39
+ Requires-Dist: requests>=2.32.3
40
+ Requires-Dist: s3fs>=2024.9.0
41
+ Requires-Dist: safetensors>=0.5.1
42
+ Requires-Dist: sentencepiece>=0.2.0
43
+ Requires-Dist: simpleicons>=7.21.0
44
+ Requires-Dist: sqlite-minutils>=4.0.3
45
+ Requires-Dist: sqlmodel>=0.0.22
46
+ Requires-Dist: starlette>=0.46.1
47
+ Requires-Dist: stripe>=11.5.0
48
+ Requires-Dist: term-image>=0.7.2
49
+ Requires-Dist: tiktoken>=0.7.0
50
+ Requires-Dist: timm>=1.0.14
51
+ Requires-Dist: torch>=2.5.1
52
+ Requires-Dist: torchao>=0.7.0
53
+ Requires-Dist: torchvision>=0.20.1
54
+ Requires-Dist: tqdm>=4.67.1
55
+ Requires-Dist: transformers>=4.48.2
56
+ Requires-Dist: validators>=0.34.0
57
+ Requires-Dist: vllm>=0.7.1
58
+ Requires-Dist: wandb>=0.19.4
59
+ Requires-Dist: wheel>=0.45.1
60
+ Description-Content-Type: text/markdown
61
+
62
+ # formless
63
+
64
+ A hard handwriting image OCR system via a public API, website, and PyPI package, utilizing a fine-tuned Qwen2.5-VL-7B-Instruct. Utilizes FineWeb-inspired data quality filtering and stratified deduplication alongside SFT and DPO on worst-performing samples to reduce character error rate by 8.18% compared to the base model.
65
+
66
+ ## Usage
67
+
68
+ Use the web app:
69
+
70
+ ```bash
71
+ https://bit.ly/formless-fe
72
+ ```
73
+
74
+ Or hit the API:
75
+
76
+ ```bash
77
+ curl -X POST -H "Content-Type: application/json" -d '{"image_url": "<image-url>"}' https://andrewhinh--formless-api-modal-get.modal.run
78
+ ```
79
+
80
+ Or use the CLI:
81
+
82
+ ```bash
83
+ uv run formless -i <image-url> [-v]
84
+ or
85
+ uv run formless -p <local-image-path> [-v]
86
+ ```
87
+
88
+ Or use in Python:
89
+
90
+ ```python
91
+ from formless import scan
92
+ scan(image_url="<image-url>", verbose=1)
93
+ scan(image_path="<local-image-path>", verbose=1)
94
+ ```
95
+
96
+ ## Training results
97
+
98
+ Base model:
99
+
100
+ ```bash
101
+ train CER: 0.9216
102
+ valid CER: 0.9276
103
+ test CER: 0.9430
104
+ ```
105
+
106
+ Base quant model:
107
+
108
+ ```bash
109
+ train CER: 0.9192
110
+ valid CER: 0.9232
111
+ test CER: 0.9452
112
+ ```
113
+
114
+ SFT model:
115
+
116
+ ```bash
117
+ train CER: 0.7965
118
+ valid CER: 0.8601
119
+ test CER: 0.8659
120
+ ```
121
+
122
+ SFT quant model:
123
+
124
+ ```bash
125
+ train CER: 0.8380
126
+ valid CER: 1.0214
127
+ test CER: 0.8228
128
+ ```
129
+
130
+ DPO model:
131
+
132
+ ```bash
133
+ train CER: 0.7827
134
+ valid CER: 0.8518
135
+ test CER: 0.9757
136
+ ```
137
+
138
+ DPO quant model:
139
+
140
+ ```bash
141
+ train CER: 0.7979
142
+ valid CER: 1.0352
143
+ test CER: 0.9868
144
+ ```
145
+
146
+ ## Development
147
+
148
+ ### Set Up
149
+
150
+ Set up the environment:
151
+
152
+ ```bash
153
+ make setup
154
+ ```
155
+
156
+ Create a `.env` (+ `.env.dev`):
157
+
158
+ ```bash
159
+ HF_TOKEN=
160
+ OPENAI_API_KEY=
161
+
162
+ POSTGRES_URL=
163
+ POSTGRES_PRISMA_URL=
164
+ SUPABASE_URL=
165
+ NEXT_PUBLIC_SUPABASE_URL=
166
+ POSTGRES_URL_NON_POOLING=
167
+ SUPABASE_JWT_SECRET=
168
+ POSTGRES_USER=
169
+ NEXT_PUBLIC_SUPABASE_ANON_KEY=
170
+ POSTGRES_PASSWORD=
171
+ POSTGRES_DATABASE=
172
+ SUPABASE_SERVICE_ROLE_KEY=
173
+ POSTGRES_HOST=
174
+ SUPABASE_ANON_KEY=
175
+
176
+ STRIPE_PUBLISHABLE_KEY=
177
+ STRIPE_SECRET_KEY=
178
+ STRIPE_WEBHOOK_SECRET=
179
+ DOMAIN=
180
+ API_URL=
181
+
182
+ WANDB_API_KEY=
183
+ WANDB_PROJECT=
184
+ WANDB_ENTITY=
185
+ ```
186
+
187
+ ### Useful Tips
188
+
189
+ Migrate db (do before running the frontend/api):
190
+
191
+ ```bash
192
+ make migrate ENV=<env> MSG=<message>
193
+ ```
194
+
195
+ Visit `http://localhost:4040/` to see the Spark UI when running `training/etl.py`.
196
+
197
+ ### Repository Structure
198
+
199
+ ```bash
200
+ .
201
+ ├── api # API.
202
+ ├── db # database.
203
+ ├── frontend # frontend.
204
+ ├── src/formless # python bindings.
205
+ ├── training # training.
206
+ ```
207
+
208
+ ### API
209
+
210
+ Test the API with an example input:
211
+
212
+ ```bash
213
+ modal run api/app.py
214
+ ```
215
+
216
+ Serve the API locally:
217
+
218
+ ```bash
219
+ uv run api/app.py
220
+ ```
221
+
222
+ Serve the API on Modal:
223
+
224
+ ```bash
225
+ modal serve api/app.py
226
+ ```
227
+
228
+ Deploy on dev:
229
+
230
+ ```bash
231
+ modal deploy api/app.py
232
+ ```
233
+
234
+ Deploy on main:
235
+
236
+ ```bash
237
+ modal deploy --env=main api/app.py
238
+ ```
239
+
240
+ ### Frontend
241
+
242
+ Serve the web app locally:
243
+
244
+ ```bash
245
+ uv run frontend/app.py
246
+ stripe listen --forward-to <url>/webhook
247
+ # update API_URL, STRIPE_WEBHOOK_SECRET, and DOMAIN in .env.dev
248
+ ```
249
+
250
+ Serve the web app on Modal:
251
+
252
+ ```bash
253
+ modal serve frontend/app.py
254
+ stripe listen --forward-to <url>/webhook
255
+ # update API_URL, STRIPE_WEBHOOK_SECRET, and DOMAIN in .env.dev
256
+ ```
257
+
258
+ Deploy on dev:
259
+
260
+ ```bash
261
+ modal deploy frontend/app.py
262
+ # update API_URL, STRIPE_WEBHOOK_SECRET, and DOMAIN in .env.dev
263
+ ```
264
+
265
+ Deploy on main:
266
+
267
+ ```bash
268
+ modal deploy --env=main frontend/app.py
269
+ ```
270
+
271
+ ### PyPI
272
+
273
+ Run the package:
274
+
275
+ ```bash
276
+ uv run formless -v
277
+ # update API_URL in src/formless/__init__.py
278
+ ```
279
+
280
+ Build the package:
281
+
282
+ ```bash
283
+ uvx --from build pyproject-build --installer uv
284
+ ```
285
+
286
+ Upload the package:
287
+
288
+ ```bash
289
+ uvx twine upload dist/*
290
+ ```
291
+
292
+ Test the uploaded package:
293
+
294
+ ```bash
295
+ uv run --with formless --no-project -- formless -v
296
+ ```
297
+
298
+ ### Training
299
+
300
+ Download data:
301
+
302
+ ```bash
303
+ make data
304
+ ```
305
+
306
+ Optionally upload to a Modal volume:
307
+
308
+ ```bash
309
+ make upload
310
+ ```
311
+
312
+ Label subset of data to train writing quality classifier:
313
+
314
+ ```bash
315
+ uv run training/etl.py --cls
316
+ ```
317
+
318
+ or
319
+
320
+ ```bash
321
+ modal run training/etl.py --cls
322
+ ```
323
+
324
+ Run classifier training:
325
+
326
+ ```bash
327
+ uv run training/train.py --cls
328
+ ```
329
+
330
+ or
331
+
332
+ ```bash
333
+ modal run training/train.py --cls
334
+ ```
335
+
336
+ Use trained classifier to filter train/val/test data to train VLM using SFT:
337
+
338
+ ```bash
339
+ uv run training/etl.py --sft
340
+ ```
341
+
342
+ or
343
+
344
+ ```bash
345
+ modal run training/etl.py --sft
346
+ ```
347
+
348
+ Eval base model:
349
+
350
+ ```bash
351
+ uv run training/eval.py --base
352
+ ```
353
+
354
+ or
355
+
356
+ ```bash
357
+ modal run training/eval.py --base
358
+ ```
359
+
360
+ Eval quantized base model:
361
+
362
+ ```bash
363
+ uv run training/eval.py --base --quant
364
+ ```
365
+
366
+ or
367
+
368
+ ```bash
369
+ modal run training/eval.py --base --quant
370
+ ```
371
+
372
+ Run SFT:
373
+
374
+ ```bash
375
+ cd training && uv sync && cd LLaMA-Factory && uv pip install -e ".[torch,metrics]" && cd .. && FORCE_TORCHRUN=1 uv run train.py --sft && cd ..
376
+ ```
377
+
378
+ or
379
+
380
+ ```bash
381
+ modal run training/train.py --sft
382
+ ```
383
+
384
+ Eval SFT model:
385
+
386
+ ```bash
387
+ uv run training/eval.py --sft
388
+ ```
389
+
390
+ or
391
+
392
+ ```bash
393
+ modal run training/eval.py --sft
394
+ ```
395
+
396
+ Quantize the SFT model:
397
+
398
+ ```bash
399
+ uv run training/quantize.py --sft
400
+ ```
401
+
402
+ or
403
+
404
+ ```bash
405
+ modal run training/quantize.py --sft
406
+ ```
407
+
408
+ Eval quantized SFT model:
409
+
410
+ ```bash
411
+ uv run training/eval.py --sft --quant
412
+ ```
413
+
414
+ or
415
+
416
+ ```bash
417
+ modal run training/eval.py --sft --quant
418
+ ```
419
+
420
+ Run trained VLM on train data and construct new dataset with only relabelled incorrect examples for DPO training:
421
+
422
+ ```bash
423
+ uv run training/etl.py --dpo
424
+ ```
425
+
426
+ or
427
+
428
+ ```bash
429
+ modal run training/etl.py --dpo
430
+ ```
431
+
432
+ Run DPO:
433
+
434
+ ```bash
435
+ cd training && uv sync && cd LLaMA-Factory && uv pip install -e ".[torch,metrics]" && cd .. && FORCE_TORCHRUN=1 uv run train.py --dpo && cd ..
436
+ ```
437
+
438
+ or
439
+
440
+ ```bash
441
+ modal run training/train.py --dpo
442
+ ```
443
+
444
+ Eval DPO model:
445
+
446
+ ```bash
447
+ uv run training/eval.py --dpo
448
+ ```
449
+
450
+ or
451
+
452
+ ```bash
453
+ modal run training/eval.py --dpo
454
+ ```
455
+
456
+ Quantize the DPO model:
457
+
458
+ ```bash
459
+ uv run training/quantize.py --dpo
460
+ ```
461
+
462
+ or
463
+
464
+ ```bash
465
+ modal run training/quantize.py --dpo
466
+ ```
467
+
468
+ Eval quantized DPO model:
469
+
470
+ ```bash
471
+ uv run training/eval.py --dpo --quant
472
+ ```
473
+
474
+ or
475
+
476
+ ```bash
477
+ modal run training/eval.py --dpo --quant
478
+ ```