Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example OCRBench dataset #2677

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/custom/OCRBench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

from typing import Any, Dict, Optional
from swift.llm import (load_dataset, register_dataset, DatasetMeta)
from swift.llm.dataset import ResponsePreprocessor



class OCRBenchPreprocessor(ResponsePreprocessor):

def preprocess(self, row: Dict[str, Any]) -> Optional[Dict[str, Any]]:
import os
row = super().preprocess(row)
image = row['image_path']
if not image:
return
image = os.path.join(self.prefix_path, image)
if not os.path.exists(image):
return
row['images'] = [image]
return row

def prepare_dataset(self, dataset):
"local dataset"

self.prefix_path = "/datasets/OCRBench/OCRBench_Images/"
return super().prepare_dataset(dataset)


register_dataset(
DatasetMeta(
dataset_path='/datasets/OCRBench/OCRBench.json',
preprocess_func=OCRBenchPreprocessor(),

))




if __name__ == '__main__':
# The Shell script can view `examples/pytorch/llm/scripts/custom`.
# test dataset
train_dataset, val_dataset = load_dataset('/datasets/OCRBench/OCRBench.json')
print(f'train_dataset: {train_dataset}')
print(f'val_dataset: {val_dataset}')