electblake commited on
Commit
e62015b
·
1 Parent(s): 4debd81

refactor(samples): load named JSON presets from files

Browse files
app.py CHANGED
@@ -4,205 +4,26 @@ import gradio as gr
4
 
5
  from model import extract, generate_template
6
 
 
7
  structured_json_templates = {
8
- "Basic receipt": """{
9
- "merchant": "verbatim-string",
10
- "purchase_date": "date",
11
- "purchase_time": "time",
12
- "total": "number",
13
- "currency": "currency",
14
- "item_count": "integer",
15
- "paid": "boolean",
16
- "payment_method": ["cash", "credit-card", "debit-card", "other"],
17
- "items": ["verbatim-string"]
18
- }""",
19
- "Advanced receipt": """{
20
- "merchant": {
21
- "name": "verbatim-string",
22
- "address": "verbatim-string",
23
- "phone": "phone-number"
24
- },
25
- "purchase_date": "date",
26
- "purchase_time": "time",
27
- "store_number": "verbatim-string",
28
- "operator_number": "verbatim-string",
29
- "terminal_number": "verbatim-string",
30
- "transaction_number": "verbatim-string",
31
- "items": [
32
- {
33
- "description": "verbatim-string",
34
- "sku": "verbatim-string",
35
- "quantity": "number",
36
- "unit": "unit-code",
37
- "unit_price": "number",
38
- "discount": "number",
39
- "line_total": "number",
40
- "tax_code": "verbatim-string"
41
- }
42
- ],
43
- "item_count": "integer",
44
- "subtotal": "number",
45
- "tax": "number",
46
- "total": "number",
47
- "currency": "currency",
48
- "payment_method": ["cash", "credit-card", "debit-card", "other"],
49
- "card_last_four": "verbatim-string",
50
- "amount_tendered": "number",
51
- "change_due": "number"
52
- }""",
53
- "Basic bank statement": """{
54
- "financial_institution": "verbatim-string",
55
- "account_holder": "verbatim-string",
56
- "account_number": "verbatim-string",
57
- "statement_period": {
58
- "start_date": "date",
59
- "end_date": "date"
60
- },
61
- "opening_balance": "number",
62
- "closing_balance": "number",
63
- "currency": "currency",
64
- "transaction_count": "integer",
65
- "transactions": [
66
- {
67
- "date": "date",
68
- "description": "verbatim-string",
69
- "transaction_type": ["debit", "credit", "other"],
70
- "amount": "number",
71
- "balance": "number"
72
- }
73
- ]
74
- }""",
75
- "Advanced bank statement": """{
76
- "financial_institution": {
77
- "name": "verbatim-string",
78
- "address": "verbatim-string",
79
- "phone": "phone-number",
80
- "website": "url"
81
- },
82
- "account_holder": {
83
- "name": "verbatim-string",
84
- "address": "verbatim-string"
85
- },
86
- "account": {
87
- "type": "verbatim-string",
88
- "number": "verbatim-string",
89
- "branch_number": "verbatim-string",
90
- "transit_number": "verbatim-string",
91
- "routing_number": "verbatim-string",
92
- "iban": "iban",
93
- "bic": "bic"
94
- },
95
- "statement_period": {
96
- "start_date": "date",
97
- "end_date": "date"
98
- },
99
- "summary": {
100
- "opening_balance": "number",
101
- "total_credits": "number",
102
- "total_debits": "number",
103
- "total_fees": "number",
104
- "total_interest": "number",
105
- "closing_balance": "number",
106
- "currency": "currency"
107
- },
108
- "transaction_count": "integer",
109
- "transactions": [
110
- {
111
- "transaction_date": "date",
112
- "posting_date": "date",
113
- "description": "verbatim-string",
114
- "reference_number": "verbatim-string",
115
- "category": [
116
- "deposit",
117
- "withdrawal",
118
- "transfer",
119
- "payment",
120
- "purchase",
121
- "fee",
122
- "interest",
123
- "refund",
124
- "other"
125
- ],
126
- "debit": "number",
127
- "credit": "number",
128
- "balance": "number",
129
- "currency": "currency"
130
- }
131
- ]
132
- }""",
133
- "Invoice with line items": """{
134
- "document_type": "verbatim-string",
135
- "vendor": {
136
- "name": "verbatim-string",
137
- "address": "verbatim-string",
138
- "email": "email-address",
139
- "phone": "phone-number"
140
- },
141
- "invoice_number": "verbatim-string",
142
- "invoice_date": "verbatim-string",
143
- "line_items": [
144
- {
145
- "description": "verbatim-string",
146
- "category": ["product", "service", "shipping", "tax", "other"],
147
- "quantity": "integer",
148
- "unit_price": "number",
149
- "unit": "unit-code",
150
- "amount": "number"
151
- }
152
- ],
153
- "subtotal": "number",
154
- "tax": "number",
155
- "total": "number",
156
- "currency": "currency"
157
- }""",
158
- "Advanced global record": """{
159
- "summary": "string",
160
- "reference_text": "verbatim-string",
161
- "sequence_number": "integer",
162
- "amount": "number",
163
- "effective_date": "date",
164
- "effective_time": "time",
165
- "signed_at": "date-time",
166
- "term": "duration",
167
- "active": "boolean",
168
- "country": "country",
169
- "currency": "currency",
170
- "language": "language",
171
- "language_tag": "language-tag",
172
- "script": "script",
173
- "website": "url",
174
- "email": "email-address",
175
- "phone": "phone-number",
176
- "iban": "iban",
177
- "bic": "bic",
178
- "measurement_unit": "unit-code",
179
- "regions": {
180
- "us": "region:US",
181
- "fr": "region:FR",
182
- "ie": "region:IE",
183
- "gb": "region:GB",
184
- "it": "region:IT",
185
- "es": "region:ES",
186
- "de": "region:DE",
187
- "pt": "region:PT",
188
- "ca": "region:CA",
189
- "mx": "region:MX",
190
- "br": "region:BR",
191
- "au": "region:AU",
192
- "jp": "region:JP",
193
- "kr": "region:KR"
194
- },
195
- "contacts": [
196
- {
197
- "name": "verbatim-string",
198
- "role": "string",
199
- "email": "email-address"
200
- }
201
- ],
202
- "status": ["draft", "active", "expired", "unknown"],
203
- "applicable_labels": [["legal", "financial", "technical", "personal"]],
204
- "keywords": ["string"]
205
- }""",
206
  }
207
 
208
  default_structured_json_template = "Invoice with line items"
@@ -258,8 +79,8 @@ with gr.Blocks(title="Image Data Extractor") as demo:
258
  ("Invoice with line items", "invoice-with-items.png"),
259
  ("Advanced bank statement", "BankStatementChequing.png"),
260
  ("Basic bank statement", "bank statement blog image.webp"),
261
- ("Advanced global record", "tags.png"),
262
- ("Advanced global record", "task-list.png"),
263
  ]
264
  ],
265
  inputs=[template_preset, image, text, enable_thinking],
 
4
 
5
  from model import extract, generate_template
6
 
7
+ templates_directory = Path(__file__).parent / "data" / "templates"
8
  structured_json_templates = {
9
+ "Basic receipt": (templates_directory / "basic-receipt.json").read_text(
10
+ encoding="utf-8"
11
+ ),
12
+ "Invoice with line items": (
13
+ templates_directory / "invoice-with-line-items.json"
14
+ ).read_text(encoding="utf-8"),
15
+ "Basic bank statement": (
16
+ templates_directory / "basic-bank-statement.json"
17
+ ).read_text(encoding="utf-8"),
18
+ "Advanced bank statement": (
19
+ templates_directory / "advanced-bank-statement.json"
20
+ ).read_text(encoding="utf-8"),
21
+ "Model task catalog": (
22
+ templates_directory / "model-task-catalog.json"
23
+ ).read_text(encoding="utf-8"),
24
+ "Todo list": (templates_directory / "todo-list.json").read_text(
25
+ encoding="utf-8"
26
+ ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  }
28
 
29
  default_structured_json_template = "Invoice with line items"
 
79
  ("Invoice with line items", "invoice-with-items.png"),
80
  ("Advanced bank statement", "BankStatementChequing.png"),
81
  ("Basic bank statement", "bank statement blog image.webp"),
82
+ ("Model task catalog", "tags.png"),
83
+ ("Todo list", "task-list.png"),
84
  ]
85
  ],
86
  inputs=[template_preset, image, text, enable_thinking],
data/samples/Screenshot 2025-06-25 003204.png DELETED

Git LFS Details

  • SHA256: 9688cb8030429bde6f4f8dc096767c77585853b21a4cfa7c02748a6e5311911f
  • Pointer size: 131 Bytes
  • Size of remote file: 239 kB
data/samples/Screenshot 2026-06-01 150055.png DELETED

Git LFS Details

  • SHA256: 32b6c3e0a4dd8946c9b2eccc88dfb1acf542ac3a233818decb754bbb4ce54136
  • Pointer size: 132 Bytes
  • Size of remote file: 1.23 MB
data/samples/Screenshot 2026-07-15 163643.png DELETED

Git LFS Details

  • SHA256: 2948fb218c36dd1aa3569ee50b5a2a2fb342205354899fb70aa5c26bc195f363
  • Pointer size: 131 Bytes
  • Size of remote file: 255 kB
data/samples/{Screenshot 2026-07-18 171416.png → tags.png} RENAMED
File without changes
data/samples/{Screenshot 2026-05-27 175656.png → task-list.png} RENAMED
File without changes
data/templates/advanced-bank-statement.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "financial_institution": {
3
+ "name": "verbatim-string",
4
+ "address": "verbatim-string",
5
+ "phone": "phone-number",
6
+ "website": "url"
7
+ },
8
+ "account_holder": {
9
+ "name": "verbatim-string",
10
+ "address": "verbatim-string"
11
+ },
12
+ "account": {
13
+ "type": "verbatim-string",
14
+ "number": "verbatim-string",
15
+ "branch_number": "verbatim-string",
16
+ "transit_number": "verbatim-string",
17
+ "routing_number": "verbatim-string",
18
+ "iban": "iban",
19
+ "bic": "bic"
20
+ },
21
+ "statement_period": {
22
+ "start_date": "date",
23
+ "end_date": "date"
24
+ },
25
+ "summary": {
26
+ "opening_balance": "number",
27
+ "total_credits": "number",
28
+ "total_debits": "number",
29
+ "total_fees": "number",
30
+ "total_interest": "number",
31
+ "closing_balance": "number",
32
+ "currency": "currency"
33
+ },
34
+ "transaction_count": "integer",
35
+ "transactions": [
36
+ {
37
+ "transaction_date": "date",
38
+ "posting_date": "date",
39
+ "description": "verbatim-string",
40
+ "reference_number": "verbatim-string",
41
+ "category": [
42
+ "deposit",
43
+ "withdrawal",
44
+ "transfer",
45
+ "payment",
46
+ "purchase",
47
+ "fee",
48
+ "interest",
49
+ "refund",
50
+ "other"
51
+ ],
52
+ "debit": "number",
53
+ "credit": "number",
54
+ "balance": "number",
55
+ "currency": "currency"
56
+ }
57
+ ]
58
+ }
data/templates/basic-bank-statement.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "financial_institution": "verbatim-string",
3
+ "account_holder": "verbatim-string",
4
+ "account_number": "verbatim-string",
5
+ "statement_period": {
6
+ "start_date": "date",
7
+ "end_date": "date"
8
+ },
9
+ "opening_balance": "number",
10
+ "closing_balance": "number",
11
+ "currency": "currency",
12
+ "transaction_count": "integer",
13
+ "transactions": [
14
+ {
15
+ "date": "date",
16
+ "description": "verbatim-string",
17
+ "transaction_type": ["debit", "credit", "other"],
18
+ "amount": "number",
19
+ "balance": "number"
20
+ }
21
+ ]
22
+ }
data/templates/basic-receipt.json ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "merchant": "verbatim-string",
3
+ "purchase_date": "date",
4
+ "purchase_time": "time",
5
+ "total": "number",
6
+ "currency": "currency",
7
+ "item_count": "integer",
8
+ "paid": "boolean",
9
+ "payment_method": ["cash", "credit-card", "debit-card", "other"],
10
+ "items": ["verbatim-string"]
11
+ }
data/templates/generated-bank-statement-template.json DELETED
@@ -1,40 +0,0 @@
1
- {
2
- "account_holder": {
3
- "address": {
4
- "street_address": "string",
5
- "postal_code": "string",
6
- "city": "verbatim-string",
7
- "province": "region:CA"
8
- },
9
- "name": "verbatim-string"
10
- },
11
- "statement_period": {
12
- "end_date": "date",
13
- "start_date": "date"
14
- },
15
- "account_number": "string",
16
- "bank_details": {
17
- "name": "verbatim-string",
18
- "address": {
19
- "street_address": "verbatim-string",
20
- "postal_code": "verbatim-string",
21
- "city": "verbatim-string",
22
- "province": "region:CA"
23
- },
24
- "phone_number": "phone-number"
25
- },
26
- "transactions": [
27
- {
28
- "description": "string",
29
- "date": "date",
30
- "withdrawals": "number",
31
- "balance": "number",
32
- "ref": "verbatim-string",
33
- "deposits": "number"
34
- }
35
- ],
36
- "totals": {
37
- "withdrawals": "number",
38
- "deposits": "number"
39
- }
40
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
data/templates/invoice-with-line-items.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "document_type": "verbatim-string",
3
+ "vendor": {
4
+ "name": "verbatim-string",
5
+ "address": "verbatim-string",
6
+ "email": "email-address",
7
+ "phone": "phone-number"
8
+ },
9
+ "invoice_number": "verbatim-string",
10
+ "invoice_date": "verbatim-string",
11
+ "line_items": [
12
+ {
13
+ "description": "verbatim-string",
14
+ "category": ["product", "service", "shipping", "tax", "other"],
15
+ "quantity": "integer",
16
+ "unit_price": "number",
17
+ "unit": "unit-code",
18
+ "amount": "number"
19
+ }
20
+ ],
21
+ "subtotal": "number",
22
+ "tax": "number",
23
+ "total": "number",
24
+ "currency": "currency"
25
+ }
data/templates/model-task-catalog.json ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "Image Generation": [
3
+ [
4
+ "Text to Image",
5
+ "Image to Text",
6
+ "Image-to-Image",
7
+ "Image-to-Video",
8
+ "Unconditional Image Generation",
9
+ "Video Classification",
10
+ "Text-to-Video",
11
+ "Zero-Shot Image Classification",
12
+ "Mask Generation",
13
+ "Zero-Shot Object Detection",
14
+ "Text-to-3D",
15
+ "Image-to-3D",
16
+ "Image Feature Extraction",
17
+ "Keypoint Detection",
18
+ "Video-to-Video"
19
+ ]
20
+ ],
21
+ "Natural Language Processing": [
22
+ [
23
+ "Text Classification",
24
+ "Token Classification",
25
+ "Table Question Answering",
26
+ "Question Answering",
27
+ "Zero-Shot Classification",
28
+ "Translation",
29
+ "Summarization",
30
+ "Feature Extraction",
31
+ "Text Generation",
32
+ "Fill-Mask",
33
+ "Sentence Similarity",
34
+ "Text Ranking"
35
+ ]
36
+ ],
37
+ "Audio": [
38
+ [
39
+ "Text-to-Speech",
40
+ "Text-to-Audio",
41
+ "Automatic Speech Recognition",
42
+ "Audio-to-Audio",
43
+ "Audio Classification",
44
+ "Voice Activity Detection"
45
+ ]
46
+ ],
47
+ "Tabular": [
48
+ ["Tabular Classification", "Tabular Regression", "Time Series Forecasting"]
49
+ ],
50
+ "Reinforcement Learning": [["Reinforcement Learning", "Robotics"]],
51
+ "Other": [["Graph Machine Learning"]]
52
+ }
data/templates/todo-list.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "todos": [
3
+ {
4
+ "status": ["completed", "pending"],
5
+ "description": "string",
6
+ "related_file": "verbatim-string",
7
+ "related_cell": "verbatim-string"
8
+ }
9
+ ]
10
+ }