adria-nsanc commited on
Commit
7a47cc7
·
verified ·
1 Parent(s): fa1785d

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +55 -0
  2. clean.py +152 -0
  3. dataset_infos.json +26 -0
  4. metadata_sample.jsonl +24 -0
README.md ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - dataset
5
+ - education
6
+ - image-audio
7
+ ---
8
+
9
+ # Education Image Audio Data Notes
10
+
11
+ ## Dataset summary
12
+
13
+ A documented **Education** data-preparation workflow for **Image Audio** records. The bundled rows demonstrate the schema and validation path rather than pretending to be a full training corpus.
14
+
15
+ ## Included material
16
+
17
+ - `clean.py` — loading, cleaning, and split preparation code.
18
+ - `dataset_infos.json` — schema and split metadata.
19
+ - `metadata_sample.jsonl` — small, human-readable records for checking the schema.
20
+ - `README.md` — data card and usage notes.
21
+
22
+ ## Processing choices
23
+
24
+ | Stage | Setting |
25
+ |---|---|
26
+ | Storage format | arrow |
27
+ | Preprocessing | adaptive |
28
+ | Augmentation | none |
29
+ | Split strategy | temporal |
30
+ | Sampling | balanced |
31
+ | Quality checks | strict |
32
+ | Labeling | self training |
33
+
34
+ ## Validation checklist
35
+
36
+ Before using the prepared data, verify source licenses, duplicates across splits, missing values, label balance, and modality-specific corruption. Record the source version and every filtering rule so a later run can reproduce the same rows.
37
+
38
+ ## Intended use
39
+
40
+ The repository is suitable for testing the data pipeline, adapting it to a documented source, and preparing controlled research splits. Release status: **metadata sample; full source data not bundled**. The sample is for schema inspection only and should not be reported as a full training corpus.
41
+
42
+ ## Risks and limitations
43
+
44
+ The loader cannot guarantee that an external source is representative, correctly licensed, or free of sensitive information. Users remain responsible for source review, privacy checks, and bias analysis before training or redistribution.
45
+
46
+ ## Files
47
+
48
+ - `clean.py` — primary artifact
49
+ - `README.md` — this documentation
50
+ - `dataset_infos.json` — schema metadata
51
+ - `metadata_sample.jsonl` — schema sample
52
+
53
+ ## License
54
+
55
+ Released under **apache-2.0**. Review the source-data terms separately when this repository is used with external datasets.
clean.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json, os, hashlib, logging
2
+ from pathlib import Path
3
+
4
+ log = logging.getLogger(__name__)
5
+
6
+ # --- real data source: education ---
7
+ TV_DATASET = None
8
+ HF_CANDIDATES = ['sciq', 'ai2_arc', 'race']
9
+ IMAGE_FIELD = None
10
+ TEXT_FIELD = 'question'
11
+ LABEL_FIELD = 'correct_answer'
12
+ PROMPT_TEMPLATE = 'a photo of a {label}'
13
+ DATASET_URL = 'https://huggingface.co/datasets/sciq'
14
+
15
+ def fetch_real_samples(max_samples=5000, cache_dir='./_cache'):
16
+ # 本地没有数据时自动下载真实公开数据集: torchvision -> HuggingFace -> 手动说明
17
+ out = []
18
+ if TV_DATASET is not None:
19
+ try:
20
+ import torchvision
21
+ ctor = getattr(torchvision.datasets, TV_DATASET)
22
+ try:
23
+ ds = ctor(root=cache_dir, split='train', download=True)
24
+ except TypeError:
25
+ try:
26
+ ds = ctor(root=cache_dir, train=True, download=True)
27
+ except TypeError:
28
+ ds = ctor(root=cache_dir, download=True)
29
+ classes = getattr(ds, 'classes', None)
30
+ os.makedirs(os.path.join(cache_dir, 'tv'), exist_ok=True)
31
+ for i, item in enumerate(ds):
32
+ if len(out) >= max_samples:
33
+ break
34
+ img, label = item[0], item[1]
35
+ name = classes[label] if classes else str(label)
36
+ p = os.path.join(cache_dir, 'tv', str(i) + '.png')
37
+ try:
38
+ img.save(p)
39
+ except Exception:
40
+ continue
41
+ out.append({'image': p, 'text': PROMPT_TEMPLATE.format(label=name)})
42
+ if out:
43
+ return out
44
+ except Exception as e:
45
+ print('torchvision load failed:', e)
46
+ for repo in HF_CANDIDATES:
47
+ try:
48
+ from datasets import load_dataset
49
+ try:
50
+ ds = load_dataset(repo, split='train', streaming=True)
51
+ except Exception:
52
+ ds = load_dataset(repo, split='train')
53
+ img_dir = os.path.join(cache_dir, 'hf_images')
54
+ os.makedirs(img_dir, exist_ok=True)
55
+ for i, ex in enumerate(ds):
56
+ if len(out) >= max_samples:
57
+ break
58
+ txt = None
59
+ if TEXT_FIELD is not None and TEXT_FIELD in ex:
60
+ v = ex[TEXT_FIELD]
61
+ txt = v if isinstance(v, str) else ' '.join(map(str, v if isinstance(v, (list, tuple)) else [v]))
62
+ if txt is None and LABEL_FIELD in ex:
63
+ txt = PROMPT_TEMPLATE.format(label=ex[LABEL_FIELD])
64
+ if txt is None:
65
+ continue
66
+ out.append({'text': txt})
67
+ if out:
68
+ return out
69
+ except Exception as e:
70
+ print('HF load failed for', repo, ':', e)
71
+ print('Automatic download failed. Please get the data manually from:')
72
+ print(' ' + DATASET_URL)
73
+ return out
74
+
75
+
76
+ def load(data_dir):
77
+ # 加载原始数据
78
+ files = list(Path(data_dir).glob('*.jsonl'))
79
+ if not files:
80
+ files = list(Path(data_dir).glob('*.json'))
81
+ samples = []
82
+ for f in files:
83
+ with open(f, encoding="utf-8") as fp:
84
+ if f.suffix == '.json':
85
+ d = json.load(fp)
86
+ samples.extend(d if isinstance(d, list) else [d])
87
+ else:
88
+ samples.extend(json.loads(l) for l in fp if l.strip())
89
+ return samples
90
+
91
+ def filter_quality(samples, min_score=0.5):
92
+ # 质量过滤
93
+ results = []
94
+ for s in samples:
95
+ text = s.get('education', s.get('text', ''))
96
+ if len(text.split()) >= 3:
97
+ results.append(s)
98
+ return results
99
+
100
+ def dedup(samples):
101
+ seen = set()
102
+ out = []
103
+ for s in samples:
104
+ fp = s.get('image', s.get('audio', ''))
105
+ if fp and os.path.exists(fp):
106
+ h = hashlib.md5(open(fp, 'rb').read()).hexdigest()
107
+ if h in seen:
108
+ continue
109
+ seen.add(h)
110
+ out.append(s)
111
+ return out
112
+
113
+ def preprocess(samples, output_directory, img_size=224):
114
+ # 预处理
115
+ os.makedirs(output_directory, exist_ok=True)
116
+ processed = []
117
+ for s in samples:
118
+ item = {}
119
+ if 'image' in s:
120
+ try:
121
+ from PIL import Image as IM
122
+ img = IM.open(s['image']).convert('RGB')
123
+ img = img.resize((img_size, img_size))
124
+ p = os.path.join(output_directory, os.path.basename(s['image']))
125
+ img.save(p, 'JPEG', quality=95)
126
+ item['image'] = p
127
+ except Exception:
128
+ continue
129
+ text = s.get('education', s.get('text', ''))
130
+ item['text'] = text
131
+ item['domain'] = 'education'
132
+ processed.append(item)
133
+ return processed
134
+
135
+ def save_jsonl(data, path):
136
+ with open(path, 'w', encoding="utf-8") as f:
137
+ for d in data:
138
+ f.write(json.dumps(d, ensure_ascii=False) + '\n')
139
+
140
+ def main():
141
+ import sys
142
+ data_dir = sys.argv[1] if len(sys.argv) > 1 else './data'
143
+ out = sys.argv[2] if len(sys.argv) > 2 else './output'
144
+ samples = load(data_dir) or fetch_real_samples()
145
+ samples = filter_quality(samples)
146
+ samples = dedup(samples)
147
+ result = preprocess(samples, out)
148
+ save_jsonl(result, os.path.join(out, 'dataset.jsonl'))
149
+ print(f'Done: {len(result)} samples')
150
+
151
+ if __name__ == '__main__':
152
+ main()
dataset_infos.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "description": "Metadata sample and preparation pipeline for education / image_audio data",
3
+ "features": {
4
+ "id": {
5
+ "dtype": "string"
6
+ },
7
+ "description": {
8
+ "dtype": "string"
9
+ },
10
+ "label": {
11
+ "dtype": "int64"
12
+ },
13
+ "domain": {
14
+ "dtype": "string"
15
+ },
16
+ "modality": {
17
+ "dtype": "string"
18
+ }
19
+ },
20
+ "splits": {
21
+ "sample": {
22
+ "num_examples": 24
23
+ }
24
+ },
25
+ "release_status": "metadata sample; full source data not bundled"
26
+ }
metadata_sample.jsonl ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {"id": "sample-001", "description": "schema validation example", "label": 0, "domain": "education", "modality": "image_audio"}
2
+ {"id": "sample-002", "description": "quality-control example", "label": 1, "domain": "education", "modality": "image_audio"}
3
+ {"id": "sample-003", "description": "split inspection example", "label": 2, "domain": "education", "modality": "image_audio"}
4
+ {"id": "sample-004", "description": "label review example", "label": 3, "domain": "education", "modality": "image_audio"}
5
+ {"id": "sample-005", "description": "deduplication example", "label": 0, "domain": "education", "modality": "image_audio"}
6
+ {"id": "sample-006", "description": "format conversion example", "label": 1, "domain": "education", "modality": "image_audio"}
7
+ {"id": "sample-007", "description": "schema validation example", "label": 2, "domain": "education", "modality": "image_audio"}
8
+ {"id": "sample-008", "description": "quality-control example", "label": 3, "domain": "education", "modality": "image_audio"}
9
+ {"id": "sample-009", "description": "split inspection example", "label": 0, "domain": "education", "modality": "image_audio"}
10
+ {"id": "sample-010", "description": "label review example", "label": 1, "domain": "education", "modality": "image_audio"}
11
+ {"id": "sample-011", "description": "deduplication example", "label": 2, "domain": "education", "modality": "image_audio"}
12
+ {"id": "sample-012", "description": "format conversion example", "label": 3, "domain": "education", "modality": "image_audio"}
13
+ {"id": "sample-013", "description": "schema validation example", "label": 0, "domain": "education", "modality": "image_audio"}
14
+ {"id": "sample-014", "description": "quality-control example", "label": 1, "domain": "education", "modality": "image_audio"}
15
+ {"id": "sample-015", "description": "split inspection example", "label": 2, "domain": "education", "modality": "image_audio"}
16
+ {"id": "sample-016", "description": "label review example", "label": 3, "domain": "education", "modality": "image_audio"}
17
+ {"id": "sample-017", "description": "deduplication example", "label": 0, "domain": "education", "modality": "image_audio"}
18
+ {"id": "sample-018", "description": "format conversion example", "label": 1, "domain": "education", "modality": "image_audio"}
19
+ {"id": "sample-019", "description": "schema validation example", "label": 2, "domain": "education", "modality": "image_audio"}
20
+ {"id": "sample-020", "description": "quality-control example", "label": 3, "domain": "education", "modality": "image_audio"}
21
+ {"id": "sample-021", "description": "split inspection example", "label": 0, "domain": "education", "modality": "image_audio"}
22
+ {"id": "sample-022", "description": "label review example", "label": 1, "domain": "education", "modality": "image_audio"}
23
+ {"id": "sample-023", "description": "deduplication example", "label": 2, "domain": "education", "modality": "image_audio"}
24
+ {"id": "sample-024", "description": "format conversion example", "label": 3, "domain": "education", "modality": "image_audio"}