formless 0.2.0__tar.gz → 0.4.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.
- {formless-0.2.0 → formless-0.4.0}/.github/workflows/pre-commit.yml +3 -1
- {formless-0.2.0 → formless-0.4.0}/.gitignore +1 -0
- {formless-0.2.0 → formless-0.4.0}/PKG-INFO +48 -9
- {formless-0.2.0 → formless-0.4.0}/README.md +46 -8
- {formless-0.2.0 → formless-0.4.0}/api/app.py +81 -47
- formless-0.4.0/frontend/app.py +475 -0
- {formless-0.2.0 → formless-0.4.0}/pyproject.toml +2 -1
- {formless-0.2.0 → formless-0.4.0}/src/formless/__init__.py +10 -7
- {formless-0.2.0 → formless-0.4.0}/utils.py +2 -0
- {formless-0.2.0 → formless-0.4.0}/uv.lock +113 -1
- formless-0.2.0/frontend/app.py +0 -166
- {formless-0.2.0 → formless-0.4.0}/.pre-commit-config.yaml +0 -0
- {formless-0.2.0 → formless-0.4.0}/LICENSE +0 -0
- {formless-0.2.0 → formless-0.4.0}/frontend/favicon.ico +0 -0
- {formless-0.2.0 → formless-0.4.0}/training/__init__.py +0 -0
- {formless-0.2.0 → formless-0.4.0}/training/etl.py +0 -0
- {formless-0.2.0 → formless-0.4.0}/training/inference.py +0 -0
- {formless-0.2.0 → formless-0.4.0}/training/train.py +0 -0
- {formless-0.2.0 → formless-0.4.0}/training/train_modal.py +0 -0
- {formless-0.2.0 → formless-0.4.0}/training/utils.py +0 -0
|
@@ -35,5 +35,7 @@ jobs:
|
|
|
35
35
|
# - name: Run tests
|
|
36
36
|
# # For example, using `pytest`
|
|
37
37
|
# run: uv run pytest tests
|
|
38
|
+
- name: Install pre-commit
|
|
39
|
+
run: uvx pre-commit install
|
|
38
40
|
- name: Run pre-commit hooks
|
|
39
|
-
run:
|
|
41
|
+
run: uvx pre-commit run --all-files
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: formless
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Handwritten + image OCR.
|
|
5
5
|
License-File: LICENSE
|
|
6
6
|
Requires-Python: >=3.12
|
|
7
7
|
Requires-Dist: modal>=0.64.178
|
|
8
|
+
Requires-Dist: pre-commit>=4.0.1
|
|
8
9
|
Requires-Dist: python-dotenv>=1.0.1
|
|
9
10
|
Requires-Dist: requests>=2.32.3
|
|
10
11
|
Requires-Dist: term-image>=0.7.2
|
|
@@ -41,10 +42,26 @@ Set up the environment:
|
|
|
41
42
|
|
|
42
43
|
```bash
|
|
43
44
|
uv sync --all-extras --dev
|
|
44
|
-
uv run pre-commit install
|
|
45
45
|
modal setup
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
+
Create a `.env` (+ `.env.dev` + `.env.local`):
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
API_URL=
|
|
52
|
+
HF_TOKEN=
|
|
53
|
+
|
|
54
|
+
LIVE=
|
|
55
|
+
DEBUG=
|
|
56
|
+
STRIPE_PUBLISHABLE_KEY=
|
|
57
|
+
STRIPE_SECRET_KEY=
|
|
58
|
+
STRIPE_WEBHOOK_SECRET=
|
|
59
|
+
DOMAIN=
|
|
60
|
+
|
|
61
|
+
WANDB_API_KEY=
|
|
62
|
+
WANDB_ENTITY=
|
|
63
|
+
```
|
|
64
|
+
|
|
48
65
|
### Repository Structure
|
|
49
66
|
|
|
50
67
|
```bash
|
|
@@ -57,30 +74,51 @@ modal setup
|
|
|
57
74
|
|
|
58
75
|
### API
|
|
59
76
|
|
|
60
|
-
|
|
77
|
+
Test the API:
|
|
61
78
|
|
|
62
79
|
```bash
|
|
63
80
|
modal run api/app.py
|
|
64
81
|
```
|
|
65
82
|
|
|
66
|
-
|
|
83
|
+
Run the API "locally":
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
modal serve --env=dev api/app.py
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Deploy on dev:
|
|
67
90
|
|
|
68
91
|
```bash
|
|
69
|
-
modal deploy api/app.py
|
|
92
|
+
modal deploy --env=dev api/app.py
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Deploy on main:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
modal deploy --env=main api/app.py
|
|
70
99
|
```
|
|
71
100
|
|
|
72
101
|
### Frontend
|
|
73
102
|
|
|
74
|
-
Run the web app:
|
|
103
|
+
Run the web app "locally":
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
modal serve --env=dev frontend/app.py
|
|
107
|
+
stripe listen --forward-to <url>/webhook
|
|
108
|
+
# update API_URL, STRIPE_WEBHOOK_SECRET, and DOMAIN in .env.dev
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Deploy on dev:
|
|
75
112
|
|
|
76
113
|
```bash
|
|
77
|
-
modal
|
|
114
|
+
modal deploy --env=dev frontend/app.py
|
|
115
|
+
# update API_URL, STRIPE_WEBHOOK_SECRET, and DOMAIN in .env.dev
|
|
78
116
|
```
|
|
79
117
|
|
|
80
|
-
Deploy:
|
|
118
|
+
Deploy on main:
|
|
81
119
|
|
|
82
120
|
```bash
|
|
83
|
-
modal deploy frontend/app.py
|
|
121
|
+
modal deploy --env=main frontend/app.py
|
|
84
122
|
```
|
|
85
123
|
|
|
86
124
|
### PyPI
|
|
@@ -89,6 +127,7 @@ Run the package:
|
|
|
89
127
|
|
|
90
128
|
```bash
|
|
91
129
|
uv run formless -v
|
|
130
|
+
# update API_URL in src/formless/__init__.py
|
|
92
131
|
```
|
|
93
132
|
|
|
94
133
|
Build the package:
|
|
@@ -29,10 +29,26 @@ Set up the environment:
|
|
|
29
29
|
|
|
30
30
|
```bash
|
|
31
31
|
uv sync --all-extras --dev
|
|
32
|
-
uv run pre-commit install
|
|
33
32
|
modal setup
|
|
34
33
|
```
|
|
35
34
|
|
|
35
|
+
Create a `.env` (+ `.env.dev` + `.env.local`):
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
API_URL=
|
|
39
|
+
HF_TOKEN=
|
|
40
|
+
|
|
41
|
+
LIVE=
|
|
42
|
+
DEBUG=
|
|
43
|
+
STRIPE_PUBLISHABLE_KEY=
|
|
44
|
+
STRIPE_SECRET_KEY=
|
|
45
|
+
STRIPE_WEBHOOK_SECRET=
|
|
46
|
+
DOMAIN=
|
|
47
|
+
|
|
48
|
+
WANDB_API_KEY=
|
|
49
|
+
WANDB_ENTITY=
|
|
50
|
+
```
|
|
51
|
+
|
|
36
52
|
### Repository Structure
|
|
37
53
|
|
|
38
54
|
```bash
|
|
@@ -45,30 +61,51 @@ modal setup
|
|
|
45
61
|
|
|
46
62
|
### API
|
|
47
63
|
|
|
48
|
-
|
|
64
|
+
Test the API:
|
|
49
65
|
|
|
50
66
|
```bash
|
|
51
67
|
modal run api/app.py
|
|
52
68
|
```
|
|
53
69
|
|
|
54
|
-
|
|
70
|
+
Run the API "locally":
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
modal serve --env=dev api/app.py
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Deploy on dev:
|
|
55
77
|
|
|
56
78
|
```bash
|
|
57
|
-
modal deploy api/app.py
|
|
79
|
+
modal deploy --env=dev api/app.py
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Deploy on main:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
modal deploy --env=main api/app.py
|
|
58
86
|
```
|
|
59
87
|
|
|
60
88
|
### Frontend
|
|
61
89
|
|
|
62
|
-
Run the web app:
|
|
90
|
+
Run the web app "locally":
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
modal serve --env=dev frontend/app.py
|
|
94
|
+
stripe listen --forward-to <url>/webhook
|
|
95
|
+
# update API_URL, STRIPE_WEBHOOK_SECRET, and DOMAIN in .env.dev
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Deploy on dev:
|
|
63
99
|
|
|
64
100
|
```bash
|
|
65
|
-
modal
|
|
101
|
+
modal deploy --env=dev frontend/app.py
|
|
102
|
+
# update API_URL, STRIPE_WEBHOOK_SECRET, and DOMAIN in .env.dev
|
|
66
103
|
```
|
|
67
104
|
|
|
68
|
-
Deploy:
|
|
105
|
+
Deploy on main:
|
|
69
106
|
|
|
70
107
|
```bash
|
|
71
|
-
modal deploy frontend/app.py
|
|
108
|
+
modal deploy --env=main frontend/app.py
|
|
72
109
|
```
|
|
73
110
|
|
|
74
111
|
### PyPI
|
|
@@ -77,6 +114,7 @@ Run the package:
|
|
|
77
114
|
|
|
78
115
|
```bash
|
|
79
116
|
uv run formless -v
|
|
117
|
+
# update API_URL in src/formless/__init__.py
|
|
80
118
|
```
|
|
81
119
|
|
|
82
120
|
Build the package:
|
|
@@ -5,9 +5,12 @@ from pathlib import Path
|
|
|
5
5
|
from uuid import uuid4
|
|
6
6
|
|
|
7
7
|
import modal
|
|
8
|
+
from fastapi import FastAPI, HTTPException, Request, Security
|
|
9
|
+
from fastapi.security import APIKeyHeader
|
|
8
10
|
from PIL import ImageFile
|
|
9
11
|
|
|
10
12
|
from utils import (
|
|
13
|
+
DATA_VOLUME,
|
|
11
14
|
DEFAULT_IMG_URL,
|
|
12
15
|
GPU_IMAGE,
|
|
13
16
|
MINUTES,
|
|
@@ -16,11 +19,11 @@ from utils import (
|
|
|
16
19
|
Colors,
|
|
17
20
|
)
|
|
18
21
|
|
|
22
|
+
parent_path: Path = Path(__file__).parent
|
|
19
23
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
|
20
|
-
# -----------------------------------------------------------------------------
|
|
21
24
|
|
|
25
|
+
# -----------------------------------------------------------------------------
|
|
22
26
|
|
|
23
|
-
only_download = False # turn off gpu for initial download
|
|
24
27
|
model = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
|
25
28
|
gpu_memory_utilization = 0.90
|
|
26
29
|
max_model_len = 8192
|
|
@@ -29,7 +32,7 @@ enforce_eager = True
|
|
|
29
32
|
|
|
30
33
|
question = "What is the content of this image?"
|
|
31
34
|
temperature = 0.2
|
|
32
|
-
max_tokens =
|
|
35
|
+
max_tokens = 1024
|
|
33
36
|
|
|
34
37
|
# -----------------------------------------------------------------------------
|
|
35
38
|
|
|
@@ -44,12 +47,29 @@ config = {k: str(v) if isinstance(v, Path) else v for k, v in config.items()} #
|
|
|
44
47
|
# -----------------------------------------------------------------------------
|
|
45
48
|
|
|
46
49
|
|
|
50
|
+
# container build-time fns
|
|
51
|
+
def download_model():
|
|
52
|
+
from huggingface_hub import login, snapshot_download
|
|
53
|
+
|
|
54
|
+
login(token=os.getenv("HF_TOKEN"), new_session=False)
|
|
55
|
+
snapshot_download(
|
|
56
|
+
config["model"],
|
|
57
|
+
ignore_patterns=["*.pt", "*.bin"],
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
47
61
|
# Modal
|
|
62
|
+
in_prod: bool = os.getenv("MODAL_ENVIRONMENT", "dev") == "main"
|
|
63
|
+
SECRETS = [modal.Secret.from_dotenv(path=parent_path, filename=".env" if in_prod else ".env.dev")]
|
|
48
64
|
IMAGE = GPU_IMAGE.pip_install( # add Python dependencies
|
|
49
|
-
"vllm==0.6.2", "term-image==0.7.2"
|
|
65
|
+
"vllm==0.6.2", "term-image==0.7.2", "python-fasthtml==0.6.10", "sqlite-utils==3.18"
|
|
66
|
+
).run_function(
|
|
67
|
+
download_model,
|
|
68
|
+
secrets=SECRETS,
|
|
69
|
+
volumes=VOLUME_CONFIG,
|
|
50
70
|
)
|
|
51
71
|
API_TIMEOUT = 5 * MINUTES
|
|
52
|
-
API_CONTAINER_IDLE_TIMEOUT =
|
|
72
|
+
API_CONTAINER_IDLE_TIMEOUT = 1 * MINUTES # max
|
|
53
73
|
API_ALLOW_CONCURRENT_INPUTS = 1000 # max
|
|
54
74
|
|
|
55
75
|
GPU_TYPE = "H100"
|
|
@@ -65,55 +85,58 @@ app = modal.App(name=APP_NAME)
|
|
|
65
85
|
# -----------------------------------------------------------------------------
|
|
66
86
|
|
|
67
87
|
|
|
68
|
-
#
|
|
69
|
-
@app.
|
|
88
|
+
# Main API
|
|
89
|
+
@app.function(
|
|
70
90
|
image=IMAGE,
|
|
71
|
-
gpu=
|
|
91
|
+
gpu=GPU_CONFIG,
|
|
72
92
|
volumes=VOLUME_CONFIG,
|
|
73
|
-
secrets=
|
|
93
|
+
secrets=SECRETS,
|
|
74
94
|
timeout=API_TIMEOUT,
|
|
75
95
|
container_idle_timeout=API_CONTAINER_IDLE_TIMEOUT,
|
|
76
96
|
allow_concurrent_inputs=API_ALLOW_CONCURRENT_INPUTS,
|
|
77
97
|
)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
98
|
+
@modal.asgi_app()
|
|
99
|
+
def modal_get():
|
|
100
|
+
import requests
|
|
101
|
+
from fasthtml import common as fh
|
|
102
|
+
from PIL import Image
|
|
103
|
+
from term_image.image import from_file
|
|
104
|
+
from vllm import LLM, SamplingParams
|
|
105
|
+
|
|
106
|
+
f_app = FastAPI()
|
|
107
|
+
|
|
108
|
+
db_path = f"/{DATA_VOLUME}/main.db"
|
|
109
|
+
tables = fh.database(db_path).t
|
|
110
|
+
api_keys = tables.api_keys
|
|
111
|
+
if api_keys not in tables:
|
|
112
|
+
api_keys.create(api_key=str, pk="api_key")
|
|
113
|
+
ApiKey = api_keys.dataclass()
|
|
114
|
+
|
|
115
|
+
llm = LLM(
|
|
116
|
+
model=config["model"],
|
|
117
|
+
gpu_memory_utilization=config["gpu_memory_utilization"],
|
|
118
|
+
max_model_len=config["max_model_len"],
|
|
119
|
+
max_num_seqs=config["max_num_seqs"],
|
|
120
|
+
enforce_eager=config["enforce_eager"],
|
|
121
|
+
tensor_parallel_size=GPU_COUNT,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
async def verify_api_key(
|
|
125
|
+
api_key_header: str = Security(APIKeyHeader(name="X-API-Key")),
|
|
126
|
+
) -> bool:
|
|
127
|
+
if api_key_header in api_keys:
|
|
128
|
+
return True
|
|
129
|
+
raise HTTPException(status_code=401, detail="Could not validate credentials")
|
|
130
|
+
|
|
131
|
+
@f_app.post("/")
|
|
132
|
+
async def main(request: Request, api_key: bool = Security(verify_api_key)) -> str:
|
|
133
|
+
body = await request.json()
|
|
111
134
|
|
|
112
135
|
start = time.monotonic_ns()
|
|
113
136
|
request_id = uuid4()
|
|
114
137
|
print(f"Generating response to request {request_id}")
|
|
115
138
|
|
|
116
|
-
image_url =
|
|
139
|
+
image_url = body.get("image_url")
|
|
117
140
|
response = requests.get(image_url, stream=True)
|
|
118
141
|
response.raise_for_status()
|
|
119
142
|
image = Image.open(response.raw).convert("RGB")
|
|
@@ -131,10 +154,11 @@ class Model:
|
|
|
131
154
|
"multi_modal_data": {"image": image},
|
|
132
155
|
}
|
|
133
156
|
|
|
134
|
-
outputs =
|
|
157
|
+
outputs = llm.generate(inputs, sampling_params=sampling_params)
|
|
135
158
|
generated_text = outputs[0].outputs[0].text.strip()
|
|
136
159
|
|
|
137
160
|
# show the question, image, and response in the terminal for demonstration purposes
|
|
161
|
+
response = requests.get(image_url)
|
|
138
162
|
image_filename = image_url.split("/")[-1]
|
|
139
163
|
image_path = os.path.join(tempfile.gettempdir(), f"{uuid4()}-{image_filename}")
|
|
140
164
|
with open(image_path, "wb") as file:
|
|
@@ -152,6 +176,14 @@ class Model:
|
|
|
152
176
|
|
|
153
177
|
return generated_text
|
|
154
178
|
|
|
179
|
+
@f_app.post("/api-key")
|
|
180
|
+
async def apikey() -> str:
|
|
181
|
+
api_key = str(uuid4())
|
|
182
|
+
api_keys.insert(ApiKey(api_key=api_key))
|
|
183
|
+
return api_key
|
|
184
|
+
|
|
185
|
+
return f_app
|
|
186
|
+
|
|
155
187
|
|
|
156
188
|
## For testing
|
|
157
189
|
@app.local_entrypoint()
|
|
@@ -160,12 +192,14 @@ def main(
|
|
|
160
192
|
):
|
|
161
193
|
import requests
|
|
162
194
|
|
|
163
|
-
|
|
195
|
+
response = requests.post(f"{modal_get.web_url}/api-key")
|
|
196
|
+
assert response.ok, response.status_code
|
|
197
|
+
api_key = response.json()
|
|
164
198
|
|
|
165
|
-
response = requests.post(
|
|
199
|
+
response = requests.post(modal_get.web_url, json={"image_url": DEFAULT_IMG_URL}, headers={"X-API-Key": api_key})
|
|
166
200
|
assert response.ok, response.status_code
|
|
167
201
|
|
|
168
202
|
if twice:
|
|
169
203
|
# second response is faster, because the Function is already running
|
|
170
|
-
response = requests.post(
|
|
204
|
+
response = requests.post(modal_get.web_url, json={"image_url": DEFAULT_IMG_URL}, headers={"X-API-Key": api_key})
|
|
171
205
|
assert response.ok, response.status_code
|