|
|
from datasets import load_dataset, concatenate_datasets |
|
|
import json |
|
|
import os |
|
|
|
|
|
|
|
|
print("Loading dataset...") |
|
|
dataset = load_dataset("unsloth/LaTeX_OCR") |
|
|
|
|
|
def add_question_and_rename(example): |
|
|
"""Add question field and rename text to answer""" |
|
|
|
|
|
example["question"] = "Convert this into LaTeX code, only provide the LaTeX code." |
|
|
|
|
|
|
|
|
if "text" in example: |
|
|
example["answer"] = example["text"] |
|
|
del example["text"] |
|
|
|
|
|
return example |
|
|
|
|
|
print("Modifying dataset...") |
|
|
|
|
|
modified_dataset = dataset.map(add_question_and_rename) |
|
|
|
|
|
|
|
|
os.makedirs("./parquet_files", exist_ok=True) |
|
|
os.makedirs("./modified_latex_ocr_dataset", exist_ok=True) |
|
|
|
|
|
|
|
|
print("Saving dataset locally...") |
|
|
modified_dataset.save_to_disk("./modified_latex_ocr_dataset") |
|
|
|
|
|
|
|
|
print("Saving as Parquet files (per split)...") |
|
|
for split_name, split_data in modified_dataset.items(): |
|
|
parquet_file = f"./parquet_files/modified_latex_ocr_{split_name}.parquet" |
|
|
split_data.to_parquet(parquet_file) |
|
|
print(f"Saved {split_name} split to {parquet_file}") |
|
|
|
|
|
|
|
|
if len(modified_dataset) > 1: |
|
|
print("Saving as consolidated Parquet file...") |
|
|
|
|
|
all_datasets = [] |
|
|
for split_name, split_data in modified_dataset.items(): |
|
|
|
|
|
split_data_with_info = split_data.map(lambda x: {**x, "split": split_name}) |
|
|
all_datasets.append(split_data_with_info) |
|
|
|
|
|
|
|
|
consolidated_dataset = concatenate_datasets(all_datasets) |
|
|
consolidated_dataset.to_parquet("./parquet_files/modified_latex_ocr_all.parquet") |
|
|
print("Saved consolidated dataset to ./parquet_files/modified_latex_ocr_all.parquet") |
|
|
|
|
|
|
|
|
print("Saving text-only JSON files...") |
|
|
for split_name, split_data in modified_dataset.items(): |
|
|
text_only_data = [] |
|
|
for i, sample in enumerate(split_data): |
|
|
text_sample = { |
|
|
"id": i, |
|
|
"question": sample["question"], |
|
|
"answer": sample["answer"] |
|
|
} |
|
|
|
|
|
for key, value in sample.items(): |
|
|
if key not in ["image", "question", "answer"] and not hasattr(value, 'save'): |
|
|
text_sample[key] = value |
|
|
|
|
|
text_only_data.append(text_sample) |
|
|
|
|
|
|
|
|
output_file = f"modified_latex_ocr_{split_name}_text_only.json" |
|
|
with open(output_file, 'w') as f: |
|
|
json.dump(text_only_data, f, indent=2, ensure_ascii=False) |
|
|
print(f"Saved {split_name} text data to {output_file}") |
|
|
|
|
|
print("\n" + "="*50) |
|
|
print("DATASET MODIFICATION COMPLETE!") |
|
|
print("="*50) |
|
|
|
|
|
|
|
|
print(f"\nDataset structure: {modified_dataset}") |
|
|
for split_name in modified_dataset.keys(): |
|
|
print(f" {split_name} split: {len(modified_dataset[split_name])} samples") |
|
|
|
|
|
|
|
|
print(f"\nOutput files created:") |
|
|
print(f" 📁 ./modified_latex_ocr_dataset/ - Full dataset with images") |
|
|
print(f" 📁 ./parquet_files/ - Parquet files") |
|
|
|
|
|
if os.path.exists("./parquet_files"): |
|
|
for file in sorted(os.listdir("./parquet_files")): |
|
|
if file.endswith(".parquet"): |
|
|
file_path = os.path.join("./parquet_files", file) |
|
|
size_mb = os.path.getsize(file_path) / (1024 * 1024) |
|
|
print(f" 📄 {file}: {size_mb:.2f} MB") |
|
|
|
|
|
|
|
|
json_files = [f for f in os.listdir(".") if f.startswith("modified_latex_ocr") and f.endswith(".json")] |
|
|
if json_files: |
|
|
print(f" 📄 Text-only JSON files:") |
|
|
for file in sorted(json_files): |
|
|
size_kb = os.path.getsize(file) / 1024 |
|
|
print(f" 📄 {file}: {size_kb:.2f} KB") |
|
|
|
|
|
|
|
|
if len(modified_dataset) > 0: |
|
|
first_split = list(modified_dataset.keys())[0] |
|
|
sample = modified_dataset[first_split][0] |
|
|
print(f"\n📋 Sample from {first_split} split:") |
|
|
print(f" Keys: {list(sample.keys())}") |
|
|
print(f" Question: {sample['question']}") |
|
|
if 'image' in sample: |
|
|
print(f" Image: {type(sample['image'])}") |
|
|
answer_preview = sample['answer'][:100] + "..." if len(sample['answer']) > 100 else sample['answer'] |
|
|
print(f" Answer: {answer_preview}") |