Upload code/data_builders/build_500m_sarus_1m_dataset.py with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392# ==============================================================================# 🚀 ViuAI Sarus-500M — Master 500K-1M Mega Chat & Knowledge Dataset Builder# Target Hugging Face Repo: ViuAI/500M_Sarus_SFT# ==============================================================================# Seamlessly builds 500,000 to 1,000,000 High-Quality Samples:# 1. Ingests local/HF identity (sft_identity.json) & greetings (sft_greetings.json)# 2. Ingests all 21 raw capability domains from ViuAI/sarus-sft-data-raw (~288,000 samples)# 3. High-speed streams additional Science, World & Indian History, Multi-turn Chat,# and Instruction Following from top Hugging Face open datasets.# 4. Tokenizes directly with 64,003 vocab into memory-mapped NumPy shards.# 5. Auto-uploads raw JSONL + tokenized shards to Hugging Face Hub (ViuAI/500M_Sarus_SFT).# ==============================================================================
import osimport sysimport jsonimport reimport timeimport randomimport shutilimport argparseimport numpy as npfrom datasets import load_datasetfrom huggingface_hub import HfApi, hf_hub_download, loginfrom tokenizers import Tokenizer
if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)if hasattr(sys.stderr, "reconfigure"): sys.stderr.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
# ------------------------------------------------------------------------------# 1. Config & CLI Arguments# ------------------------------------------------------------------------------parser = argparse.ArgumentParser(description="ViuAI Sarus-500M Mega SFT Dataset Builder (500k-1M)")parser.add_argument("--target_samples", type=int, default=600000, help="Target total samples (e.g. 500000 to 1000000)")parser.add_argument("--repo_id", type=str, default="ViuAI/500M_Sarus_SFT", help="Hugging Face Dataset Repo ID")parser.add_argument("--model_repo", type=str, default="ViuAI/ViuAI-500M", help="Hugging Face Model Repo for Tokenizer/Base weights")parser.add_argument("--raw_repo", type=str, default="ViuAI/sarus-sft-data-raw", help="Hugging Face Raw SFT Repo")parser.add_argument("--push_to_hf", action="store_true", help="Auto push to Hugging Face Hub")parser.add_argument("--hf_token", type=str, default=None, help="Hugging Face API Token")parser.add_argument("--max_seq_len", type=int, default=2048, help="Maximum sequence length")parser.add_argument("--val_ratio", type=float, default=0.015, help="Validation split ratio")args = parser.parse_args()
HF_TOKEN = args.hf_token or os.environ.get("HF_TOKEN")if HF_TOKEN: try: login(token=HF_TOKEN) print("✅ Logged in to Hugging Face Hub successfully.") except Exception as e: print(f"⚠️ Notice: HF login warning: {e}")
BASE_DIR = os.getcwd()OUTPUT_RAW_DIR = os.path.join(BASE_DIR, "data", "500M_Sarus_SFT_raw")OUTPUT_TOKENIZED_DIR = os.path.join(BASE_DIR, "tokenized_data", "500M_Sarus_SFT")os.makedirs(OUTPUT_RAW_DIR, exist_ok=True)os.makedirs(OUTPUT_TOKENIZED_DIR, exist_ok=True)
USER_TOKEN = "<|user|>"ASSISTANT_TOKEN = "<|assistant|>"EOT_TOKEN = "<|endofturn|>"EOT_ID = 64002PAD_TOKEN_ID = 64002
DOMAIN_MAP = { 0: "identity_persona", 1: "greetings_warm", 2: "science_ncert_stem", 3: "indian_history_affairs", 4: "multiturn_conversations", 5: "everyday_chat_empathy", 6: "instruction_constraints", 7: "practical_logic_qa", 8: "workplace_corporate_writing", 9: "python_tech_troubleshoot", 10: "cooking_kitchen_science", 11: "safety_antihallucination"}
# ------------------------------------------------------------------------------# 2. Text Cleaner & Safety Filter# ------------------------------------------------------------------------------FORBIDDEN_IDENTITIES = [ (re.compile(r"\b(i am|as an?)\s+(ai\s+language\s+model|large\s+language\s+model)\s+created\s+by\s+openai\b", re.I), "I am Sarus, a lightweight cognitive AI developed by ViuAI."), (re.compile(r"\b(trained|developed|created)\s+by\s+openai\b", re.I), "developed by ViuAI"), (re.compile(r"\bchatgpt\b", re.I), "Sarus")]
DEVANAGARI_REGEX = re.compile(r"[\u0900-\u097F]")
def clean_text_pair(instruction: str, response: str): if not instruction or not response: return None, None inst = instruction.strip() resp = response.strip()
# Rule: 100% Pure English for Sarus SFT (Zero Devanagari Hindi in English chat mode) if DEVANAGARI_REGEX.search(inst) or DEVANAGARI_REGEX.search(resp): return None, None
# Scrub competitor identities for pat, repl in FORBIDDEN_IDENTITIES: resp = pat.sub(repl, resp)
# Strip any stray <think> or </think> tags resp = re.sub(r"\[/?THINK\]", "", resp, flags=re.I) resp = re.sub(r"</?think>", "", resp, flags=re.I).strip()
if len(inst) < 3 or len(resp) < 5: return None, None
return inst, resp
# ------------------------------------------------------------------------------# 3. Tokenizer Setup# ------------------------------------------------------------------------------def load_tokenizer(): tok_candidates = [ os.path.join(BASE_DIR, "tokenizer", "tokenizer.json"), os.path.join(BASE_DIR, "tokenizer.json") ] for p in tok_candidates: if os.path.exists(p): print(f"✅ Found local tokenizer: {p}") return Tokenizer.from_file(p)
print("🌐 Downloading tokenizer from Hugging Face...") dl = hf_hub_download(repo_id=args.model_repo, filename="tokenizer/tokenizer.json", token=HF_TOKEN) os.makedirs(os.path.join(BASE_DIR, "tokenizer"), exist_ok=True) shutil.copy(dl, os.path.join(BASE_DIR, "tokenizer", "tokenizer.json")) return Tokenizer.from_file(dl)
# ------------------------------------------------------------------------------# 4. Master Ingestion Engine# ------------------------------------------------------------------------------def main(): print("=" * 85) print("🚀 ViuAI Sarus-500M — Master 500K-1M SFT Dataset Builder") print(f" • Target Sample Count: {args.target_samples:,}") print(f" • Output Repo: {args.repo_id}") print(f" • Raw Directory: {OUTPUT_RAW_DIR}") print(f" • Tokenized Shards: {OUTPUT_TOKENIZED_DIR}") print("=" * 85)
tokenizer = load_tokenizer() all_samples = []
# -------------------------------------------------------------------------- # Step 1: Ingest Local Identity & Greetings # -------------------------------------------------------------------------- print("\n📦 [1/4] Ingesting Identity & Greetings...") ident_files = [ ("data/sft_identity.json", 0), ("data/sft_greetings.json", 1), ] for local_f, dom_id in ident_files: p = os.path.join(BASE_DIR, local_f) if not os.path.exists(p): try: print(f" ⬇️ Fetching {local_f} from Hugging Face {args.model_repo}...") dl = hf_hub_download(repo_id=args.model_repo, filename=local_f, token=HF_TOKEN) p = dl except Exception as e: print(f" ⚠️ Could not fetch {local_f}: {e}") continue
with open(p, "r", encoding="utf-8") as fp: items = json.load(fp) cnt = 0 for it in items: inst, resp = clean_text_pair(it.get("instruction", ""), it.get("response", "")) if inst and resp: all_samples.append({"instruction": inst, "response": resp, "domain_id": dom_id}) cnt += 1 print(f" ✅ Ingested {cnt:,} clean samples from {local_f}")
# -------------------------------------------------------------------------- # Step 2: Ingest Raw 21-Domain Datasets (Science, History, Chat, etc.) # -------------------------------------------------------------------------- print("\n📦 [2/4] Ingesting 21 Core Domains (Science, History, Chat, Workplace)...") sft_v1_files = [ ("d1_ncert_curriculum.jsonl", 2), ("d2_indian_history_affairs.jsonl", 3), ("d3_everyday_chat_empathy.jsonl", 5), ("d4_multiturn_dialogue.jsonl", 4), ("d5_instruction_following.jsonl", 6), ("d6_typo_robustness_slangs.jsonl", 5), ("d7_summarization_tldr.jsonl", 8), ("d8_corporate_workplace_writing.jsonl", 8), ("d9_cooking_kitchen_science.jsonl", 10), ("d10_antihallucination_trick_qa.jsonl", 11), ("d11_extraction_table_formatting.jsonl", 8), ("d12_comparisons_pros_cons.jsonl", 7), ("d13_health_wellness_remedies.jsonl", 2), ("d14_career_guidance_interview.jsonl", 8), ("d15_tech_troubleshooting.jsonl", 9), ("d16_indian_festivals_heritage.jsonl", 3), ("d17_finance_budgeting_scam_safety.jsonl", 11), ("d18_mental_wellness_exam_habits.jsonl", 5), ("d19_creative_writing_scripts.jsonl", 8), ("d20_commonsense_practical_logic.jsonl", 7) ]
for fname, dom_id in sft_v1_files: local_p = os.path.join(BASE_DIR, "data", "sft_v1_raw", fname) if not os.path.exists(local_p): try: print(f" ⬇️ Fetching {fname} from {args.raw_repo}...") dl = hf_hub_download(repo_id=args.raw_repo, filename=f"sft_v1_raw/{fname}", repo_type="dataset", token=HF_TOKEN) local_p = dl except Exception as e: print(f" ⚠️ Notice: Could not fetch {fname}: {e}") continue
cnt = 0 with open(local_p, "r", encoding="utf-8") as fp: for line in fp: try: data = json.loads(line) inst, resp = clean_text_pair(data.get("instruction", ""), data.get("response", "")) if inst and resp: all_samples.append({"instruction": inst, "response": resp, "domain_id": dom_id}) cnt += 1 except Exception: continue print(f" • {fname:38s}: Ingested {cnt:6,d} clean samples")
print(f"📊 Current Ingested Total: {len(all_samples):,} samples")
# -------------------------------------------------------------------------- # Step 3: Stream High-Quality Open Datasets up to Target Samples # -------------------------------------------------------------------------- remaining_needed = max(0, args.target_samples - len(all_samples)) if remaining_needed > 0: print(f"\n🌐 [3/4] Streaming {remaining_needed:,} additional high-grade conversational samples from Hugging Face...") streams = [ ("HuggingFaceH4/ultrachat_200k", "train_sft", 4, int(remaining_needed * 0.40)), ("teknium/OpenHermes-2.5", None, 6, int(remaining_needed * 0.35)), ("HuggingFaceTB/cosmopedia", "web_samples", 2, int(remaining_needed * 0.25)) ]
for ds_name, split, dom_id, count_target in streams: if count_target <= 0: continue print(f" 🌊 Streaming from {ds_name} (Target: ~{count_target:,} samples)...") stream_count = 0 try: ds = load_dataset(ds_name, split=split or "train", streaming=True, token=HF_TOKEN) for item in ds: inst, resp = "", "" if "messages" in item: msgs = item["messages"] if len(msgs) >= 2 and msgs[0]["role"] == "user" and msgs[1]["role"] == "assistant": inst = msgs[0]["content"] resp = msgs[1]["content"] elif "conversations" in item: convs = item["conversations"] if len(convs) >= 2: inst = convs[0].get("value", "") resp = convs[1].get("value", "") elif "prompt" in item and "text" in item: inst = item["prompt"] resp = item["text"] elif "instruction" in item and "output" in item: inst = item["instruction"] resp = item["output"]
c_inst, c_resp = clean_text_pair(inst, resp) if c_inst and c_resp: all_samples.append({"instruction": c_inst, "response": c_resp, "domain_id": dom_id}) stream_count += 1 if stream_count >= count_target: break if stream_count % 25000 == 0: print(f" ... Streamed {stream_count:,} / {count_target:,} samples") except Exception as e: print(f" ⚠️ Notice while streaming {ds_name}: {e}. Continuing with available data.") print(f" ✅ Finished {ds_name}: Collected {stream_count:,} samples")
total_count = len(all_samples) print(f"\n🎉 Total Validated Dataset Size: {total_count:,} samples across {len(DOMAIN_MAP)} domains!")
# -------------------------------------------------------------------------- # Step 4: Tokenize & Pack Memory-Mapped Binary Shards # -------------------------------------------------------------------------- print("\n⚡ [4/4] Shuffling & Packing into NumPy Memory-Mapped Binary Shards...") random.seed(42) random.shuffle(all_samples)
val_count = max(1000, int(total_count * args.val_ratio)) train_count = total_count - val_count
print(f" • Train Split: {train_count:,} samples") print(f" • Val Split: {val_count:,} samples")
train_tokens, train_labels, train_offsets, train_domains = [], [], [0], [] val_tokens, val_labels, val_offsets, val_domains = [], [], [0], []
t0 = time.time() for idx, item in enumerate(all_samples): inst = item["instruction"] resp = item["response"] did = item["domain_id"]
prompt_str = f"{USER_TOKEN}\n{inst}{EOT_TOKEN}\n{ASSISTANT_TOKEN}\n" resp_str = f"{resp}{EOT_TOKEN}"
p_ids = tokenizer.encode(prompt_str).ids r_ids = tokenizer.encode(resp_str).ids
if len(r_ids) < 4: continue
sample_tokens = p_ids + r_ids sample_labels = [-100] * len(p_ids) + r_ids
if len(sample_tokens) > args.max_seq_len: sample_tokens = sample_tokens[:args.max_seq_len] sample_labels = sample_labels[:args.max_seq_len] sample_tokens[-1] = EOT_ID sample_labels[-1] = EOT_ID
is_val = (idx < val_count) if is_val: val_tokens.extend(sample_tokens) val_labels.extend(sample_labels) val_offsets.append(len(val_tokens)) val_domains.append(did) else: train_tokens.extend(sample_tokens) train_labels.extend(sample_labels) train_offsets.append(len(train_tokens)) train_domains.append(did)
if (idx + 1) % 50000 == 0 or (idx + 1) == total_count: rate = (idx + 1) / max(0.001, time.time() - t0) print(f" • Tokenized {idx + 1:,} / {total_count:,} samples ({rate:.0f} samples/sec)...")
# Save to disk print("\n💾 Writing binary shards to disk...") np.save(os.path.join(OUTPUT_TOKENIZED_DIR, "train_tokens.npy"), np.array(train_tokens, dtype=np.uint16)) np.save(os.path.join(OUTPUT_TOKENIZED_DIR, "train_labels.npy"), np.array(train_labels, dtype=np.int32)) np.save(os.path.join(OUTPUT_TOKENIZED_DIR, "train_offsets.npy"), np.array(train_offsets, dtype=np.int64)) np.save(os.path.join(OUTPUT_TOKENIZED_DIR, "train_domains.npy"), np.array(train_domains, dtype=np.int16))
np.save(os.path.join(OUTPUT_TOKENIZED_DIR, "val_tokens.npy"), np.array(val_tokens, dtype=np.uint16)) np.save(os.path.join(OUTPUT_TOKENIZED_DIR, "val_labels.npy"), np.array(val_labels, dtype=np.int32)) np.save(os.path.join(OUTPUT_TOKENIZED_DIR, "val_offsets.npy"), np.array(val_offsets, dtype=np.int64)) np.save(os.path.join(OUTPUT_TOKENIZED_DIR, "val_domains.npy"), np.array(val_domains, dtype=np.int16))
metadata = { "dataset_name": "500M_Sarus_SFT", "total_samples": total_count, "train_samples": len(train_offsets) - 1, "val_samples": len(val_offsets) - 1, "total_train_tokens": len(train_tokens), "total_val_tokens": len(val_tokens), "domain_names": DOMAIN_MAP, "format": "Direct Response (<|user|> / <|assistant|>, Zero <think>)", "created_at": time.strftime("%Y-%m-%d %H:%M:%S") } with open(os.path.join(OUTPUT_TOKENIZED_DIR, "metadata.json"), "w", encoding="utf-8") as fp: json.dump(metadata, fp, indent=2)
print(f"✅ Tokenized files saved in {OUTPUT_TOKENIZED_DIR}!") print(f" • Total Train Tokens: {len(train_tokens):,}") print(f" • Total Val Tokens: {len(val_tokens):,}")
# -------------------------------------------------------------------------- # Step 5: Auto-upload to Hugging Face Hub # -------------------------------------------------------------------------- if args.push_to_hf: print(f"\n🚀 Uploading dataset to Hugging Face Hub ({args.repo_id})...") api = HfApi(token=HF_TOKEN) try: api.create_repo(repo_id=args.repo_id, repo_type="dataset", exist_ok=True, token=HF_TOKEN) api.upload_folder( folder_path=OUTPUT_TOKENIZED_DIR, repo_id=args.repo_id, repo_type="dataset", token=HF_TOKEN ) print(f"🎉 SUCCESS! Dataset pushed to https://huggingface.co/datasets/{args.repo_id}") except Exception as e: print(f"⚠️ Hugging Face upload error: {e}")
if __name__ == "__main__": main()code/data_builders/build_500m_sarus_1m_dataset.pyAdded+392