JesseLiu commited on
Commit
a63f83e
·
verified ·
1 Parent(s): 55e9ec9

Update MedQA_Maze.py

Browse files
Files changed (1) hide show
  1. MedQA_Maze.py +52 -35
MedQA_Maze.py CHANGED
@@ -1,28 +1,15 @@
1
  import os
2
  import json
3
  import datasets
 
 
 
4
 
5
  _CITATION = ""
6
  _DESCRIPTION = ""
7
 
 
8
 
9
- _URLs = {
10
- "all": {
11
- "train": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/all/train.jsonl",
12
- "test": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/all/test.jsonl"
13
- },
14
- "basic": {
15
- "test": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/basic/test.jsonl"
16
- },
17
- "advance": {
18
- "test": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/advance/test.jsonl"
19
- },
20
- "challenge": {
21
- "test": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/challenge/test.jsonl"
22
- }
23
- }
24
-
25
- # Rest of your code remains the same...
26
  class MedQaMazeConfig(datasets.BuilderConfig):
27
  def __init__(self, **kwargs):
28
  super().__init__(version=datasets.Version("1.0.0"), **kwargs)
@@ -31,8 +18,8 @@ class MedQaMaze(datasets.GeneratorBasedBuilder):
31
  BUILDER_CONFIGS = [
32
  MedQaMazeConfig(name="default", description="A default config"),
33
  MedQaMazeConfig(name="advance", description="Advanced-level test data"),
34
- MedQaMazeConfig(name="all", description="Full dataset with train and test"),
35
- MedQaMazeConfig(name="basic", description="Basic-level test data"),
36
  MedQaMazeConfig(name="challenge", description="Challenge-level test data"),
37
  ]
38
 
@@ -56,10 +43,25 @@ class MedQaMaze(datasets.GeneratorBasedBuilder):
56
  def _split_generators(self, dl_manager):
57
  """Returns SplitGenerators."""
58
  config_name = self.config.name if self.config.name != "default" else "all"
59
-
60
- # Download the files
61
- urls = _URLs[config_name]
62
- data_files = dl_manager.download_and_extract(urls)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  splits = []
65
  if "train" in data_files:
@@ -69,7 +71,6 @@ class MedQaMaze(datasets.GeneratorBasedBuilder):
69
  gen_kwargs={"filepath": data_files["train"]}
70
  )
71
  )
72
-
73
  if "test" in data_files:
74
  splits.append(
75
  datasets.SplitGenerator(
@@ -78,21 +79,37 @@ class MedQaMaze(datasets.GeneratorBasedBuilder):
78
  )
79
  )
80
 
 
 
 
81
  return splits
82
 
83
  def _generate_examples(self, filepath):
84
  """Yields examples."""
85
- with open(filepath, encoding="utf-8") as f:
86
- for idx, line in enumerate(f):
 
 
 
 
 
 
 
 
 
87
  try:
88
- data = json.loads(line.strip())
89
- yield idx, {
90
- "context": data.get("context", ""),
91
- "question": data.get("question", ""),
92
- "prerequisit": data.get("prerequisit", ""),
93
- "groundtruth_zoo": data.get("groundtruth_zoo", []),
94
- "answer": data.get("answer", ""),
95
  }
96
- except json.JSONDecodeError:
97
- print(f"Warning: Skipping invalid JSON at line {idx} in {filepath}")
 
 
 
 
98
  continue
 
1
  import os
2
  import json
3
  import datasets
4
+ import logging
5
+
6
+ logger = logging.getLogger(__name__)
7
 
8
  _CITATION = ""
9
  _DESCRIPTION = ""
10
 
11
+ _BASE_URL = "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/resolve/main"
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  class MedQaMazeConfig(datasets.BuilderConfig):
14
  def __init__(self, **kwargs):
15
  super().__init__(version=datasets.Version("1.0.0"), **kwargs)
 
18
  BUILDER_CONFIGS = [
19
  MedQaMazeConfig(name="default", description="A default config"),
20
  MedQaMazeConfig(name="advance", description="Advanced-level test data"),
21
+ MedQaMazeConfig(name="all", description="Full dataset with train and test"),
22
+ MedQaMazeConfig(name="basic", description="Basic-level test data"),
23
  MedQaMazeConfig(name="challenge", description="Challenge-level test data"),
24
  ]
25
 
 
43
  def _split_generators(self, dl_manager):
44
  """Returns SplitGenerators."""
45
  config_name = self.config.name if self.config.name != "default" else "all"
46
+
47
+ urls = {}
48
+ if config_name == "all":
49
+ urls = {
50
+ "train": f"{_BASE_URL}/all/train.jsonl",
51
+ "test": f"{_BASE_URL}/all/test.jsonl"
52
+ }
53
+ elif config_name in ["basic", "advance", "challenge"]:
54
+ urls = {
55
+ "test": f"{_BASE_URL}/{config_name}/test.jsonl"
56
+ }
57
+ else:
58
+ raise ValueError(f"Unsupported config: {config_name}")
59
+
60
+ try:
61
+ data_files = dl_manager.download(urls)
62
+ logger.info(f"Downloaded files: {data_files}")
63
+ except Exception as e:
64
+ raise ValueError(f"Failed to download files: {e}")
65
 
66
  splits = []
67
  if "train" in data_files:
 
71
  gen_kwargs={"filepath": data_files["train"]}
72
  )
73
  )
 
74
  if "test" in data_files:
75
  splits.append(
76
  datasets.SplitGenerator(
 
79
  )
80
  )
81
 
82
+ if not splits:
83
+ raise ValueError(f"No valid splits found for config {config_name}")
84
+
85
  return splits
86
 
87
  def _generate_examples(self, filepath):
88
  """Yields examples."""
89
+ logger.info(f"Generating examples from {filepath}")
90
+
91
+ if not os.path.exists(filepath):
92
+ raise ValueError(f"File not found: {filepath}")
93
+
94
+ with open(filepath, 'r', encoding='utf-8') as f:
95
+ content = f.read().strip()
96
+ # Split by newlines and filter out empty lines
97
+ lines = [line.strip() for line in content.split('\n') if line.strip()]
98
+
99
+ for idx, line in enumerate(lines):
100
  try:
101
+ data = json.loads(line)
102
+ example = {
103
+ "context": str(data.get("context", "")),
104
+ "question": str(data.get("question", "")),
105
+ "prerequisit": str(data.get("prerequisit", "")),
106
+ "groundtruth_zoo": [str(x) for x in data.get("groundtruth_zoo", [])],
107
+ "answer": str(data.get("answer", "")),
108
  }
109
+ yield idx, example
110
+ except json.JSONDecodeError as e:
111
+ logger.error(f"Error parsing JSON at line {idx} in {filepath}: {e}\nLine content: {line[:100]}...")
112
+ continue
113
+ except Exception as e:
114
+ logger.error(f"Unexpected error processing line {idx} in {filepath}: {e}")
115
  continue