dataset-creation / README.md
davanstrien's picture
davanstrien HF Staff
Add PDF to dataset conversion script with examples and documentation
cb5bdd5
---
viewer: false
tags: [uv-script, dataset-creation, pdf-processing, document-processing, tool]
task: other
language: en
---
# Dataset Creation Scripts
Ready-to-run scripts for creating Hugging Face datasets from local files.
## Available Scripts
### πŸ“„ pdf-to-dataset.py
Convert directories of PDF files into Hugging Face datasets.
**Features:**
- πŸ“ Uploads PDFs as dataset objects for flexible processing
- 🏷️ Automatic labeling from folder structure
- πŸš€ Zero configuration - just point at your PDFs
- πŸ“€ Direct upload to Hugging Face Hub
**Usage:**
```bash
# Basic usage
uv run pdf-to-dataset.py /path/to/pdfs username/my-dataset
# Create private dataset
uv run pdf-to-dataset.py /path/to/pdfs username/my-dataset --private
# Organized by categories (folder structure creates labels)
# /pdfs/invoice/doc1.pdf β†’ label: "invoice"
# /pdfs/receipt/doc2.pdf β†’ label: "receipt"
uv run pdf-to-dataset.py /path/to/organized-pdfs username/categorized-docs
```
**Output Format:**
The script creates a dataset where each example contains a `pdf` object that can be processed using the datasets library. Users can then extract text, convert to images, or perform other operations as needed.
```python
from datasets import load_dataset
# Load your uploaded dataset
dataset = load_dataset("username/my-dataset")
# Access PDF objects
pdf = dataset["train"][0]["pdf"]
```
**Requirements:**
- Directory containing PDF files
- Hugging Face account (for uploading)
- No GPU needed - runs on CPU
## Installation
No installation needed! Just run with `uv`:
```bash
# Run directly from GitHub
uv run https://huggingface.co/datasets/uv-scripts/dataset-creation/resolve/main/pdf-to-dataset.py --help
# Or clone and run locally
git clone https://huggingface.co/datasets/uv-scripts/dataset-creation
cd dataset-creation
uv run pdf-to-dataset.py /path/to/pdfs my-dataset
```
## Authentication
Scripts use Hugging Face authentication:
1. Pass token via `--hf-token` argument
2. Set `HF_TOKEN` environment variable
3. Use cached credentials from `huggingface-cli login`
## Examples
### Create a Dataset from Research Papers
```bash
uv run pdf-to-dataset.py ~/Documents/papers username/research-papers
```
### Organize Documents by Type
```bash
# Directory structure:
# documents/
# β”œβ”€β”€ invoices/
# β”‚ β”œβ”€β”€ invoice1.pdf
# β”‚ └── invoice2.pdf
# └── receipts/
# β”œβ”€β”€ receipt1.pdf
# └── receipt2.pdf
uv run pdf-to-dataset.py documents/ username/financial-docs
# Creates dataset with labels: "invoices" and "receipts"
```
## Tips
- **Large PDFs**: The script handles large PDFs efficiently by uploading them as objects
- **Organization**: Use subdirectories to automatically create labeled datasets
- **Privacy**: Use `--private` flag for sensitive documents
- **Processing**: After upload, use the datasets library to extract text, images, or metadata as needed
## License
MIT