Upload 2 files
Browse files- README.md +42 -0
- dataset_135359330_code_video_text.py +160 -0
README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-4.0
|
| 3 |
+
tags:
|
| 4 |
+
- active
|
| 5 |
+
- adaptive
|
| 6 |
+
- code
|
| 7 |
+
- domain-specific
|
| 8 |
+
- manual
|
| 9 |
+
- randaugment
|
| 10 |
+
- stratified-90-10
|
| 11 |
+
- video-text
|
| 12 |
+
- webdataset
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# dataset_135359330_code_video_text.py
|
| 16 |
+
|
| 17 |
+
## Dataset Summary
|
| 18 |
+
|
| 19 |
+
A **code** dataset with **video text** modality, stored in **webdataset** format.
|
| 20 |
+
|
| 21 |
+
## Preprocessing & Augmentation
|
| 22 |
+
|
| 23 |
+
- **Preprocessing**: domain specific
|
| 24 |
+
- **Augmentation**: randaugment
|
| 25 |
+
|
| 26 |
+
## Splits & Sampling
|
| 27 |
+
|
| 28 |
+
- **Split strategy**: stratified 90 10
|
| 29 |
+
- **Sampling**: active
|
| 30 |
+
|
| 31 |
+
## Quality & Labeling
|
| 32 |
+
|
| 33 |
+
- **Quality filtering**: adaptive
|
| 34 |
+
- **Labeling**: manual
|
| 35 |
+
|
| 36 |
+
## Files
|
| 37 |
+
|
| 38 |
+
- `dataset_135359330_code_video_text.py` — main artifact of this repository
|
| 39 |
+
|
| 40 |
+
## License
|
| 41 |
+
|
| 42 |
+
See the license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os, hashlib, logging
|
| 2 |
+
from typing import List, Dict, Optional
|
| 3 |
+
|
| 4 |
+
log = logging.getLogger(__name__)
|
| 5 |
+
|
| 6 |
+
# --- real data source: code ---
|
| 7 |
+
TV_DATASET = None
|
| 8 |
+
HF_CANDIDATES = ['code_search_net', 'codeparrot/github-code']
|
| 9 |
+
IMAGE_FIELD = None
|
| 10 |
+
TEXT_FIELD = 'func_code_string'
|
| 11 |
+
LABEL_FIELD = 'func_documentation_string'
|
| 12 |
+
PROMPT_TEMPLATE = 'a photo of a {label}'
|
| 13 |
+
DATASET_URL = 'https://huggingface.co/datasets/code_search_net'
|
| 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 |
+
class CodeDataset:
|
| 77 |
+
def __init__(self, data_dir: str, output_dir: str, img_size: int = 224):
|
| 78 |
+
self.data_dir = data_dir
|
| 79 |
+
self.output_dir = output_dir
|
| 80 |
+
self.img_size = img_size
|
| 81 |
+
self.samples = []
|
| 82 |
+
self.processed = []
|
| 83 |
+
|
| 84 |
+
def load(self) -> List[Dict]:
|
| 85 |
+
|
| 86 |
+
from pathlib import Path
|
| 87 |
+
for f in Path(self.data_dir).glob('*.jsonl'):
|
| 88 |
+
with open(f) as fp:
|
| 89 |
+
for line in fp:
|
| 90 |
+
if line.strip():
|
| 91 |
+
self.samples.append(json.loads(line))
|
| 92 |
+
log.info(f'Loaded {len(self.samples)} samples')
|
| 93 |
+
return self.samples
|
| 94 |
+
|
| 95 |
+
def filter(self) -> List[Dict]:
|
| 96 |
+
|
| 97 |
+
filtered = []
|
| 98 |
+
for s in self.samples:
|
| 99 |
+
text = s.get('code', s.get('text', ''))
|
| 100 |
+
if len(text.split()) >= 3:
|
| 101 |
+
filtered.append(s)
|
| 102 |
+
self.samples = filtered
|
| 103 |
+
return filtered
|
| 104 |
+
|
| 105 |
+
def deduplicate(self) -> List[Dict]:
|
| 106 |
+
seen = set()
|
| 107 |
+
result = []
|
| 108 |
+
for s in self.samples:
|
| 109 |
+
fp = s.get('image', s.get('audio', ''))
|
| 110 |
+
if fp and os.path.exists(fp):
|
| 111 |
+
h = hashlib.md5(open(fp, 'rb').read()).hexdigest()
|
| 112 |
+
if h in seen:
|
| 113 |
+
continue
|
| 114 |
+
seen.add(h)
|
| 115 |
+
result.append(s)
|
| 116 |
+
self.samples = result
|
| 117 |
+
return result
|
| 118 |
+
|
| 119 |
+
def process(self) -> List[Dict]:
|
| 120 |
+
os.makedirs(self.output_dir, exist_ok=True)
|
| 121 |
+
for s in self.samples:
|
| 122 |
+
item = {}
|
| 123 |
+
if 'image' in s:
|
| 124 |
+
try:
|
| 125 |
+
from PIL import Image
|
| 126 |
+
img = Image.open(s['image']).convert('RGB')
|
| 127 |
+
img = img.resize((self.img_size, self.img_size))
|
| 128 |
+
p = os.path.join(self.output_dir, os.path.basename(s['image']))
|
| 129 |
+
img.save(p, 'JPEG', quality=95)
|
| 130 |
+
item['image'] = p
|
| 131 |
+
except Exception as e:
|
| 132 |
+
log.warning(f'Failed: {e}')
|
| 133 |
+
continue
|
| 134 |
+
item['text'] = s.get('code', s.get('text', ''))
|
| 135 |
+
item['domain'] = 'code'
|
| 136 |
+
self.processed.append(item)
|
| 137 |
+
return self.processed
|
| 138 |
+
|
| 139 |
+
def save(self):
|
| 140 |
+
path = os.path.join(self.output_dir, 'dataset.jsonl')
|
| 141 |
+
with open(path, 'w') as f:
|
| 142 |
+
for d in self.processed:
|
| 143 |
+
f.write(json.dumps(d, ensure_ascii=False) + '\n')
|
| 144 |
+
log.info(f'Saved {len(self.processed)} samples to {path}')
|
| 145 |
+
|
| 146 |
+
def run(self):
|
| 147 |
+
self.load()
|
| 148 |
+
if not self.samples:
|
| 149 |
+
self.samples = fetch_real_samples()
|
| 150 |
+
self.filter()
|
| 151 |
+
self.deduplicate()
|
| 152 |
+
self.process()
|
| 153 |
+
self.save()
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
if __name__ == '__main__':
|
| 157 |
+
import sys
|
| 158 |
+
ds = CodeDataset(sys.argv[1] if len(sys.argv) > 1 else './data',
|
| 159 |
+
sys.argv[2] if len(sys.argv) > 2 else './output')
|
| 160 |
+
ds.run()
|