singlepipe 0.0.1__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 (49) hide show
  1. singlepipe/__init__.py +8 -0
  2. singlepipe/adapters/__init__.py +0 -0
  3. singlepipe/adapters/hf_adapter.py +119 -0
  4. singlepipe/model_types/__init__.py +3 -0
  5. singlepipe/model_types/types.py +79 -0
  6. singlepipe/schemas/__init__.py +25 -0
  7. singlepipe/schemas/audio_schemas/__init__.py +0 -0
  8. singlepipe/schemas/audio_schemas/audio_classification.py +40 -0
  9. singlepipe/schemas/audio_schemas/speech_to_text.py +40 -0
  10. singlepipe/schemas/audio_schemas/text_to_audio.py +40 -0
  11. singlepipe/schemas/audio_schemas/zero_shot_audio_classification.py +40 -0
  12. singlepipe/schemas/image_schemas/__init__.py +0 -0
  13. singlepipe/schemas/image_schemas/depth_estimation.py +40 -0
  14. singlepipe/schemas/image_schemas/image_classification.py +40 -0
  15. singlepipe/schemas/image_schemas/image_embedding.py +40 -0
  16. singlepipe/schemas/image_schemas/image_segmentation.py +40 -0
  17. singlepipe/schemas/image_schemas/keypoint_matching.py +40 -0
  18. singlepipe/schemas/image_schemas/mask_generation.py +40 -0
  19. singlepipe/schemas/image_schemas/object_detection.py +290 -0
  20. singlepipe/schemas/image_schemas/zero_shot_image_classification.py +40 -0
  21. singlepipe/schemas/image_schemas/zero_shot_object_detection.py +40 -0
  22. singlepipe/schemas/multimodal_schemas/__init__.py +0 -0
  23. singlepipe/schemas/multimodal_schemas/document_question_answering.py +40 -0
  24. singlepipe/schemas/multimodal_schemas/image_text_to_text.py +40 -0
  25. singlepipe/schemas/multimodal_schemas/table_question_answering.py +40 -0
  26. singlepipe/schemas/ocr_schemas/__init__.py +0 -0
  27. singlepipe/schemas/ocr_schemas/ocr.py +40 -0
  28. singlepipe/schemas/registry.py +97 -0
  29. singlepipe/schemas/table_schemas/__init__.py +0 -0
  30. singlepipe/schemas/table_schemas/tabular_classification.py +40 -0
  31. singlepipe/schemas/table_schemas/tabular_regression.py +40 -0
  32. singlepipe/schemas/text_schemas/__init__.py +0 -0
  33. singlepipe/schemas/text_schemas/fill_mask.py +40 -0
  34. singlepipe/schemas/text_schemas/question_answering.py +40 -0
  35. singlepipe/schemas/text_schemas/summarization.py +40 -0
  36. singlepipe/schemas/text_schemas/text2text_generation.py +40 -0
  37. singlepipe/schemas/text_schemas/text_classification.py +40 -0
  38. singlepipe/schemas/text_schemas/text_embedding.py +40 -0
  39. singlepipe/schemas/text_schemas/text_generation.py +40 -0
  40. singlepipe/schemas/text_schemas/token_classification.py +40 -0
  41. singlepipe/schemas/text_schemas/translation.py +40 -0
  42. singlepipe/schemas/text_schemas/zero_shot_text_classification.py +40 -0
  43. singlepipe/schemas/video_schemas/__init__.py +0 -0
  44. singlepipe/schemas/video_schemas/video_classification.py +40 -0
  45. singlepipe-0.0.1.dist-info/METADATA +135 -0
  46. singlepipe-0.0.1.dist-info/RECORD +49 -0
  47. singlepipe-0.0.1.dist-info/WHEEL +5 -0
  48. singlepipe-0.0.1.dist-info/licenses/LICENSE +21 -0
  49. singlepipe-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,290 @@
1
+ from __future__ import annotations
2
+
3
+ import io
4
+ from collections.abc import Iterable, Iterator
5
+ from pathlib import Path
6
+ from typing import Any, Self
7
+
8
+ import httpx
9
+ import numpy as np
10
+ from PIL import Image
11
+ from pydantic import (
12
+ BaseModel,
13
+ ConfigDict,
14
+ Field,
15
+ computed_field,
16
+ field_validator,
17
+ model_validator,
18
+ )
19
+
20
+ try:
21
+ import torch
22
+ _TORCH_AVAILABLE = True
23
+ except ImportError:
24
+ _TORCH_AVAILABLE = False
25
+
26
+
27
+
28
+ def _normalize_ndarray(array: np.ndarray) -> Image.Image:
29
+ """Normalize an HWC ndarray to a PIL RGB image.
30
+
31
+ Expects:
32
+ - shape (H, W) — grayscale, or
33
+ - shape (H, W, C) with C in {1, 3, 4} — HWC layout.
34
+
35
+ Batched arrays and CHW layout are not supported: convert them explicitly
36
+ before passing. Float dtypes are assumed to be in [0, 1] and scaled to
37
+ uint8 [0, 255]; other non-uint8 dtypes are cast as-is.
38
+ """
39
+ if array.ndim == 2 or array.ndim == 3 and array.shape[2] in {1, 3, 4}:
40
+ pass
41
+ else:
42
+ raise ValueError(
43
+ f"Expected ndarray with shape (H, W) or (H, W, C) with C in {{1, 3, 4}}; "
44
+ f"got shape {array.shape}. "
45
+ "If you have a CHW array, transpose it first; batches are not supported."
46
+ )
47
+
48
+ if np.issubdtype(array.dtype, np.floating):
49
+ array = np.clip(array * 255.0, 0, 255).astype(np.uint8)
50
+ elif array.dtype != np.uint8:
51
+ array = array.astype(np.uint8)
52
+
53
+ return Image.fromarray(array).convert("RGB")
54
+
55
+
56
+ def _looks_like_url(s: str) -> bool:
57
+ return s.startswith(("http://", "https://"))
58
+
59
+
60
+ def _load_from_url(url: str, timeout: float = 10.0) -> Image.Image:
61
+ response = httpx.get(url, timeout=timeout, follow_redirects=True)
62
+ response.raise_for_status()
63
+ return Image.open(io.BytesIO(response.content)).convert("RGB")
64
+
65
+
66
+
67
+ class BBox(BaseModel):
68
+ """Axis-aligned bounding box in xyxy format.
69
+
70
+ Coordinates are absolute pixels with top-left origin.
71
+ Must satisfy ``xmin < xmax`` and ``ymin < ymax``.
72
+ Float values are allowed (sub-pixel precision).
73
+ """
74
+
75
+ model_config = ConfigDict(frozen=True)
76
+
77
+ xmin: float = Field(ge=0, title="Coordinate Xmin")
78
+ ymin: float = Field(ge=0, title="Coordinate Ymin")
79
+ xmax: float = Field(ge=0, title="Coordinate Xmax")
80
+ ymax: float = Field(ge=0, title="Coordinate Ymax")
81
+
82
+ @model_validator(mode="after")
83
+ def check_geometry(self) -> BBox:
84
+ if self.xmin >= self.xmax:
85
+ raise ValueError("xmax must be greater than xmin")
86
+ if self.ymin >= self.ymax:
87
+ raise ValueError("ymax must be greater than ymin")
88
+ return self
89
+
90
+ @computed_field # type: ignore[misc]
91
+ @property
92
+ def width(self) -> float:
93
+ return self.xmax - self.xmin
94
+
95
+ @computed_field # type: ignore[misc]
96
+ @property
97
+ def height(self) -> float:
98
+ return self.ymax - self.ymin
99
+
100
+ @computed_field # type: ignore[misc]
101
+ @property
102
+ def area(self) -> float:
103
+ return self.width * self.height
104
+
105
+ @computed_field # type: ignore[misc]
106
+ @property
107
+ def center(self) -> tuple[float, float]:
108
+ return (
109
+ (self.xmin + self.xmax) / 2.0,
110
+ (self.ymin + self.ymax) / 2.0,
111
+ )
112
+
113
+ def as_tuple(self) -> tuple[float, float, float, float]:
114
+ """Return ``(xmin, ymin, xmax, ymax)``."""
115
+ return (self.xmin, self.ymin, self.xmax, self.ymax)
116
+
117
+ def clip(self, image_width: float, image_height: float) -> BBox:
118
+ """Return a new box clipped to the image bounds."""
119
+ return BBox(
120
+ xmin=max(0.0, min(self.xmin, image_width)),
121
+ ymin=max(0.0, min(self.ymin, image_height)),
122
+ xmax=max(0.0, min(self.xmax, image_width)),
123
+ ymax=max(0.0, min(self.ymax, image_height)),
124
+ )
125
+
126
+ def iou(self, other: BBox) -> float:
127
+ """Intersection over Union with another box. Returns 0 if disjoint."""
128
+ inter_xmin = max(self.xmin, other.xmin)
129
+ inter_ymin = max(self.ymin, other.ymin)
130
+ inter_xmax = min(self.xmax, other.xmax)
131
+ inter_ymax = min(self.ymax, other.ymax)
132
+
133
+ inter_w = max(0.0, inter_xmax - inter_xmin)
134
+ inter_h = max(0.0, inter_ymax - inter_ymin)
135
+ intersection = inter_w * inter_h
136
+
137
+ if intersection == 0.0:
138
+ return 0.0
139
+
140
+ union = self.area + other.area - intersection
141
+ return intersection / union
142
+
143
+
144
+ class Detection(BaseModel):
145
+ """Single object detection prediction: label, confidence, and bounding box."""
146
+
147
+ model_config = ConfigDict(frozen=True)
148
+
149
+ score: float = Field(ge=0, le=1, title="Model confidence")
150
+ label: str = Field(title="Predicted class")
151
+ label_id: int | None = Field(default=None, title="Class id, if known")
152
+ box: BBox = Field(title="Bounding box")
153
+
154
+
155
+
156
+ class ObjectDetectionOutput(BaseModel):
157
+ """Object detection result for a single image."""
158
+
159
+ predictions: list[Detection] = Field(default_factory=list, title="Predictions")
160
+ image_width: int | None = Field(default=None, ge=1, title="Source image width")
161
+ image_height: int | None = Field(default=None, ge=1, title="Source image height")
162
+
163
+ @model_validator(mode="after")
164
+ def check_boxes_within_image(self) -> ObjectDetectionOutput:
165
+ if self.image_width is None or self.image_height is None:
166
+ return self
167
+ for i, det in enumerate(self.predictions):
168
+ box = det.box
169
+ if (
170
+ box.xmax > self.image_width
171
+ or box.ymax > self.image_height
172
+ ):
173
+ raise ValueError(
174
+ f"Detection {i} box {box.as_tuple()} exceeds image size "
175
+ f"({self.image_width}x{self.image_height})"
176
+ )
177
+ return self
178
+
179
+ @classmethod
180
+ def from_pipeline_output(cls, raw: Any, **context: Any) -> Self:
181
+ return cls.model_validate(
182
+ {
183
+ "predictions": raw,
184
+ "image_width": context.get("image_width"),
185
+ "image_height": context.get("image_height"),
186
+ }
187
+ )
188
+
189
+ def filter_by_score(self, threshold: float) -> ObjectDetectionOutput:
190
+ """Return a new output containing only detections with ``score >= threshold``."""
191
+ return ObjectDetectionOutput(
192
+ predictions=[d for d in self.predictions if d.score >= threshold],
193
+ image_width=self.image_width,
194
+ image_height=self.image_height,
195
+ )
196
+
197
+ def filter_by_label(self, labels: str | Iterable[str]) -> ObjectDetectionOutput:
198
+ """Return a new output containing only detections with matching labels."""
199
+ wanted = {labels} if isinstance(labels, str) else set(labels)
200
+ return ObjectDetectionOutput(
201
+ predictions=[d for d in self.predictions if d.label in wanted],
202
+ image_width=self.image_width,
203
+ image_height=self.image_height,
204
+ )
205
+
206
+ def __len__(self) -> int:
207
+ return len(self.predictions)
208
+
209
+ def __iter__(self) -> Iterator[Detection]:
210
+ return iter(self.predictions)
211
+
212
+
213
+
214
+ class ObjectDetectionInput(BaseModel):
215
+ """Input for object detection.
216
+
217
+ Accepts:
218
+ - ``PIL.Image.Image``
219
+ - ``numpy.ndarray`` (HWC or CHW; uint8 or float in [0, 1])
220
+ - ``torch.Tensor`` (CHW; uint8 or float in [0, 1]); optional dependency
221
+ - ``str`` — local file path or ``http(s)://`` URL
222
+ - ``pathlib.Path`` — local file path
223
+ - ``bytes`` — raw image bytes
224
+
225
+ Normalizes to ``PIL.Image.Image`` in RGB mode.
226
+ Batches (B > 1) are not supported; pass one image at a time.
227
+ """
228
+
229
+ model_config = ConfigDict(arbitrary_types_allowed=True)
230
+
231
+ image: Image.Image
232
+
233
+ @field_validator("image", mode="before")
234
+ @classmethod
235
+ def convert_format(cls, image: Any) -> Image.Image:
236
+ if isinstance(image, Image.Image):
237
+ return image.convert("RGB")
238
+
239
+ if _TORCH_AVAILABLE and isinstance(image, torch.Tensor):
240
+ tensor = image.detach().cpu()
241
+
242
+ if tensor.ndim == 4:
243
+ if tensor.shape[0] != 1:
244
+ raise ValueError(
245
+ f"Batch tensor is not supported; got shape {tuple(tensor.shape)}. "
246
+ "Pass one image at a time."
247
+ )
248
+ tensor = tensor.squeeze(0)
249
+
250
+ if tensor.ndim == 3 and tensor.shape[0] in {1, 3, 4}:
251
+ tensor = tensor.permute(1, 2, 0)
252
+ elif tensor.ndim == 2:
253
+ pass
254
+ else:
255
+ raise ValueError(
256
+ f"Expected CHW tensor with shape (C, H, W) with C in {{1, 3, 4}}; "
257
+ f"got shape {tuple(tensor.shape)}."
258
+ )
259
+
260
+ return _normalize_ndarray(tensor.numpy())
261
+
262
+ if isinstance(image, np.ndarray):
263
+ return _normalize_ndarray(image)
264
+
265
+ if isinstance(image, str):
266
+ if _looks_like_url(image):
267
+ return _load_from_url(image)
268
+ return Image.open(image).convert("RGB")
269
+
270
+ if isinstance(image, Path):
271
+ return Image.open(image).convert("RGB")
272
+
273
+ if isinstance(image, bytes):
274
+ return Image.open(io.BytesIO(image)).convert("RGB")
275
+
276
+ raise TypeError(f"Unsupported image type: {type(image).__name__}")
277
+
278
+ @classmethod
279
+ def from_raw(cls, raw: Any) -> Self:
280
+ return cls.model_validate({"image": raw})
281
+
282
+ @property
283
+ def output_context(self) -> dict[str, Any]:
284
+ return {
285
+ "image_width": self.image.width,
286
+ "image_height": self.image.height,
287
+ }
288
+
289
+ def to_pipeline_input(self) -> Image.Image:
290
+ return self.image
@@ -0,0 +1,40 @@
1
+ """Placeholder schemas for TaskType.ZERO_SHOT_IMAGE_CLASSIFICATION.
2
+
3
+ Not yet implemented. Exists to unlock ``HFAdapter`` type overloads.
4
+ Replace with real schemas when this task gains first-class support.
5
+ """
6
+
7
+ from typing import Any, Self
8
+
9
+ from pydantic import BaseModel, ConfigDict
10
+
11
+
12
+ class ZeroShotImageClassificationInput(BaseModel):
13
+ """Placeholder input schema. Fields TBD."""
14
+
15
+ model_config = ConfigDict(arbitrary_types_allowed=True)
16
+
17
+ @classmethod
18
+ def from_raw(cls, raw: Any) -> Self:
19
+ raise NotImplementedError(
20
+ "ZeroShotImageClassificationInput is not implemented yet."
21
+ )
22
+
23
+ def to_pipeline_input(self) -> Any:
24
+ raise NotImplementedError(
25
+ "ZeroShotImageClassificationInput is not implemented yet."
26
+ )
27
+
28
+ @property
29
+ def output_context(self) -> dict[str, Any]:
30
+ return {}
31
+
32
+
33
+ class ZeroShotImageClassificationOutput(BaseModel):
34
+ """Placeholder output schema. Fields TBD."""
35
+
36
+ @classmethod
37
+ def from_pipeline_output(cls, raw_output: Any, **context: Any) -> Self:
38
+ raise NotImplementedError(
39
+ "ZeroShotImageClassificationOutput is not implemented yet."
40
+ )
@@ -0,0 +1,40 @@
1
+ """Placeholder schemas for TaskType.ZERO_SHOT_OBJECT_DETECTION.
2
+
3
+ Not yet implemented. Exists to unlock ``HFAdapter`` type overloads.
4
+ Replace with real schemas when this task gains first-class support.
5
+ """
6
+
7
+ from typing import Any, Self
8
+
9
+ from pydantic import BaseModel, ConfigDict
10
+
11
+
12
+ class ZeroShotObjectDetectionInput(BaseModel):
13
+ """Placeholder input schema. Fields TBD."""
14
+
15
+ model_config = ConfigDict(arbitrary_types_allowed=True)
16
+
17
+ @classmethod
18
+ def from_raw(cls, raw: Any) -> Self:
19
+ raise NotImplementedError(
20
+ "ZeroShotObjectDetectionInput is not implemented yet."
21
+ )
22
+
23
+ def to_pipeline_input(self) -> Any:
24
+ raise NotImplementedError(
25
+ "ZeroShotObjectDetectionInput is not implemented yet."
26
+ )
27
+
28
+ @property
29
+ def output_context(self) -> dict[str, Any]:
30
+ return {}
31
+
32
+
33
+ class ZeroShotObjectDetectionOutput(BaseModel):
34
+ """Placeholder output schema. Fields TBD."""
35
+
36
+ @classmethod
37
+ def from_pipeline_output(cls, raw_output: Any, **context: Any) -> Self:
38
+ raise NotImplementedError(
39
+ "ZeroShotObjectDetectionOutput is not implemented yet."
40
+ )
File without changes
@@ -0,0 +1,40 @@
1
+ """Placeholder schemas for TaskType.DOCUMENT_QUESTION_ANSWERING.
2
+
3
+ Not yet implemented. Exists to unlock ``HFAdapter`` type overloads.
4
+ Replace with real schemas when this task gains first-class support.
5
+ """
6
+
7
+ from typing import Any, Self
8
+
9
+ from pydantic import BaseModel, ConfigDict
10
+
11
+
12
+ class DocumentQuestionAnsweringInput(BaseModel):
13
+ """Placeholder input schema. Fields TBD."""
14
+
15
+ model_config = ConfigDict(arbitrary_types_allowed=True)
16
+
17
+ @classmethod
18
+ def from_raw(cls, raw: Any) -> Self:
19
+ raise NotImplementedError(
20
+ "DocumentQuestionAnsweringInput is not implemented yet."
21
+ )
22
+
23
+ def to_pipeline_input(self) -> Any:
24
+ raise NotImplementedError(
25
+ "DocumentQuestionAnsweringInput is not implemented yet."
26
+ )
27
+
28
+ @property
29
+ def output_context(self) -> dict[str, Any]:
30
+ return {}
31
+
32
+
33
+ class DocumentQuestionAnsweringOutput(BaseModel):
34
+ """Placeholder output schema. Fields TBD."""
35
+
36
+ @classmethod
37
+ def from_pipeline_output(cls, raw_output: Any, **context: Any) -> Self:
38
+ raise NotImplementedError(
39
+ "DocumentQuestionAnsweringOutput is not implemented yet."
40
+ )
@@ -0,0 +1,40 @@
1
+ """Placeholder schemas for TaskType.IMAGE_TEXT_TO_TEXT.
2
+
3
+ Not yet implemented. Exists to unlock ``HFAdapter`` type overloads.
4
+ Replace with real schemas when this task gains first-class support.
5
+ """
6
+
7
+ from typing import Any, Self
8
+
9
+ from pydantic import BaseModel, ConfigDict
10
+
11
+
12
+ class ImageTextToTextInput(BaseModel):
13
+ """Placeholder input schema. Fields TBD."""
14
+
15
+ model_config = ConfigDict(arbitrary_types_allowed=True)
16
+
17
+ @classmethod
18
+ def from_raw(cls, raw: Any) -> Self:
19
+ raise NotImplementedError(
20
+ "ImageTextToTextInput is not implemented yet."
21
+ )
22
+
23
+ def to_pipeline_input(self) -> Any:
24
+ raise NotImplementedError(
25
+ "ImageTextToTextInput is not implemented yet."
26
+ )
27
+
28
+ @property
29
+ def output_context(self) -> dict[str, Any]:
30
+ return {}
31
+
32
+
33
+ class ImageTextToTextOutput(BaseModel):
34
+ """Placeholder output schema. Fields TBD."""
35
+
36
+ @classmethod
37
+ def from_pipeline_output(cls, raw_output: Any, **context: Any) -> Self:
38
+ raise NotImplementedError(
39
+ "ImageTextToTextOutput is not implemented yet."
40
+ )
@@ -0,0 +1,40 @@
1
+ """Placeholder schemas for TaskType.TABLE_QUESTION_ANSWERING.
2
+
3
+ Not yet implemented. Exists to unlock ``HFAdapter`` type overloads.
4
+ Replace with real schemas when this task gains first-class support.
5
+ """
6
+
7
+ from typing import Any, Self
8
+
9
+ from pydantic import BaseModel, ConfigDict
10
+
11
+
12
+ class TableQuestionAnsweringInput(BaseModel):
13
+ """Placeholder input schema. Fields TBD."""
14
+
15
+ model_config = ConfigDict(arbitrary_types_allowed=True)
16
+
17
+ @classmethod
18
+ def from_raw(cls, raw: Any) -> Self:
19
+ raise NotImplementedError(
20
+ "TableQuestionAnsweringInput is not implemented yet."
21
+ )
22
+
23
+ def to_pipeline_input(self) -> Any:
24
+ raise NotImplementedError(
25
+ "TableQuestionAnsweringInput is not implemented yet."
26
+ )
27
+
28
+ @property
29
+ def output_context(self) -> dict[str, Any]:
30
+ return {}
31
+
32
+
33
+ class TableQuestionAnsweringOutput(BaseModel):
34
+ """Placeholder output schema. Fields TBD."""
35
+
36
+ @classmethod
37
+ def from_pipeline_output(cls, raw_output: Any, **context: Any) -> Self:
38
+ raise NotImplementedError(
39
+ "TableQuestionAnsweringOutput is not implemented yet."
40
+ )
File without changes
@@ -0,0 +1,40 @@
1
+ """Placeholder schemas for TaskType.OCR.
2
+
3
+ Not yet implemented. Exists to unlock ``HFAdapter`` type overloads.
4
+ Replace with real schemas when this task gains first-class support.
5
+ """
6
+
7
+ from typing import Any, Self
8
+
9
+ from pydantic import BaseModel, ConfigDict
10
+
11
+
12
+ class OCRInput(BaseModel):
13
+ """Placeholder input schema. Fields TBD."""
14
+
15
+ model_config = ConfigDict(arbitrary_types_allowed=True)
16
+
17
+ @classmethod
18
+ def from_raw(cls, raw: Any) -> Self:
19
+ raise NotImplementedError(
20
+ "OCRInput is not implemented yet."
21
+ )
22
+
23
+ def to_pipeline_input(self) -> Any:
24
+ raise NotImplementedError(
25
+ "OCRInput is not implemented yet."
26
+ )
27
+
28
+ @property
29
+ def output_context(self) -> dict[str, Any]:
30
+ return {}
31
+
32
+
33
+ class OCROutput(BaseModel):
34
+ """Placeholder output schema. Fields TBD."""
35
+
36
+ @classmethod
37
+ def from_pipeline_output(cls, raw_output: Any, **context: Any) -> Self:
38
+ raise NotImplementedError(
39
+ "OCROutput is not implemented yet."
40
+ )
@@ -0,0 +1,97 @@
1
+ """Task-to-schema registry.
2
+
3
+ Maps ``TaskType`` values to pairs of pydantic input/output schemas.
4
+ Adapters (like ``HFAdapter``) look up schemas here based on the task
5
+ they're constructed for, so the same adapter code works across tasks
6
+ by pulling the right validation types from the registry.
7
+
8
+ Built-in schemas are registered from ``singlepipe/schemas/__init__.py``
9
+ after both the registry and the concrete schemas are loaded, which
10
+ avoids circular imports.
11
+ """
12
+ from typing import NamedTuple
13
+
14
+ from pydantic import BaseModel
15
+
16
+ from singlepipe.model_types import TaskType
17
+
18
+
19
+ class InputOutputSchema(NamedTuple):
20
+ """A pair of pydantic schemas describing a task's input and output.
21
+
22
+ Both fields hold the schema *classes*, not instances — adapters
23
+ call ``.model_validate(...)`` or ``.from_pipeline_output(...)`` on
24
+ them to materialize objects.
25
+ """
26
+
27
+ input_schema: type[BaseModel]
28
+ output_schema: type[BaseModel]
29
+
30
+
31
+ _REGISTRY: dict[TaskType, InputOutputSchema] = {}
32
+
33
+
34
+ def register(
35
+ task: TaskType,
36
+ input_schema: type[BaseModel],
37
+ output_schema: type[BaseModel],
38
+ ) -> None:
39
+ """Register input/output schemas for a task.
40
+
41
+ Overwrites any previous registration for the same task without
42
+ warning. Intended for use in package initialization (built-in
43
+ schemas) and by users adding schemas for custom tasks.
44
+
45
+ Parameters
46
+ ----------
47
+ task:
48
+ The task these schemas describe.
49
+ input_schema:
50
+ Pydantic class validating raw inputs; must expose
51
+ ``to_pipeline_input()`` for adapter use.
52
+ output_schema:
53
+ Pydantic class validating raw outputs; must expose
54
+ ``from_pipeline_output(raw, ...)`` for adapter use.
55
+
56
+ Examples
57
+ --------
58
+ >>> register(
59
+ ... TaskType.OBJECT_DETECTION,
60
+ ... ObjectDetectionInput,
61
+ ... ObjectDetectionOutput,
62
+ ... )
63
+ """
64
+ _REGISTRY[task] = InputOutputSchema(input_schema, output_schema)
65
+
66
+
67
+ def get_schemas(task: TaskType) -> InputOutputSchema:
68
+ """Look up the input/output schemas registered for a task.
69
+
70
+ Parameters
71
+ ----------
72
+ task:
73
+ The task to look up.
74
+
75
+ Returns
76
+ -------
77
+ InputOutputSchema
78
+ A named tuple ``(input_schema, output_schema)`` that can be
79
+ destructured directly: ``in_, out = get_schemas(task)``.
80
+
81
+ Raises
82
+ ------
83
+ KeyError
84
+ If no schemas are registered for ``task``. The error message
85
+ lists all currently available tasks.
86
+
87
+ Examples
88
+ --------
89
+ >>> input_schema, output_schema = get_schemas(TaskType.OBJECT_DETECTION)
90
+ >>> instance = input_schema.model_validate({"image": some_image})
91
+ """
92
+ if task not in _REGISTRY:
93
+ raise KeyError(
94
+ f"No schemas registered for task: {task}. "
95
+ f"Available: {sorted(_REGISTRY.keys())}"
96
+ )
97
+ return _REGISTRY[task]
File without changes
@@ -0,0 +1,40 @@
1
+ """Placeholder schemas for TaskType.TABULAR_CLASSIFICATION.
2
+
3
+ Not yet implemented. Exists to unlock ``HFAdapter`` type overloads.
4
+ Replace with real schemas when this task gains first-class support.
5
+ """
6
+
7
+ from typing import Any, Self
8
+
9
+ from pydantic import BaseModel, ConfigDict
10
+
11
+
12
+ class TabularClassificationInput(BaseModel):
13
+ """Placeholder input schema. Fields TBD."""
14
+
15
+ model_config = ConfigDict(arbitrary_types_allowed=True)
16
+
17
+ @classmethod
18
+ def from_raw(cls, raw: Any) -> Self:
19
+ raise NotImplementedError(
20
+ "TabularClassificationInput is not implemented yet."
21
+ )
22
+
23
+ def to_pipeline_input(self) -> Any:
24
+ raise NotImplementedError(
25
+ "TabularClassificationInput is not implemented yet."
26
+ )
27
+
28
+ @property
29
+ def output_context(self) -> dict[str, Any]:
30
+ return {}
31
+
32
+
33
+ class TabularClassificationOutput(BaseModel):
34
+ """Placeholder output schema. Fields TBD."""
35
+
36
+ @classmethod
37
+ def from_pipeline_output(cls, raw_output: Any, **context: Any) -> Self:
38
+ raise NotImplementedError(
39
+ "TabularClassificationOutput is not implemented yet."
40
+ )