-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_document.py
More file actions
189 lines (157 loc) · 6.22 KB
/
parse_document.py
File metadata and controls
189 lines (157 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""Synchronous document parse operator for the LandingAI ADE FiftyOne plugin."""
from pathlib import Path
import fiftyone.operators as foo
import fiftyone.operators.types as types
try:
from .utils import (
add_model_input,
add_password_input,
add_region_input,
check_api_key,
grounding_to_detections,
filter_ade_samples,
get_api_key,
get_client,
)
except ImportError:
from utils import (
add_model_input,
add_password_input,
add_region_input,
check_api_key,
grounding_to_detections,
filter_ade_samples,
get_api_key,
get_client,
)
class ADEParseDocument(foo.Operator):
"""Parse selected documents to structured Markdown with spatial grounding.
Converts PDFs, images, spreadsheets, and Office files into Markdown stored
as a dataset field, with bounding box grounding as ``fo.Detections``.
"""
@property
def config(self):
return foo.OperatorConfig(
name="ade_parse_document",
label="ADE: Parse Document",
description=(
"Convert selected documents to structured Markdown with spatial "
"grounding using LandingAI ADE."
),
dynamic=True,
allow_immediate_execution=True,
allow_delegated_execution=True,
)
def resolve_input(self, ctx):
inputs = types.Object()
if not check_api_key(inputs, ctx):
return types.Property(inputs, invalid=True)
inputs.view_target(ctx)
add_model_input(inputs)
add_region_input(inputs)
add_password_input(inputs)
inputs.str(
"result_field",
label="Output field (Markdown)",
description="Dataset field where parsed Markdown will be stored.",
default="ade_parse",
required=True,
)
inputs.bool(
"store_grounding",
label="Store spatial grounding",
description=(
"Store bounding box coordinates for each document element "
"(text blocks, tables, figures) as fo.Detections."
),
default=True,
)
if ctx.params.get("store_grounding", True):
inputs.str(
"grounding_field",
label="Output field (Grounding Detections)",
description="Dataset field where fo.Detections grounding boxes will be stored.",
default="ade_grounding",
required=True,
)
return types.Property(inputs)
def execute(self, ctx):
api_key = get_api_key(ctx)
region = ctx.params.get("region", "us")
model = ctx.params.get("model", "dpt-2-latest")
password = (ctx.params.get("password") or "").strip()
result_field = ctx.params.get("result_field", "ade_parse")
store_grounding = ctx.params.get("store_grounding", True)
grounding_field = ctx.params.get("grounding_field", "ade_grounding")
client = get_client(api_key, region)
samples = filter_ade_samples(ctx.target_view())
total = len(samples)
if total == 0:
return {
"processed": 0,
"total": 0,
"errors": [],
"message": "No supported documents found among the selected samples.",
}
processed = 0
errors = []
for i, sample in enumerate(samples):
ctx.set_progress(progress=i / total, label=f"Parsing {i + 1}/{total}…")
try:
parse_kwargs = {"document": Path(sample.filepath), "model": model}
if password:
parse_kwargs["password"] = password
response = client.parse(**parse_kwargs)
version = getattr(response.metadata, "version", None)
sample[result_field] = response.markdown
sample[f"{result_field}_metadata"] = {
"page_count": response.metadata.page_count,
"credit_usage": float(response.metadata.credit_usage or 0),
"filename": response.metadata.filename,
"duration_ms": response.metadata.duration_ms,
"version": version,
"model_version": version,
}
if store_grounding and response.grounding:
detections = grounding_to_detections(response.grounding)
if detections:
sample[grounding_field] = detections
sample.save()
processed += 1
except Exception as e:
errors.append({"filepath": sample.filepath, "error": str(e)})
ctx.trigger("reload_dataset")
return {
"processed": processed,
"total": total,
"errors": errors[:5],
"error_count": len(errors),
"result_field": result_field,
"grounding_field": grounding_field if store_grounding else None,
}
def resolve_output(self, ctx):
outputs = types.Object()
result = ctx.results or {}
processed = result.get("processed", 0)
total = result.get("total", 0)
errors = result.get("errors", [])
error_count = result.get("error_count", len(errors))
result_field = result.get("result_field", "ade_parse")
grounding_field = result.get("grounding_field")
message = result.get("message", "")
if message:
outputs.view("notice", types.Notice(label=message))
return types.Property(outputs)
summary = f"Processed {processed}/{total} samples. Markdown stored in '{result_field}'."
if grounding_field:
summary += f" Grounding stored in '{grounding_field}'."
if error_count:
summary += f" {error_count} error(s) — see below."
outputs.view("summary", types.Notice(label=summary))
for i, err in enumerate(errors):
outputs.str(
f"error_{i}",
label=f"Error: {err.get('filepath', 'unknown')}",
default=err.get("error", ""),
)
return types.Property(outputs)