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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
ShareGPT Semantic Deduplication Script using SemHash
|
| 4 |
+
|
| 5 |
+
This script deduplicates ShareGPT format JSONL files using semantic similarity.
|
| 6 |
+
It extracts conversation content and removes semantically similar conversations.
|
| 7 |
+
|
| 8 |
+
Usage:
|
| 9 |
+
python deduplicate_sharegpt.py input.jsonl output_dir [options]
|
| 10 |
+
|
| 11 |
+
Example ShareGPT format:
|
| 12 |
+
{"conversations": [{"from": "human", "value": "Hello"}, {"from": "gpt", "value": "Hi there!"}]}
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import argparse
|
| 16 |
+
import json
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
from pathlib import Path
|
| 20 |
+
from typing import List, Dict, Any, Optional
|
| 21 |
+
import logging
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
from semhash import SemHash
|
| 25 |
+
except ImportError:
|
| 26 |
+
print("Error: SemHash not installed. Please run: pip install semhash")
|
| 27 |
+
sys.exit(1)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def setup_logging(verbose: bool = False):
|
| 31 |
+
"""Setup logging configuration."""
|
| 32 |
+
level = logging.DEBUG if verbose else logging.INFO
|
| 33 |
+
logging.basicConfig(
|
| 34 |
+
level=level,
|
| 35 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 36 |
+
datefmt='%Y-%m-%d %H:%M:%S'
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def extract_conversation_text(conversation: List[Dict[str, str]],
|
| 41 |
+
mode: str = "full") -> str:
|
| 42 |
+
"""
|
| 43 |
+
Extract text from a conversation for deduplication.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
conversation: List of conversation turns
|
| 47 |
+
mode: How to extract text ("full", "human_only", "assistant_only", "first_turn")
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
Extracted text string
|
| 51 |
+
"""
|
| 52 |
+
texts = []
|
| 53 |
+
|
| 54 |
+
for turn in conversation:
|
| 55 |
+
from_role = turn.get("from", "")
|
| 56 |
+
value = turn.get("value", "")
|
| 57 |
+
|
| 58 |
+
if mode == "full":
|
| 59 |
+
texts.append(f"{from_role}: {value}")
|
| 60 |
+
elif mode == "human_only" and from_role in ["human", "user"]:
|
| 61 |
+
texts.append(value)
|
| 62 |
+
elif mode == "assistant_only" and from_role in ["gpt", "assistant"]:
|
| 63 |
+
texts.append(value)
|
| 64 |
+
elif mode == "first_turn" and len(texts) == 0:
|
| 65 |
+
texts.append(value)
|
| 66 |
+
|
| 67 |
+
return " ".join(texts).strip()
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def load_sharegpt_jsonl(file_path: str) -> List[Dict[str, Any]]:
|
| 71 |
+
"""Load ShareGPT JSONL file."""
|
| 72 |
+
conversations = []
|
| 73 |
+
|
| 74 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 75 |
+
for line_num, line in enumerate(f, 1):
|
| 76 |
+
line = line.strip()
|
| 77 |
+
if not line:
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
data = json.loads(line)
|
| 82 |
+
conversations.append(data)
|
| 83 |
+
except json.JSONDecodeError as e:
|
| 84 |
+
logging.warning(f"Skipping invalid JSON on line {line_num}: {e}")
|
| 85 |
+
continue
|
| 86 |
+
|
| 87 |
+
logging.info(f"Loaded {len(conversations)} conversations from {file_path}")
|
| 88 |
+
return conversations
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def save_conversations(conversations: List[Dict[str, Any]],
|
| 92 |
+
output_path: str):
|
| 93 |
+
"""Save conversations to JSONL file."""
|
| 94 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
| 95 |
+
for conv in conversations:
|
| 96 |
+
f.write(json.dumps(conv, ensure_ascii=False) + '\n')
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def main():
|
| 100 |
+
parser = argparse.ArgumentParser(
|
| 101 |
+
description="Semantically deduplicate ShareGPT JSONL files using SemHash",
|
| 102 |
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
| 103 |
+
epilog="""
|
| 104 |
+
Examples:
|
| 105 |
+
# Basic deduplication
|
| 106 |
+
python deduplicate_sharegpt.py input.jsonl output_dir
|
| 107 |
+
|
| 108 |
+
# Only consider human messages for similarity
|
| 109 |
+
python deduplicate_sharegpt.py input.jsonl output_dir --mode human_only
|
| 110 |
+
|
| 111 |
+
# Use custom similarity threshold
|
| 112 |
+
python deduplicate_sharegpt.py input.jsonl output_dir --threshold 0.8
|
| 113 |
+
|
| 114 |
+
# Enable verbose logging
|
| 115 |
+
python deduplicate_sharegpt.py input.jsonl output_dir --verbose
|
| 116 |
+
"""
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
parser.add_argument("input_file", help="Input ShareGPT JSONL file")
|
| 120 |
+
parser.add_argument("output_dir", help="Output directory for deduplicated files")
|
| 121 |
+
|
| 122 |
+
parser.add_argument("--mode", choices=["full", "human_only", "assistant_only", "first_turn"],
|
| 123 |
+
default="full", help="Text extraction mode (default: full)")
|
| 124 |
+
|
| 125 |
+
parser.add_argument("--threshold", type=float, default=0.85,
|
| 126 |
+
help="Similarity threshold for deduplication (default: 0.85)")
|
| 127 |
+
|
| 128 |
+
parser.add_argument("--min-length", type=int, default=10,
|
| 129 |
+
help="Minimum text length to consider (default: 10)")
|
| 130 |
+
|
| 131 |
+
parser.add_argument("--max-conversations", type=int,
|
| 132 |
+
help="Maximum number of conversations to process")
|
| 133 |
+
|
| 134 |
+
parser.add_argument("--verbose", "-v", action="store_true",
|
| 135 |
+
help="Enable verbose logging")
|
| 136 |
+
|
| 137 |
+
parser.add_argument("--save-explanations", action="store_true",
|
| 138 |
+
help="Save explanation file showing why items were removed")
|
| 139 |
+
|
| 140 |
+
args = parser.parse_args()
|
| 141 |
+
|
| 142 |
+
# Setup logging
|
| 143 |
+
setup_logging(args.verbose)
|
| 144 |
+
|
| 145 |
+
# Validate input file
|
| 146 |
+
if not os.path.exists(args.input_file):
|
| 147 |
+
logging.error(f"Input file not found: {args.input_file}")
|
| 148 |
+
sys.exit(1)
|
| 149 |
+
|
| 150 |
+
# Create output directory
|
| 151 |
+
output_dir = Path(args.output_dir)
|
| 152 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 153 |
+
|
| 154 |
+
# Load conversations
|
| 155 |
+
logging.info("Loading conversations...")
|
| 156 |
+
conversations = load_sharegpt_jsonl(args.input_file)
|
| 157 |
+
|
| 158 |
+
if args.max_conversations:
|
| 159 |
+
conversations = conversations[:args.max_conversations]
|
| 160 |
+
logging.info(f"Limited to {len(conversations)} conversations")
|
| 161 |
+
|
| 162 |
+
# Extract text for deduplication
|
| 163 |
+
logging.info(f"Extracting text using mode: {args.mode}")
|
| 164 |
+
texts = []
|
| 165 |
+
valid_indices = []
|
| 166 |
+
|
| 167 |
+
for i, conv in enumerate(conversations):
|
| 168 |
+
if "conversations" not in conv:
|
| 169 |
+
logging.warning(f"Skipping conversation {i}: missing 'conversations' field")
|
| 170 |
+
continue
|
| 171 |
+
|
| 172 |
+
text = extract_conversation_text(conv["conversations"], args.mode)
|
| 173 |
+
|
| 174 |
+
if len(text) < args.min_length:
|
| 175 |
+
logging.debug(f"Skipping conversation {i}: text too short ({len(text)} chars)")
|
| 176 |
+
continue
|
| 177 |
+
|
| 178 |
+
texts.append(text)
|
| 179 |
+
valid_indices.append(i)
|
| 180 |
+
|
| 181 |
+
logging.info(f"Prepared {len(texts)} conversations for deduplication")
|
| 182 |
+
|
| 183 |
+
if not texts:
|
| 184 |
+
logging.error("No valid conversations found for deduplication")
|
| 185 |
+
sys.exit(1)
|
| 186 |
+
|
| 187 |
+
# Perform deduplication
|
| 188 |
+
logging.info("Initializing SemHash...")
|
| 189 |
+
try:
|
| 190 |
+
semhash = SemHash.from_records(records=texts)
|
| 191 |
+
logging.info("Performing self-deduplication...")
|
| 192 |
+
result = semhash.self_deduplicate(threshold=args.threshold)
|
| 193 |
+
|
| 194 |
+
# Get deduplicated texts
|
| 195 |
+
deduplicated_texts = result.selected
|
| 196 |
+
|
| 197 |
+
# Find which original conversations correspond to the deduplicated texts
|
| 198 |
+
deduplicated_conversations = []
|
| 199 |
+
deduplicated_indices = []
|
| 200 |
+
|
| 201 |
+
for dedup_text in deduplicated_texts:
|
| 202 |
+
for i, original_text in enumerate(texts):
|
| 203 |
+
if original_text == dedup_text:
|
| 204 |
+
deduplicated_conversations.append(conversations[valid_indices[i]])
|
| 205 |
+
deduplicated_indices.append(i)
|
| 206 |
+
break
|
| 207 |
+
|
| 208 |
+
logging.info(f"Deduplication complete:")
|
| 209 |
+
logging.info(f" Original: {len(texts)} conversations")
|
| 210 |
+
logging.info(f" Deduplicated: {len(deduplicated_conversations)} conversations")
|
| 211 |
+
logging.info(f" Removed: {len(texts) - len(deduplicated_conversations)} conversations")
|
| 212 |
+
logging.info(f" Reduction: {((len(texts) - len(deduplicated_conversations)) / len(texts) * 100):.1f}%")
|
| 213 |
+
|
| 214 |
+
except Exception as e:
|
| 215 |
+
logging.error(f"Deduplication failed: {e}")
|
| 216 |
+
sys.exit(1)
|
| 217 |
+
|
| 218 |
+
# Save results
|
| 219 |
+
output_file = output_dir / "deduplicated.jsonl"
|
| 220 |
+
logging.info(f"Saving deduplicated conversations to {output_file}")
|
| 221 |
+
save_conversations(deduplicated_conversations, str(output_file))
|
| 222 |
+
|
| 223 |
+
# Save statistics
|
| 224 |
+
stats_file = output_dir / "deduplication_stats.json"
|
| 225 |
+
stats = {
|
| 226 |
+
"input_file": args.input_file,
|
| 227 |
+
"mode": args.mode,
|
| 228 |
+
"threshold": args.threshold,
|
| 229 |
+
"min_length": args.min_length,
|
| 230 |
+
"original_count": len(conversations),
|
| 231 |
+
"valid_count": len(texts),
|
| 232 |
+
"deduplicated_count": len(deduplicated_conversations),
|
| 233 |
+
"removed_count": len(texts) - len(deduplicated_conversations),
|
| 234 |
+
"reduction_percentage": ((len(texts) - len(deduplicated_conversations)) / len(texts) * 100) if texts else 0,
|
| 235 |
+
"duplicate_ratio": result.duplicate_ratio if hasattr(result, 'duplicate_ratio') else None,
|
| 236 |
+
"exact_duplicate_ratio": result.exact_duplicate_ratio if hasattr(result, 'exact_duplicate_ratio') else None
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
with open(stats_file, 'w', encoding='utf-8') as f:
|
| 240 |
+
json.dump(stats, f, indent=2, ensure_ascii=False)
|
| 241 |
+
|
| 242 |
+
logging.info(f"Statistics saved to {stats_file}")
|
| 243 |
+
|
| 244 |
+
# Save explanations if requested
|
| 245 |
+
if args.save_explanations:
|
| 246 |
+
explanations_file = output_dir / "explanations.txt"
|
| 247 |
+
logging.info(f"Saving explanations to {explanations_file}")
|
| 248 |
+
|
| 249 |
+
try:
|
| 250 |
+
# Get removed/duplicate texts from the result object
|
| 251 |
+
removed_texts = result.filtered if hasattr(result, 'filtered') else []
|
| 252 |
+
|
| 253 |
+
with open(explanations_file, 'w', encoding='utf-8') as f:
|
| 254 |
+
f.write(f"Deduplication Explanations\n")
|
| 255 |
+
f.write(f"={'=' * 50}\n\n")
|
| 256 |
+
f.write(f"Threshold: {args.threshold}\n")
|
| 257 |
+
f.write(f"Mode: {args.mode}\n")
|
| 258 |
+
f.write(f"Duplicate ratio: {result.duplicate_ratio if hasattr(result, 'duplicate_ratio') else 'N/A'}\n")
|
| 259 |
+
f.write(f"Exact duplicate ratio: {result.exact_duplicate_ratio if hasattr(result, 'exact_duplicate_ratio') else 'N/A'}\n\n")
|
| 260 |
+
|
| 261 |
+
if removed_texts:
|
| 262 |
+
f.write(f"Removed {len(removed_texts)} conversations:\n\n")
|
| 263 |
+
for i, removed_text in enumerate(removed_texts):
|
| 264 |
+
# Find the original conversation index for this removed text
|
| 265 |
+
original_idx = None
|
| 266 |
+
for j, original_text in enumerate(texts):
|
| 267 |
+
if original_text == removed_text:
|
| 268 |
+
original_idx = valid_indices[j]
|
| 269 |
+
break
|
| 270 |
+
|
| 271 |
+
f.write(f"Removed conversation {original_idx or i}:\n")
|
| 272 |
+
f.write(f"Text: {removed_text[:200]}{'...' if len(removed_text) > 200 else ''}\n")
|
| 273 |
+
f.write(f"Reason: Semantically similar to retained conversation\n\n")
|
| 274 |
+
else:
|
| 275 |
+
f.write("No specific removal information available from SemHash result.\n")
|
| 276 |
+
f.write("Use result.get_least_similar_from_duplicates() for threshold tuning.\n")
|
| 277 |
+
|
| 278 |
+
except Exception as e:
|
| 279 |
+
logging.warning(f"Could not save explanations: {e}")
|
| 280 |
+
|
| 281 |
+
logging.info("Deduplication completed successfully!")
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
if __name__ == "__main__":
|
| 285 |
+
main()
|