Add prepare.py, README.md, dataset_infos.json, metadata_sample.jsonl
Browse files- README.md +73 -0
- dataset_infos.json +26 -0
- metadata_sample.jsonl +24 -0
- prepare.py +123 -0
README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc0-1.0
|
| 3 |
+
tags:
|
| 4 |
+
- dataset
|
| 5 |
+
- legal
|
| 6 |
+
- image-depth
|
| 7 |
+
- metadata-sample
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# Preparation pipeline: Legal / Image Depth
|
| 11 |
+
|
| 12 |
+
## Overview
|
| 13 |
+
|
| 14 |
+
Preparation notes and schema examples for **Legal** tasks using **Image Depth** data. Full source material is intentionally not bundled, so provenance and licensing remain explicit.
|
| 15 |
+
|
| 16 |
+
## Data Sources
|
| 17 |
+
|
| 18 |
+
Source material is referenced but not bundled. Record the source version, retrieval date and any filtering rule so the same rows can be reproduced later.
|
| 19 |
+
|
| 20 |
+
## Included Material
|
| 21 |
+
|
| 22 |
+
1. `prepare.py` — loading, cleaning, and split preparation code.
|
| 23 |
+
2. `dataset_infos.json` — schema and split metadata.
|
| 24 |
+
3. `metadata_sample.jsonl` — small, human-readable records for checking the schema.
|
| 25 |
+
4. `README.md` — data card and usage notes.
|
| 26 |
+
|
| 27 |
+
## Risks And Limitations
|
| 28 |
+
|
| 29 |
+
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.
|
| 30 |
+
|
| 31 |
+
## Acknowledgments
|
| 32 |
+
|
| 33 |
+
This repo borrows ideas freely from public reference implementations; credit to their authors.
|
| 34 |
+
|
| 35 |
+
## Validation Checklist
|
| 36 |
+
|
| 37 |
+
1. check source licenses before redistribution
|
| 38 |
+
2. scan for duplicates across splits
|
| 39 |
+
3. inspect missing values and label balance
|
| 40 |
+
4. watch for modality-specific corruption
|
| 41 |
+
|
| 42 |
+
## Citation
|
| 43 |
+
|
| 44 |
+
```bibtex
|
| 45 |
+
@misc{preparationpipelinel},
|
| 46 |
+
title = {Preparation pipeline: Legal / Image Depth},
|
| 47 |
+
year = {{2026}},
|
| 48 |
+
publisher = {{Hugging Face}},
|
| 49 |
+
}
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
## Processing Choices
|
| 53 |
+
|
| 54 |
+
1. **Storage format**: jsonl
|
| 55 |
+
2. **Preprocessing**: aggressive
|
| 56 |
+
3. **Augmentation**: none
|
| 57 |
+
4. **Split strategy**: kfold 5
|
| 58 |
+
5. **Sampling**: balanced
|
| 59 |
+
6. **Quality checks**: strict
|
| 60 |
+
7. **Labeling**: self training
|
| 61 |
+
|
| 62 |
+
## Files
|
| 63 |
+
|
| 64 |
+
| File | Purpose |
|
| 65 |
+
|---|---|
|
| 66 |
+
| `prepare.py` | primary artifact |
|
| 67 |
+
| `README.md` | this documentation |
|
| 68 |
+
| `dataset_infos.json` | schema metadata |
|
| 69 |
+
| `metadata_sample.jsonl` | schema sample |
|
| 70 |
+
|
| 71 |
+
## License
|
| 72 |
+
|
| 73 |
+
Distributed under the **cc0-1.0** license. External datasets keep their own terms — check them separately.
|
dataset_infos.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"description": "Metadata sample and preparation pipeline for legal / image_depth 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": "legal", "modality": "image_depth"}
|
| 2 |
+
{"id": "sample-002", "description": "quality-control example", "label": 1, "domain": "legal", "modality": "image_depth"}
|
| 3 |
+
{"id": "sample-003", "description": "split inspection example", "label": 2, "domain": "legal", "modality": "image_depth"}
|
| 4 |
+
{"id": "sample-004", "description": "label review example", "label": 3, "domain": "legal", "modality": "image_depth"}
|
| 5 |
+
{"id": "sample-005", "description": "deduplication example", "label": 0, "domain": "legal", "modality": "image_depth"}
|
| 6 |
+
{"id": "sample-006", "description": "format conversion example", "label": 1, "domain": "legal", "modality": "image_depth"}
|
| 7 |
+
{"id": "sample-007", "description": "schema validation example", "label": 2, "domain": "legal", "modality": "image_depth"}
|
| 8 |
+
{"id": "sample-008", "description": "quality-control example", "label": 3, "domain": "legal", "modality": "image_depth"}
|
| 9 |
+
{"id": "sample-009", "description": "split inspection example", "label": 0, "domain": "legal", "modality": "image_depth"}
|
| 10 |
+
{"id": "sample-010", "description": "label review example", "label": 1, "domain": "legal", "modality": "image_depth"}
|
| 11 |
+
{"id": "sample-011", "description": "deduplication example", "label": 2, "domain": "legal", "modality": "image_depth"}
|
| 12 |
+
{"id": "sample-012", "description": "format conversion example", "label": 3, "domain": "legal", "modality": "image_depth"}
|
| 13 |
+
{"id": "sample-013", "description": "schema validation example", "label": 0, "domain": "legal", "modality": "image_depth"}
|
| 14 |
+
{"id": "sample-014", "description": "quality-control example", "label": 1, "domain": "legal", "modality": "image_depth"}
|
| 15 |
+
{"id": "sample-015", "description": "split inspection example", "label": 2, "domain": "legal", "modality": "image_depth"}
|
| 16 |
+
{"id": "sample-016", "description": "label review example", "label": 3, "domain": "legal", "modality": "image_depth"}
|
| 17 |
+
{"id": "sample-017", "description": "deduplication example", "label": 0, "domain": "legal", "modality": "image_depth"}
|
| 18 |
+
{"id": "sample-018", "description": "format conversion example", "label": 1, "domain": "legal", "modality": "image_depth"}
|
| 19 |
+
{"id": "sample-019", "description": "schema validation example", "label": 2, "domain": "legal", "modality": "image_depth"}
|
| 20 |
+
{"id": "sample-020", "description": "quality-control example", "label": 3, "domain": "legal", "modality": "image_depth"}
|
| 21 |
+
{"id": "sample-021", "description": "split inspection example", "label": 0, "domain": "legal", "modality": "image_depth"}
|
| 22 |
+
{"id": "sample-022", "description": "label review example", "label": 1, "domain": "legal", "modality": "image_depth"}
|
| 23 |
+
{"id": "sample-023", "description": "deduplication example", "label": 2, "domain": "legal", "modality": "image_depth"}
|
| 24 |
+
{"id": "sample-024", "description": "format conversion example", "label": 3, "domain": "legal", "modality": "image_depth"}
|
prepare.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, os, hashlib
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
# legal dataset processor
|
| 5 |
+
# modality: image_depth, preprocessing: aggressive
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# --- real data source: legal ---
|
| 9 |
+
TV_DATASET = None
|
| 10 |
+
HF_CANDIDATES = ['coastalcph/lex_glue', 'lex_glue']
|
| 11 |
+
IMAGE_FIELD = None
|
| 12 |
+
TEXT_FIELD = 'text'
|
| 13 |
+
LABEL_FIELD = 'label'
|
| 14 |
+
PROMPT_TEMPLATE = 'a photo of a {label}'
|
| 15 |
+
DATASET_URL = 'https://huggingface.co/datasets/coastalcph/lex_glue'
|
| 16 |
+
|
| 17 |
+
def fetch_real_samples(max_samples=5000, cache_dir='./_cache'):
|
| 18 |
+
# 本地没有数据时自动下载真实公开数据集: torchvision -> HuggingFace -> 手动说明
|
| 19 |
+
out = []
|
| 20 |
+
if TV_DATASET is not None:
|
| 21 |
+
try:
|
| 22 |
+
import torchvision
|
| 23 |
+
ctor = getattr(torchvision.datasets, TV_DATASET)
|
| 24 |
+
try:
|
| 25 |
+
ds = ctor(root=cache_dir, split='train', download=True)
|
| 26 |
+
except TypeError:
|
| 27 |
+
try:
|
| 28 |
+
ds = ctor(root=cache_dir, train=True, download=True)
|
| 29 |
+
except TypeError:
|
| 30 |
+
ds = ctor(root=cache_dir, download=True)
|
| 31 |
+
classes = getattr(ds, 'classes', None)
|
| 32 |
+
os.makedirs(os.path.join(cache_dir, 'tv'), exist_ok=True)
|
| 33 |
+
for i, item in enumerate(ds):
|
| 34 |
+
if len(out) >= max_samples:
|
| 35 |
+
break
|
| 36 |
+
img, label = item[0], item[1]
|
| 37 |
+
name = classes[label] if classes else str(label)
|
| 38 |
+
p = os.path.join(cache_dir, 'tv', str(i) + '.png')
|
| 39 |
+
try:
|
| 40 |
+
img.save(p)
|
| 41 |
+
except Exception:
|
| 42 |
+
continue
|
| 43 |
+
out.append({'image': p, 'text': PROMPT_TEMPLATE.format(label=name)})
|
| 44 |
+
if out:
|
| 45 |
+
return out
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print('torchvision load failed:', e)
|
| 48 |
+
for repo in HF_CANDIDATES:
|
| 49 |
+
try:
|
| 50 |
+
from datasets import load_dataset
|
| 51 |
+
try:
|
| 52 |
+
ds = load_dataset(repo, split='train', streaming=True)
|
| 53 |
+
except Exception:
|
| 54 |
+
ds = load_dataset(repo, split='train')
|
| 55 |
+
img_dir = os.path.join(cache_dir, 'hf_images')
|
| 56 |
+
os.makedirs(img_dir, exist_ok=True)
|
| 57 |
+
for i, ex in enumerate(ds):
|
| 58 |
+
if len(out) >= max_samples:
|
| 59 |
+
break
|
| 60 |
+
txt = None
|
| 61 |
+
if TEXT_FIELD is not None and TEXT_FIELD in ex:
|
| 62 |
+
v = ex[TEXT_FIELD]
|
| 63 |
+
txt = v if isinstance(v, str) else ' '.join(map(str, v if isinstance(v, (list, tuple)) else [v]))
|
| 64 |
+
if txt is None and LABEL_FIELD in ex:
|
| 65 |
+
txt = PROMPT_TEMPLATE.format(label=ex[LABEL_FIELD])
|
| 66 |
+
if txt is None:
|
| 67 |
+
continue
|
| 68 |
+
out.append({'text': txt})
|
| 69 |
+
if out:
|
| 70 |
+
return out
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print('HF load failed for', repo, ':', e)
|
| 73 |
+
print('Automatic download failed. Please get the data manually from:')
|
| 74 |
+
print(' ' + DATASET_URL)
|
| 75 |
+
return out
|
| 76 |
+
|
| 77 |
+
def build_dataset(src, dst, sz=224):
|
| 78 |
+
samples = []
|
| 79 |
+
for f in Path(src).glob('*.jsonl'):
|
| 80 |
+
with open(f, encoding="utf-8") as fp:
|
| 81 |
+
for line in fp:
|
| 82 |
+
if line.strip():
|
| 83 |
+
samples.append(json.loads(line))
|
| 84 |
+
|
| 85 |
+
if not samples:
|
| 86 |
+
samples = fetch_real_samples()
|
| 87 |
+
|
| 88 |
+
# dedup
|
| 89 |
+
seen = set()
|
| 90 |
+
unique = []
|
| 91 |
+
for s in samples:
|
| 92 |
+
fp = s.get('image', s.get('audio', ''))
|
| 93 |
+
if fp and os.path.exists(fp):
|
| 94 |
+
h = hashlib.md5(open(fp, 'rb').read()).hexdigest()
|
| 95 |
+
if h not in seen:
|
| 96 |
+
seen.add(h)
|
| 97 |
+
unique.append(s)
|
| 98 |
+
else:
|
| 99 |
+
unique.append(s)
|
| 100 |
+
|
| 101 |
+
os.makedirs(dst, exist_ok=True)
|
| 102 |
+
out = []
|
| 103 |
+
for s in unique:
|
| 104 |
+
item = {}
|
| 105 |
+
if 'image' in s:
|
| 106 |
+
from PIL import Image
|
| 107 |
+
img = Image.open(s['image']).convert('RGB').resize((sz, sz))
|
| 108 |
+
p = os.path.join(dst, os.path.basename(s['image']))
|
| 109 |
+
img.save(p, 'JPEG', quality=95)
|
| 110 |
+
item['image'] = p
|
| 111 |
+
item['text'] = s.get('legal', s.get('text', ''))
|
| 112 |
+
item['domain'] = 'legal'
|
| 113 |
+
out.append(item)
|
| 114 |
+
|
| 115 |
+
with open(os.path.join(dst, 'dataset.jsonl'), 'w', encoding="utf-8") as f:
|
| 116 |
+
for d in out:
|
| 117 |
+
f.write(json.dumps(d, ensure_ascii=False) + '\n')
|
| 118 |
+
return out
|
| 119 |
+
|
| 120 |
+
if __name__ == '__main__':
|
| 121 |
+
import sys
|
| 122 |
+
result = build_dataset(sys.argv[1], sys.argv[2])
|
| 123 |
+
print(f'Processed {len(result)} samples')
|