formless 0.1.0__py3-none-any.whl
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/__init__.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import traceback
|
|
2
|
+
import os
|
|
3
|
+
import tempfile
|
|
4
|
+
import time
|
|
5
|
+
from uuid import uuid4
|
|
6
|
+
from dotenv import load_dotenv
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from term_image.image import from_file
|
|
9
|
+
import requests
|
|
10
|
+
from rich import print
|
|
11
|
+
from rich.progress import Progress, SpinnerColumn, TextColumn
|
|
12
|
+
import typer
|
|
13
|
+
from typing_extensions import Annotated
|
|
14
|
+
|
|
15
|
+
from utils import DEFAULT_IMG_URL
|
|
16
|
+
|
|
17
|
+
parent_path = Path(__file__).parent.parent.parent
|
|
18
|
+
load_dotenv(parent_path / ".env")
|
|
19
|
+
|
|
20
|
+
# Typer CLI
|
|
21
|
+
app = typer.Typer(
|
|
22
|
+
rich_markup_mode="rich",
|
|
23
|
+
)
|
|
24
|
+
state = {"verbose": False}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Fns
|
|
28
|
+
def run() -> None:
|
|
29
|
+
api_url = os.getenv("API_URL")
|
|
30
|
+
image_url = state["image_url"]
|
|
31
|
+
response = requests.post(api_url, json={"image_url": image_url})
|
|
32
|
+
assert response.ok, response.status_code
|
|
33
|
+
return response.json()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@app.command(
|
|
37
|
+
help="Handwritten + image OCR.",
|
|
38
|
+
epilog="Made by [bold blue]Andrew Hinh.[/bold blue] :mechanical_arm::person_climbing:",
|
|
39
|
+
context_settings={"allow_extra_args": False, "ignore_unknown_options": True},
|
|
40
|
+
)
|
|
41
|
+
def main(
|
|
42
|
+
image_url: Annotated[
|
|
43
|
+
str, typer.Option("--image-url", "-i", help="Image URL", rich_help_panel="Inputs")
|
|
44
|
+
] = DEFAULT_IMG_URL,
|
|
45
|
+
verbose: Annotated[
|
|
46
|
+
int, typer.Option("--verbose", "-v", count=True, help="Verbose mode", rich_help_panel="General")
|
|
47
|
+
] = 0,
|
|
48
|
+
):
|
|
49
|
+
try:
|
|
50
|
+
start = time.monotonic_ns()
|
|
51
|
+
request_id = uuid4()
|
|
52
|
+
|
|
53
|
+
state.update(
|
|
54
|
+
{
|
|
55
|
+
"image_url": image_url,
|
|
56
|
+
"verbose": verbose > 0,
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
if state["verbose"]:
|
|
61
|
+
response = requests.get(image_url)
|
|
62
|
+
image_filename = image_url.split("/")[-1]
|
|
63
|
+
image_path = os.path.join(tempfile.gettempdir(), f"{uuid4()}-{image_filename}")
|
|
64
|
+
with open(image_path, "wb") as file:
|
|
65
|
+
file.write(response.content)
|
|
66
|
+
terminal_image = from_file(image_path)
|
|
67
|
+
terminal_image.draw()
|
|
68
|
+
|
|
69
|
+
if state["verbose"]:
|
|
70
|
+
print("[red]Press[/red] [blue]Ctrl+C[/blue] [red]to stop at any time.[/red]")
|
|
71
|
+
with Progress(
|
|
72
|
+
SpinnerColumn(), TextColumn("[progress.description]{task.description}"), transient=True
|
|
73
|
+
) as progress:
|
|
74
|
+
progress.add_task(f"Generating response to request {request_id}", total=None)
|
|
75
|
+
generated_text = run()
|
|
76
|
+
else:
|
|
77
|
+
generated_text = run()
|
|
78
|
+
print(f"[bold green]{generated_text}[/bold green]")
|
|
79
|
+
|
|
80
|
+
if state["verbose"]:
|
|
81
|
+
print(
|
|
82
|
+
f"[red]request[/red] [blue]{request_id}[/blue] [red]completed in[/red] [blue]{round((time.monotonic_ns() - start) / 1e9, 2)}[/blue] [red]seconds[/red]"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
except KeyboardInterrupt:
|
|
86
|
+
if state["verbose"]:
|
|
87
|
+
print("[red]\n\nExiting...[/red]")
|
|
88
|
+
except Exception as e:
|
|
89
|
+
if state["verbose"]:
|
|
90
|
+
print(f"[red]Failed with error: {e}[/red]")
|
|
91
|
+
print(traceback.format_exc())
|
|
92
|
+
print("[red]\n\nExiting...[/red]")
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: formless
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Handwritten + image OCR.
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: modal>=0.64.178
|
|
8
|
+
Requires-Dist: python-dotenv>=1.0.1
|
|
9
|
+
Requires-Dist: requests>=2.32.3
|
|
10
|
+
Requires-Dist: term-image>=0.7.2
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# formless
|
|
14
|
+
|
|
15
|
+
Handwritten + image OCR.
|
|
16
|
+
|
|
17
|
+
## Set Up
|
|
18
|
+
|
|
19
|
+
Set up the environment:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
uv sync --all-extras --dev
|
|
23
|
+
uv run pre-commit install
|
|
24
|
+
modal setup
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Repository Structure
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
.
|
|
31
|
+
├── api # API.
|
|
32
|
+
├── frontend # frontend.
|
|
33
|
+
├── src/formless # python bindings.
|
|
34
|
+
├── training # training.
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Development
|
|
38
|
+
|
|
39
|
+
### API
|
|
40
|
+
|
|
41
|
+
Run the API:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
modal run api/app.py
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Deploy:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
modal deploy api/app.py
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Frontend
|
|
54
|
+
|
|
55
|
+
Run the web app:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
modal serve frontend/app.py
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Deploy:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
modal deploy frontend/app.py
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### PyPI
|
|
68
|
+
|
|
69
|
+
Run the package:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
uv run formless -v
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Build the package:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
uvx --from build pyproject-build --installer uv
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Upload the package:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
uvx twine upload dist/*
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Test the uploaded package:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
uv run --with formless --no-project -- formless -vv
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Training
|
|
94
|
+
|
|
95
|
+
Run ETL on HF dataset:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
modal run ft/etl.py
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Train the model:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
modal run ft/train_modal.py
|
|
105
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
formless/__init__.py,sha256=AO1foezRnbfa9yx8y2fKMGSPfEyg0VbriZE1Yyjjlto,2975
|
|
2
|
+
formless-0.1.0.dist-info/METADATA,sha256=yiE8t5sZpBCbxUna2mCWdymHKg7YyAmwzxpotZw6pyc,1331
|
|
3
|
+
formless-0.1.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
4
|
+
formless-0.1.0.dist-info/entry_points.txt,sha256=xcu6NS8jcdeRe8rwNAa7SEuPT5sc9rCz7j0pedbLO1s,42
|
|
5
|
+
formless-0.1.0.dist-info/licenses/LICENSE,sha256=2dLETw8QTr-4rGnrlbXC6yuHZst7d8X_Q-xJ3pytOdA,1068
|
|
6
|
+
formless-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Andrew Hinh
|
|
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.
|