preprocess the data Args: model_dir (str): model path mode: preprocessor mode (model mode)
(self,
model_dir: str,
mode=ModeKeys.INFERENCE,
*args,
**kwargs)
| 84 | class OfaPreprocessor(Preprocessor): |
| 85 | |
| 86 | def __init__(self, |
| 87 | model_dir: str, |
| 88 | mode=ModeKeys.INFERENCE, |
| 89 | *args, |
| 90 | **kwargs): |
| 91 | """preprocess the data |
| 92 | |
| 93 | Args: |
| 94 | model_dir (str): model path |
| 95 | mode: preprocessor mode (model mode) |
| 96 | """ |
| 97 | super().__init__(*args, **kwargs) |
| 98 | preprocess_mapping = { |
| 99 | Tasks.ocr_recognition: OfaOcrRecognitionPreprocessor, |
| 100 | Tasks.image_captioning: OfaImageCaptioningPreprocessor, |
| 101 | Tasks.visual_grounding: OfaVisualGroundingPreprocessor, |
| 102 | Tasks.visual_question_answering: |
| 103 | OfaVisualQuestionAnsweringPreprocessor, |
| 104 | Tasks.visual_entailment: OfaVisualEntailmentPreprocessor, |
| 105 | Tasks.image_classification: OfaImageClassificationPreprocessor, |
| 106 | Tasks.text_classification: OfaTextClassificationPreprocessor, |
| 107 | Tasks.text_summarization: OfaSummarizationPreprocessor, |
| 108 | Tasks.text_to_image_synthesis: OfaTextToImageSynthesisPreprocessor, |
| 109 | Tasks.auto_speech_recognition: OfaASRPreprocessor, |
| 110 | Tasks.sudoku: OfaSudokuPreprocessor, |
| 111 | Tasks.text2sql: OfaTextToSqlPreprocessor |
| 112 | } |
| 113 | model_dir = model_dir if osp.exists(model_dir) else snapshot_download( |
| 114 | model_dir, user_agent={Invoke.KEY: Invoke.PREPROCESSOR}) |
| 115 | self.cfg = Config.from_file( |
| 116 | osp.join(model_dir, ModelFile.CONFIGURATION)) |
| 117 | self.preprocess = preprocess_mapping[self.cfg.task]( |
| 118 | cfg=self.cfg, model_dir=model_dir, mode=mode) |
| 119 | self.keys = OFA_TASK_KEY_MAPPING[self.cfg.task] |
| 120 | self.tokenizer = self.preprocess.tokenizer |
| 121 | if kwargs.get('no_collate', None): |
| 122 | self.no_collate = True |
| 123 | else: |
| 124 | self.no_collate = False |
| 125 | |
| 126 | # just for modelscope demo |
| 127 | def _build_dict(self, input: Union[Input, List[Input]]) -> Dict[str, Any]: |