Upload create_dataset.py with huggingface_hub
Browse files- create_dataset.py +128 -0
create_dataset.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import argparse
|
| 3 |
+
import random
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
import datasets
|
| 7 |
+
from huggingface_hub import HfApi, RepoCard
|
| 8 |
+
from transformers import HfArgumentParser
|
| 9 |
+
|
| 10 |
+
random.seed(0)
|
| 11 |
+
|
| 12 |
+
def generate_unique_multiplication_data(a_max, b_max, n_train, n_test):
|
| 13 |
+
"""Generate train and test datasets for each multiplication range ensuring no overlap."""
|
| 14 |
+
datasets = {}
|
| 15 |
+
|
| 16 |
+
for a in range(1, a_max + 1):
|
| 17 |
+
for b in range(1, b_max + 1):
|
| 18 |
+
all_pairs = [(x, y) for x in range(1, a + 1) for y in range(1, b + 1)]
|
| 19 |
+
|
| 20 |
+
# Convert to list before sampling to avoid Python 3.9+ warning
|
| 21 |
+
test_data = set(random.sample(list(all_pairs), min(n_test, len(all_pairs))))
|
| 22 |
+
train_data = set(random.sample(list(set(all_pairs) - test_data), min(n_train, len(set(all_pairs) - test_data))))
|
| 23 |
+
|
| 24 |
+
datasets[f"{a}x{b}"] = {"train": list(train_data), "test": list(test_data)}
|
| 25 |
+
|
| 26 |
+
return datasets
|
| 27 |
+
|
| 28 |
+
def save_to_jsonl(data, file_path):
|
| 29 |
+
"""Save dataset to JSONL format."""
|
| 30 |
+
with open(file_path, "w") as f:
|
| 31 |
+
for a, b in data:
|
| 32 |
+
json.dump({"problem": f"What is {a} times {b}?", "answer": str(a * b)}, f)
|
| 33 |
+
f.write("\n")
|
| 34 |
+
|
| 35 |
+
def prepare_datasets(output_dir):
|
| 36 |
+
"""Prepare train and test datasets ensuring no overlap for all 1x1 to 15x15 combinations."""
|
| 37 |
+
output_dir = Path(output_dir)
|
| 38 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 39 |
+
|
| 40 |
+
all_datasets = generate_unique_multiplication_data(a_max=15, b_max=15, n_train=1000, n_test=100)
|
| 41 |
+
|
| 42 |
+
train_files, test_files = [], []
|
| 43 |
+
for name, data in all_datasets.items():
|
| 44 |
+
train_file = output_dir / f"multiplication_train_{name}.jsonl"
|
| 45 |
+
test_file = output_dir / f"multiplication_test_{name}.jsonl"
|
| 46 |
+
|
| 47 |
+
save_to_jsonl(data["train"], train_file)
|
| 48 |
+
save_to_jsonl(data["test"], test_file)
|
| 49 |
+
|
| 50 |
+
train_files.append(train_file)
|
| 51 |
+
test_files.append(test_file)
|
| 52 |
+
|
| 53 |
+
print(f"\n✅ Datasets saved to {output_dir}")
|
| 54 |
+
return train_files, test_files
|
| 55 |
+
|
| 56 |
+
def process_file(file_path):
|
| 57 |
+
"""Convert JSONL data into Hugging Face dataset format."""
|
| 58 |
+
with open(file_path, "r") as f:
|
| 59 |
+
data = [json.loads(line.strip()) for line in f if line.strip()]
|
| 60 |
+
|
| 61 |
+
dataset = {
|
| 62 |
+
"messages": [[
|
| 63 |
+
{"role": "user", "content": item["problem"]},
|
| 64 |
+
{"role": "assistant", "content": item["answer"]},
|
| 65 |
+
] for item in data],
|
| 66 |
+
"ground_truth": [item["answer"] for item in data],
|
| 67 |
+
"dataset": ["multiplication"] * len(data),
|
| 68 |
+
}
|
| 69 |
+
return datasets.Dataset.from_dict(dataset)
|
| 70 |
+
|
| 71 |
+
def push_to_huggingface(train_files, test_files, hf_entity):
|
| 72 |
+
"""Push datasets to Hugging Face Hub and print the dataset link."""
|
| 73 |
+
api = HfApi()
|
| 74 |
+
hf_entity = hf_entity or api.whoami()["name"]
|
| 75 |
+
|
| 76 |
+
print("\n📤 Uploading datasets to Hugging Face...\n")
|
| 77 |
+
|
| 78 |
+
for file in train_files + test_files:
|
| 79 |
+
dataset = process_file(file)
|
| 80 |
+
dataset_name = file.stem
|
| 81 |
+
repo_id = f"{hf_entity}/{dataset_name}"
|
| 82 |
+
hf_url = f"https://huggingface.co/datasets/{repo_id}"
|
| 83 |
+
|
| 84 |
+
print(f"✅ Dataset uploaded: {dataset_name}")
|
| 85 |
+
# print(f"🔗 Click to view: {hf_url}\n") # 👈 PRINTS THE LINK
|
| 86 |
+
|
| 87 |
+
dataset.push_to_hub(repo_id)
|
| 88 |
+
|
| 89 |
+
api.upload_file(
|
| 90 |
+
path_or_fileobj=__file__,
|
| 91 |
+
path_in_repo="create_dataset.py",
|
| 92 |
+
repo_type="dataset",
|
| 93 |
+
repo_id=repo_id,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Add RepoCard with Hugging Face link
|
| 97 |
+
repo_card = RepoCard(
|
| 98 |
+
content=f"""\
|
| 99 |
+
# Multiplication Dataset - {dataset_name}
|
| 100 |
+
|
| 101 |
+
This dataset contains multiplication problems for numbers up to 15x15.
|
| 102 |
+
|
| 103 |
+
## Dataset Format
|
| 104 |
+
|
| 105 |
+
- `messages`: User question and assistant answer.
|
| 106 |
+
- `ground_truth`: Correct multiplication result.
|
| 107 |
+
- `dataset`: "multiplication"
|
| 108 |
+
|
| 109 |
+
## Hugging Face Dataset Link
|
| 110 |
+
➡️ [View dataset on Hugging Face]({hf_url})
|
| 111 |
+
"""
|
| 112 |
+
)
|
| 113 |
+
repo_card.push_to_hub(repo_id, repo_type="dataset")
|
| 114 |
+
|
| 115 |
+
def main():
|
| 116 |
+
parser = argparse.ArgumentParser()
|
| 117 |
+
parser.add_argument("--output_dir", type=str, default="math_data", help="Output directory")
|
| 118 |
+
parser.add_argument("--push_to_hub", action="store_true", help="Upload to Hugging Face")
|
| 119 |
+
parser.add_argument("--hf_entity", type=str, default=None, help="Hugging Face entity")
|
| 120 |
+
args = parser.parse_args()
|
| 121 |
+
|
| 122 |
+
train_files, test_files = prepare_datasets(args.output_dir)
|
| 123 |
+
|
| 124 |
+
if args.push_to_hub:
|
| 125 |
+
push_to_huggingface(train_files, test_files, args.hf_entity)
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
main()
|