easyannotate 0.4.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.
Files changed (109) hide show
  1. data_factory/__init__.py +82 -0
  2. data_factory/__main__.py +6 -0
  3. data_factory/annotate/__init__.py +15 -0
  4. data_factory/annotate/attach.py +398 -0
  5. data_factory/annotate/demo.py +161 -0
  6. data_factory/annotate/images.py +443 -0
  7. data_factory/annotate/labels.py +399 -0
  8. data_factory/annotate/loop.py +359 -0
  9. data_factory/annotate/projects.py +207 -0
  10. data_factory/annotate/review.py +471 -0
  11. data_factory/annotate/schema.sql +171 -0
  12. data_factory/annotate/shapes.py +488 -0
  13. data_factory/annotate/store.py +292 -0
  14. data_factory/annotate/tools/__init__.py +39 -0
  15. data_factory/annotate/tools/fake.py +170 -0
  16. data_factory/annotate/tools/install.py +254 -0
  17. data_factory/annotate/tools/local.py +389 -0
  18. data_factory/annotate/tools/postprocess.py +175 -0
  19. data_factory/annotate/tools/protocol.py +238 -0
  20. data_factory/annotate/tools/registry.py +196 -0
  21. data_factory/annotate/tools/results.py +83 -0
  22. data_factory/annotate/tools/serve.py +106 -0
  23. data_factory/cli/__init__.py +21 -0
  24. data_factory/cli/main.py +837 -0
  25. data_factory/cli/menu.py +173 -0
  26. data_factory/cli/render.py +158 -0
  27. data_factory/core/__init__.py +11 -0
  28. data_factory/core/annotations/__init__.py +5 -0
  29. data_factory/core/annotations/coco.py +394 -0
  30. data_factory/core/annotations/csvfmt.py +174 -0
  31. data_factory/core/annotations/dota.py +98 -0
  32. data_factory/core/annotations/formats.py +626 -0
  33. data_factory/core/annotations/labelme.py +153 -0
  34. data_factory/core/annotations/model.py +334 -0
  35. data_factory/core/annotations/voc.py +231 -0
  36. data_factory/core/annotations/yolo.py +283 -0
  37. data_factory/core/bktree.py +89 -0
  38. data_factory/core/fs.py +245 -0
  39. data_factory/core/imaging.py +626 -0
  40. data_factory/core/ops/__init__.py +44 -0
  41. data_factory/core/ops/analyze.py +383 -0
  42. data_factory/core/ops/annotate_export.py +326 -0
  43. data_factory/core/ops/audit.py +274 -0
  44. data_factory/core/ops/augment.py +454 -0
  45. data_factory/core/ops/collect.py +241 -0
  46. data_factory/core/ops/combine.py +582 -0
  47. data_factory/core/ops/convert.py +212 -0
  48. data_factory/core/ops/dataset.py +246 -0
  49. data_factory/core/ops/dedup.py +235 -0
  50. data_factory/core/ops/export.py +168 -0
  51. data_factory/core/ops/image.py +407 -0
  52. data_factory/core/ops/labels.py +215 -0
  53. data_factory/core/ops/pairing.py +133 -0
  54. data_factory/core/ops/rename.py +154 -0
  55. data_factory/core/ops/report.py +323 -0
  56. data_factory/core/ops/visualize.py +164 -0
  57. data_factory/core/pairs.py +144 -0
  58. data_factory/core/parallel.py +191 -0
  59. data_factory/core/provenance.py +136 -0
  60. data_factory/core/registry.py +339 -0
  61. data_factory/core/stats.py +403 -0
  62. data_factory/core/types.py +148 -0
  63. data_factory/core/writer.py +194 -0
  64. data_factory/desktop.py +207 -0
  65. data_factory/hardware.py +243 -0
  66. data_factory/pipeline.py +192 -0
  67. data_factory/py.typed +0 -0
  68. data_factory/service/__init__.py +26 -0
  69. data_factory/service/app.py +483 -0
  70. data_factory/service/binding.py +174 -0
  71. data_factory/service/deps.py +133 -0
  72. data_factory/service/jobs.py +177 -0
  73. data_factory/service/limits.py +71 -0
  74. data_factory/service/routers/__init__.py +11 -0
  75. data_factory/service/routers/annotate.py +582 -0
  76. data_factory/service/routers/local.py +464 -0
  77. data_factory/service/routers/media.py +154 -0
  78. data_factory/service/routers/predictions.py +116 -0
  79. data_factory/service/routers/promote.py +182 -0
  80. data_factory/service/routers/tools.py +308 -0
  81. data_factory/service/static/annotate/api.js +246 -0
  82. data_factory/service/static/annotate/app.js +1696 -0
  83. data_factory/service/static/annotate/assist.js +106 -0
  84. data_factory/service/static/annotate/filmstrip.js +286 -0
  85. data_factory/service/static/annotate/hit.js +146 -0
  86. data_factory/service/static/annotate/labels.js +197 -0
  87. data_factory/service/static/annotate/render.js +286 -0
  88. data_factory/service/static/annotate/scene.js +277 -0
  89. data_factory/service/static/annotate/shapelist.js +72 -0
  90. data_factory/service/static/annotate/tools/box.js +96 -0
  91. data_factory/service/static/annotate/tools/line.js +96 -0
  92. data_factory/service/static/annotate/tools/obb.js +88 -0
  93. data_factory/service/static/annotate/tools/point.js +49 -0
  94. data_factory/service/static/annotate/tools/polygon.js +110 -0
  95. data_factory/service/static/annotate/tools/select.js +227 -0
  96. data_factory/service/static/annotate/undo.js +176 -0
  97. data_factory/service/static/annotate/viewport.js +156 -0
  98. data_factory/service/static/annotate.html +390 -0
  99. data_factory/service/static/index.html +632 -0
  100. data_factory/service/workspace.py +244 -0
  101. easyannotate/__init__.py +64 -0
  102. easyannotate/__main__.py +5 -0
  103. easyannotate/project.py +181 -0
  104. easyannotate/py.typed +0 -0
  105. easyannotate-0.4.0.dist-info/METADATA +767 -0
  106. easyannotate-0.4.0.dist-info/RECORD +109 -0
  107. easyannotate-0.4.0.dist-info/WHEEL +4 -0
  108. easyannotate-0.4.0.dist-info/entry_points.txt +4 -0
  109. easyannotate-0.4.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,292 @@
1
+ """The annotation database: one SQLite file, opened through a thin ``Store``.
2
+
3
+ **Why SQLite.** Files-only has no place to put a class order, a project name or
4
+ a review state, and "which images are still unreviewed" degrades into a tree
5
+ walk. Postgres cannot be a dependency of a thing installed with ``pip`` and run
6
+ on a laptop. SQLite is in the standard library, is ACID, and in WAL mode gives
7
+ one writer with many concurrent readers -- which is exactly the shape of
8
+ annotation traffic. The same schema then runs locally and hosted, which is what
9
+ lets one core serve both tiers.
10
+
11
+ **Why plain ``sqlite3``.** No ORM. ``Store`` is already the seam: if the hosted
12
+ tier ever needs Postgres, it is this class that changes and nothing above it.
13
+
14
+ **Threads.** FastAPI runs synchronous handlers in a worker threadpool, and a
15
+ ``sqlite3`` connection must not cross threads. Each thread therefore gets its
16
+ own connection, created on first use. WAL is what makes that cheap.
17
+
18
+ **Migrations.** ``PRAGMA user_version`` is a ladder, not a framework: version 0
19
+ is an empty file, and each rung is applied in order inside one transaction. An
20
+ older build opening a newer database refuses rather than guessing.
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ import re
26
+ import secrets
27
+ import sqlite3
28
+ import threading
29
+ from collections.abc import Iterator
30
+ from contextlib import contextmanager
31
+ from pathlib import Path
32
+ from typing import Any
33
+
34
+ #: Bumped when a rung is added to ``MIGRATIONS``.
35
+ SCHEMA_VERSION = 7
36
+
37
+ #: The version-1 DDL lives beside this module rather than in a string, so it can
38
+ #: be read, diffed and run through a SQLite client directly.
39
+ SCHEMA_FILE = Path(__file__).with_name("schema.sql")
40
+
41
+ #: Ids are opaque tokens, not row numbers. An id that a client holds is an id a
42
+ #: client can try to guess, and sequential integers are trivially enumerable.
43
+ ID_BYTES = 12
44
+ ID_PATTERN = re.compile(r"^[0-9a-f]{24}$")
45
+
46
+ #: The principal used when nobody has logged in -- the entire local story.
47
+ #: Every query is scoped by owner from day one; locally the owner is just
48
+ #: always this. Retrofitting the scoping in Phase 5 would be a rewrite.
49
+ LOCAL_OWNER = "local"
50
+
51
+
52
+ class StoreError(Exception):
53
+ """Raised for anything a caller did wrong with the store."""
54
+
55
+
56
+ class NotFound(StoreError):
57
+ """The row does not exist, or does not belong to this owner.
58
+
59
+ Deliberately one exception for both cases. Distinguishing "no such project"
60
+ from "not yours" tells a stranger which project ids are real.
61
+ """
62
+
63
+
64
+ class Conflict(StoreError):
65
+ """The row changed underneath a save that was based on an older version."""
66
+
67
+
68
+ def new_id() -> str:
69
+ return secrets.token_hex(ID_BYTES)
70
+
71
+
72
+ def valid_id(value: str) -> bool:
73
+ return bool(ID_PATTERN.match(value or ""))
74
+
75
+
76
+ def require_id(value: str, what: str = "id") -> str:
77
+ """Reject anything that is not one of our ids before it reaches a query.
78
+
79
+ Parameterised SQL already stops injection; this stops the subtler thing --
80
+ an id like ``../..`` reaching a path built from it further downstream.
81
+ """
82
+ if not valid_id(value):
83
+ raise NotFound(f"not a valid {what}")
84
+ return value
85
+
86
+
87
+ class Store:
88
+ """A connection to one annotation database, migrated on open."""
89
+
90
+ def __init__(self, path: Path | str) -> None:
91
+ self.path = Path(path)
92
+ self.path.parent.mkdir(parents=True, exist_ok=True)
93
+ self._local = threading.local()
94
+ self._write_lock = threading.Lock()
95
+ self._migrate()
96
+
97
+ # -- connections -------------------------------------------------------
98
+ @property
99
+ def connection(self) -> sqlite3.Connection:
100
+ """This thread's connection, opened on first use."""
101
+ existing = getattr(self._local, "conn", None)
102
+ if existing is not None:
103
+ return existing
104
+ conn = sqlite3.connect(self.path, timeout=15.0)
105
+ conn.row_factory = sqlite3.Row
106
+ # WAL: readers never block the writer and the writer never blocks
107
+ # readers. Without it the editor's polling would stall every save.
108
+ conn.execute("PRAGMA journal_mode=WAL")
109
+ # NORMAL is the documented safe pairing with WAL: durable across a
110
+ # process crash, which is the failure we actually care about.
111
+ conn.execute("PRAGMA synchronous=NORMAL")
112
+ # Off by default in sqlite3, and the composite shape -> label
113
+ # reference is worth nothing unenforced.
114
+ conn.execute("PRAGMA foreign_keys=ON")
115
+ conn.execute("PRAGMA busy_timeout=15000")
116
+ self._local.conn = conn
117
+ return conn
118
+
119
+ @contextmanager
120
+ def write(self) -> Iterator[sqlite3.Connection]:
121
+ """A write transaction. Commits on success, rolls back on anything else.
122
+
123
+ The lock serialises writers *in this process* so two threads racing on
124
+ the same file get a Python-level wait instead of a SQLITE_BUSY that
125
+ surfaces to a user as a failed save.
126
+ """
127
+ with self._write_lock:
128
+ conn = self.connection
129
+ try:
130
+ with conn:
131
+ # IMMEDIATE takes SQLite's write lock now rather than at
132
+ # the first INSERT, so a read-check-write inside this
133
+ # block is atomic -- across processes too: the CLI and the
134
+ # server open the same file.
135
+ conn.execute("BEGIN IMMEDIATE")
136
+ yield conn
137
+ except sqlite3.IntegrityError as exc:
138
+ raise StoreError(str(exc)) from exc
139
+
140
+ def query(self, sql: str, params: tuple[Any, ...] = ()) -> list[sqlite3.Row]:
141
+ return list(self.connection.execute(sql, params))
142
+
143
+ def one(self, sql: str, params: tuple[Any, ...] = ()) -> sqlite3.Row | None:
144
+ return self.connection.execute(sql, params).fetchone()
145
+
146
+ def close(self) -> None:
147
+ conn = getattr(self._local, "conn", None)
148
+ if conn is not None:
149
+ conn.close()
150
+ self._local.conn = None
151
+
152
+ # -- migration ---------------------------------------------------------
153
+ def _migrate(self) -> None:
154
+ conn = self.connection
155
+ version = int(conn.execute("PRAGMA user_version").fetchone()[0])
156
+ if version > SCHEMA_VERSION:
157
+ raise StoreError(
158
+ f"{self.path.name} is schema version {version}; this build "
159
+ f"understands up to {SCHEMA_VERSION}. Upgrade Data Factory to "
160
+ f"open it. (Refusing rather than half-reading it.)"
161
+ )
162
+ if version == SCHEMA_VERSION:
163
+ return
164
+ with self._write_lock:
165
+ # One transaction per rung, with the version bump inside it. A
166
+ # crash mid-ladder then leaves a database at a real version that
167
+ # the next start resumes from, never a half-built schema that
168
+ # claims to be the old one.
169
+ for rung in range(version, SCHEMA_VERSION):
170
+ MIGRATIONS[rung](conn, rung + 1)
171
+
172
+
173
+ def _step(conn: sqlite3.Connection, target: int, *statements: str) -> None:
174
+ with conn:
175
+ conn.execute("BEGIN IMMEDIATE")
176
+ for statement in statements:
177
+ conn.execute(statement)
178
+ # PRAGMA does not take a parameter, and the value is an int constant
179
+ # of ours -- never anything from outside.
180
+ conn.execute(f"PRAGMA user_version={int(target)}")
181
+
182
+
183
+ def _to_v1(conn: sqlite3.Connection, target: int) -> None:
184
+ # executescript commits whatever is open and then runs the text as given,
185
+ # so the transaction has to be part of the script itself.
186
+ conn.executescript(
187
+ "BEGIN IMMEDIATE;\n"
188
+ + SCHEMA_FILE.read_text(encoding="utf-8")
189
+ + f"\nPRAGMA user_version={int(target)};\nCOMMIT;"
190
+ )
191
+
192
+
193
+ def _to_v2(conn: sqlite3.Connection, target: int) -> None:
194
+ """Record which sidecar format an attached project writes back to.
195
+
196
+ Deliberately *not* added to ``schema.sql``. Keeping the base script at
197
+ version 1 means every fresh database climbs the whole ladder, so each
198
+ migration is exercised on every new install rather than only on the one
199
+ upgrade nobody tested.
200
+ """
201
+ _step(conn, target, "ALTER TABLE project ADD COLUMN sidecar_format TEXT NOT NULL DEFAULT ''")
202
+
203
+
204
+ def _to_v3(conn: sqlite3.Connection, target: int) -> None:
205
+ """Remember how many shapes a model predicted for each image.
206
+
207
+ Without it the agreement metric is quietly wrong in the most common case.
208
+ A person reviewing predictions mostly *deletes* false positives, and a
209
+ deleted row leaves no trace -- so agreement computed from surviving shapes
210
+ would count every deletion as though the prediction had never been made,
211
+ and a model that hallucinated ten boxes an image would score 100%.
212
+ """
213
+ _step(conn, target, "ALTER TABLE image ADD COLUMN predicted_count INTEGER NOT NULL DEFAULT 0")
214
+
215
+
216
+ def _to_v4(conn: sqlite3.Connection, target: int) -> None:
217
+ """What a shape's geometry means: box, polygon, line, point or obb.
218
+
219
+ Empty means the historical rule -- a polygon if there is an outline of
220
+ three or more points, else a box -- so every existing row keeps meaning
221
+ exactly what it meant.
222
+ """
223
+ _step(conn, target, "ALTER TABLE shape ADD COLUMN kind TEXT NOT NULL DEFAULT ''")
224
+
225
+
226
+ def _to_v5(conn: sqlite3.Connection, target: int) -> None:
227
+ """How likely an image's predictions are to be wrong, for review order.
228
+
229
+ Kept on the row, not computed per request, because ordering 20,000
230
+ images by it has to be an index scan, not 20,000 shape listings.
231
+ """
232
+ _step(
233
+ conn,
234
+ target,
235
+ "ALTER TABLE image ADD COLUMN uncertainty REAL NOT NULL DEFAULT 0",
236
+ "CREATE INDEX IF NOT EXISTS image_uncertain ON image(project_id, state, uncertainty DESC, seq)",
237
+ )
238
+
239
+
240
+ def _to_v6(conn: sqlite3.Connection, target: int) -> None:
241
+ """Score every image that already had predictions before v5.
242
+
243
+ v5 added the column at 0 and left it there, so a project's existing
244
+ predictions sorted in file order under "least sure first" until each
245
+ image happened to be saved again.
246
+ """
247
+ from .shapes import score_rows
248
+
249
+ with conn:
250
+ conn.execute("BEGIN IMMEDIATE")
251
+ rows = conn.execute(
252
+ "SELECT image_id, label_idx, xmin, ymin, xmax, ymax, confidence FROM shape"
253
+ " WHERE origin = 'model' AND edited_at IS NULL"
254
+ ).fetchall()
255
+ by_image: dict[str, list] = {}
256
+ for row in rows:
257
+ by_image.setdefault(row[0], []).append(row[1:])
258
+ conn.executemany(
259
+ "UPDATE image SET uncertainty = ? WHERE id = ?",
260
+ [(score_rows(items), image_id) for image_id, items in by_image.items()],
261
+ )
262
+ conn.execute(f"PRAGMA user_version={int(target)}")
263
+
264
+
265
+ def _to_v7(conn: sqlite3.Connection, target: int) -> None:
266
+ """Count predictions that arrived without set_predictions.
267
+
268
+ Model shapes read from an attached folder's own files were stored
269
+ without a predicted_count until 0.4.0, so agreement left them out. The
270
+ surviving model shapes are a floor on what was predicted -- deletions
271
+ since are not recoverable -- so raise the count to at least that.
272
+ """
273
+ _step(
274
+ conn,
275
+ target,
276
+ "UPDATE image SET predicted_count = MAX(predicted_count,"
277
+ " (SELECT COUNT(*) FROM shape WHERE shape.image_id = image.id AND shape.origin = 'model'))",
278
+ )
279
+
280
+
281
+ #: Rung *n* migrates a database at version *n* to version *n+1*.
282
+ MIGRATIONS = [_to_v1, _to_v2, _to_v3, _to_v4, _to_v5, _to_v6, _to_v7]
283
+
284
+
285
+ def open_store(root: Path | str) -> Store:
286
+ """Open the annotation database for a workspace root.
287
+
288
+ Placed *beside* the dataset sandboxes, never inside one -- a database file
289
+ inside a sandbox would be swept on TTL, zipped into a user's download, and
290
+ served by the static routes.
291
+ """
292
+ return Store(Path(root).parent / "annotations.db")
@@ -0,0 +1,39 @@
1
+ """Heavy tools -- SAM, model inference -- behind a process boundary.
2
+
3
+ Nothing in this package imports torch, and nothing here imports a module that
4
+ does. That is the whole point: the base wheel stays installable in seconds, and
5
+ a crashing GPU tool takes down a child process rather than the editor.
6
+
7
+ See :mod:`data_factory.annotate.tools.protocol` for the contract, and
8
+ :mod:`data_factory.annotate.tools.fake` for a reference implementation you can
9
+ copy.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from .protocol import (
15
+ PROTOCOL_VERSION,
16
+ Tool,
17
+ ToolError,
18
+ ToolHealth,
19
+ ToolNotReady,
20
+ ToolSpec,
21
+ ToolUnavailable,
22
+ )
23
+ from .registry import REGISTRY, NullTool, ToolRegistry
24
+
25
+ from . import postprocess
26
+
27
+ __all__ = [
28
+ "PROTOCOL_VERSION",
29
+ "REGISTRY",
30
+ "NullTool",
31
+ "Tool",
32
+ "ToolError",
33
+ "ToolHealth",
34
+ "ToolNotReady",
35
+ "ToolRegistry",
36
+ "ToolSpec",
37
+ "ToolUnavailable",
38
+ "postprocess",
39
+ ]
@@ -0,0 +1,170 @@
1
+ """A tool that does nothing useful, on purpose.
2
+
3
+ Two jobs:
4
+
5
+ * **It is the test fixture for the protocol.** Every behaviour the real SAM
6
+ sidecar must have -- slow work, cold state, crashes, exceptions, silence --
7
+ is reachable here without installing PyTorch, so the boundary is verified in
8
+ a second rather than a minute and in CI rather than only on a GPU box.
9
+ * **It is the reference implementation.** A tool author copies this file,
10
+ replaces ``invoke``, and gets framing, correlation and error replies for
11
+ free.
12
+
13
+ Run it as ``python -m data_factory.annotate.tools.fake``.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import os
19
+ import time
20
+ from typing import Any
21
+
22
+ from .protocol import ToolError, ToolNotReady, ToolSpec
23
+ from .serve import serve
24
+
25
+ #: Milliseconds of pretend work, so a test can assert on timeouts.
26
+ WORK_MS = int(os.environ.get("DFX_FAKE_WORK_MS", "20"))
27
+
28
+ SPECS = [
29
+ ToolSpec(
30
+ name="fake.box",
31
+ title="Fake box tool",
32
+ summary="Returns a fixed shape. Exists to prove the sidecar boundary.",
33
+ kind="interactive",
34
+ inputs={
35
+ "type": "object",
36
+ "properties": {"box": {"type": "array", "items": {"type": "number"}}},
37
+ "required": ["box"],
38
+ },
39
+ version="1",
40
+ requires=("prepare",),
41
+ ),
42
+ # A prompt-kind tool, so the editor's "find every ..." panel and the batch
43
+ # route can be driven end to end without a model: it answers one box per
44
+ # image, labelled with whatever was typed.
45
+ ToolSpec(
46
+ name="fake.text",
47
+ title="Fake find every...",
48
+ summary="Returns one box labelled with the prompt. Exists to test the prompt panel.",
49
+ kind="prompt",
50
+ inputs={
51
+ "type": "object",
52
+ "properties": {"text": {"type": "string"}},
53
+ "required": ["text"],
54
+ },
55
+ version="1",
56
+ requires=(),
57
+ ),
58
+ # A model-kind tool, so `predict` and the HTTP refusal can be tested
59
+ # without torch. Its "model" is any file; it labels boxes with its stem.
60
+ ToolSpec(
61
+ name="fake.model",
62
+ title="Fake trained model",
63
+ summary="Returns one box labelled with the model file's stem.",
64
+ kind="model",
65
+ inputs={
66
+ "type": "object",
67
+ "properties": {"model": {"type": "string"}},
68
+ "required": ["model"],
69
+ },
70
+ version="1",
71
+ requires=(),
72
+ ),
73
+ ]
74
+
75
+
76
+ class FakeTool:
77
+ def __init__(self) -> None:
78
+ self.prepared: set[str] = set()
79
+
80
+ def describe(self) -> list[dict[str, Any]]:
81
+ return [spec.as_dict() for spec in SPECS]
82
+
83
+ def health(self, params: dict[str, Any]) -> dict[str, Any]:
84
+ return {"state": "ready", "detail": f"fake tool, {WORK_MS}ms per call"}
85
+
86
+ def prepare(self, params: dict[str, Any]) -> None:
87
+ time.sleep(WORK_MS / 1000.0)
88
+ self.prepared.add(str(params.get("image_id") or ""))
89
+
90
+ def invoke(self, params: dict[str, Any]) -> dict[str, Any]:
91
+ image_id = str(params.get("image_id") or "")
92
+
93
+ # A library printing to stdout, as Ultralytics does while it
94
+ # downloads weights: from Python, and straight to the descriptor.
95
+ if os.environ.get("DFX_FAKE_NOISE"):
96
+ import sys
97
+
98
+ sys.stdout.write("\r\x1b[KDownloading weights: 0% \u2500\u2500" * 40)
99
+ sys.stdout.flush()
100
+ os.write(1, "\r\x1b[KDownloading weights: 100% \u2501".encode())
101
+
102
+ # Behaviours a test can ask for by name, each mirroring a real failure
103
+ # mode of a GPU-backed tool.
104
+ behaviour = str(params.get("behaviour") or "")
105
+ if behaviour == "raise":
106
+ raise RuntimeError("the tool blew up")
107
+ if behaviour == "exit":
108
+ # os._exit, not sys.exit: serve() catches BaseException and always
109
+ # replies, so a SystemExit would be *handled* rather than fatal.
110
+ # A real segfault or CUDA OOM does not unwind, and this is the only
111
+ # honest way to simulate one.
112
+ os._exit(9)
113
+ if behaviour == "hang":
114
+ time.sleep(3600) # never replies; the caller must time out
115
+ if behaviour == "silent":
116
+ raise SystemExit # a BaseException, not an Exception
117
+ if behaviour == "unknown_tool":
118
+ raise ToolError("no such tool", code="unknown_tool")
119
+
120
+ if str(params.get("tool") or "") == "fake.model":
121
+ model = str(params.get("model") or "")
122
+ if not model or not os.path.isfile(model):
123
+ raise ToolError(f"no model file at {model}")
124
+ label = os.path.splitext(os.path.basename(model))[0]
125
+ return {
126
+ "shapes": [
127
+ {"label": label, "bbox": [10, 10, 50, 40], "polygon": None, "confidence": 0.42}
128
+ ],
129
+ "ms": WORK_MS,
130
+ }
131
+
132
+ if str(params.get("tool") or "") == "fake.text":
133
+ text = str(params.get("text") or "").strip()
134
+ if not text:
135
+ raise ToolError("fake.text needs text")
136
+ return {
137
+ "shapes": [
138
+ {"label": text.split(",")[0].strip(), "bbox": [5, 5, 40, 30], "polygon": None, "confidence": 0.8}
139
+ ],
140
+ "ms": WORK_MS,
141
+ }
142
+
143
+ if image_id and image_id not in self.prepared:
144
+ # Exactly how the real SAM tool behaves on a cold image: not an
145
+ # error, just not yet -- the UI retries rather than showing a
146
+ # failure the user can do nothing about.
147
+ raise ToolNotReady(f"{image_id} has not been prepared", retry_after_ms=250)
148
+
149
+ time.sleep(WORK_MS / 1000.0)
150
+ box = params.get("box") or [0, 0, 10, 10]
151
+ x1, y1, x2, y2 = (int(v) for v in box[:4])
152
+ return {
153
+ "shapes": [
154
+ {
155
+ "label": str(params.get("label") or "object"),
156
+ "bbox": [x1, y1, x2, y2],
157
+ "polygon": [[x1, y1], [x2, y1], [x2, y2], [x1, y2]],
158
+ "confidence": 0.99,
159
+ }
160
+ ],
161
+ "ms": WORK_MS,
162
+ }
163
+
164
+
165
+ def main() -> int:
166
+ return serve(FakeTool())
167
+
168
+
169
+ if __name__ == "__main__":
170
+ raise SystemExit(main())