Cie1 commited on
Commit
e95139d
·
verified ·
1 Parent(s): e0041d0

Update MMSearch-Plus dataset with encrypted text fields (images unchanged)

Browse files
Files changed (1) hide show
  1. mmsearch_plus.py +67 -92
mmsearch_plus.py CHANGED
@@ -7,8 +7,9 @@ Images are NOT encrypted and remain as PIL Image objects for accessibility.
7
  import base64
8
  import hashlib
9
  import os
10
- from typing import Dict, Any, List
11
  import datasets
 
12
 
13
  _CITATION = """\
14
  @article{tao2025mmsearch,
@@ -29,13 +30,6 @@ _HOMEPAGE = "https://mmsearch-plus.github.io/"
29
 
30
  _LICENSE = "CC BY-NC 4.0"
31
 
32
- _URLS = {
33
- "train": [
34
- "data-00000-of-00002.arrow",
35
- "data-00001-of-00002.arrow"
36
- ]
37
- }
38
-
39
  def derive_key(password: str, length: int) -> bytes:
40
  """Derive encryption key from password using SHA-256."""
41
  hasher = hashlib.sha256()
@@ -56,82 +50,76 @@ def decrypt_text(ciphertext_b64: str, password: str) -> str:
56
  except Exception:
57
  return ciphertext_b64
58
 
59
- class MmsearchPlus(datasets.GeneratorBasedBuilder):
60
- """MMSearch-Plus dataset with transparent decryption."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  VERSION = datasets.Version("1.0.0")
63
 
64
  def _info(self):
65
- # Define features to handle the complete dataset schema
66
- features = datasets.Features({
67
- "question": datasets.Value("string"),
68
- "answer": datasets.Sequence(datasets.Value("string")),
69
- "num_images": datasets.Value("int64"),
70
- "arxiv_id": datasets.Value("string"),
71
- "video_url": datasets.Value("string"),
72
- "category": datasets.Value("string"),
73
- "difficulty": datasets.Value("string"),
74
- "subtask": datasets.Value("string"),
75
- # Image fields (not encrypted, kept as PIL Images)
76
- "img_1": datasets.Image(),
77
- "img_2": datasets.Image(),
78
- "img_3": datasets.Image(),
79
- "img_4": datasets.Image(),
80
- "img_5": datasets.Image(),
81
- # Additional fields that might exist in the dataset
82
- "choices": datasets.Sequence(datasets.Value("string")),
83
- "question_zh": datasets.Value("string"),
84
- "answer_zh": datasets.Sequence(datasets.Value("string")),
85
- "regex": datasets.Value("string"),
86
- "text_criteria": datasets.Value("string"),
87
- "original_filename": datasets.Value("string"),
88
- "screenshots_dir": datasets.Value("string"),
89
- "time_points": datasets.Sequence(datasets.Value("string")),
90
- "search_query": datasets.Value("string"),
91
- "question_type": datasets.Value("string"),
92
- "requires_image_understanding": datasets.Value("bool"),
93
- "source": datasets.Value("string"),
94
- "content_keywords": datasets.Value("string"),
95
- "reasoning": datasets.Value("string"),
96
- "processed_at": datasets.Value("string"),
97
- "model_used": datasets.Value("string"),
98
- "entry_index": datasets.Value("int64"),
99
- "original_image_paths": datasets.Sequence(datasets.Value("string")),
100
- "masked_image_paths": datasets.Sequence(datasets.Value("string")),
101
- "is_valid": datasets.Value("bool"),
102
- })
103
-
104
- return datasets.DatasetInfo(
105
  description=_DESCRIPTION,
106
- features=features,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  homepage=_HOMEPAGE,
108
  license=_LICENSE,
109
  citation=_CITATION,
110
  )
111
 
112
  def _split_generators(self, dl_manager):
113
- # Get canary from environment variable or kwargs
114
  canary = os.environ.get("MMSEARCH_PLUS")
115
-
116
- # Check if passed in the builder's initialization
117
- if hasattr(self, 'canary'):
118
- canary = self.canary
119
-
120
  if not canary:
121
  raise ValueError(
122
- "Canary string is required for decryption. Either set the MMSEARCH_PLUS "
123
- "environment variable or pass it via the dataset loading kwargs. "
124
- "Example: load_dataset('path/to/dataset', trust_remote_code=True) after setting "
125
- "os.environ['MMSEARCH_PLUS'] = 'your_canary_string'"
 
126
  )
127
-
128
- # Download files
129
- urls = _URLS["train"]
130
  downloaded_files = dl_manager.download(urls)
131
-
132
  return [
133
- datasets.SplitGenerator(
134
- name=datasets.Split.TRAIN,
135
  gen_kwargs={
136
  "filepaths": downloaded_files,
137
  "canary": canary,
@@ -141,34 +129,21 @@ class MmsearchPlus(datasets.GeneratorBasedBuilder):
141
 
142
  def _generate_examples(self, filepaths, canary):
143
  """Generate examples with transparent decryption."""
 
 
 
144
  key = 0
145
-
146
  for filepath in filepaths:
147
- # Load the arrow file
148
  arrow_dataset = datasets.Dataset.from_file(filepath)
149
-
150
  for idx in range(len(arrow_dataset)):
151
  example = arrow_dataset[idx]
152
-
153
- # Decrypt text fields - matches encryption script fields
154
- text_fields = ['question', 'video_url', 'arxiv_id']
155
-
156
- for field in text_fields:
157
- if example.get(field):
158
- example[field] = decrypt_text(example[field], canary)
159
-
160
- # Handle answer field (list of strings)
161
- if example.get("answer"):
162
- decrypted_answers = []
163
- for answer in example["answer"]:
164
- if answer:
165
- decrypted_answers.append(decrypt_text(answer, canary))
166
- else:
167
- decrypted_answers.append(answer)
168
- example["answer"] = decrypted_answers
169
-
170
- # Images are not encrypted - they remain as PIL Image objects
171
- # No image decryption needed as per the encryption script
172
-
173
  yield key, example
174
- key += 1
 
 
 
7
  import base64
8
  import hashlib
9
  import os
10
+ from typing import Dict, Any
11
  import datasets
12
+ from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Features, Value, Sequence, Image, Split
13
 
14
  _CITATION = """\
15
  @article{tao2025mmsearch,
 
30
 
31
  _LICENSE = "CC BY-NC 4.0"
32
 
 
 
 
 
 
 
 
33
  def derive_key(password: str, length: int) -> bytes:
34
  """Derive encryption key from password using SHA-256."""
35
  hasher = hashlib.sha256()
 
50
  except Exception:
51
  return ciphertext_b64
52
 
53
+ def decrypt_example(example: Dict[str, Any], canary: str) -> Dict[str, Any]:
54
+ """Decrypt text fields in a single example."""
55
+ # Decrypt text fields - matches encryption script fields
56
+ text_fields = ['question', 'video_url', 'arxiv_id']
57
+
58
+ for field in text_fields:
59
+ if field in example and example[field]:
60
+ example[field] = decrypt_text(example[field], canary)
61
+
62
+ # Handle answer field (list of strings)
63
+ if 'answer' in example and example['answer']:
64
+ decrypted_answers = []
65
+ for answer in example['answer']:
66
+ if answer:
67
+ decrypted_answers.append(decrypt_text(answer, canary))
68
+ else:
69
+ decrypted_answers.append(answer)
70
+ example['answer'] = decrypted_answers
71
+
72
+ # Images are NOT encrypted - they remain as PIL Image objects
73
+ return example
74
+
75
+ class MmsearchPlus(GeneratorBasedBuilder):
76
+ """MMSearch-Plus dataset builder with transparent decryption."""
77
 
78
  VERSION = datasets.Version("1.0.0")
79
 
80
  def _info(self):
81
+ return DatasetInfo(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  description=_DESCRIPTION,
83
+ features=Features({
84
+ "question": Value("string"),
85
+ "answer": Sequence(Value("string")),
86
+ "num_images": Value("int64"),
87
+ "arxiv_id": Value("string"),
88
+ "video_url": Value("string"),
89
+ "category": Value("string"),
90
+ "difficulty": Value("string"),
91
+ "subtask": Value("string"),
92
+ "img_1": Image(),
93
+ "img_2": Image(),
94
+ "img_3": Image(),
95
+ "img_4": Image(),
96
+ "img_5": Image(),
97
+ }),
98
  homepage=_HOMEPAGE,
99
  license=_LICENSE,
100
  citation=_CITATION,
101
  )
102
 
103
  def _split_generators(self, dl_manager):
104
+ # Get canary from environment variable
105
  canary = os.environ.get("MMSEARCH_PLUS")
106
+
 
 
 
 
107
  if not canary:
108
  raise ValueError(
109
+ "\n\n⚠️ Canary string is required for decryption!\n\n"
110
+ "Please set the environment variable before loading:\n"
111
+ " import os\n"
112
+ " os.environ['MMSEARCH_PLUS'] = 'your_canary_string'\n\n"
113
+ "Hint: The canary is the name of this dataset repository (without the username).\n"
114
  )
115
+
116
+ # Download arrow files
117
+ urls = ["data-00000-of-00002.arrow", "data-00001-of-00002.arrow"]
118
  downloaded_files = dl_manager.download(urls)
119
+
120
  return [
121
+ SplitGenerator(
122
+ name=Split.TRAIN,
123
  gen_kwargs={
124
  "filepaths": downloaded_files,
125
  "canary": canary,
 
129
 
130
  def _generate_examples(self, filepaths, canary):
131
  """Generate examples with transparent decryption."""
132
+ import sys
133
+ print(f"🔓 [MMSearch-Plus] Decrypting dataset with canary: {canary[:10]}...", file=sys.stderr)
134
+
135
  key = 0
 
136
  for filepath in filepaths:
137
+ # Load the arrow file directly
138
  arrow_dataset = datasets.Dataset.from_file(filepath)
139
+
140
  for idx in range(len(arrow_dataset)):
141
  example = arrow_dataset[idx]
142
+
143
+ # Apply decryption
144
+ example = decrypt_example(example, canary)
145
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  yield key, example
147
+ key += 1
148
+
149
+ print(f"✅ [MMSearch-Plus] Decrypted {key} samples successfully!", file=sys.stderr)