|
1 | 1 | # src/ocrsmith/config/schema.py |
2 | | -from pydantic import BaseModel, Field, field_validator, model_validator |
3 | | -from typing import List, Optional, Tuple, Union, Literal |
4 | | -import yaml |
| 2 | +from typing import Literal |
| 3 | + |
| 4 | +from pydantic import BaseModel, Field |
| 5 | + |
5 | 6 |
|
6 | 7 | class FontConfig(BaseModel): |
7 | | - name: Optional[str] = None |
| 8 | + name: str | None = None |
8 | 9 | path: str |
9 | | - size: Optional[int] = 24 |
| 10 | + size: int | None = 24 |
| 11 | + |
10 | 12 |
|
11 | 13 | class SolidBackgroundConfig(BaseModel): |
12 | 14 | type: Literal["solid"] |
13 | | - color: Union[str, Tuple[int, int, int]] = (255, 255, 255) |
| 15 | + color: str | tuple[int, int, int] = (255, 255, 255) |
14 | 16 | weight: float = 1.0 |
15 | 17 |
|
| 18 | + |
16 | 19 | class ImageBackgroundConfig(BaseModel): |
17 | 20 | type: Literal["image"] |
18 | 21 | image_path: str |
19 | 22 | mode: Literal["stretch", "crop", "tile", "center"] = "stretch" |
20 | 23 | weight: float = 1.0 |
21 | 24 |
|
| 25 | + |
22 | 26 | class GradientBackgroundConfig(BaseModel): |
23 | 27 | type: Literal["gradient"] |
24 | | - start_color: Tuple[int, int, int] = (255, 255, 255) |
25 | | - end_color: Tuple[int, int, int] = (200, 200, 200) |
| 28 | + start_color: tuple[int, int, int] = (255, 255, 255) |
| 29 | + end_color: tuple[int, int, int] = (200, 200, 200) |
26 | 30 | direction: Literal["horizontal", "vertical", "diagonal"] = "horizontal" |
27 | 31 | weight: float = 1.0 |
28 | 32 |
|
| 33 | + |
29 | 34 | class TextureBackgroundConfig(BaseModel): |
30 | 35 | type: Literal["texture"] |
31 | | - base_color: Tuple[int, int, int] = (240, 240, 240) |
| 36 | + base_color: tuple[int, int, int] = (240, 240, 240) |
32 | 37 | noise_level: int = Field(default=20, ge=0, le=255) |
33 | 38 | weight: float = 1.0 |
34 | 39 |
|
35 | | -BackgroundConfigUnion = Union[ |
36 | | - SolidBackgroundConfig, |
37 | | - ImageBackgroundConfig, |
38 | | - GradientBackgroundConfig, |
39 | | - TextureBackgroundConfig |
40 | | -] |
| 40 | + |
| 41 | +BackgroundConfigUnion = ( |
| 42 | + SolidBackgroundConfig | ImageBackgroundConfig | GradientBackgroundConfig | TextureBackgroundConfig |
| 43 | +) |
| 44 | + |
41 | 45 |
|
42 | 46 | class SimpleTextRendererConfig(BaseModel): |
43 | 47 | type: Literal["simple"] |
44 | | - color: Tuple[int, int, int] = (0, 0, 0) |
| 48 | + color: tuple[int, int, int] = (0, 0, 0) |
45 | 49 | weight: float = 1.0 |
46 | 50 |
|
| 51 | + |
47 | 52 | class OutlinedTextRendererConfig(BaseModel): |
48 | 53 | type: Literal["outlined"] |
49 | | - fill_color: Tuple[int, int, int] = (255, 255, 255) |
50 | | - outline_color: Tuple[int, int, int] = (0, 0, 0) |
| 54 | + fill_color: tuple[int, int, int] = (255, 255, 255) |
| 55 | + outline_color: tuple[int, int, int] = (0, 0, 0) |
51 | 56 | outline_width: int = 2 |
52 | 57 | weight: float = 1.0 |
53 | 58 |
|
| 59 | + |
54 | 60 | class ShadowedTextRendererConfig(BaseModel): |
55 | 61 | type: Literal["shadowed"] |
56 | | - text_color: Tuple[int, int, int] = (0, 0, 0) |
57 | | - shadow_color: Tuple[int, int, int] = (128, 128, 128) |
58 | | - shadow_offset: Tuple[int, int] = (2, 2) |
| 62 | + text_color: tuple[int, int, int] = (0, 0, 0) |
| 63 | + shadow_color: tuple[int, int, int] = (128, 128, 128) |
| 64 | + shadow_offset: tuple[int, int] = (2, 2) |
59 | 65 | weight: float = 1.0 |
60 | 66 |
|
| 67 | + |
61 | 68 | class GradientTextRendererConfig(BaseModel): |
62 | 69 | type: Literal["gradient"] |
63 | | - start_color: Tuple[int, int, int] = (255, 0, 0) |
64 | | - end_color: Tuple[int, int, int] = (0, 0, 255) |
| 70 | + start_color: tuple[int, int, int] = (255, 0, 0) |
| 71 | + end_color: tuple[int, int, int] = (0, 0, 255) |
65 | 72 | weight: float = 1.0 |
66 | 73 |
|
| 74 | + |
67 | 75 | class HorizontalTextRendererConfig(BaseModel): |
68 | 76 | type: Literal["horizontal"] |
69 | 77 | weight: float = 1.0 |
70 | 78 |
|
71 | | -TextRendererConfigUnion = Union[ |
72 | | - HorizontalTextRendererConfig, |
73 | | - SimpleTextRendererConfig, |
74 | | - OutlinedTextRendererConfig, |
75 | | - ShadowedTextRendererConfig, |
76 | | - GradientTextRendererConfig |
77 | | -] |
| 79 | + |
| 80 | +TextRendererConfigUnion = ( |
| 81 | + HorizontalTextRendererConfig |
| 82 | + | SimpleTextRendererConfig |
| 83 | + | OutlinedTextRendererConfig |
| 84 | + | ShadowedTextRendererConfig |
| 85 | + | GradientTextRendererConfig |
| 86 | +) |
| 87 | + |
78 | 88 |
|
79 | 89 | class CenterPlacementConfig(BaseModel): |
80 | 90 | type: Literal["center"] |
81 | 91 | padding: int = 20 |
82 | 92 | weight: float = 1.0 |
83 | 93 |
|
| 94 | + |
84 | 95 | class RandomPlacementConfig(BaseModel): |
85 | 96 | type: Literal["random"] |
86 | 97 | margin: int = 50 |
87 | 98 | weight: float = 1.0 |
88 | 99 |
|
| 100 | + |
89 | 101 | class GridPlacementConfig(BaseModel): |
90 | 102 | type: Literal["grid"] |
91 | 103 | rows: int = 3 |
92 | 104 | cols: int = 3 |
93 | 105 | padding: int = 10 |
94 | 106 | weight: float = 1.0 |
95 | 107 |
|
| 108 | + |
96 | 109 | class PageNumberPlacementConfig(BaseModel): |
97 | 110 | type: Literal["page_number"] |
98 | 111 | position: Literal["bottom_left", "bottom_right", "bottom_center"] = "bottom_right" |
99 | 112 | margin: int = 20 |
100 | 113 | weight: float = 1.0 |
101 | 114 |
|
| 115 | + |
102 | 116 | class PageTitlePlacementConfig(BaseModel): |
103 | 117 | type: Literal["page_title"] |
104 | 118 | position: Literal["top_left", "top_right", "top_center"] = "top_center" |
105 | 119 | margin: int = 30 |
106 | 120 | weight: float = 1.0 |
107 | 121 |
|
108 | | -TextPlacementConfigUnion = Union[ |
109 | | - CenterPlacementConfig, |
110 | | - RandomPlacementConfig, |
111 | | - GridPlacementConfig, |
112 | | - PageNumberPlacementConfig, |
113 | | - PageTitlePlacementConfig |
114 | | -] |
| 122 | + |
| 123 | +TextPlacementConfigUnion = ( |
| 124 | + CenterPlacementConfig |
| 125 | + | RandomPlacementConfig |
| 126 | + | GridPlacementConfig |
| 127 | + | PageNumberPlacementConfig |
| 128 | + | PageTitlePlacementConfig |
| 129 | +) |
| 130 | + |
115 | 131 |
|
116 | 132 | class BaseAugmentationConfig(BaseModel): |
117 | | - probability: Optional[float] = 1.0 |
118 | | - enabled: Optional[bool] = True |
| 133 | + probability: float | None = 1.0 |
| 134 | + enabled: bool | None = True |
119 | 135 | weight: float = 1.0 |
120 | 136 |
|
| 137 | + |
121 | 138 | class BlurAugmentationConfig(BaseAugmentationConfig): |
122 | 139 | type: Literal["blur"] |
123 | | - blur_radius: Union[float, Tuple[float, float]] = 1.0 |
| 140 | + blur_radius: float | tuple[float, float] = 1.0 |
| 141 | + |
124 | 142 |
|
125 | 143 | class NoiseAugmentationConfig(BaseAugmentationConfig): |
126 | 144 | type: Literal["noise"] |
127 | | - noise_factor: Union[float, Tuple[float, float]] = 0.1 |
| 145 | + noise_factor: float | tuple[float, float] = 0.1 |
| 146 | + |
128 | 147 |
|
129 | 148 | class RotationAugmentationConfig(BaseAugmentationConfig): |
130 | 149 | type: Literal["rotation"] |
131 | | - max_angle: Union[float, Tuple[float, float]] = 5.0 |
| 150 | + max_angle: float | tuple[float, float] = 5.0 |
| 151 | + |
132 | 152 |
|
133 | 153 | class BrightnessAugmentationConfig(BaseAugmentationConfig): |
134 | 154 | type: Literal["brightness"] |
135 | | - brightness_factor: Union[float, Tuple[float, float]] = 0.8 |
| 155 | + brightness_factor: float | tuple[float, float] = 0.8 |
| 156 | + |
| 157 | + |
| 158 | +AugmentationConfigUnion = ( |
| 159 | + BlurAugmentationConfig |
| 160 | + | NoiseAugmentationConfig |
| 161 | + | RotationAugmentationConfig |
| 162 | + | BrightnessAugmentationConfig |
| 163 | +) |
136 | 164 |
|
137 | | -AugmentationConfigUnion = Union[ |
138 | | - BlurAugmentationConfig, |
139 | | - NoiseAugmentationConfig, |
140 | | - RotationAugmentationConfig, |
141 | | - BrightnessAugmentationConfig |
142 | | -] |
143 | 165 |
|
144 | 166 | class LayoutConfig(BaseModel): |
145 | 167 | type: str |
146 | | - padding: Optional[int] = 50 |
147 | | - max_width: Optional[int] = None |
148 | | - max_height: Optional[int] = None |
149 | | - min_width: Optional[int] = None |
150 | | - min_height: Optional[int] = None |
| 168 | + padding: int | None = 50 |
| 169 | + max_width: int | None = None |
| 170 | + max_height: int | None = None |
| 171 | + min_width: int | None = None |
| 172 | + min_height: int | None = None |
| 173 | + |
151 | 174 |
|
152 | 175 | class OutputConfig(BaseModel): |
153 | 176 | images_dir: str |
154 | 177 | metadata_file: str |
155 | 178 |
|
| 179 | + |
156 | 180 | class DatasetConfig(BaseModel): |
157 | 181 | source: str |
158 | 182 | path: str |
159 | 183 |
|
| 184 | + |
160 | 185 | class TextDataConfig(BaseModel): |
161 | 186 | """Configuration for loading text data from various sources.""" |
162 | | - source_type: Literal['csv', 'parquet', 'huggingface'] |
| 187 | + |
| 188 | + source_type: Literal["csv", "parquet", "huggingface"] |
163 | 189 | source_path: str |
164 | | - text_column: str = 'text' |
165 | | - title_column: Optional[str] = None |
| 190 | + text_column: str = "text" |
| 191 | + title_column: str | None = None |
166 | 192 | # Optional fields, mainly for Hugging Face datasets |
167 | | - split: Optional[str] = 'train' |
168 | | - name: Optional[str] = None # For datasets with multiple configurations (e.g., 'wikitext-103-raw-v1') |
169 | | - data_dir: Optional[str] = None # For datasets that require manual download |
| 193 | + split: str | None = "train" |
| 194 | + name: str | None = None # For datasets with multiple configurations (e.g., 'wikitext-103-raw-v1') |
| 195 | + data_dir: str | None = None # For datasets that require manual download |
| 196 | + |
170 | 197 |
|
171 | 198 | class AppConfig(BaseModel): |
172 | | - fonts: List[FontConfig] |
173 | | - backgrounds: List[BackgroundConfigUnion] |
174 | | - text_renderers: Optional[List[TextRendererConfigUnion]] = [] |
175 | | - text_placements: Optional[List[TextPlacementConfigUnion]] = [] |
176 | | - augmentations: Optional[List[AugmentationConfigUnion]] = [] |
177 | | - augmentation_order: Literal['random','fixed'] = 'random' |
| 199 | + fonts: list[FontConfig] |
| 200 | + backgrounds: list[BackgroundConfigUnion] |
| 201 | + text_renderers: list[TextRendererConfigUnion] | None = [] |
| 202 | + text_placements: list[TextPlacementConfigUnion] | None = [] |
| 203 | + augmentations: list[AugmentationConfigUnion] | None = [] |
| 204 | + augmentation_order: Literal["random", "fixed"] = "random" |
178 | 205 | layout: LayoutConfig |
179 | 206 | output: OutputConfig |
180 | | - text_data: Optional[TextDataConfig] = None |
181 | | - seed: Optional[int] = None |
182 | | - |
| 207 | + text_data: TextDataConfig | None = None |
| 208 | + seed: int | None = None |
0 commit comments