Download deduper.py from Alignment-Lab-AI/rawbuzzcodemess: direct link, hf CLI and curl.
- Browser
- Download file 4.76 kB
-
https://huggingface.co/datasets/Alignment-Lab-AI/rawbuzzcodemess/resolve/main/deduper.py
- Command line
-
hf download hf://datasets/Alignment-Lab-AI/rawbuzzcodemess/deduper.py
-
curl -L -o deduper.py https://huggingface.co/datasets/Alignment-Lab-AI/rawbuzzcodemess/resolve/main/deduper.py
4.76 kB
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| import pyarrow.compute as pc | |
| import pyarrow.dataset as ds | |
| from pyarrow import fs | |
| import os | |
| from tqdm import tqdm | |
| import multiprocessing | |
| import json | |
| def deduplicate_rows(file_path, start_index, end_index, batch_size, progress_queue, removed_rows_queue): | |
| # Open parquet file and process rows in batches | |
| table = pq.read_table(file_path, columns=None, use_threads=True) | |
| num_rows = table.num_rows | |
| deduped_rows = [] | |
| removed_rows = 0 | |
| seen_conversations = set() | |
| for i in range(start_index, end_index, batch_size): | |
| batch_end = min(i + batch_size, end_index) | |
| batch = table.slice(i, batch_end - i).to_pylist() | |
| # Deduplicate rows within the batch based on stringified 'conversations' field | |
| for row in batch: | |
| conversations_str = json.dumps(row['conversations']) # Stringify 'conversations' list of dicts | |
| if conversations_str not in seen_conversations: | |
| seen_conversations.add(conversations_str) | |
| deduped_rows.append(row) | |
| else: | |
| removed_rows += 1 | |
| progress_queue.put(batch_end - i) # Update the progress queue with the number of processed rows | |
| removed_rows_queue.put(removed_rows) # Update the removed rows queue | |
| return pa.Table.from_pylist(deduped_rows) | |
| def deduplicate_file(file_path, batch_size, progress_bar, total_removed_rows): | |
| # Get the total number of rows in the parquet file | |
| num_rows = pq.read_metadata(file_path).num_rows | |
| # Create shared queues for progress updates and removed rows count | |
| progress_queue = multiprocessing.Manager().Queue() | |
| removed_rows_queue = multiprocessing.Manager().Queue() | |
| # Split the file into chunks for parallel processing | |
| num_workers = min(multiprocessing.cpu_count(), num_rows) # Limit the number of workers to the number of rows | |
| chunk_size = num_rows // num_workers | |
| # Process parquet file rows in separate processes | |
| processes = [] | |
| for i in range(num_workers): | |
| start_index = i * chunk_size | |
| end_index = (i + 1) * chunk_size if i < num_workers - 1 else num_rows | |
| process = multiprocessing.Process(target=deduplicate_rows, args=(file_path, start_index, end_index, batch_size, progress_queue, removed_rows_queue)) | |
| process.start() | |
| processes.append(process) | |
| # Monitor the progress queue and update the progress bar | |
| processed_rows = 0 | |
| while processed_rows < num_rows: | |
| processed_rows += progress_queue.get() | |
| progress_bar.update(processed_rows - progress_bar.n) | |
| # Wait for all processes to finish and collect the deduplicated tables | |
| deduped_tables = [] | |
| for process in processes: | |
| process.join() | |
| deduped_table = process.exitcode | |
| if isinstance(deduped_table, pa.Table): | |
| deduped_tables.append(deduped_table) | |
| # Combine the deduplicated tables from all workers | |
| if deduped_tables: | |
| deduped_table = pa.concat_tables(deduped_tables) | |
| else: | |
| deduped_table = pa.Table.from_pylist([]) # Create an empty table if no deduplicated rows | |
| # Write the deduplicated table back to the original file | |
| pq.write_table(deduped_table, file_path) | |
| # Update the total removed rows count | |
| while not removed_rows_queue.empty(): | |
| total_removed_rows.value += removed_rows_queue.get() | |
| def deduplicate_files(input_dir, batch_size): | |
| # Get list of parquet files in the input directory | |
| parquet_files = [os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith('.parquet')] | |
| total_removed_rows = multiprocessing.Value('i', 0) # Shared variable for total removed rows count | |
| for file_path in parquet_files: | |
| num_rows = pq.read_metadata(file_path).num_rows | |
| progress_bar = tqdm(total=num_rows, desc=f"Deduplicating {os.path.basename(file_path)}", unit="row") | |
| deduplicate_file(file_path, batch_size, progress_bar, total_removed_rows) | |
| progress_bar.close() | |
| print(f"Removed {total_removed_rows.value} duplicate rows from {os.path.basename(file_path)}") | |
| print(f"Total removed rows across all files: {total_removed_rows.value}") | |
| # Check the number of rows in the files after deduplication | |
| total_rows_after = 0 | |
| for file_path in parquet_files: | |
| num_rows = pq.read_metadata(file_path).num_rows | |
| total_rows_after += num_rows | |
| print(f"Total rows after deduplication: {total_rows_after}") | |
| if __name__ == '__main__': | |
| input_dir = 'deduped' | |
| batch_size = 100 # Process rows in batches of 100 | |
| deduplicate_files(input_dir, batch_size) | |