Upload code/build_sft_basic_chatbot.py with huggingface_hub
Browse files569 unmodified lines5705715725735745751 unmodified line5775785795805815825835844 unmodified lines58959059159259359459516 unmodified lines61261361461561661761819 unmodified lines63863964064164264364421 unmodified lines6666676686696706716726736746756766776786796806816828 unmodified lines6916926936946956966976986997007017027037047057067077086 unmodified lines7157167177187197207217227237247257267277287297307317327337347357 unmodified lines743744745746747748749750751752753754755756757758759760761569 unmodified linesreturn anchors# ==============================================================================# Section 5: Master Dataset Assembly (~15,000 Samples)# ==============================================================================1 unmodified linefrom datasets import load_datasetmaster_pool = []# 1. Gold Foundation Anchors (Replicated 35x = ~1,600 rock-solid anchor rows)print("\n📦 Step 1/8: Loading Gold Foundation Anchors...")gold_anchors = get_gold_anchors()for prompt, resp, did in gold_anchors:for _ in range(35):4 unmodified linesprint(f" ✅ Added {len(master_pool)} Gold Foundation Anchor samples.")# 2. Multi-Turn Conversations from SODA (Target: 3,000 multi-turn dialogues)print("\n📦 Step 2/8: Loading Multi-Turn Context Conversations...")try:ds_soda = load_dataset("allenai/soda", split="train", streaming=True)count = 016 unmodified linesprint(f" ⚠️ Multi-turn load error: {e}")# 3. Single-Turn Chitchat from SODA with Typo injection (Target: 2,500 clean chats)print("\n📦 Step 3/8: Loading Conversational Chitchat (with Typo engine)...")try:ds_soda_single = load_dataset("allenai/soda", split="train", streaming=True)count = 019 unmodified linesprint(f" ⚠️ Chitchat load error: {e}")# 4. Clean Daily Math from GSM8K (Target: 2,200 clean step arithmetic)print("\n📦 Step 4/8: Loading Clean GSM8K Everyday Math...")try:ds_math = load_dataset("openai/gsm8k", "main", split="train")count = 021 unmodified linesexcept Exception as e:print(f" ⚠️ Math load error: {e}")# 5. Factual Indian GK & Science from Alpaca-Hindi (Target: 2,200 samples)print("\n📦 Step 5/8: Loading Factual Indian GK & Science...")try:ds_hi = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train")count = 0GK_CREATIVE_BLOCK = {"कविता", "हाइकू", "नारा", "शायरी", "गीत", "दोहा"}for row in ds_hi:if count >= 2200:breakinst = row.get("instruction", "").strip()resp = row.get("output", "").strip()if not inst or not resp or len(inst) < 10 or len(resp) < 15:continueif any(kw in inst for kw in GK_CREATIVE_BLOCK):8 unmodified linesexcept Exception as e:print(f" ⚠️ Hindi GK load error: {e}")# 6. Hindi Stories & Moral Tales from Alpaca-Hindi (Target: 1,200 clean stories)print("\n📦 Step 6/8: Loading Hindi Stories & Moral Tales...")try:ds_hi_story = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train")count = 0STORY_KEYWORDS = {"कहानी", "कथा", "किस्सा", "शिक्षाप्रद", "पंचतंत्र", "नैतिक"}for row in ds_hi_story:if count >= 1200:breakinst = row.get("instruction", "").strip()resp = row.get("output", "").strip()if not inst or not resp or len(resp) < 100:continueif any(kw in inst for kw in STORY_KEYWORDS):if is_duplicate(inst):6 unmodified linesexcept Exception as e:print(f" ⚠️ Hindi Story load error: {e}")# 7. English Stories & Creative Narratives (Target: 1,200 clean stories)print("\n📦 Step 7/8: Loading English Stories & Creative Narratives...")try:ds_en_story = load_dataset("yahma/alpaca-cleaned", split="train")count = 0EN_STORY_KEYWORDS = {"story", "tale", "fable", "narrative", "parable", "bedtime", "legend"}for row in ds_en_story:if count >= 1200:breakinst = row.get("instruction", "").strip()inp = row.get("input", "").strip()resp = row.get("output", "").strip()if inp:inst = f"{inst}\n\n{inp}"if not inst or not resp or len(resp) < 100:continuelower_inst = inst.lower()if any(kw in lower_inst for kw in EN_STORY_KEYWORDS):7 unmodified linesexcept Exception as e:print(f" ⚠️ English Story load error: {e}")# 8. Clean Workplace & Daily Tasks (Target: 1,800 samples)print("\n📦 Step 8/8: Loading Practical Everyday Tasks & Emails...")try:ds_alp = load_dataset("yahma/alpaca-cleaned", split="train")count = 0for row in ds_alp:if count >= 1800:breakinst = row.get("instruction", "").strip()inp = row.get("input", "").strip()resp = row.get("output", "").strip()if inp:inst = f"{inst}\n\n{inp}"if not inst or not resp or len(resp) < 20:continueif is_duplicate(inst):569 unmodified lines5705715725735745755765775785795805815825835845855865875885895905915925931 unmodified line5955965975985996006016024 unmodified lines60760860961061161261316 unmodified lines63063163263363463563619 unmodified lines65665765865966066166221 unmodified lines6846856866876886896906916926936946956966976986998 unmodified lines7087097107117127137147157167177187197207217227237246 unmodified lines7317327337347357367377387397407417427437447457467477 unmodified lines755756757758759760761762763764765766767768769569 unmodified linesreturn anchorsdef extract_qa(row):convs = row.get("conversations", [])if isinstance(convs, list) and len(convs) >= 2:u = convs[0].get("value") or convs[0].get("content") or ""a = convs[1].get("value") or convs[1].get("content") or ""return u.strip(), a.strip()msgs = row.get("messages", [])if isinstance(msgs, list) and len(msgs) >= 2:u = msgs[0].get("content") or msgs[0].get("value") or ""a = msgs[1].get("content") or msgs[1].get("value") or ""return u.strip(), a.strip()inst = row.get("instruction") or row.get("prompt") or row.get("question") or ""inp = row.get("input") or ""out = row.get("output") or row.get("response") or row.get("answer") or ""if inp and inp.strip():inst = f"{inst}\n\n{inp}"return inst.strip(), out.strip()# ==============================================================================# Section 5: Master Dataset Assembly (~15,000 Samples)# ==============================================================================1 unmodified linefrom datasets import load_datasetmaster_pool = []# 1. Gold Foundation Anchors (Replicated 35x = ~1,800 rock-solid anchor rows)print("\n📦 Step 1/9: Loading Gold Foundation Anchors...")gold_anchors = get_gold_anchors()for prompt, resp, did in gold_anchors:for _ in range(35):4 unmodified linesprint(f" ✅ Added {len(master_pool)} Gold Foundation Anchor samples.")# 2. Multi-Turn Conversations from SODA (Target: 3,000 multi-turn dialogues)print("\n📦 Step 2/9: Loading Multi-Turn Context Conversations...")try:ds_soda = load_dataset("allenai/soda", split="train", streaming=True)count = 016 unmodified linesprint(f" ⚠️ Multi-turn load error: {e}")# 3. Single-Turn Chitchat from SODA with Typo injection (Target: 2,500 clean chats)print("\n📦 Step 3/9: Loading Conversational Chitchat (with Typo engine)...")try:ds_soda_single = load_dataset("allenai/soda", split="train", streaming=True)count = 019 unmodified linesprint(f" ⚠️ Chitchat load error: {e}")# 4. Clean Daily Math from GSM8K (Target: 2,200 clean step arithmetic)print("\n📦 Step 4/9: Loading Clean GSM8K Everyday Math...")try:ds_math = load_dataset("openai/gsm8k", "main", split="train")count = 021 unmodified linesexcept Exception as e:print(f" ⚠️ Math load error: {e}")# 5. Factual Indian GK & Science from Alpaca-Hindi (Target: 2,000 samples)print("\n📦 Step 5/9: Loading Factual Indian GK & Science...")try:ds_hi = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train")count = 0GK_CREATIVE_BLOCK = {"कविता", "हाइकू", "नारा", "शायरी", "गीत", "दोहा", "कहानी", "कथा"}for row in ds_hi:if count >= 2000:breakinst, resp = extract_qa(row)if not inst or not resp or len(inst) < 10 or len(resp) < 15:continueif any(kw in inst for kw in GK_CREATIVE_BLOCK):8 unmodified linesexcept Exception as e:print(f" ⚠️ Hindi GK load error: {e}")# 6. Hindi Stories & Moral Tales (Target: 1,000 clean stories)print("\n📦 Step 6/9: Loading Hindi Stories & Moral Tales...")try:ds_hi_story = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train")count = 0STORY_KEYWORDS = ["कहानी", "कथा", "किस्��ा", "शिक्षाप्रद", "पंचतंत्र", "नैतिक"]for row in ds_hi_story:if count >= 1000:breakinst, resp = extract_qa(row)if not inst or not resp or len(resp) < 80:continueif any(kw in inst for kw in STORY_KEYWORDS):if is_duplicate(inst):6 unmodified linesexcept Exception as e:print(f" ⚠️ Hindi Story load error: {e}")# 7. English Stories & Creative Narratives (Target: 1,000 clean stories)print("\n📦 Step 7/9: Loading English Stories & Creative Narratives...")try:ds_en_story = load_dataset("yahma/alpaca-cleaned", split="train")count = 0EN_STORY_KEYWORDS = ["story", "tale", "fable", "narrative", "parable", "bedtime", "legend"]for row in ds_en_story:if count >= 1000:breakinst, resp = extract_qa(row)if not inst or not resp or len(resp) < 80:continuelower_inst = inst.lower()if any(kw in lower_inst for kw in EN_STORY_KEYWORDS):7 unmodified linesexcept Exception as e:print(f" ⚠️ English Story load error: {e}")# 8. Clean Workplace & Daily Tasks (Target: 1,500 samples)print("\n📦 Step 8/9: Loading Practical Everyday Tasks & Emails...")try:ds_alp = load_dataset("yahma/alpaca-cleaned", split="train")count = 0for row in ds_alp:if count >= 1500:breakinst, resp = extract_qa(row)if not inst or not resp or len(resp) < 20:continueif is_duplicate(inst):
code/build_sft_basic_chatbot.py+44-36