RyanWW commited on
Commit
f03859b
·
verified ·
1 Parent(s): b054627

Upload Spatial457.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. Spatial457.py +18 -11
Spatial457.py CHANGED
@@ -40,32 +40,39 @@ class Spatial457(datasets.GeneratorBasedBuilder):
40
  base_url = "https://huggingface.co/datasets/RyanWW/Spatial457/resolve/main"
41
 
42
  json_url = f"{base_url}/questions/{self.config.name}.json"
43
- image_url_template = f"{base_url}/images"
44
  # 下载 json 文件
45
  task_json = dl_manager.download(json_url)
46
- # 下载整个 images 目录(如果太大,也可以改成逐张下载或改为 tar 包)
47
- image_dir = dl_manager.download_and_extract(image_url_template)
48
-
49
- # data_dir = dl_manager.manual_dir # expects local `images/` and `questions/`
50
- # task_json = os.path.join(data_dir, "questions", f"{self.config.name}.json")
51
- # image_dir = os.path.join(data_dir, "images")
 
 
 
 
 
 
 
52
 
53
  return [
54
  datasets.SplitGenerator(
55
  name=datasets.Split.VALIDATION,
56
- gen_kwargs={"json_file": task_json, "image_dir": image_dir}
57
  )
58
  ]
59
 
60
- def _generate_examples(self, json_file, image_dir):
61
  with open(json_file, "r", encoding="utf-8") as f:
62
  all_data = json.load(f)["questions"]
63
 
64
  for idx, q in enumerate(all_data):
65
- img_path = os.path.join(image_dir, q["image_filename"])
 
66
  yield idx, {
67
  "image": img_path,
68
- "image_filename": q["image_filename"],
69
  "question": q["question"],
70
  "answer": str(q["answer"]),
71
  "question_index": q["question_index"],
 
40
  base_url = "https://huggingface.co/datasets/RyanWW/Spatial457/resolve/main"
41
 
42
  json_url = f"{base_url}/questions/{self.config.name}.json"
 
43
  # 下载 json 文件
44
  task_json = dl_manager.download(json_url)
45
+
46
+ # 读取 JSON 获取所有图片文件名
47
+ with open(task_json, "r", encoding="utf-8") as f:
48
+ all_data = json.load(f)["questions"]
49
+
50
+ # 构建所有图片的 URL
51
+ image_urls = {
52
+ q["image_filename"]: f"{base_url}/images/{q['image_filename']}"
53
+ for q in all_data
54
+ }
55
+
56
+ # 批量下载所有图片
57
+ downloaded_images = dl_manager.download(image_urls)
58
 
59
  return [
60
  datasets.SplitGenerator(
61
  name=datasets.Split.VALIDATION,
62
+ gen_kwargs={"json_file": task_json, "downloaded_images": downloaded_images}
63
  )
64
  ]
65
 
66
+ def _generate_examples(self, json_file, downloaded_images):
67
  with open(json_file, "r", encoding="utf-8") as f:
68
  all_data = json.load(f)["questions"]
69
 
70
  for idx, q in enumerate(all_data):
71
+ img_filename = q["image_filename"]
72
+ img_path = downloaded_images[img_filename]
73
  yield idx, {
74
  "image": img_path,
75
+ "image_filename": img_filename,
76
  "question": q["question"],
77
  "answer": str(q["answer"]),
78
  "question_index": q["question_index"],