Upload code/build_sft_v19_dataset.py with huggingface_hub
Browse files434 unmodified lines43543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948034 unmodified lines5155165175185195205215225235245255265275285295305315325335342 unmodified lines5375385395405415425435445453 unmodified lines5495505515525535545555565575585592 unmodified lines5625635645655665675685694 unmodified lines574575576577578579580581582583584131 unmodified lines7167177187197207217227237247257267277287297307317327337347357367377387397407417426 unmodified lines749750751752753754755756757758759760761762763216 unmodified lines9809819829839849859869873 unmodified lines991992993994995996997998999100010011002434 unmodified linesif len(samples) >= target_count:break# If more needed, supplement from Open-Thoughts (general reasoning)if len(samples) < target_count:try:ds = load_dataset("open-thoughts/Open-Thoughts-114k", split="train", streaming=True)for row in ds:convs = row.get("conversations", [])if len(convs) < 2:continueuser_msg, asst_msg = "", ""for msg in convs:if msg.get("from") == "user" or msg.get("role") == "user":user_msg = (msg.get("value") or msg.get("content") or "").strip()elif msg.get("from") == "assistant" or msg.get("role") == "assistant":asst_msg = (msg.get("value") or msg.get("content") or "").strip()if not user_msg or not asst_msg or is_duplicate(user_msg):continuecode_signals = ["function", "code", "program", "python", "java", "sql", "html", "css"]if any(sig in user_msg.lower() for sig in code_signals):continuethought, response = "", asst_msgif "<|begin_of_thought|>" in asst_msg and "<|end_of_thought|>" in asst_msg:thought_part = asst_msg.split("<|end_of_thought|>")[0].replace("<|begin_of_thought|>", "").strip()response_part = asst_msg.split("<|end_of_thought|>")[-1].strip()if "<|begin_of_solution|>" in response_part:response_part = response_part.replace("<|begin_of_solution|>", "").replace("<|end_of_solution|>", "").strip()if len(thought_part) > 40 and len(thought_part) < 2500 and len(response_part) > 10:thought, response = thought_part, response_partelif "<think>" in asst_msg and "</think>" in asst_msg:parts = asst_msg.split("</think>")th, resp = parts[0].replace("<think>", "").strip(), parts[1].strip()if len(th) > 40 and len(resp) > 10:thought, response = th, respif thought:s = create_training_sample(user_msg, thought, response, domain_id=3)if s:samples.append(s)if len(samples) >= target_count:breakexcept Exception as e:print(f" ⚠️ Open-Thoughts supplement error: {e}")print(f" ✅ [Reasoning] Ingested {len(samples):,} samples — ALL with organic thinking.")return samples34 unmodified linesdef build_domain_career_productivity(target_count=5000) -> list:print(f"\n[Domain 5] Ingesting Career & Workplace — Strict Corporate Filter (Target: {target_count:,})...")samples = []# Precision career keywords — multi-word phrases to avoid false positivescareer_keywords = ["write an email", "draft an email", "compose an email", "formal email","resume", "cover letter", "leave application", "meeting agenda","business proposal", "job interview", "job application", "team lead","client meeting", "performance appraisal", "workplace conflict","presentation deck", "performance review", "resignation letter","office memo", "employee onboarding", "sprint planning", "daily standup","quarterly review", "okr", "kpi", "stakeholder management","board meeting", "hr policy", "salary negotiation", "promotion request","project plan", "business strategy", "marketing plan", "investor pitch","annual report", "company policy", "corporate training", "team building","work-life balance", "career growth", "professional development","linkedin", "freelancing", "remote work", "internship", "hiring"]blocklist = ["water", "color", "planet", "dinosaur", "physics", "atom", "novel",2 unmodified lines"animal", "plant", "weather", "solar", "galaxy", "ocean", "mountain","religion", "mythology", "fairy tale", "homework", "theory of","translate", "calculate", "equation", "photosynthesis", "continent","programming", "python", "javascript", "html", "algorithm", "code","medical", "disease", "symptom", "diagnosis", "medicine","historical", "war", "kingdom", "dynasty", "emperor", "ancient"]# 1. Alpaca Cleanedtry:3 unmodified linesinp = row.get("input", "").strip()out = row.get("output", "").strip()full_p = f"{inst}\n{inp}".strip() if inp else instif not full_p or not out or is_duplicate(full_p):continueif any(bad in full_p.lower() for bad in blocklist):continueif any(k in full_p.lower() for k in career_keywords):s = create_training_sample(full_p, "", out, domain_id=5)if s:samples.append(s)2 unmodified linesexcept Exception as e:print(f" ⚠️ Error Career Alpaca: {e}")# 2. Supplement from smoltalk everyday-conversations & openhermes-100kfor cfg in ["everyday-conversations", "openhermes-100k"]:if len(samples) >= target_count:breaktry:4 unmodified linescontinueuser_msg = messages[0].get("content", "").strip()asst_msg = messages[1].get("content", "").strip()if not user_msg or not asst_msg or is_duplicate(user_msg):continueif any(bad in user_msg.lower() for bad in blocklist):continueif any(k in user_msg.lower() for k in career_keywords):s = create_training_sample(user_msg, "", asst_msg, domain_id=5)if s:samples.append(s)131 unmodified linesdef build_domain_gk_polity(target_count=10000) -> list:print(f"\n[Domain 9] Ingesting GK, Polity & Indian History (Target: {target_count:,})...")samples = []# Explicit GK/Polity/History whitelist — ONLY rows matching these are takengk_whitelist = [# Hindi GK keywords"संविधान", "भारत", "इतिहास", "राजनीति", "अनुच्छेद", "लोकसभा", "राज्यसभा","प्रधानमंत्री", "राष्ट्रपति", "सुप्रीम कोर्ट", "उच्च न्यायालय", "मौलिक अधिकार","नीति आयोग", "योजना आयोग", "चुनाव आयोग", "संसद", "विधानसभा", "राज्य��ाल","स्वतंत्रता", "आजादी", "गांधी", "नेहरू", "अंबेडकर", "भगत सिंह","मुगल", "मराठा", "ब्रिटिश", "राजवंश", "साम्राज्य", "क्रांति","भूगोल", "जनगणना", "राजधानी", "नदी", "पर्वत", "जलवायु","विज्ञान", "अंतरिक्ष", "इसरो", "परमाणु", "ऊर्जा",# English GK keywords"constitution", "polity", "amendment", "parliament", "lok sabha", "rajya sabha","supreme court", "high court", "fundamental rights", "directive principles","indian history", "freedom struggle", "independence", "british rule", "mughal","geography", "capital of", "river", "mountain", "census","isro", "space", "nuclear", "election commission", "prime minister","president of india", "governor", "chief minister"]# Creative content blocklistcreative_blocklist = ["कविता", "कहानी", "हाइकू", "नारा", "सपना", "चुटकुला", "fairy tale", "poem"]try:ds = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train", streaming=True)6 unmodified linesu = row.get("instruction", "").strip()a = row.get("output", "").strip()if not u or not a or is_duplicate(u):continue# Block creative contentif any(bad in u.lower() for bad in creative_blocklist):continue# MUST match at least one GK keyword to be includedif not any(kw in u for kw in gk_whitelist) and not any(kw in u.lower() for kw in gk_whitelist):continues = create_training_sample(u, "", a, domain_id=9)216 unmodified linestry:ds = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train", streaming=True)gk_keywords = ["संविधान", "भारत", "इतिहास", "राजनीति", "अनुच्छेद", "लोकसभा","राज्यसभा", "प्रधानमंत्री", "constitution", "polity", "amendment"]for row in ds:convs = row.get("conversations", [])if len(convs) >= 2:3 unmodified linesu = row.get("instruction", "").strip()a = row.get("output", "").strip()if not u or not a or is_duplicate(u):continueif any(kw in u for kw in gk_keywords):continueif any(bad in u.lower() for bad in ["कविता", "कहानी", "हाइकू", "नारा"]):continues = create_training_sample(u, "", a, domain_id=12)434 unmodified lines43543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147234 unmodified lines5075085095105115125135145155165175185195202 unmodified lines5235245255265275285293 unmodified lines5335345355365375385395405415425435445452 unmodified lines5485495505515525535545554 unmodified lines560561562563564565566567568569570571572131 unmodified lines7047057067077087097106 unmodified lines717718719720721722723724725726727728729730216 unmodified lines9479489499509519523 unmodified lines956957958959960961962963964965966967968434 unmodified linesif len(samples) >= target_count:break# If more needed, supplement from smol-magpie-ultra and Open-Thoughts (real reasoning)if len(samples) < target_count:for cfg in ["smol-magpie-ultra", "openhermes-100k"]:if len(samples) >= target_count:breaktry:ds_r = load_dataset("HuggingFaceTB/smoltalk", cfg, split="train", streaming=True)for row in ds_r:messages = row.get("messages", [])if len(messages) < 2:continueu = messages[0].get("content", "").strip()a = messages[1].get("content", "").strip()if not u or not a or is_duplicate(u):continue# Check for deep reasoning / analytical promptsreasoning_signals = ["why", "how", "explain the reason", "compare", "contrast","analyze", "deduce", "evaluate", "what caused", "logic","pros and cons", "step by step", "implications"]if any(sig in u.lower() for sig in reasoning_signals):thought, response = "", aif "<think>" in a and "</think>" in a:parts = a.split("</think>")thought = parts[0].replace("<think>", "").strip()response = parts[1].strip()s = create_training_sample(u, thought, response, domain_id=3)if s:samples.append(s)if len(samples) >= target_count:breakexcept Exception as e:print(f" ⚠️ Error reasoning supplement ({cfg}): {e}")print(f" ✅ [Reasoning] Ingested {len(samples):,} samples — ALL with organic thinking.")return samples34 unmodified linesdef build_domain_career_productivity(target_count=5000) -> list:print(f"\n[Domain 5] Ingesting Career & Workplace — Strict Corporate Filter (Target: {target_count:,})...")samples = []career_keywords = ["email", "resume", "cover letter", "leave application", "meeting","proposal", "interview", "job", "career", "manager", "team","client", "workplace", "presentation", "performance", "resignation","memo", "onboarding", "sprint", "standup", "okr", "kpi","stakeholder", "hr policy", "salary", "promotion", "project plan","strategy", "task", "deadline", "professional", "feedback","colleague", "workload", "leadership", "negotiation"]blocklist = ["water", "color", "planet", "dinosaur", "physics", "atom", "novel",2 unmodified lines"animal", "plant", "weather", "solar", "galaxy", "ocean", "mountain","religion", "mythology", "fairy tale", "homework", "theory of","translate", "calculate", "equation", "photosynthesis", "continent","medical", "disease", "symptom", "diagnosis", "medicine"]# 1. Alpaca Cleanedtry:3 unmodified linesinp = row.get("input", "").strip()out = row.get("output", "").strip()full_p = f"{inst}\n{inp}".strip() if inp else instif not full_p or not out:continueif any(bad in full_p.lower() for bad in blocklist):continueif any(k in full_p.lower() for k in career_keywords):if is_duplicate(full_p):continues = create_training_sample(full_p, "", out, domain_id=5)if s:samples.append(s)2 unmodified linesexcept Exception as e:print(f" ⚠️ Error Career Alpaca: {e}")# 2. Supplement from smoltalk configsfor cfg in ["smol-magpie-ultra", "openhermes-100k", "everyday-conversations", "smol-rewrite", "smol-summarize"]:if len(samples) >= target_count:breaktry:4 unmodified linescontinueuser_msg = messages[0].get("content", "").strip()asst_msg = messages[1].get("content", "").strip()if not user_msg or not asst_msg:continueif any(bad in user_msg.lower() for bad in blocklist):continueif any(k in user_msg.lower() for k in career_keywords):if is_duplicate(user_msg):continues = create_training_sample(user_msg, "", asst_msg, domain_id=5)if s:samples.append(s)131 unmodified linesdef build_domain_gk_polity(target_count=10000) -> list:print(f"\n[Domain 9] Ingesting GK, Polity & Indian History (Target: {target_count:,})...")samples = []creative_blocklist = ["कविता", "कहानी", "हाइकू", "नारा", "सपना", "चुटकुला", "fairy tale", "poem", "शायरी"]try:ds = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train", streaming=True)6 unmodified linesu = row.get("instruction", "").strip()a = row.get("output", "").strip()if not u or not a or len(u) < 10 or len(a) < 15:continue# Filter out creative contentif any(bad in u.lower() for bad in creative_blocklist):continueif is_duplicate(u):continues = create_training_sample(u, "", a, domain_id=9)216 unmodified linestry:ds = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train", streaming=True)for row in ds:convs = row.get("conversations", [])if len(convs) >= 2:3 unmodified linesu = row.get("instruction", "").strip()a = row.get("output", "").strip()if not u or not a or len(u) < 8 or len(a) < 15:continueif any(bad in u.lower() for bad in ["कविता", "कहानी", "हाइकू", "नारा", "सपना", "शायरी"]):continueif is_duplicate(u):continues = create_training_sample(u, "", a, domain_id=12)
code/build_sft_v19_dataset.py+55-89