Update script/preprocess.py
Browse files- script/preprocess.py +61 -16
script/preprocess.py
CHANGED
|
@@ -8,7 +8,7 @@ from tqdm import tqdm
|
|
| 8 |
|
| 9 |
def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer_name, clip_model_name, output_dir):
|
| 10 |
"""
|
| 11 |
-
Preprocesses raw JSON and image data into a Hugging Face Dataset
|
| 12 |
|
| 13 |
Args:
|
| 14 |
raw_json_path (str): Path to the raw JSON file.
|
|
@@ -27,7 +27,21 @@ def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer
|
|
| 27 |
images = []
|
| 28 |
questions = []
|
| 29 |
answers = []
|
| 30 |
-
ids = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Load CLIP and Tokenizer
|
| 33 |
clip_processor = CLIPProcessor.from_pretrained(clip_model_name)
|
|
@@ -36,22 +50,39 @@ def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer
|
|
| 36 |
|
| 37 |
# Process each data point
|
| 38 |
for i, item in enumerate(tqdm(data, desc="Processing data")):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Create Hugging Face Dataset
|
| 57 |
dataset_dict = {
|
|
@@ -59,6 +90,20 @@ def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer
|
|
| 59 |
'question': questions,
|
| 60 |
'answer': answers,
|
| 61 |
'id': ids,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
dataset = Dataset.from_dict(dataset_dict)
|
|
|
|
| 8 |
|
| 9 |
def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer_name, clip_model_name, output_dir):
|
| 10 |
"""
|
| 11 |
+
Preprocesses raw JSON with the specified format and image data into a Hugging Face Dataset.
|
| 12 |
|
| 13 |
Args:
|
| 14 |
raw_json_path (str): Path to the raw JSON file.
|
|
|
|
| 27 |
images = []
|
| 28 |
questions = []
|
| 29 |
answers = []
|
| 30 |
+
ids = []
|
| 31 |
+
subjects = []
|
| 32 |
+
topics = []
|
| 33 |
+
options = []
|
| 34 |
+
certification_levels = []
|
| 35 |
+
difficulty_levels = []
|
| 36 |
+
grade_levels = []
|
| 37 |
+
learning_objectives = []
|
| 38 |
+
question_types = []
|
| 39 |
+
time_requireds = []
|
| 40 |
+
keywords_list = []
|
| 41 |
+
sources = []
|
| 42 |
+
years = []
|
| 43 |
+
sections = []
|
| 44 |
+
languages = []
|
| 45 |
|
| 46 |
# Load CLIP and Tokenizer
|
| 47 |
clip_processor = CLIPProcessor.from_pretrained(clip_model_name)
|
|
|
|
| 50 |
|
| 51 |
# Process each data point
|
| 52 |
for i, item in enumerate(tqdm(data, desc="Processing data")):
|
| 53 |
+
question = item.get('question', '')
|
| 54 |
+
answer = item.get('answer', '')
|
| 55 |
+
image = None # Initialize image as None
|
| 56 |
+
image_path = item.get('image', None)
|
| 57 |
+
id_field = str(i)
|
| 58 |
|
| 59 |
+
if image_path:
|
| 60 |
+
try:
|
| 61 |
+
full_image_path = os.path.join(image_dir, image_path)
|
| 62 |
+
image = PILImage.open(full_image_path).convert("RGB")
|
| 63 |
+
except FileNotFoundError:
|
| 64 |
+
print(f"Warning: Image not found at {full_image_path}")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Warning: Error processing image {full_image_path}: {e}")
|
| 67 |
|
| 68 |
+
images.append(image)
|
| 69 |
+
questions.append(question)
|
| 70 |
+
answers.append(answer)
|
| 71 |
+
ids.append(id_field)
|
| 72 |
+
subjects.append(item.get('subject', None))
|
| 73 |
+
topics.append(item.get('topic', None))
|
| 74 |
+
options.append(item.get('options', None))
|
| 75 |
+
certification_levels.append(item.get('certification_level', None))
|
| 76 |
+
difficulty_levels.append(item.get('difficulty_level', None))
|
| 77 |
+
grade_levels.append(item.get('grade_level', None))
|
| 78 |
+
learning_objectives.append(item.get('learning_objective', None))
|
| 79 |
+
question_types.append(item.get('question_type', None))
|
| 80 |
+
time_requireds.append(item.get('time_required', None))
|
| 81 |
+
keywords_list.append(item.get('keywords', None))
|
| 82 |
+
sources.append(item.get('source', None))
|
| 83 |
+
years.append(item.get('year', None))
|
| 84 |
+
sections.append(item.get('section', None))
|
| 85 |
+
languages.append(item.get('language', None))
|
| 86 |
|
| 87 |
# Create Hugging Face Dataset
|
| 88 |
dataset_dict = {
|
|
|
|
| 90 |
'question': questions,
|
| 91 |
'answer': answers,
|
| 92 |
'id': ids,
|
| 93 |
+
'subject': subjects,
|
| 94 |
+
'topic': topics,
|
| 95 |
+
'options': options,
|
| 96 |
+
'certification_level': certification_levels,
|
| 97 |
+
'difficulty_level': difficulty_levels,
|
| 98 |
+
'grade_level': grade_levels,
|
| 99 |
+
'learning_objective': learning_objectives,
|
| 100 |
+
'question_type': question_types,
|
| 101 |
+
'time_required': time_requireds,
|
| 102 |
+
'keywords': keywords_list,
|
| 103 |
+
'source': sources,
|
| 104 |
+
'year': years,
|
| 105 |
+
'section': sections,
|
| 106 |
+
'language': languages,
|
| 107 |
}
|
| 108 |
|
| 109 |
dataset = Dataset.from_dict(dataset_dict)
|