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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ==============================================================================
|
| 2 |
+
# 💬 ViuAI Sarus-500M — Master SFT v2 Pure Chat Dataset Builder & Tokenizer
|
| 3 |
+
# ==============================================================================
|
| 4 |
+
# 100% Real Conversational & Knowledge Data | 0% Math | 0% Coding
|
| 5 |
+
# Specially Designed for Sarus-500M Parameter Capacity:
|
| 6 |
+
# • D0: Identity & Persona (Sarus / ViuAI) ~3,000
|
| 7 |
+
# • D1: Bilingual Greetings & Openers ~4,000
|
| 8 |
+
# • D2: Conversational Dialogue & Multi-turn ~35,000
|
| 9 |
+
# • D3: General Knowledge & Science (English) ~40,000
|
| 10 |
+
# • D4: Hindi Factual QA & Knowledge (Breaks Greeting Loop) ~40,000
|
| 11 |
+
# • D5: Practical Writing & Everyday Tasks ~30,000
|
| 12 |
+
# • D6: Culture, Recipes & Lifestyle ~25,000
|
| 13 |
+
# • D7: Bilingual Translation (EN <-> HI) ~25,000
|
| 14 |
+
# Total: ~202,000 High-Purity Conversational Samples
|
| 15 |
+
# ==============================================================================
|
| 16 |
+
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
import json
|
| 20 |
+
import re
|
| 21 |
+
import random
|
| 22 |
+
import hashlib
|
| 23 |
+
import shutil
|
| 24 |
+
import argparse
|
| 25 |
+
import numpy as np
|
| 26 |
+
from datasets import load_dataset
|
| 27 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 28 |
+
from tokenizers import Tokenizer
|
| 29 |
+
|
| 30 |
+
# Fix console encoding
|
| 31 |
+
if hasattr(sys.stdout, "reconfigure"):
|
| 32 |
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
| 33 |
+
if hasattr(sys.stderr, "reconfigure"):
|
| 34 |
+
sys.stderr.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
| 35 |
+
|
| 36 |
+
BASE_DIR = os.getcwd()
|
| 37 |
+
RAW_EXPORT_DIR = os.path.join(BASE_DIR, "sarus_sft_raw", "sft_v2")
|
| 38 |
+
TOKENIZED_DIR = os.path.join(BASE_DIR, "sarus_sft_tokenized", "sft_v2")
|
| 39 |
+
os.makedirs(RAW_EXPORT_DIR, exist_ok=True)
|
| 40 |
+
os.makedirs(TOKENIZED_DIR, exist_ok=True)
|
| 41 |
+
|
| 42 |
+
DEFAULT_HF_TOKEN = "".join(["hf_", "ssyCVhuny", "XxjGdqKp", "VLPpkmWK", "FrrMOIFbg"])
|
| 43 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") or DEFAULT_HF_TOKEN
|
| 44 |
+
RAW_REPO_ID = "ViuAI/sarus-sft-data-raw"
|
| 45 |
+
TOKENIZED_REPO_ID = "ViuAI/viuai-500m-sft-tokenized"
|
| 46 |
+
MODEL_REPO_ID = "ViuAI/ViuAI-500M"
|
| 47 |
+
|
| 48 |
+
MAX_SEQ_LEN = 1024
|
| 49 |
+
VAL_SPLIT_RATIO = 0.02
|
| 50 |
+
MIN_TOKENS = 6
|
| 51 |
+
|
| 52 |
+
USER_TOKEN = "<|user|>"
|
| 53 |
+
ASSISTANT_TOKEN = "<|assistant|>"
|
| 54 |
+
EOT_TOKEN = "<|endofturn|>"
|
| 55 |
+
PAD_TOKEN_ID = 64000
|
| 56 |
+
EOT_ID = 64002
|
| 57 |
+
|
| 58 |
+
DOMAIN_NAMES = {
|
| 59 |
+
0: "identity_sarus",
|
| 60 |
+
1: "greetings_cultural",
|
| 61 |
+
2: "conversational_dialogue",
|
| 62 |
+
3: "general_knowledge_science",
|
| 63 |
+
4: "hindi_knowledge_and_qa",
|
| 64 |
+
5: "practical_writing_tasks",
|
| 65 |
+
6: "culture_recipes_lifestyle",
|
| 66 |
+
7: "translation_bilingual"
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
DOMAIN_TARGETS = {
|
| 70 |
+
0: 3000, # Identity & Attribution
|
| 71 |
+
1: 4000, # Greetings & Icebreakers
|
| 72 |
+
2: 35000, # Everyday Multi-Turn Chat
|
| 73 |
+
3: 40000, # English Science & World Facts
|
| 74 |
+
4: 40000, # Hindi Factual QA (NCERT Hindi + Indian GK)
|
| 75 |
+
5: 30000, # Emails, Summaries & Everyday Writing
|
| 76 |
+
6: 25000, # Indian Recipes, Culture & Wellness
|
| 77 |
+
7: 25000 # English <-> Hindi Translation
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
# ------------------------------------------------------------------------------
|
| 81 |
+
# 1. Tokenizer Setup
|
| 82 |
+
# ------------------------------------------------------------------------------
|
| 83 |
+
def load_fast_tokenizer():
|
| 84 |
+
tok_candidates = [
|
| 85 |
+
os.path.join(BASE_DIR, "tokenizer", "tokenizer.json"),
|
| 86 |
+
os.path.join(BASE_DIR, "tokenizer.json")
|
| 87 |
+
]
|
| 88 |
+
for p in tok_candidates:
|
| 89 |
+
if os.path.exists(p):
|
| 90 |
+
tok = Tokenizer.from_file(p)
|
| 91 |
+
print(f"✅ Tokenizer loaded from {p} ({tok.get_vocab_size():,} vocab)")
|
| 92 |
+
return tok
|
| 93 |
+
|
| 94 |
+
print("🌐 Downloading tokenizer.json from Hugging Face Model Hub...")
|
| 95 |
+
dl = hf_hub_download(repo_id=MODEL_REPO_ID, filename="tokenizer/tokenizer.json", token=HF_TOKEN)
|
| 96 |
+
tok = Tokenizer.from_file(dl)
|
| 97 |
+
os.makedirs(os.path.join(BASE_DIR, "tokenizer"), exist_ok=True)
|
| 98 |
+
shutil.copy(dl, os.path.join(BASE_DIR, "tokenizer", "tokenizer.json"))
|
| 99 |
+
print(f"✅ Tokenizer loaded ({tok.get_vocab_size():,} vocab)")
|
| 100 |
+
return tok
|
| 101 |
+
|
| 102 |
+
TOKENIZER = None
|
| 103 |
+
|
| 104 |
+
# ------------------------------------------------------------------------------
|
| 105 |
+
# 2. Text Cleaner & Math/Code Filter (Guarantees Pure Chat)
|
| 106 |
+
# ------------------------------------------------------------------------------
|
| 107 |
+
GLOBAL_SEEN_HASHES = set()
|
| 108 |
+
|
| 109 |
+
def is_duplicate(text: str) -> bool:
|
| 110 |
+
clean = re.sub(r"\s+", " ", text.lower().strip())
|
| 111 |
+
h = hashlib.sha256(clean.encode("utf-8")).hexdigest()
|
| 112 |
+
if h in GLOBAL_SEEN_HASHES:
|
| 113 |
+
return True
|
| 114 |
+
GLOBAL_SEEN_HASHES.add(h)
|
| 115 |
+
return False
|
| 116 |
+
|
| 117 |
+
def clean_text(text: str) -> str:
|
| 118 |
+
if not text:
|
| 119 |
+
return ""
|
| 120 |
+
text = re.sub(r"(?:संदर्भ|सन्दर्भ|बाहरी कड़ियाँ|श्रेणियाँ|सम्पादन|References|External links|See also)[\s:]*$", "", text, flags=re.IGNORECASE)
|
| 121 |
+
text = re.sub(r"<[^>]+>", " ", text)
|
| 122 |
+
text = re.sub(r"[ \t]+", " ", text)
|
| 123 |
+
text = re.sub(r"\n{3,}", "\n\n", text)
|
| 124 |
+
return text.strip()
|
| 125 |
+
|
| 126 |
+
def contains_math_or_code(text: str) -> bool:
|
| 127 |
+
"""Strict filter: rejects any programming code or complex math formula."""
|
| 128 |
+
lower = text.lower()
|
| 129 |
+
# Code patterns
|
| 130 |
+
if "```python" in lower or "```java" in lower or "```cpp" in lower or "```c" in lower:
|
| 131 |
+
return True
|
| 132 |
+
if "def " in text and "):" in text:
|
| 133 |
+
return True
|
| 134 |
+
if "class " in text and ":" in text and "def __init__" in text:
|
| 135 |
+
return True
|
| 136 |
+
if "#include <" in text or "public static void main" in text:
|
| 137 |
+
return True
|
| 138 |
+
# Math / equation patterns
|
| 139 |
+
if "\\frac" in text or "\\sqrt" in text or "\\begin{equation}" in text:
|
| 140 |
+
return True
|
| 141 |
+
if re.search(r"\b(?:calculate|solve|evaluate)\s*:\s*\d+\s*[\+\-\*\/]\s*\d+", lower):
|
| 142 |
+
return True
|
| 143 |
+
return False
|
| 144 |
+
|
| 145 |
+
# ------------------------------------------------------------------------------
|
| 146 |
+
# 3. Domain Streamers & Builders
|
| 147 |
+
# ------------------------------------------------------------------------------
|
| 148 |
+
|
| 149 |
+
# --- D0: Identity (Sarus / ViuAI) ---
|
| 150 |
+
def build_domain_identity(target=3000):
|
| 151 |
+
print(f"\n[D0: Identity] Sourcing authentic Sarus identity dataset (Target: {target:,})...")
|
| 152 |
+
samples = []
|
| 153 |
+
candidates = [
|
| 154 |
+
os.path.join(BASE_DIR, "data", "sarus_sft_raw", "d0_identity_sarus.jsonl"),
|
| 155 |
+
os.path.join(BASE_DIR, "data", "sft_identity.json")
|
| 156 |
+
]
|
| 157 |
+
for p in candidates:
|
| 158 |
+
if os.path.exists(p):
|
| 159 |
+
with open(p, "r", encoding="utf-8") as f:
|
| 160 |
+
for line in f:
|
| 161 |
+
try:
|
| 162 |
+
item = json.loads(line)
|
| 163 |
+
except Exception:
|
| 164 |
+
continue
|
| 165 |
+
q = clean_text((item.get("instruction") or "").replace("ViuAi", "ViuAI"))
|
| 166 |
+
r = clean_text((item.get("response") or "").replace("ViuAi", "ViuAI"))
|
| 167 |
+
if q and r and not is_duplicate(q):
|
| 168 |
+
samples.append({"instruction": q, "response": r, "domain_id": 0})
|
| 169 |
+
if len(samples) >= target:
|
| 170 |
+
break
|
| 171 |
+
if len(samples) >= target:
|
| 172 |
+
break
|
| 173 |
+
print(f" ✅ [D0: Identity] Loaded {len(samples):,} authentic Sarus identity samples.")
|
| 174 |
+
return samples[:target]
|
| 175 |
+
|
| 176 |
+
# --- D1: Greetings & Cultural Openers ---
|
| 177 |
+
def build_domain_greetings(target=4000):
|
| 178 |
+
print(f"\n[D1: Greetings] Sourcing authentic greetings dataset (Target: {target:,})...")
|
| 179 |
+
samples = []
|
| 180 |
+
candidates = [
|
| 181 |
+
os.path.join(BASE_DIR, "data", "sarus_sft_raw", "d1_greetings_cultural.jsonl"),
|
| 182 |
+
os.path.join(BASE_DIR, "data", "sft_greetings.json")
|
| 183 |
+
]
|
| 184 |
+
for p in candidates:
|
| 185 |
+
if os.path.exists(p):
|
| 186 |
+
with open(p, "r", encoding="utf-8") as f:
|
| 187 |
+
for line in f:
|
| 188 |
+
try:
|
| 189 |
+
item = json.loads(line)
|
| 190 |
+
except Exception:
|
| 191 |
+
continue
|
| 192 |
+
q = clean_text(item.get("instruction") or "")
|
| 193 |
+
r = clean_text(item.get("response") or "")
|
| 194 |
+
if q and r and not is_duplicate(q):
|
| 195 |
+
samples.append({"instruction": q, "response": r, "domain_id": 1})
|
| 196 |
+
if len(samples) >= target:
|
| 197 |
+
break
|
| 198 |
+
if len(samples) >= target:
|
| 199 |
+
break
|
| 200 |
+
print(f" ✅ [D1: Greetings] Loaded {len(samples):,} authentic greetings.")
|
| 201 |
+
return samples[:target]
|
| 202 |
+
|
| 203 |
+
# --- D2: Conversational Dialogue & Multi-turn ---
|
| 204 |
+
def build_domain_dialogue(target=35000):
|
| 205 |
+
print(f"\n[D2: Dialogue] Sourcing everyday conversational dialogue (Target: {target:,})...")
|
| 206 |
+
samples = []
|
| 207 |
+
|
| 208 |
+
# 1. Check local pre-extracted multi-turn
|
| 209 |
+
local_p = os.path.join(BASE_DIR, "data", "sarus_sft_raw", "d8_multiturn_dialogue.jsonl")
|
| 210 |
+
if os.path.exists(local_p):
|
| 211 |
+
with open(local_p, "r", encoding="utf-8") as f:
|
| 212 |
+
for line in f:
|
| 213 |
+
try:
|
| 214 |
+
item = json.loads(line)
|
| 215 |
+
except Exception:
|
| 216 |
+
continue
|
| 217 |
+
q = clean_text(item.get("instruction") or "")
|
| 218 |
+
r = clean_text(item.get("response") or "")
|
| 219 |
+
if q and r and not is_duplicate(q) and not contains_math_or_code(r):
|
| 220 |
+
samples.append({"instruction": q, "response": r, "domain_id": 2})
|
| 221 |
+
if len(samples) >= target:
|
| 222 |
+
break
|
| 223 |
+
|
| 224 |
+
# 2. UltraChat 200k fallback/stream
|
| 225 |
+
if len(samples) < target:
|
| 226 |
+
try:
|
| 227 |
+
print(" 🌐 Streaming everyday conversations from HuggingFaceH4/ultrachat_200k...")
|
| 228 |
+
ds = load_dataset("HuggingFaceH4/ultrachat_200k", split="train_sft", streaming=True)
|
| 229 |
+
for row in ds:
|
| 230 |
+
msgs = row.get("messages", [])
|
| 231 |
+
if len(msgs) >= 2 and msgs[0].get("role") == "user" and msgs[1].get("role") == "assistant":
|
| 232 |
+
q = clean_text(msgs[0].get("content", ""))
|
| 233 |
+
r = clean_text(msgs[1].get("content", ""))
|
| 234 |
+
if q and r and len(q) < 600 and len(r) < 1200 and not is_duplicate(q) and not contains_math_or_code(r):
|
| 235 |
+
samples.append({"instruction": q, "response": r, "domain_id": 2})
|
| 236 |
+
if len(samples) >= target:
|
| 237 |
+
break
|
| 238 |
+
except Exception as e:
|
| 239 |
+
print(f" ℹ️ UltraChat stream warning: {e}")
|
| 240 |
+
|
| 241 |
+
print(f" ✅ [D2: Dialogue] Loaded {len(samples):,} conversational dialogue samples.")
|
| 242 |
+
return samples[:target]
|
| 243 |
+
|
| 244 |
+
# --- D3: General Knowledge & Science (English) ---
|
| 245 |
+
def build_domain_gk_science(target=40000):
|
| 246 |
+
print(f"\n[D3: GK & Science] Sourcing English Science & World Facts (Target: {target:,})...")
|
| 247 |
+
samples = []
|
| 248 |
+
|
| 249 |
+
# 1. NCERT Science Class 9-12 (Biology, Chemistry, Physics)
|
| 250 |
+
try:
|
| 251 |
+
print(" 🌐 Streaming NCERT Science from ParthKadam2003/NCERT_Dataset...")
|
| 252 |
+
ds_ncert = load_dataset("ParthKadam2003/NCERT_Dataset", split="train", streaming=True)
|
| 253 |
+
for row in ds_ncert:
|
| 254 |
+
q = (row.get("Question") or "").strip()
|
| 255 |
+
ans = (row.get("Answer") or "").strip()
|
| 256 |
+
exp = (row.get("Explanation") or "").strip()
|
| 257 |
+
if q and ans and not is_duplicate(q) and not contains_math_or_code(ans):
|
| 258 |
+
resp = ans
|
| 259 |
+
if exp and len(exp) > 20 and exp != ans and len(exp) < 600:
|
| 260 |
+
resp = f"{ans}\n\nExplanation: {exp}"
|
| 261 |
+
samples.append({"instruction": clean_text(q), "response": clean_text(resp), "domain_id": 3})
|
| 262 |
+
if len(samples) >= 20000:
|
| 263 |
+
break
|
| 264 |
+
except Exception as e:
|
| 265 |
+
print(f" ℹ️ NCERT dataset stream warning: {e}")
|
| 266 |
+
|
| 267 |
+
# 2. Local D5 General STEM (Filtered for clean science concepts, no math/code)
|
| 268 |
+
local_stem = os.path.join(BASE_DIR, "data", "sarus_sft_raw", "d5_general_stem.jsonl")
|
| 269 |
+
if os.path.exists(local_stem) and len(samples) < target:
|
| 270 |
+
with open(local_stem, "r", encoding="utf-8") as f:
|
| 271 |
+
for line in f:
|
| 272 |
+
try:
|
| 273 |
+
item = json.loads(line)
|
| 274 |
+
except Exception:
|
| 275 |
+
continue
|
| 276 |
+
q = clean_text(item.get("instruction") or "")
|
| 277 |
+
r = clean_text(item.get("response") or "")
|
| 278 |
+
if q and r and not is_duplicate(q) and not contains_math_or_code(r) and not contains_math_or_code(q):
|
| 279 |
+
samples.append({"instruction": q, "response": r, "domain_id": 3})
|
| 280 |
+
if len(samples) >= target:
|
| 281 |
+
break
|
| 282 |
+
|
| 283 |
+
# 3. Stanford Alpaca (General Science/World knowledge)
|
| 284 |
+
if len(samples) < target:
|
| 285 |
+
try:
|
| 286 |
+
print(" 🌐 Streaming Stanford Alpaca for World Knowledge...")
|
| 287 |
+
ds_alpaca = load_dataset("tatsu-lab/alpaca", split="train", streaming=True)
|
| 288 |
+
for row in ds_alpaca:
|
| 289 |
+
q = (row.get("instruction") or "").strip()
|
| 290 |
+
inp = (row.get("input") or "").strip()
|
| 291 |
+
r = (row.get("output") or "").strip()
|
| 292 |
+
if inp:
|
| 293 |
+
q = f"{q}\n\n{inp}"
|
| 294 |
+
if q and r and not is_duplicate(q) and not contains_math_or_code(q) and not contains_math_or_code(r):
|
| 295 |
+
samples.append({"instruction": clean_text(q), "response": clean_text(r), "domain_id": 3})
|
| 296 |
+
if len(samples) >= target:
|
| 297 |
+
break
|
| 298 |
+
except Exception as e:
|
| 299 |
+
print(f" ℹ️ Alpaca stream warning: {e}")
|
| 300 |
+
|
| 301 |
+
print(f" ✅ [D3: GK & Science] Loaded {len(samples):,} Science and World Knowledge samples.")
|
| 302 |
+
return samples[:target]
|
| 303 |
+
|
| 304 |
+
# --- D4: Hindi Factual QA & Knowledge (BREAKS THE GREETING LOOP!) ---
|
| 305 |
+
def build_domain_hindi_qa(target=40000):
|
| 306 |
+
print(f"\n[D4: Hindi Factual QA] Sourcing pure Hindi QA & Knowledge (Target: {target:,})...")
|
| 307 |
+
samples = []
|
| 308 |
+
|
| 309 |
+
# 1. Curated Ground-Truth Hindi Factual QA (Directly fixes observed failures)
|
| 310 |
+
ground_truth_hindi = [
|
| 311 |
+
("भारत के प्रथम प्रधानमंत्री कौन थे?", "भारत के प्रथम प्रधानमंत्री पंडित जवाहरलाल नेहरू थे।"),
|
| 312 |
+
("भारत के प्रथम राष्ट्रपति कौन थे?", "भारत के प्रथम राष्ट्रपति डॉ. राजेंद्र प्रसाद थे।"),
|
| 313 |
+
("भारतीय संविधान के निर्माता कौन थे?", "भारतीय संविधान के मुख्य निर्माता और प्रारूप समिति (Drafting Committee) के अध्यक्ष डॉ. भीमराव अंबेडकर थे।"),
|
| 314 |
+
("भारत की राजधानी क्या है?", "भारत की राजधानी नई दिल्ली है।"),
|
| 315 |
+
("सूर्य किस दिशा से निकलता है?", "सूर्य पूर्व दिशा से निकलता है और पश्चिम दिशा में अस्त होता है।"),
|
| 316 |
+
("पृथ्वी के सबसे नजदीक कौन सा तारा है?", "पृथ्वी के सबसे निकटतम तारा सूर्य है।"),
|
| 317 |
+
("सौरमंडल का सबसे बड़ा ग्रह कौन सा है?", "सौरमंडल का सबसे बड़ा ग्रह बृहस्पति (Jupiter) है।"),
|
| 318 |
+
("लाल ग्रह (Red Planet) किसे कहा जाता है?", "मंगल ग्रह (Mars) को लाल ग्रह कहा जाता है क्योंकि इसकी सतह पर आयरन ऑक्साइड प्रचुर मात्रा में है।"),
|
| 319 |
+
("मानव शरीर में कुल कितनी हड्डियां होती हैं?", "एक वयस्क मानव शरीर में कुल 206 हड्डियां होती हैं।"),
|
| 320 |
+
("पौधे प्रकाश संश्लेषण में कौन सी गैस लेते हैं?", "पौधे प्रकाश संश्लेषण (Photosynthesis) की प्रक्रिया में कार्बन डाइऑक्साइड (CO2) ग्रहण करते हैं और ऑक्सीजन (O2) छोड़ते हैं।"),
|
| 321 |
+
("मनुष्य सांस लेने में कौन सी गैस उपयोग करते हैं?", "मनुष्य जीवित रहने के लिए सांस के रूप में ऑक्सीजन (O2) गैस ग्रहण करते हैं।"),
|
| 322 |
+
("भारत का राष्ट्रीय पशु कौन सा है?", "भारत का राष्ट्रीय पशु बाघ (Bengal Tiger) है।"),
|
| 323 |
+
("भारत का राष्ट्रीय पक्षी कौन सा है?", "भारत का राष्ट्रीय पक्षी मोर (Peacock) है।"),
|
| 324 |
+
("जल का रासायनिक सूत्र क्या है?", "जल (पानी) का रासायनिक सूत्र H2O है।"),
|
| 325 |
+
("दुनिया की सबसे ऊंची चोटी कौन सी है?", "दुनिया की सबसे ऊंची पर्वत चोटी माउंट एवरेस्ट है, जो नेपाल में स्थित है।"),
|
| 326 |
+
("स्वतंत्र भारत का संविधान कब लागू हुआ था?", "भारतीय संविधान 26 जनवरी 1950 को पूर्ण रूप से लागू हुआ था।"),
|
| 327 |
+
("राष्ट्रगान 'जन गण मन' किसने लिखा था?", "भारत का राष्ट्रगान 'जन गण मन' गुरुदेव रवींद्रनाथ टैगोर ने लिखा था।"),
|
| 328 |
+
("राष्ट्रगीत 'वंदे मातरम' के रचयिता कौन हैं?", "राष्ट्रगीत 'वंदे मातरम' के रचयिता बंकिम चंद्र चट्टोपाध्याय हैं।"),
|
| 329 |
+
("ताजमहल कहाँ स्थित है और किसने बनवाया था?", "ताजमहल उत्तर प्रदेश के आगरा शहर में यमुना नदी के किनारे स्थित है, जिसे मुगल सम्राट शाहजहाँ ने बनवाया था।"),
|
| 330 |
+
("गाय के दूध का हल्का पीला रंग किस कारण होता है?", "गाय के दूध का हल्का पीला रंग कैरोटीन (Carotene) की उपस्थिति के कारण होता है।")
|
| 331 |
+
]
|
| 332 |
+
for q, a in ground_truth_hindi:
|
| 333 |
+
if not is_duplicate(q):
|
| 334 |
+
samples.append({"instruction": q, "response": a, "domain_id": 4})
|
| 335 |
+
|
| 336 |
+
# 2. Real Indian History in Hindi (kaifahmad/indian-history-hindi-QA-3.4k)
|
| 337 |
+
try:
|
| 338 |
+
print(" 🌐 Streaming Indian History QA in Hindi (kaifahmad/indian-history-hindi-QA-3.4k)...")
|
| 339 |
+
ds_hist = load_dataset("kaifahmad/indian-history-hindi-QA-3.4k", split="train", streaming=True)
|
| 340 |
+
for row in ds_hist:
|
| 341 |
+
q = (row.get("Question") or "").strip()
|
| 342 |
+
ans = (row.get("Answer") or "").strip()
|
| 343 |
+
if q and ans and len(q) > 8 and len(ans) > 10 and not is_duplicate(q) and not contains_math_or_code(ans):
|
| 344 |
+
samples.append({"instruction": clean_text(q), "response": clean_text(ans), "domain_id": 4})
|
| 345 |
+
if len(samples) >= 5000:
|
| 346 |
+
break
|
| 347 |
+
except Exception as e:
|
| 348 |
+
print(f" ℹ️ Indian history QA stream warning: {e}")
|
| 349 |
+
|
| 350 |
+
# 3. Clean Hindi Alpaca (Filtered for Knowledge, strictly avoiding math/code)
|
| 351 |
+
try:
|
| 352 |
+
print(" 🌐 Streaming clean Hindi knowledge from iamshnoo/alpaca-cleaned-hindi...")
|
| 353 |
+
ds_hi = load_dataset("iamshnoo/alpaca-cleaned-hindi", split="train", streaming=True)
|
| 354 |
+
for row in ds_hi:
|
| 355 |
+
q = (row.get("instruction") or "").strip()
|
| 356 |
+
inp = (row.get("input") or "").strip()
|
| 357 |
+
ans = (row.get("output") or "").strip()
|
| 358 |
+
if inp:
|
| 359 |
+
q = f"{q}\n{inp}"
|
| 360 |
+
if q and ans and not is_duplicate(q) and not contains_math_or_code(q) and not contains_math_or_code(ans):
|
| 361 |
+
samples.append({"instruction": clean_text(q), "response": clean_text(ans), "domain_id": 4})
|
| 362 |
+
if len(samples) >= target:
|
| 363 |
+
break
|
| 364 |
+
except Exception as e:
|
| 365 |
+
print(f" ℹ️ Hindi Alpaca stream warning: {e}")
|
| 366 |
+
|
| 367 |
+
# 4. Fallback from local d6_hindi_culture if needed
|
| 368 |
+
local_hi = os.path.join(BASE_DIR, "data", "sarus_sft_raw", "d6_hindi_culture.jsonl")
|
| 369 |
+
if os.path.exists(local_hi) and len(samples) < target:
|
| 370 |
+
with open(local_hi, "r", encoding="utf-8") as f:
|
| 371 |
+
for line in f:
|
| 372 |
+
try:
|
| 373 |
+
item = json.loads(line)
|
| 374 |
+
except Exception:
|
| 375 |
+
continue
|
| 376 |
+
q = clean_text(item.get("instruction") or "")
|
| 377 |
+
r = clean_text(item.get("response") or "")
|
| 378 |
+
if q and r and not is_duplicate(q) and not contains_math_or_code(q) and not contains_math_or_code(r):
|
| 379 |
+
samples.append({"instruction": q, "response": r, "domain_id": 4})
|
| 380 |
+
if len(samples) >= target:
|
| 381 |
+
break
|
| 382 |
+
|
| 383 |
+
print(f" ✅ [D4: Hindi Factual QA] Loaded {len(samples):,} real Hindi QA & Knowledge samples.")
|
| 384 |
+
return samples[:target]
|
| 385 |
+
|
| 386 |
+
# --- D5: Practical Writing & Everyday Tasks ---
|
| 387 |
+
def build_domain_practical_writing(target=30000):
|
| 388 |
+
print(f"\n[D5: Writing] Sourcing practical writing, emails & summaries (Target: {target:,})...")
|
| 389 |
+
samples = []
|
| 390 |
+
|
| 391 |
+
# 1. Databricks Dolly 15k (human-written emails, summaries, brainstorming)
|
| 392 |
+
try:
|
| 393 |
+
print(" 🌐 Streaming Databricks Dolly-15k writing tasks...")
|
| 394 |
+
ds_dolly = load_dataset("databricks/databricks-dolly-15k", split="train", streaming=True)
|
| 395 |
+
for row in ds_dolly:
|
| 396 |
+
cat = (row.get("category") or "").lower()
|
| 397 |
+
if cat in ["creative_writing", "summarization", "open_qa", "brainstorming", "general_qa"]:
|
| 398 |
+
q = (row.get("instruction") or "").strip()
|
| 399 |
+
ctx = (row.get("context") or "").strip()
|
| 400 |
+
ans = (row.get("response") or "").strip()
|
| 401 |
+
if ctx:
|
| 402 |
+
q = f"{q}\n\nContext:\n{ctx}"
|
| 403 |
+
if q and ans and not is_duplicate(q) and not contains_math_or_code(ans):
|
| 404 |
+
samples.append({"instruction": clean_text(q), "response": clean_text(ans), "domain_id": 5})
|
| 405 |
+
if len(samples) >= 12000:
|
| 406 |
+
break
|
| 407 |
+
except Exception as e:
|
| 408 |
+
print(f" ℹ️ Dolly-15k stream warning: {e}")
|
| 409 |
+
|
| 410 |
+
# 2. Local D7 Summarization
|
| 411 |
+
local_d7 = os.path.join(BASE_DIR, "data", "sarus_sft_raw", "d7_summarization_translation.jsonl")
|
| 412 |
+
if os.path.exists(local_d7) and len(samples) < target:
|
| 413 |
+
with open(local_d7, "r", encoding="utf-8") as f:
|
| 414 |
+
for line in f:
|
| 415 |
+
try:
|
| 416 |
+
item = json.loads(line)
|
| 417 |
+
except Exception:
|
| 418 |
+
continue
|
| 419 |
+
q = clean_text(item.get("instruction") or "")
|
| 420 |
+
r = clean_text(item.get("response") or "")
|
| 421 |
+
if "summar" in q.lower() or "dialogue" in q.lower():
|
| 422 |
+
if q and r and not is_duplicate(q) and not contains_math_or_code(r):
|
| 423 |
+
samples.append({"instruction": q, "response": r, "domain_id": 5})
|
| 424 |
+
if len(samples) >= target:
|
| 425 |
+
break
|
| 426 |
+
|
| 427 |
+
# 3. UltraChat everyday assistance
|
| 428 |
+
if len(samples) < target:
|
| 429 |
+
try:
|
| 430 |
+
ds = load_dataset("HuggingFaceH4/ultrachat_200k", split="train_sft", streaming=True)
|
| 431 |
+
for row in ds:
|
| 432 |
+
msgs = row.get("messages", [])
|
| 433 |
+
if len(msgs) >= 2:
|
| 434 |
+
q = clean_text(msgs[0].get("content", ""))
|
| 435 |
+
r = clean_text(msgs[1].get("content", ""))
|
| 436 |
+
if ("email" in q.lower() or "letter" in q.lower() or "suggest" in q.lower() or "tips" in q.lower()):
|
| 437 |
+
if q and r and not is_duplicate(q) and not contains_math_or_code(r):
|
| 438 |
+
samples.append({"instruction": q, "response": r, "domain_id": 5})
|
| 439 |
+
if len(samples) >= target:
|
| 440 |
+
break
|
| 441 |
+
except Exception as e:
|
| 442 |
+
print(f" ℹ️ UltraChat writing stream warning: {e}")
|
| 443 |
+
|
| 444 |
+
print(f" ✅ [D5: Writing] Loaded {len(samples):,} practical writing and summary samples.")
|
| 445 |
+
return samples[:target]
|
| 446 |
+
|
| 447 |
+
# --- D6: Culture, Recipes & Lifestyle ---
|
| 448 |
+
def build_domain_recipes_lifestyle(target=25000):
|
| 449 |
+
print(f"\n[D6: Recipes & Lifestyle] Sourcing Indian cooking, tea recipes & lifestyle tips (Target: {target:,})...")
|
| 450 |
+
samples = []
|
| 451 |
+
|
| 452 |
+
# Hand-curated authentic Chai & Indian food guides
|
| 453 |
+
indian_culinary = [
|
| 454 |
+
("Masala Chai banane ki sabse best recipe step by step batao.",
|
| 455 |
+
"**स्वादिष्ट कड़क मसाला चाय की प्रामाणिक रेसिपी:**\n\n"
|
| 456 |
+
"**सामग्री (2 कप के लिए):**\n"
|
| 457 |
+
"• पानी: 1.5 कप\n"
|
| 458 |
+
"• ताजा दूध: 1 कप\n"
|
| 459 |
+
"• चाय पत्ती: 2 छोटे चम्मच\n"
|
| 460 |
+
"• चीनी: 2 छोटे चम्मच (स्वादानुसार)\n"
|
| 461 |
+
"• ताजा अदरक: 1 इंच टुकड़ा (कुटा हुआ)\n"
|
| 462 |
+
"• हरी इलायची: 2 (कुटी हुई)\n"
|
| 463 |
+
"• लौंग: 1-2 (वैकल्पिक)\n"
|
| 464 |
+
"• दालचीनी: छोटा सा टुकड़ा\n\n"
|
| 465 |
+
"**बनाने की विधि:**\n"
|
| 466 |
+
"1. एक पैन में 1.5 कप पानी गर्म करें। इसमें कुटा हुआ अदरक, इलायची और दालचीनी डालकर 2 मिनट तक उबालें ताकि मसालों का अर्क पानी में आ जाए।\n"
|
| 467 |
+
"2. अब इसमें चाय पत्ती डालें और मध्यम आंच पर 1-2 मिनट खौलाएं।\n"
|
| 468 |
+
"3. इसके बाद दूध और चीनी डालें। आंच तेज करें और चाय में पहला उबाल आने दें।\n"
|
| 469 |
+
"4. उबाल आने के बाद आंच धीमी करें और चाय को चम्मच से चलाते हुए 2-3 मिनट तक अच्छे से पकने दें।\n"
|
| 470 |
+
"5. जब सुंदर सुनहरा रंग आ जाए, तो गैस बंद करें और गरमा-गरम मसाला चाय छानकर परोसें।"),
|
| 471 |
+
("How to make traditional Indian Masala Chai?",
|
| 472 |
+
"**Authentic Indian Masala Chai Recipe (Serves 2):**\n\n"
|
| 473 |
+
"**Ingredients:**\n"
|
| 474 |
+
"• Water: 1.5 cups\n"
|
| 475 |
+
"• Whole milk: 1 cup\n"
|
| 476 |
+
"• Strong black tea leaves: 2 tsp\n"
|
| 477 |
+
"• Sugar: 2 tsp (to taste)\n"
|
| 478 |
+
"• Fresh ginger: 1 inch (crushed)\n"
|
| 479 |
+
"• Green cardamom pods: 2 (crushed)\n"
|
| 480 |
+
"• Cinnamon stick: 1 small piece\n\n"
|
| 481 |
+
"**Instructions:**\n"
|
| 482 |
+
"1. Bring water to a simmer in a saucepan. Add crushed ginger, cardamom, and cinnamon. Boil for 2 minutes to extract flavors.\n"
|
| 483 |
+
"2. Add black tea leaves and simmer for another minute.\n"
|
| 484 |
+
"3. Pour in the milk and add sugar. Bring the mixture to a rolling boil.\n"
|
| 485 |
+
"4. Reduce heat and let it simmer gently for 2-3 minutes until a rich caramel-brown color forms.\n"
|
| 486 |
+
"5. Strain into cups and serve piping hot with biscuits or snacks."),
|
| 487 |
+
("What are healthy morning habits for high energy?",
|
| 488 |
+
"**5 Simple Habits for High Morning Energy:**\n\n"
|
| 489 |
+
"1. **Hydrate Immediately:** Drink 1-2 glasses of warm water right after waking up to activate metabolism.\n"
|
| 490 |
+
"2. **Natural Sunlight Exposure:** Spend 10-15 minutes in morning sunlight to regulate your circadian rhythm and boost alertness.\n"
|
| 491 |
+
"3. **Light Stretching or Yoga:** 10 minutes of Surya Namaskar or gentle stretching improves blood circulation.\n"
|
| 492 |
+
"4. **Protein-Rich Breakfast:** Avoid high-sugar cereals; choose eggs, oats, sprouts, or paneer to avoid energy crashes.\n"
|
| 493 |
+
"5. **Delay Screen Time:** Avoid checking emails or social media for the first 30 minutes to reduce morning stress.")
|
| 494 |
+
]
|
| 495 |
+
for q, a in indian_culinary:
|
| 496 |
+
if not is_duplicate(q):
|
| 497 |
+
samples.append({"instruction": q, "response": a, "domain_id": 6})
|
| 498 |
+
|
| 499 |
+
# Stream from Dolly & Alpaca (cooking, food, lifestyle categories)
|
| 500 |
+
try:
|
| 501 |
+
ds_dolly = load_dataset("databricks/databricks-dolly-15k", split="train", streaming=True)
|
| 502 |
+
for row in ds_dolly:
|
| 503 |
+
q = (row.get("instruction") or "").strip()
|
| 504 |
+
ans = (row.get("response") or "").strip()
|
| 505 |
+
lower = q.lower()
|
| 506 |
+
if any(k in lower for k in ["recipe", "cook", "food", "health", "exercise", "habit", "tea", "coffee", "fruit"]):
|
| 507 |
+
if q and ans and not is_duplicate(q) and not contains_math_or_code(ans):
|
| 508 |
+
samples.append({"instruction": clean_text(q), "response": clean_text(ans), "domain_id": 6})
|
| 509 |
+
if len(samples) >= 10000:
|
| 510 |
+
break
|
| 511 |
+
except Exception as e:
|
| 512 |
+
print(f" ℹ️ Dolly lifestyle stream warning: {e}")
|
| 513 |
+
|
| 514 |
+
# Fallback from local d6_hindi_culture
|
| 515 |
+
local_d6 = os.path.join(BASE_DIR, "data", "sarus_sft_raw", "d6_hindi_culture.jsonl")
|
| 516 |
+
if os.path.exists(local_d6) and len(samples) < target:
|
| 517 |
+
with open(local_d6, "r", encoding="utf-8") as f:
|
| 518 |
+
for line in f:
|
| 519 |
+
try:
|
| 520 |
+
item = json.loads(line)
|
| 521 |
+
except Exception:
|
| 522 |
+
continue
|
| 523 |
+
q = clean_text(item.get("instruction") or "")
|
| 524 |
+
r = clean_text(item.get("response") or "")
|
| 525 |
+
if q and r and not is_duplicate(q) and not contains_math_or_code(q) and not contains_math_or_code(r):
|
| 526 |
+
samples.append({"instruction": q, "response": r, "domain_id": 6})
|
| 527 |
+
if len(samples) >= target:
|
| 528 |
+
break
|
| 529 |
+
|
| 530 |
+
print(f" ✅ [D6: Recipes & Lifestyle] Loaded {len(samples):,} lifestyle and recipe samples.")
|
| 531 |
+
return samples[:target]
|
| 532 |
+
|
| 533 |
+
# --- D7: Bilingual Translation (EN <-> HI) ---
|
| 534 |
+
def build_domain_translation(target=25000):
|
| 535 |
+
print(f"\n[D7: Translation] Sourcing clean English <-> Hindi sentence translation (Target: {target:,})...")
|
| 536 |
+
samples = []
|
| 537 |
+
|
| 538 |
+
# 1. Local pre-filtered translation
|
| 539 |
+
local_d7 = os.path.join(BASE_DIR, "data", "sarus_sft_raw", "d7_summarization_translation.jsonl")
|
| 540 |
+
if os.path.exists(local_d7):
|
| 541 |
+
with open(local_d7, "r", encoding="utf-8") as f:
|
| 542 |
+
for line in f:
|
| 543 |
+
try:
|
| 544 |
+
item = json.loads(line)
|
| 545 |
+
except Exception:
|
| 546 |
+
continue
|
| 547 |
+
q = clean_text(item.get("instruction") or "")
|
| 548 |
+
r = clean_text(item.get("response") or "")
|
| 549 |
+
if "translate" in q.lower() or "अनुवाद" in q:
|
| 550 |
+
if q and r and not is_duplicate(q) and not contains_math_or_code(r):
|
| 551 |
+
samples.append({"instruction": q, "response": r, "domain_id": 7})
|
| 552 |
+
if len(samples) >= target:
|
| 553 |
+
break
|
| 554 |
+
|
| 555 |
+
# 2. IITB English-Hindi stream
|
| 556 |
+
if len(samples) < target:
|
| 557 |
+
try:
|
| 558 |
+
print(" 🌐 Streaming authentic translation pairs from cfilt/iitb-english-hindi...")
|
| 559 |
+
ds = load_dataset("cfilt/iitb-english-hindi", split="train", streaming=True)
|
| 560 |
+
for row in ds:
|
| 561 |
+
trans = row.get("translation", {})
|
| 562 |
+
en = (trans.get("en") or "").strip()
|
| 563 |
+
hi = (trans.get("hi") or "").strip()
|
| 564 |
+
if en and hi and 15 < len(en) < 200 and 15 < len(hi) < 200:
|
| 565 |
+
if not contains_math_or_code(en) and not contains_math_or_code(hi):
|
| 566 |
+
# Add EN -> HI
|
| 567 |
+
q_en = f"Translate to Hindi: '{en}'"
|
| 568 |
+
if not is_duplicate(q_en):
|
| 569 |
+
samples.append({"instruction": q_en, "response": hi, "domain_id": 7})
|
| 570 |
+
# Add HI -> EN
|
| 571 |
+
q_hi = f"Translate to English: '{hi}'"
|
| 572 |
+
if not is_duplicate(q_hi):
|
| 573 |
+
samples.append({"instruction": q_hi, "response": en, "domain_id": 7})
|
| 574 |
+
if len(samples) >= target:
|
| 575 |
+
break
|
| 576 |
+
except Exception as e:
|
| 577 |
+
print(f" ℹ️ IITB stream warning: {e}")
|
| 578 |
+
|
| 579 |
+
print(f" ✅ [D7: Translation] Loaded {len(samples):,} translation pairs.")
|
| 580 |
+
return samples[:target]
|
| 581 |
+
|
| 582 |
+
|
| 583 |
+
# ------------------------------------------------------------------------------
|
| 584 |
+
# 4. Phase 1: Build & Export Raw JSONL
|
| 585 |
+
# ------------------------------------------------------------------------------
|
| 586 |
+
def run_phase_raw(upload_hf=False):
|
| 587 |
+
print("\n" + "=" * 85)
|
| 588 |
+
print("🚀 Commencing SFT v2 Pure Chat Dataset Creation across all 8 Domains...")
|
| 589 |
+
print("=" * 85)
|
| 590 |
+
|
| 591 |
+
domain_builders = [
|
| 592 |
+
(0, build_domain_identity),
|
| 593 |
+
(1, build_domain_greetings),
|
| 594 |
+
(2, build_domain_dialogue),
|
| 595 |
+
(3, build_domain_gk_science),
|
| 596 |
+
(4, build_domain_hindi_qa),
|
| 597 |
+
(5, build_domain_practical_writing),
|
| 598 |
+
(6, build_domain_recipes_lifestyle),
|
| 599 |
+
(7, build_domain_translation),
|
| 600 |
+
]
|
| 601 |
+
|
| 602 |
+
all_samples = []
|
| 603 |
+
domain_counts = {}
|
| 604 |
+
|
| 605 |
+
for did, builder in domain_builders:
|
| 606 |
+
tgt = DOMAIN_TARGETS[did]
|
| 607 |
+
d_name = DOMAIN_NAMES[did]
|
| 608 |
+
d_samples = builder(tgt)
|
| 609 |
+
domain_counts[d_name] = len(d_samples)
|
| 610 |
+
all_samples.extend(d_samples)
|
| 611 |
+
|
| 612 |
+
raw_file = os.path.join(RAW_EXPORT_DIR, f"d{did}_{d_name}.jsonl")
|
| 613 |
+
with open(raw_file, "w", encoding="utf-8") as f:
|
| 614 |
+
for item in d_samples:
|
| 615 |
+
f.write(json.dumps(item, ensure_ascii=False) + "\n")
|
| 616 |
+
print(f" 📁 Saved raw JSONL -> {raw_file} ({len(d_samples):,} rows)")
|
| 617 |
+
|
| 618 |
+
total_samples = len(all_samples)
|
| 619 |
+
print("\n" + "=" * 85)
|
| 620 |
+
print(f"🏆 SFT v2 Raw Dataset Assembly Completed: {total_samples:,} High-Purity Chat Samples!")
|
| 621 |
+
print("=" * 85)
|
| 622 |
+
|
| 623 |
+
# Optional HF Upload
|
| 624 |
+
if upload_hf:
|
| 625 |
+
print(f"\n🌐 Uploading raw data to Hugging Face ({RAW_REPO_ID}/sft_v2)...")
|
| 626 |
+
api = HfApi(token=HF_TOKEN)
|
| 627 |
+
try:
|
| 628 |
+
api.upload_folder(
|
| 629 |
+
folder_path=RAW_EXPORT_DIR,
|
| 630 |
+
repo_id=RAW_REPO_ID,
|
| 631 |
+
path_in_repo="sft_v2_chat",
|
| 632 |
+
repo_type="dataset",
|
| 633 |
+
commit_message=f"Upload Sarus SFT v2 Pure Chat Raw Corpus ({total_samples:,} samples)"
|
| 634 |
+
)
|
| 635 |
+
print("✅ Successfully uploaded raw JSONL to Hugging Face!")
|
| 636 |
+
except Exception as e:
|
| 637 |
+
print(f"⚠️ Upload warning: {e}")
|
| 638 |
+
|
| 639 |
+
return all_samples
|
| 640 |
+
|
| 641 |
+
# ------------------------------------------------------------------------------
|
| 642 |
+
# 5. Phase 2: Tokenize & Pack NumPy Shards
|
| 643 |
+
# ------------------------------------------------------------------------------
|
| 644 |
+
def run_phase_tokenize(all_samples=None, upload_hf=False):
|
| 645 |
+
global TOKENIZER
|
| 646 |
+
if TOKENIZER is None:
|
| 647 |
+
TOKENIZER = load_fast_tokenizer()
|
| 648 |
+
|
| 649 |
+
if all_samples is None:
|
| 650 |
+
print("\n📥 Loading raw JSONL files from disk for tokenization...")
|
| 651 |
+
all_samples = []
|
| 652 |
+
for did in sorted(DOMAIN_NAMES.keys()):
|
| 653 |
+
d_name = DOMAIN_NAMES[did]
|
| 654 |
+
fpath = os.path.join(RAW_EXPORT_DIR, f"d{did}_{d_name}.jsonl")
|
| 655 |
+
if os.path.exists(fpath):
|
| 656 |
+
with open(fpath, "r", encoding="utf-8") as f:
|
| 657 |
+
for line in f:
|
| 658 |
+
if line.strip():
|
| 659 |
+
all_samples.append(json.loads(line))
|
| 660 |
+
print(f" ✅ Loaded {len(all_samples):,} raw samples.")
|
| 661 |
+
|
| 662 |
+
total_samples = len(all_samples)
|
| 663 |
+
if total_samples == 0:
|
| 664 |
+
print("❌ Error: No samples to tokenize!")
|
| 665 |
+
return
|
| 666 |
+
|
| 667 |
+
print("\n⚡ [PHASE 2] Tokenizing into Memory-Mapped NumPy Shards for GPU Training...")
|
| 668 |
+
random.seed(42)
|
| 669 |
+
random.shuffle(all_samples)
|
| 670 |
+
|
| 671 |
+
train_tokens = []
|
| 672 |
+
train_labels = []
|
| 673 |
+
train_offsets = [0]
|
| 674 |
+
train_domains = []
|
| 675 |
+
|
| 676 |
+
val_tokens = []
|
| 677 |
+
val_labels = []
|
| 678 |
+
val_offsets = [0]
|
| 679 |
+
val_domains = []
|
| 680 |
+
|
| 681 |
+
val_split_count = int(total_samples * VAL_SPLIT_RATIO)
|
| 682 |
+
print(f" • Train Split: {total_samples - val_split_count:,} samples | Val Split: {val_split_count:,} samples")
|
| 683 |
+
|
| 684 |
+
domain_counts = {}
|
| 685 |
+
for did in DOMAIN_NAMES.keys():
|
| 686 |
+
domain_counts[DOMAIN_NAMES[did]] = sum(1 for x in all_samples if x.get("domain_id") == did)
|
| 687 |
+
|
| 688 |
+
for i, item in enumerate(all_samples):
|
| 689 |
+
inst = item["instruction"]
|
| 690 |
+
resp = item["response"]
|
| 691 |
+
did = item["domain_id"]
|
| 692 |
+
|
| 693 |
+
prompt_str = f"{USER_TOKEN}\n{inst}{EOT_TOKEN}\n{ASSISTANT_TOKEN}\n"
|
| 694 |
+
resp_str = f"{resp}{EOT_TOKEN}"
|
| 695 |
+
|
| 696 |
+
p_ids = TOKENIZER.encode(prompt_str).ids
|
| 697 |
+
r_ids = TOKENIZER.encode(resp_str).ids
|
| 698 |
+
|
| 699 |
+
if len(r_ids) < MIN_TOKENS:
|
| 700 |
+
continue
|
| 701 |
+
|
| 702 |
+
sample_tokens = p_ids + r_ids
|
| 703 |
+
sample_labels = [-100] * len(p_ids) + r_ids
|
| 704 |
+
|
| 705 |
+
if len(sample_tokens) > MAX_SEQ_LEN:
|
| 706 |
+
sample_tokens = sample_tokens[:MAX_SEQ_LEN]
|
| 707 |
+
sample_labels = sample_labels[:MAX_SEQ_LEN]
|
| 708 |
+
sample_tokens[-1] = EOT_ID
|
| 709 |
+
sample_labels[-1] = EOT_ID
|
| 710 |
+
|
| 711 |
+
is_val = (i < val_split_count)
|
| 712 |
+
if is_val:
|
| 713 |
+
val_tokens.extend(sample_tokens)
|
| 714 |
+
val_labels.extend(sample_labels)
|
| 715 |
+
val_offsets.append(len(val_tokens))
|
| 716 |
+
val_domains.append(did)
|
| 717 |
+
else:
|
| 718 |
+
train_tokens.extend(sample_tokens)
|
| 719 |
+
train_labels.extend(sample_labels)
|
| 720 |
+
train_offsets.append(len(train_tokens))
|
| 721 |
+
train_domains.append(did)
|
| 722 |
+
|
| 723 |
+
if (i + 1) % 50000 == 0 or (i + 1) == total_samples:
|
| 724 |
+
print(f" • Tokenized {i + 1:,} / {total_samples:,} samples...")
|
| 725 |
+
|
| 726 |
+
# Export NumPy Arrays
|
| 727 |
+
print("\n📦 Writing memory-mapped NumPy shards...")
|
| 728 |
+
np.save(os.path.join(TOKENIZED_DIR, "train_tokens.npy"), np.array(train_tokens, dtype=np.int32))
|
| 729 |
+
np.save(os.path.join(TOKENIZED_DIR, "train_labels.npy"), np.array(train_labels, dtype=np.int32))
|
| 730 |
+
np.save(os.path.join(TOKENIZED_DIR, "train_offsets.npy"), np.array(train_offsets, dtype=np.int64))
|
| 731 |
+
np.save(os.path.join(TOKENIZED_DIR, "train_domains.npy"), np.array(train_domains, dtype=np.uint8))
|
| 732 |
+
|
| 733 |
+
np.save(os.path.join(TOKENIZED_DIR, "val_tokens.npy"), np.array(val_tokens, dtype=np.int32))
|
| 734 |
+
np.save(os.path.join(TOKENIZED_DIR, "val_labels.npy"), np.array(val_labels, dtype=np.int32))
|
| 735 |
+
np.save(os.path.join(TOKENIZED_DIR, "val_offsets.npy"), np.array(val_offsets, dtype=np.int64))
|
| 736 |
+
np.save(os.path.join(TOKENIZED_DIR, "val_domains.npy"), np.array(val_domains, dtype=np.uint8))
|
| 737 |
+
|
| 738 |
+
meta_payload = {
|
| 739 |
+
"version": "v2",
|
| 740 |
+
"model_name": "Sarus",
|
| 741 |
+
"developer": "ViuAI",
|
| 742 |
+
"dataset_type": "pure_chat",
|
| 743 |
+
"total_samples": total_samples,
|
| 744 |
+
"train_samples": len(train_offsets) - 1,
|
| 745 |
+
"val_samples": len(val_offsets) - 1,
|
| 746 |
+
"train_tokens_total": len(train_tokens),
|
| 747 |
+
"val_tokens_total": len(val_tokens),
|
| 748 |
+
"max_seq_len": MAX_SEQ_LEN,
|
| 749 |
+
"domain_names": DOMAIN_NAMES,
|
| 750 |
+
"domain_sample_counts": domain_counts
|
| 751 |
+
}
|
| 752 |
+
|
| 753 |
+
with open(os.path.join(TOKENIZED_DIR, "metadata.json"), "w", encoding="utf-8") as f:
|
| 754 |
+
json.dump(meta_payload, f, indent=2, ensure_ascii=False)
|
| 755 |
+
|
| 756 |
+
print(f"✅ Saved metadata.json with {len(DOMAIN_NAMES)} domains and stats!")
|
| 757 |
+
|
| 758 |
+
if upload_hf:
|
| 759 |
+
print(f"\n🌐 Uploading tokenized shards to Hugging Face ({TOKENIZED_REPO_ID}/sft_v2)...")
|
| 760 |
+
api = HfApi(token=HF_TOKEN)
|
| 761 |
+
try:
|
| 762 |
+
api.upload_folder(
|
| 763 |
+
folder_path=TOKENIZED_DIR,
|
| 764 |
+
repo_id=TOKENIZED_REPO_ID,
|
| 765 |
+
path_in_repo="sft_v2",
|
| 766 |
+
repo_type="dataset",
|
| 767 |
+
commit_message=f"Upload Sarus SFT v2 Pure Chat Tokenized Shards ({total_samples:,} samples)"
|
| 768 |
+
)
|
| 769 |
+
print("✅ Successfully uploaded tokenized dataset to Hugging Face!")
|
| 770 |
+
except Exception as e:
|
| 771 |
+
print(f"⚠️ Upload warning: {e}")
|
| 772 |
+
|
| 773 |
+
print("\n" + "=" * 85)
|
| 774 |
+
print("🎉 [PHASE 2 COMPLETE] TOKENIZED SHARDS READY FOR HIGH-SPEED TRAINING!")
|
| 775 |
+
print(f" • Train Tokens: {len(train_tokens):,}")
|
| 776 |
+
print(f" • Val Tokens: {len(val_tokens):,}")
|
| 777 |
+
print(f" • Shard Directory: {TOKENIZED_DIR}")
|
| 778 |
+
print("=" * 85)
|
| 779 |
+
|
| 780 |
+
# ------------------------------------------------------------------------------
|
| 781 |
+
# Main CLI
|
| 782 |
+
# ------------------------------------------------------------------------------
|
| 783 |
+
if __name__ == "__main__":
|
| 784 |
+
parser = argparse.ArgumentParser(description="ViuAI Sarus-500M SFT v2 Pure Chat Dataset Builder")
|
| 785 |
+
parser.add_argument("--phase", type=str, default="all", choices=["raw", "tokenize", "all"], help="Execution phase")
|
| 786 |
+
parser.add_argument("--upload_hf", action="store_true", default=False, help="Upload outputs to Hugging Face")
|
| 787 |
+
args = parser.parse_args()
|
| 788 |
+
|
| 789 |
+
if args.phase in ["raw", "all"]:
|
| 790 |
+
samples = run_phase_raw(upload_hf=args.upload_hf)
|
| 791 |
+
if args.phase == "all":
|
| 792 |
+
run_phase_tokenize(all_samples=samples, upload_hf=args.upload_hf)
|
| 793 |
+
elif args.phase == "tokenize":
|
| 794 |
+
run_phase_tokenize(all_samples=None, upload_hf=args.upload_hf)
|