Download playground/Abbie-b200/examples/download_sample_data.py from jasonfan/devboxbackup: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/datasets/jasonfan/devboxbackup/resolve/main/playground/Abbie-b200/examples/download_sample_data.py
- Command line
-
hf download hf://datasets/jasonfan/devboxbackup/playground/Abbie-b200/examples/download_sample_data.py
-
curl -L -o download_sample_data.py https://huggingface.co/datasets/jasonfan/devboxbackup/resolve/main/playground/Abbie-b200/examples/download_sample_data.py
2.75 kB
| import base64 | |
| import io | |
| import os | |
| from typing import List | |
| import pandas as pd | |
| from datasets import load_dataset | |
| from tqdm import tqdm | |
| def write_items_to_disk(items: List[dict], output_dir: str, n_parts: int = 1): | |
| part_size = len(items) // n_parts | |
| remainder = len(items) % n_parts | |
| os.makedirs(output_dir, exist_ok=True) | |
| offset = 0 | |
| for part_nb in range(n_parts): | |
| next_offset = offset + part_size | |
| if part_nb < remainder: | |
| next_offset += 1 | |
| df = pd.DataFrame(items[offset:next_offset]) | |
| df.to_json( | |
| os.path.join(output_dir, f"part-{part_nb:04}.jsonl"), | |
| orient="records", | |
| lines=True, | |
| ) | |
| offset = next_offset | |
| def download_smoltalk_10k(output_dir: str, n_parts: int = 1): | |
| print("Loading smoltalk_10k dataset") | |
| dataset = load_dataset("maharnab/smol-smoltalk-10k", split="train") | |
| # Convert into the required format | |
| items = [] | |
| for i, row in enumerate(tqdm(dataset)): | |
| prompt = row["messages"] | |
| items.append({"prompt": prompt, "extra_info": {"item_id": i}}) | |
| # Split to parts and write to disk | |
| print("Writing to disk") | |
| write_items_to_disk( | |
| items, | |
| output_dir=os.path.join(output_dir, "smoltalk_10k", "train"), | |
| n_parts=n_parts, | |
| ) | |
| def download_geo3k(output_dir: str, n_parts: int = 1): | |
| print("Loading geo3k dataset") | |
| dataset = load_dataset("hiyouga/geometry3k", split="train") | |
| # Convert into the required format | |
| print("Forming conversations") | |
| items = [] | |
| for i, row in enumerate(tqdm(dataset)): | |
| images = row["images"] | |
| problem = row["problem"] | |
| answer = row["answer"] | |
| prompt = [ | |
| {"role": "user", "content": problem}, | |
| {"role": "assistant", "content": f"<think>Model is thinking</think><answer>{answer}</answer>"}, | |
| ] | |
| image_elements = [] | |
| for image in images: | |
| buf = io.BytesIO() | |
| image.save(buf, format="PNG") | |
| image_bytes = buf.getvalue() | |
| image_b64 = base64.b64encode(image_bytes).decode() | |
| image_elements.append({"type": "image", "image": f"data:image/png;base64,{image_b64}"}) | |
| item = {"prompt": prompt, "images": image_elements, "extra_info": {"item_id": i}} | |
| items.append(item) | |
| # Split to parts and write to disk | |
| print("Writing to disk") | |
| write_items_to_disk( | |
| items, | |
| output_dir=os.path.join(output_dir, "geo3k", "train"), | |
| n_parts=n_parts, | |
| ) | |
| def main(): | |
| output_dir = "./sample_data" | |
| n_parts = 128 | |
| download_smoltalk_10k(output_dir, n_parts=n_parts) | |
| download_geo3k(output_dir, n_parts=n_parts) | |
| if __name__ == "__main__": | |
| main() | |