def _read_from_files(self, source_file):
        """
        <s> <image> image token </image> sentence <image> image token </image> sentence </s>
        1, sample a random subsequnece:  3 sentences + the first image ... take up to 5 images + 3 sentences
        2, filter html tags <p>, <br>, <br/>
        3, single image, random sample rate as 0.5
        """
        file_path = os.path.join(self.data_dir, source_file)
        
        if not os.path.exists(file_path):
            print('| file {} not exists'.format(file_path), flush=True)
            return iter([]) # skip bad file

        try:
            with open(file_path, 'r', encoding='utf8') as f:
                lines = f.read().strip().split('\n')
        except:
            return iter([]) # skip bad file

        for doc_jsonstr in lines:
            try:
                json_obj = json.loads(doc_jsonstr)
                doc = [self.dictionary.bos()]
                start_idx = 0
                image_num = len(json_obj[IMAGE_KEY])
                if image_num == 1:
                    r = random.random()
                    if r > 0.5:
                        continue
                for image_idx, image_item in enumerate(json_obj[IMAGE_KEY]):
                    if image_idx >= self.max_image_num:
                        if len(doc) < self.tokens_per_sample:
                            yield doc
                        break
                    
                    text_snippet = json_obj[TEXT_KEY][start_idx:image_item['Span'][0]-1]
                    text_snippet = self.clean(text_snippet)
                    if len(text_snippet) != 0:
                        if image_idx == 0:
                            # crop 3 sentences before the first image
                            sentences = list(self.nlp_sentencizer(text_snippet).sents)
                            text_snippet = ' '.join([str(sent) for sent in sentences[-3:]])
                        text_token = self.text_transform(text_snippet)
                        doc.extend(text_token)
                        if len(doc) >= self.tokens_per_sample: # drop too long sentence
                            # data.append(doc[:])
                            doc = doc[:self.tokens_per_sample - 2]
                            doc.append(self.dictionary.eos())
                            yield doc
                            break
                        
                    image_tokens = [image_code_to_token(i) for i in image_item['input_ids']]
                    image_tokens = WildLoader.fs_encode_line(self.dictionary, image_tokens, append_eos=False)
                    doc.append(self.dictionary.index(BOI_SYMBOL))
                    doc.extend(image_tokens)
                    doc.append(self.dictionary.index(EOI_SYMBOL))

                    start_idx = image_item['Span'][1] + 1
                    if image_idx == image_num - 1:
                        # crop 3 sentences after the last image
                        text_snippet = json_obj[TEXT_KEY][start_idx:]
                        text_snippet = self.clean(text_snippet)
                        sentences = list(self.nlp_sentencizer(text_snippet).sents)
                        text_snippet = ' '.join([str(sent) for sent in sentences[:3]])
                        text_token = self.text_transform(text_snippet)
                        doc.extend(text_token)
                        doc.append(self.dictionary.eos())
                        if len(doc) < self.tokens_per_sample:
                            yield doc
                            break
            except:
                continue

        <s> <image> image token </image> sentence <image> image token </image> sentence </s>
        1, sample a random subsequnece:  3 sentences + the first image ... take up to 5 images + 3 sentences
        2, filter html tags <p>, <br>, <br/>
        3, single image, random sample rate as 0.5
        
kosmos-2/torchscale/examples/fairseq/tasks/data/wild_loader.py