-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
276 lines (225 loc) · 8.34 KB
/
utils.py
File metadata and controls
276 lines (225 loc) · 8.34 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""Shared utilities for the LandingAI ADE FiftyOne plugin."""
import os
from functools import lru_cache
from pathlib import Path
from typing import Optional
import fiftyone.core.labels as fol
import fiftyone.operators.types as types
try:
from landingai_ade import LandingAIADE as _LandingAIADE
except ImportError:
_LandingAIADE = None
ADE_SUPPORTED_EXTENSIONS = frozenset({
".pdf", ".docx", ".doc", ".odt",
".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".tif", ".webp",
".gif", ".apng", ".dcx", ".dds", ".dib", ".gd", ".icns",
".jp2", ".pcx", ".ppm", ".psd", ".tga",
".xlsx", ".csv",
".ppt", ".pptx",
})
_API_KEY_NAME = "VISION_AGENT_API_KEY"
@lru_cache(maxsize=1)
def _read_dotenv_api_key() -> Optional[str]:
"""Read ``VISION_AGENT_API_KEY`` from ``.env`` in the current directory."""
env_path = Path.cwd() / ".env"
if not env_path.is_file():
return None
try:
for raw_line in env_path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
key = key.strip()
if key.startswith("export "):
key = key[len("export "):].strip()
if key != _API_KEY_NAME:
continue
value = value.strip()
if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
value = value[1:-1]
if value:
return value
except OSError:
return None
return None
def _resolve_api_key(ctx) -> Optional[str]:
"""Resolve ``VISION_AGENT_API_KEY`` from secrets, env, or ``.env``."""
secrets = getattr(ctx, "secrets", {}) or {}
return (
secrets.get(_API_KEY_NAME)
or os.getenv(_API_KEY_NAME)
or _read_dotenv_api_key()
)
def get_api_key(ctx) -> str:
"""Resolve the LandingAI API key from FiftyOne secrets or environment.
Priority: FiftyOne secrets, then environment variables, then ``.env`` in the
current working directory. The plugin uses ``VISION_AGENT_API_KEY`` only.
"""
api_key = _resolve_api_key(ctx)
if not api_key:
raise ValueError(
"No LandingAI API key found. "
"Set VISION_AGENT_API_KEY in your environment or .env file, "
"or add it to FiftyOne secrets."
)
return api_key
def get_client(api_key: str, region: str = "us"):
"""Return an authenticated ``LandingAIADE`` client for the given region.
Args:
api_key: LandingAI / Vision Agent API key.
region: ``"us"`` (default) or ``"eu"``.
"""
if _LandingAIADE is None:
raise ImportError(
"The 'landingai-ade' package is required. "
"Install it with:\n\n"
" pip install landingai-ade\n\n"
"or run:\n\n"
" fiftyone plugins requirements @landingai/ade --install"
)
environment = "eu" if region == "eu" else "production"
return _LandingAIADE(apikey=api_key, environment=environment)
def _clamp(v: float) -> float:
return max(0.0, min(1.0, float(v)))
def ade_box_to_fo(box) -> list:
"""Convert an ADE bounding box to FiftyOne ``[x, y, w, h]`` format.
ADE uses ``[left, top, right, bottom]`` normalized 0–1.
FiftyOne uses ``[x, y, width, height]`` from the top-left, normalized 0–1.
"""
return [
_clamp(box.left),
_clamp(box.top),
_clamp(box.right - box.left),
_clamp(box.bottom - box.top),
]
def grounding_to_detections(grounding: dict) -> Optional[fol.Detections]:
"""Convert a ``parse_resp.grounding`` dict to ``fo.Detections``.
Covers all element types — top-level chunks (text, table, figure) and
individual table cells — each stored with ``chunk_id`` and ``page`` so
bbox lookup works for every extracted field, including numeric ones.
"""
detections = []
for element_id, g in (grounding or {}).items():
if not g or not g.box:
continue
detections.append(
fol.Detection(
label=g.type or "unknown",
bounding_box=ade_box_to_fo(g.box),
chunk_id=element_id,
page=int(g.page) if g.page is not None else 0,
)
)
return fol.Detections(detections=detections) if detections else None
def to_plain_data(value):
"""Convert SDK response objects into plain Python containers."""
if value is None or isinstance(value, (str, int, float, bool)):
return value
if isinstance(value, dict):
return {str(k): to_plain_data(v) for k, v in value.items()}
if isinstance(value, (list, tuple, set)):
return [to_plain_data(v) for v in value]
if hasattr(value, "model_dump"):
return to_plain_data(value.model_dump())
if hasattr(value, "dict"):
return to_plain_data(value.dict())
if hasattr(value, "__dict__"):
return {
k: to_plain_data(v)
for k, v in vars(value).items()
if not k.startswith("_")
}
return str(value)
def filter_ade_samples(view) -> list:
"""Return samples whose filepath has an ADE-supported extension."""
return [
s for s in view
if os.path.splitext(s.filepath)[1].lower() in ADE_SUPPORTED_EXTENSIONS
]
def check_api_key(inputs, ctx) -> bool:
"""Show an error notice and return ``False`` if no API key is configured.
Call at the top of ``resolve_input``. If it returns ``False``, return
``types.Property(inputs, invalid=True)`` immediately.
"""
key = _resolve_api_key(ctx)
if not key:
inputs.view(
"no_api_key_error",
types.Notice(
label=(
"LandingAI API key not found. "
"Set VISION_AGENT_API_KEY in your environment or .env file and restart FiftyOne, "
"or add it to your FiftyOne secrets config."
)
),
)
return False
return True
def add_model_input(inputs):
"""Add the parse model selector using the docs-style latest aliases."""
model_choices = types.RadioGroup()
model_choices.add_choice(
"dpt-2-latest",
label="dpt-2-latest (Full featured — 3 credits/page)",
)
model_choices.add_choice(
"dpt-2-mini-latest",
label="dpt-2-mini-latest (Simple docs, preview — 1.5 credits/page)",
)
inputs.enum(
"model",
values=model_choices.values(),
label="Model",
default="dpt-2-latest",
required=True,
view=model_choices,
)
def add_extract_model_input(inputs):
"""Add the extraction model selector."""
extract_choices = types.Dropdown()
extract_choices.add_choice("extract-latest", label="extract-latest (Latest extraction model)")
extract_choices.add_choice("extract-20260314", label="extract-20260314 (March 2026)")
inputs.enum(
"extract_model",
values=extract_choices.values(),
label="Extraction model",
default="extract-latest",
required=True,
view=extract_choices,
)
def add_split_model_input(inputs):
"""Add the split model selector."""
split_choices = types.Dropdown()
split_choices.add_choice("split-latest", label="split-latest (Latest split model)")
split_choices.add_choice("split-20251105", label="split-20251105 (Pinned snapshot)")
inputs.enum(
"split_model",
values=split_choices.values(),
label="Split model",
default="split-latest",
required=True,
view=split_choices,
)
def add_password_input(inputs):
"""Add an optional password input for encrypted documents."""
inputs.str(
"password",
label="Document password",
description=(
"Password for password-protected files. Requires a ZDR-enabled account. "
"Ignored for unencrypted documents."
),
)
def add_region_input(inputs):
"""Add the region selector (US vs EU endpoint)."""
region_choices = types.Dropdown()
region_choices.add_choice("us", label="US (api.va.landing.ai)")
region_choices.add_choice("eu", label="EU (api.va.eu-west-1.landing.ai)")
inputs.enum(
"region",
values=region_choices.values(),
label="Region",
default="us",
view=region_choices,
)