Preprocesses examples dictionaries as returned by our data loaders Includes loading the multi-modal data and formatting the text for the LLM
| 187 | |
| 188 | @dataclasses.dataclass |
| 189 | class ExamplePreprocessor: |
| 190 | """Preprocesses examples dictionaries as returned by our data loaders |
| 191 | |
| 192 | Includes loading the multi-modal data and formatting the text for the LLM |
| 193 | """ |
| 194 | formatter: DataFormatter |
| 195 | preprocessor: MultimodalPreprocessor |
| 196 | for_inference: bool = False |
| 197 | is_training: bool = False |
| 198 | include_image: bool = False |
| 199 | |
| 200 | def get_output_shapes(self) -> Dict[str, TensorSpec]: |
| 201 | return self.preprocessor.get_output_shapes() |
| 202 | |
| 203 | @property |
| 204 | def tokenizer(self): |
| 205 | return self.preprocessor.text_preprocessor.tokenizer |
| 206 | |
| 207 | def __call__(self, example, rng=np.random): |
| 208 | example = dict(example) |
| 209 | image: Optional[np.ndarray] = None |
| 210 | image_group: Optional[List[np.ndarray]] = None |
| 211 | video: Optional[VideoFrames] = None |
| 212 | if "image" in example: |
| 213 | is_image_group = isinstance(example["image"], (list, tuple)) |
| 214 | try: |
| 215 | if is_image_group: |
| 216 | image_group = [load_image(x) for x in example["image"]] |
| 217 | else: |
| 218 | image = load_image(example["image"]) |
| 219 | except Exception as e: |
| 220 | e.add_note(f"Could not load image: {example['image']}") |
| 221 | raise e |
| 222 | if not is_image_group: |
| 223 | # So the formatter can know the height/weight of the video |
| 224 | example["image"] = image |
| 225 | image_to_video_metadata = None |
| 226 | if "images" in example: |
| 227 | example["images"] = [load_image(x) for x in example["images"]] |
| 228 | |
| 229 | if "video" in example: |
| 230 | if isinstance(example["video"], VideoFrames): |
| 231 | video_path = None |
| 232 | video = example["video"] |
| 233 | else: |
| 234 | video_path = example["video"] |
| 235 | try: |
| 236 | decode_method = None |
| 237 | if "metadata" in example and "decode_method" in example["metadata"]: |
| 238 | decode_method = example["metadata"]["decode_method"] |
| 239 | clip = None |
| 240 | if "metadata" in example and "clip_start_time" in example["metadata"]: |
| 241 | if example["metadata"]["clip_start_time"] is not None: |
| 242 | clip = (example["metadata"]["clip_start_time"], example["metadata"]["clip_end_time"]) |
| 243 | subtitle = None |
| 244 | if 'subtitle' in example: |
| 245 | subtitle = example['subtitle'] |
| 246 | sampler_overrides = {} |
no outgoing calls
no test coverage detected