Upload code/build_sft_v13_dataset.py with huggingface_hub
Browse files103 unmodified lines10410510610710810911011111211311417 unmodified lines132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183103 unmodified linesactual_vocab_size = len(tokenizer)print(f"✅ Tokenizer Loaded. Vocab size: {actual_vocab_size}")# --- 2. Text Normalization & Deduplication Engine ---seen_hashes = set()inverted_index = defaultdict(list)stored_ngrams = []stored_responses = []def clean_text(text: str) -> str:if not text:17 unmodified linestext = re.sub(r'<<.*?>>', '', text)return clean_text(text)def get_word_ngrams(text: str, n=3):text = unicodedata.normalize("NFKC", str(text))words = re.sub(r'[^\w\s\u0900-\u097F]', '', text.lower(), flags=re.UNICODE).split()if len(words) < n:return set(words)return set(" ".join(words[i:i+n]) for i in range(len(words) - n + 1))def is_duplicate_or_near_duplicate(prompt: str, response: str) -> bool:content_raw = (prompt.strip() + "|||" + response.strip()).lower().encode("utf-8")h = hashlib.md5(content_raw).hexdigest()if h in seen_hashes:return Truengrams = get_word_ngrams(prompt, n=3)if not ngrams:seen_hashes.add(h)return Falsecandidate_scores = defaultdict(int)for ng in ngrams:for doc_id in inverted_index[ng]:candidate_scores[doc_id] += 1if candidate_scores:current_resp_cleaned = re.sub(r'[^\w\s\u0900-\u097F]', '', response.lower(), flags=re.UNICODE).strip()top_candidates = sorted(candidate_scores.items(), key=lambda x: x[1], reverse=True)[:10]for doc_id, shared_cnt in top_candidates:prev_ngrams = stored_ngrams[doc_id]intersection = shared_cntunion = len(ngrams) + len(prev_ngrams) - intersectionif union > 0 and (intersection / union) >= JACCARD_THRESHOLD:prev_resp = stored_responses[doc_id]if len(prev_resp) > 0 and len(current_resp_cleaned) > 0:r1 = set(current_resp_cleaned.split())r2 = set(prev_resp.split())resp_overlap = len(r1 & r2) / max(1, len(r1 | r2))if resp_overlap >= 0.45:return Truedoc_id = len(stored_ngrams)stored_ngrams.append(ngrams)current_resp_cleaned = re.sub(r'[^\w\s\u0900-\u097F]', '', response.lower(), flags=re.UNICODE).strip()stored_responses.append(current_resp_cleaned)for ng in ngrams:inverted_index[ng].append(doc_id)seen_hashes.add(h)return False103 unmodified lines10410510610710810911011111217 unmodified lines130131132133134135136137138139140141142143144145146147148149150151152153103 unmodified linesactual_vocab_size = len(tokenizer)print(f"✅ Tokenizer Loaded. Vocab size: {actual_vocab_size}")# --- 2. Text Normalization & High-Speed Memory-Efficient Deduplication Engine ---seen_hashes = set()seen_prompt_hashes = {}def clean_text(text: str) -> str:if not text:17 unmodified linestext = re.sub(r'<<.*?>>', '', text)return clean_text(text)def is_duplicate_or_near_duplicate(prompt: str, response: str) -> bool:# 1. Exact content hash (Prompt + Response)content_raw = (prompt.strip() + "|||" + response.strip()).lower().encode("utf-8")h = hashlib.md5(content_raw).hexdigest()if h in seen_hashes:return True# 2. Normalized prompt hash (catches duplicate questions across splits/benchmarks)p_norm = re.sub(r'[^\w\s\u0900-\u097F]', '', prompt.lower(), flags=re.UNICODE).strip()if len(p_norm) > 15:p_hash = hashlib.md5(p_norm.encode("utf-8")).hexdigest()if p_hash in seen_prompt_hashes:prev_resp_len = seen_prompt_hashes[p_hash]if abs(len(response) - prev_resp_len) < 50:return Trueelse:seen_prompt_hashes[p_hash] = len(response)seen_hashes.add(h)return False
code/build_sft_v13_dataset.py+13-43