File size: 2,941 Bytes
cb5bdd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
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