| 208 | |
| 209 | |
| 210 | class MetaPreprocessor: |
| 211 | def __init__(self): |
| 212 | self.routing = { |
| 213 | "single_turn_llava": self._preprocess_single_turn_llava, |
| 214 | "caption": self._preprocess_caption |
| 215 | } |
| 216 | |
| 217 | def preprocess(self, meta_l:List[Dict], recipe: str): |
| 218 | return self.routing[recipe](meta_l) |
| 219 | |
| 220 | @ staticmethod |
| 221 | def _preprocess_single_turn_llava(meta_l: List[Dict]): |
| 222 | new_meta = [] |
| 223 | for data_item in meta_l: |
| 224 | new_meta.append({ |
| 225 | "image": data_item['image'], |
| 226 | "instruction": data_item['conversations'][0]['value'], |
| 227 | "output": data_item['conversations'][1]['value'] |
| 228 | }) |
| 229 | return new_meta |
| 230 | |
| 231 | @ staticmethod |
| 232 | def _preprocess_caption(meta_l: List[Dict]): |
| 233 | new_meta = [] |
| 234 | for data_item in meta_l: |
| 235 | caption = data_item['caption'] |
| 236 | if isinstance(caption, list): |
| 237 | caption = random.choice(caption) |
| 238 | new_meta.append({ |
| 239 | "image": data_item['url'], |
| 240 | "output": caption |
| 241 | }) |
| 242 | |
| 243 | return new_meta |
| 244 | |
| 245 | |
| 246 | class FinetuneDistSampler(Sampler): |