File size: 7,937 Bytes
d07aeb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25ff3be
d07aeb1
 
 
 
d891d96
25ff3be
 
d07aeb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# /// script
# requires-python = ">=3.9"
# dependencies = [
#     "semhash",
#     "datasets",
#     "huggingface-hub",
#     "hf-transfer",
#     "hf-xet",
# ]
# ///
"""Deduplicate a Hugging Face dataset using SemHash.

This script uses semantic deduplication to remove duplicate entries from a dataset
based on a specified text column, then pushes the results to a new dataset repository.
"""

import argparse
import os
import sys
from datetime import datetime
from typing import Optional

from datasets import Dataset, load_dataset
from huggingface_hub import DatasetCard
from semhash import SemHash
from huggingface_hub import login

os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = (
    "1"  # Enable HF transfer to speed up transfers
)
HF_TOKEN = os.environ.get("HF_TOKEN", None)  # Get Hugging Face token from environment
assert HF_TOKEN, "HF_TOKEN environment variable must be set for authentication"
login(HF_TOKEN)


def parse_args():
    """Parse command line arguments."""
    parser = argparse.ArgumentParser(
        description="Deduplicate a Hugging Face dataset using semantic similarity"
    )
    parser.add_argument(
        "dataset_id",
        type=str,
        help="Source dataset ID (e.g., 'imdb', 'squad', 'username/dataset-name')",
    )
    parser.add_argument(
        "column",
        type=str,
        help="Column name to deduplicate on (e.g., 'text', 'question', 'context')",
    )
    parser.add_argument(
        "repo_id",
        type=str,
        help="Target repository ID for deduplicated dataset (e.g., 'username/my-deduplicated-dataset')",
    )
    parser.add_argument(
        "--split",
        type=str,
        default="train",
        help="Dataset split to process (default: train)",
    )
    parser.add_argument(
        "--threshold",
        type=float,
        default=None,
        help="Similarity threshold for deduplication (0-1, default: auto)",
    )
    parser.add_argument(
        "--method",
        type=str,
        choices=["deduplicate", "filter_outliers", "find_representative"],
        default="deduplicate",
        help="Deduplication method to use (default: deduplicate)",
    )
    parser.add_argument(
        "--private",
        action="store_true",
        help="Make the output dataset private",
    )
    parser.add_argument(
        "--max-samples",
        type=int,
        default=None,
        help="Maximum number of samples to process (for testing)",
    )

    return parser.parse_args()


def create_dataset_card(
    original_dataset_id: str,
    column: str,
    method: str,
    duplicate_ratio: float,
    original_size: int,
    deduplicated_size: int,
    threshold: Optional[float] = None,
) -> str:
    """Create a dataset card with deduplication information."""
    card_content = f"""---
tags:
- deduplicated
- semhash
- semantic-deduplication
- hfjobs
---

# Deduplicated {original_dataset_id}

This dataset is a deduplicated version of [{original_dataset_id}](https://huggingface.co/datasets/{original_dataset_id}) 
using semantic deduplication with [SemHash](https://github.com/MinishLab/semhash).

## Deduplication Details

- **Method**: {method}
- **Column**: `{column}`
- **Original size**: {original_size:,} samples
- **Deduplicated size**: {deduplicated_size:,} samples
- **Duplicate ratio**: {duplicate_ratio:.2%}
- **Reduction**: {(1 - deduplicated_size / original_size):.2%}
"""

    if threshold is not None:
        card_content += f"- **Similarity threshold**: {threshold}\n"

    card_content += f"""
- **Date processed**: {datetime.now().strftime("%Y-%m-%d")}

## How to use

```python
from datasets import load_dataset

dataset = load_dataset("{original_dataset_id.split("/")[-1]}-deduplicated")
```

## Processing script

This dataset was created using the following script:

```bash
uv run dedupe-dataset.py {original_dataset_id} {column} <repo_id> --method {method}
```

## About semantic deduplication

Unlike exact deduplication, semantic deduplication identifies and removes samples that are 
semantically similar even if they use different words. This helps create cleaner training 
datasets and prevents data leakage between train/test splits.
"""

    return card_content


def main():
    """Main function to run deduplication."""
    args = parse_args()

    # Check for HF token
    token = os.environ.get("HF_TOKEN")
    if not token:
        print(
            "Warning: HF_TOKEN not found in environment. You may not be able to push to private repos."
        )

    # Load dataset
    print(f"Loading dataset '{args.dataset_id}' (split: {args.split})...")
    try:
        if args.max_samples:
            dataset = load_dataset(
                args.dataset_id, split=f"{args.split}[:{args.max_samples}]", token=token
            )
        else:
            dataset = load_dataset(args.dataset_id, split=args.split, token=token)
    except Exception as e:
        print(f"Error loading dataset: {e}")
        sys.exit(1)

    # Validate column exists
    if args.column not in dataset.column_names:
        print(f"Error: Column '{args.column}' not found in dataset.")
        print(f"Available columns: {', '.join(dataset.column_names)}")
        sys.exit(1)

    # Convert dataset to records for semhash
    print(f"Preparing dataset for deduplication on column '{args.column}'...")
    records = [dict(row) for row in dataset]
    original_size = len(records)
    print(f"Found {original_size:,} samples")

    # Initialize SemHash with the specific column
    print("Initializing SemHash with default model...")
    semhash = SemHash.from_records(records=records, columns=[args.column])

    # Apply selected method
    print(f"Applying {args.method} method...")
    if args.method == "deduplicate":
        if args.threshold:
            result = semhash.self_deduplicate(threshold=args.threshold)
        else:
            result = semhash.self_deduplicate()
    elif args.method == "filter_outliers":
        result = semhash.self_filter_outliers()
    elif args.method == "find_representative":
        result = semhash.self_find_representative()

    # Get deduplicated records
    deduplicated_records = result.selected
    deduplicated_size = len(deduplicated_records)

    # Print statistics
    print("\nDeduplication complete!")
    print(f"Original size: {original_size:,}")
    print(f"Deduplicated size: {deduplicated_size:,}")
    print(
        f"Removed: {original_size - deduplicated_size:,} ({result.duplicate_ratio:.2%})"
    )

    # Create new dataset from deduplicated records
    print("\nCreating deduplicated dataset...")
    deduplicated_dataset = Dataset.from_list(deduplicated_records)

    # Push dataset to hub first (this creates the repo)
    print(f"\nPushing deduplicated dataset to '{args.repo_id}'...")
    try:
        deduplicated_dataset.push_to_hub(
            args.repo_id,
            private=args.private,
            token=token,
            commit_message=f"Add deduplicated version of {args.dataset_id}",
        )
        print("Dataset pushed successfully!")

        # Create and push dataset card
        print("Creating and pushing dataset card...")
        card_content = create_dataset_card(
            original_dataset_id=args.dataset_id,
            column=args.column,
            method=args.method,
            duplicate_ratio=result.duplicate_ratio,
            original_size=original_size,
            deduplicated_size=deduplicated_size,
            threshold=args.threshold,
        )

        card = DatasetCard(card_content)
        card.push_to_hub(
            repo_id=args.repo_id,
            repo_type="dataset",
            token=token,
            commit_message="Add dataset card",
        )

        print(
            f"\nSuccess! Dataset available at: https://huggingface.co/datasets/{args.repo_id}"
        )
    except Exception as e:
        print(f"Error: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()