Datasets:
Download scripts/strip-generation-ids.py from labqoat/AsyncTool: direct link, hf CLI and curl.
- Browser
- Download file 3.59 kB
-
https://huggingface.co/datasets/labqoat/AsyncTool/resolve/main/scripts/strip-generation-ids.py
- Command line
-
hf download hf://datasets/labqoat/AsyncTool/scripts/strip-generation-ids.py
-
curl -L -o strip-generation-ids.py https://huggingface.co/datasets/labqoat/AsyncTool/resolve/main/scripts/strip-generation-ids.py
3.59 kB
| #!/usr/bin/env python3 | |
| """ | |
| Strip generationId fields from messages in dataset.jsonl and dataset.parquet | |
| """ | |
| import json | |
| import pandas as pd | |
| from pathlib import Path | |
| from typing import Any, Dict, List | |
| def strip_generation_ids_from_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]: | |
| """ | |
| Remove generationId field from each message object | |
| Args: | |
| messages: List of message dictionaries | |
| Returns: | |
| List of message dictionaries without generationId fields | |
| """ | |
| cleaned_messages = [] | |
| for message in messages: | |
| # Create a copy and remove generationId if present | |
| cleaned_message = {k: v for k, v in message.items() if k != 'generationId'} | |
| cleaned_messages.append(cleaned_message) | |
| return cleaned_messages | |
| def process_jsonl(input_path: str, output_path: str): | |
| """ | |
| Process JSONL file and strip generationId from messages | |
| Args: | |
| input_path: Path to input JSONL file | |
| output_path: Path to output JSONL file | |
| """ | |
| records = [] | |
| total_removed = 0 | |
| # Read JSONL file | |
| with open(input_path, 'r', encoding='utf-8') as f: | |
| for line in f: | |
| if line.strip(): | |
| record = json.loads(line) | |
| # Strip generationId from messages | |
| if 'messages' in record: | |
| original_count = len(record['messages']) | |
| record['messages'] = strip_generation_ids_from_messages(record['messages']) | |
| total_removed += original_count | |
| records.append(record) | |
| # Write cleaned JSONL file | |
| with open(output_path, 'w', encoding='utf-8') as f: | |
| for record in records: | |
| f.write(json.dumps(record) + '\n') | |
| print(f"✓ Processed JSONL: {input_path} -> {output_path}") | |
| print(f" - Records processed: {len(records)}") | |
| print(f" - Messages cleaned: {total_removed}") | |
| print(f" - Output size: {Path(output_path).stat().st_size / 1024:.2f} KB") | |
| def process_parquet(input_path: str, output_path: str): | |
| """ | |
| Process Parquet file and strip generationId from messages | |
| Args: | |
| input_path: Path to input Parquet file | |
| output_path: Path to output Parquet file | |
| """ | |
| # Read Parquet file | |
| df = pd.read_parquet(input_path) | |
| total_removed = 0 | |
| # Process messages column | |
| def clean_messages(messages_json: str) -> str: | |
| """Parse JSON, clean, and re-serialize""" | |
| nonlocal total_removed | |
| messages = json.loads(messages_json) | |
| total_removed += len(messages) | |
| cleaned = strip_generation_ids_from_messages(messages) | |
| return json.dumps(cleaned) | |
| # Apply cleaning to messages column | |
| df['messages'] = df['messages'].apply(clean_messages) | |
| # Write cleaned Parquet file | |
| df.to_parquet(output_path, engine='pyarrow', compression='snappy') | |
| print(f"\n✓ Processed Parquet: {input_path} -> {output_path}") | |
| print(f" - Records processed: {len(df)}") | |
| print(f" - Messages cleaned: {total_removed}") | |
| print(f" - Output size: {Path(output_path).stat().st_size / 1024:.2f} KB") | |
| if __name__ == "__main__": | |
| # Process JSONL | |
| jsonl_input = "data/dataset.jsonl" | |
| jsonl_output = "data/dataset.jsonl" | |
| process_jsonl(jsonl_input, jsonl_output) | |
| # Process Parquet | |
| parquet_input = "data/dataset.parquet" | |
| parquet_output = "data/dataset.parquet" | |
| process_parquet(parquet_input, parquet_output) | |
| print("\n✅ All files processed successfully!") | |