Upload code/build_sft_basic_chatbot.py with huggingface_hub
Browse files- code/build_sft_basic_chatbot.py +44 -36
code/build_sft_basic_chatbot.py
CHANGED
|
@@ -570,6 +570,24 @@ The old man was deeply moved by the boy's compassion. Realizing that even small
|
|
| 570 |
return anchors
|
| 571 |
|
| 572 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
# ==============================================================================
|
| 574 |
# Section 5: Master Dataset Assembly (~15,000 Samples)
|
| 575 |
# ==============================================================================
|
|
@@ -577,8 +595,8 @@ def build_master_dataset():
|
|
| 577 |
from datasets import load_dataset
|
| 578 |
master_pool = []
|
| 579 |
|
| 580 |
-
# 1. Gold Foundation Anchors (Replicated 35x = ~1,
|
| 581 |
-
print("\n📦 Step 1/
|
| 582 |
gold_anchors = get_gold_anchors()
|
| 583 |
for prompt, resp, did in gold_anchors:
|
| 584 |
for _ in range(35):
|
|
@@ -589,7 +607,7 @@ def build_master_dataset():
|
|
| 589 |
print(f" ✅ Added {len(master_pool)} Gold Foundation Anchor samples.")
|
| 590 |
|
| 591 |
# 2. Multi-Turn Conversations from SODA (Target: 3,000 multi-turn dialogues)
|
| 592 |
-
print("\n📦 Step 2/
|
| 593 |
try:
|
| 594 |
ds_soda = load_dataset("allenai/soda", split="train", streaming=True)
|
| 595 |
count = 0
|
|
@@ -612,7 +630,7 @@ def build_master_dataset():
|
|
| 612 |
print(f" ⚠️ Multi-turn load error: {e}")
|
| 613 |
|
| 614 |
# 3. Single-Turn Chitchat from SODA with Typo injection (Target: 2,500 clean chats)
|
| 615 |
-
print("\n📦 Step 3/
|
| 616 |
try:
|
| 617 |
ds_soda_single = load_dataset("allenai/soda", split="train", streaming=True)
|
| 618 |
count = 0
|
|
@@ -638,7 +656,7 @@ def build_master_dataset():
|
|
| 638 |
print(f" ⚠️ Chitchat load error: {e}")
|
| 639 |
|
| 640 |
# 4. Clean Daily Math from GSM8K (Target: 2,200 clean step arithmetic)
|
| 641 |
-
print("\n📦 Step 4/
|
| 642 |
try:
|
| 643 |
ds_math = load_dataset("openai/gsm8k", "main", split="train")
|
| 644 |
count = 0
|
|
@@ -666,17 +684,16 @@ def build_master_dataset():
|
|
| 666 |
except Exception as e:
|
| 667 |
print(f" ⚠️ Math load error: {e}")
|
| 668 |
|
| 669 |
-
# 5. Factual Indian GK & Science from Alpaca-Hindi (Target: 2,
|
| 670 |
-
print("\n📦 Step 5/
|
| 671 |
try:
|
| 672 |
ds_hi = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train")
|
| 673 |
count = 0
|
| 674 |
-
GK_CREATIVE_BLOCK = {"कविता", "हाइकू", "नारा", "शायरी", "गीत", "दोहा"}
|
| 675 |
for row in ds_hi:
|
| 676 |
-
if count >=
|
| 677 |
break
|
| 678 |
-
inst =
|
| 679 |
-
resp = row.get("output", "").strip()
|
| 680 |
if not inst or not resp or len(inst) < 10 or len(resp) < 15:
|
| 681 |
continue
|
| 682 |
if any(kw in inst for kw in GK_CREATIVE_BLOCK):
|
|
@@ -691,18 +708,17 @@ def build_master_dataset():
|
|
| 691 |
except Exception as e:
|
| 692 |
print(f" ⚠️ Hindi GK load error: {e}")
|
| 693 |
|
| 694 |
-
# 6. Hindi Stories & Moral Tales
|
| 695 |
-
print("\n📦 Step 6/
|
| 696 |
try:
|
| 697 |
ds_hi_story = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train")
|
| 698 |
count = 0
|
| 699 |
-
STORY_KEYWORDS =
|
| 700 |
for row in ds_hi_story:
|
| 701 |
-
if count >=
|
| 702 |
break
|
| 703 |
-
inst =
|
| 704 |
-
resp
|
| 705 |
-
if not inst or not resp or len(resp) < 100:
|
| 706 |
continue
|
| 707 |
if any(kw in inst for kw in STORY_KEYWORDS):
|
| 708 |
if is_duplicate(inst):
|
|
@@ -715,21 +731,17 @@ def build_master_dataset():
|
|
| 715 |
except Exception as e:
|
| 716 |
print(f" ⚠️ Hindi Story load error: {e}")
|
| 717 |
|
| 718 |
-
# 7. English Stories & Creative Narratives (Target: 1,
|
| 719 |
-
print("\n📦 Step 7/
|
| 720 |
try:
|
| 721 |
ds_en_story = load_dataset("yahma/alpaca-cleaned", split="train")
|
| 722 |
count = 0
|
| 723 |
-
EN_STORY_KEYWORDS =
|
| 724 |
for row in ds_en_story:
|
| 725 |
-
if count >=
|
| 726 |
break
|
| 727 |
-
inst =
|
| 728 |
-
|
| 729 |
-
resp = row.get("output", "").strip()
|
| 730 |
-
if inp:
|
| 731 |
-
inst = f"{inst}\n\n{inp}"
|
| 732 |
-
if not inst or not resp or len(resp) < 100:
|
| 733 |
continue
|
| 734 |
lower_inst = inst.lower()
|
| 735 |
if any(kw in lower_inst for kw in EN_STORY_KEYWORDS):
|
|
@@ -743,19 +755,15 @@ def build_master_dataset():
|
|
| 743 |
except Exception as e:
|
| 744 |
print(f" ⚠️ English Story load error: {e}")
|
| 745 |
|
| 746 |
-
# 8. Clean Workplace & Daily Tasks (Target: 1,
|
| 747 |
-
print("\n📦 Step 8/
|
| 748 |
try:
|
| 749 |
ds_alp = load_dataset("yahma/alpaca-cleaned", split="train")
|
| 750 |
count = 0
|
| 751 |
for row in ds_alp:
|
| 752 |
-
if count >=
|
| 753 |
break
|
| 754 |
-
inst =
|
| 755 |
-
inp = row.get("input", "").strip()
|
| 756 |
-
resp = row.get("output", "").strip()
|
| 757 |
-
if inp:
|
| 758 |
-
inst = f"{inst}\n\n{inp}"
|
| 759 |
if not inst or not resp or len(resp) < 20:
|
| 760 |
continue
|
| 761 |
if is_duplicate(inst):
|
|
|
|
| 570 |
return anchors
|
| 571 |
|
| 572 |
|
| 573 |
+
def extract_qa(row):
|
| 574 |
+
convs = row.get("conversations", [])
|
| 575 |
+
if isinstance(convs, list) and len(convs) >= 2:
|
| 576 |
+
u = convs[0].get("value") or convs[0].get("content") or ""
|
| 577 |
+
a = convs[1].get("value") or convs[1].get("content") or ""
|
| 578 |
+
return u.strip(), a.strip()
|
| 579 |
+
msgs = row.get("messages", [])
|
| 580 |
+
if isinstance(msgs, list) and len(msgs) >= 2:
|
| 581 |
+
u = msgs[0].get("content") or msgs[0].get("value") or ""
|
| 582 |
+
a = msgs[1].get("content") or msgs[1].get("value") or ""
|
| 583 |
+
return u.strip(), a.strip()
|
| 584 |
+
inst = row.get("instruction") or row.get("prompt") or row.get("question") or ""
|
| 585 |
+
inp = row.get("input") or ""
|
| 586 |
+
out = row.get("output") or row.get("response") or row.get("answer") or ""
|
| 587 |
+
if inp and inp.strip():
|
| 588 |
+
inst = f"{inst}\n\n{inp}"
|
| 589 |
+
return inst.strip(), out.strip()
|
| 590 |
+
|
| 591 |
# ==============================================================================
|
| 592 |
# Section 5: Master Dataset Assembly (~15,000 Samples)
|
| 593 |
# ==============================================================================
|
|
|
|
| 595 |
from datasets import load_dataset
|
| 596 |
master_pool = []
|
| 597 |
|
| 598 |
+
# 1. Gold Foundation Anchors (Replicated 35x = ~1,800 rock-solid anchor rows)
|
| 599 |
+
print("\n📦 Step 1/9: Loading Gold Foundation Anchors...")
|
| 600 |
gold_anchors = get_gold_anchors()
|
| 601 |
for prompt, resp, did in gold_anchors:
|
| 602 |
for _ in range(35):
|
|
|
|
| 607 |
print(f" ✅ Added {len(master_pool)} Gold Foundation Anchor samples.")
|
| 608 |
|
| 609 |
# 2. Multi-Turn Conversations from SODA (Target: 3,000 multi-turn dialogues)
|
| 610 |
+
print("\n📦 Step 2/9: Loading Multi-Turn Context Conversations...")
|
| 611 |
try:
|
| 612 |
ds_soda = load_dataset("allenai/soda", split="train", streaming=True)
|
| 613 |
count = 0
|
|
|
|
| 630 |
print(f" ⚠️ Multi-turn load error: {e}")
|
| 631 |
|
| 632 |
# 3. Single-Turn Chitchat from SODA with Typo injection (Target: 2,500 clean chats)
|
| 633 |
+
print("\n📦 Step 3/9: Loading Conversational Chitchat (with Typo engine)...")
|
| 634 |
try:
|
| 635 |
ds_soda_single = load_dataset("allenai/soda", split="train", streaming=True)
|
| 636 |
count = 0
|
|
|
|
| 656 |
print(f" ⚠️ Chitchat load error: {e}")
|
| 657 |
|
| 658 |
# 4. Clean Daily Math from GSM8K (Target: 2,200 clean step arithmetic)
|
| 659 |
+
print("\n📦 Step 4/9: Loading Clean GSM8K Everyday Math...")
|
| 660 |
try:
|
| 661 |
ds_math = load_dataset("openai/gsm8k", "main", split="train")
|
| 662 |
count = 0
|
|
|
|
| 684 |
except Exception as e:
|
| 685 |
print(f" ⚠️ Math load error: {e}")
|
| 686 |
|
| 687 |
+
# 5. Factual Indian GK & Science from Alpaca-Hindi (Target: 2,000 samples)
|
| 688 |
+
print("\n📦 Step 5/9: Loading Factual Indian GK & Science...")
|
| 689 |
try:
|
| 690 |
ds_hi = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train")
|
| 691 |
count = 0
|
| 692 |
+
GK_CREATIVE_BLOCK = {"कविता", "हाइकू", "नारा", "शायरी", "गीत", "दोहा", "कहानी", "कथा"}
|
| 693 |
for row in ds_hi:
|
| 694 |
+
if count >= 2000:
|
| 695 |
break
|
| 696 |
+
inst, resp = extract_qa(row)
|
|
|
|
| 697 |
if not inst or not resp or len(inst) < 10 or len(resp) < 15:
|
| 698 |
continue
|
| 699 |
if any(kw in inst for kw in GK_CREATIVE_BLOCK):
|
|
|
|
| 708 |
except Exception as e:
|
| 709 |
print(f" ⚠️ Hindi GK load error: {e}")
|
| 710 |
|
| 711 |
+
# 6. Hindi Stories & Moral Tales (Target: 1,000 clean stories)
|
| 712 |
+
print("\n📦 Step 6/9: Loading Hindi Stories & Moral Tales...")
|
| 713 |
try:
|
| 714 |
ds_hi_story = load_dataset("FreedomIntelligence/alpaca-gpt4-hindi", split="train")
|
| 715 |
count = 0
|
| 716 |
+
STORY_KEYWORDS = ["कहानी", "कथा", "किस्��ा", "शिक्षाप्रद", "पंचतंत्र", "नैतिक"]
|
| 717 |
for row in ds_hi_story:
|
| 718 |
+
if count >= 1000:
|
| 719 |
break
|
| 720 |
+
inst, resp = extract_qa(row)
|
| 721 |
+
if not inst or not resp or len(resp) < 80:
|
|
|
|
| 722 |
continue
|
| 723 |
if any(kw in inst for kw in STORY_KEYWORDS):
|
| 724 |
if is_duplicate(inst):
|
|
|
|
| 731 |
except Exception as e:
|
| 732 |
print(f" ⚠️ Hindi Story load error: {e}")
|
| 733 |
|
| 734 |
+
# 7. English Stories & Creative Narratives (Target: 1,000 clean stories)
|
| 735 |
+
print("\n📦 Step 7/9: Loading English Stories & Creative Narratives...")
|
| 736 |
try:
|
| 737 |
ds_en_story = load_dataset("yahma/alpaca-cleaned", split="train")
|
| 738 |
count = 0
|
| 739 |
+
EN_STORY_KEYWORDS = ["story", "tale", "fable", "narrative", "parable", "bedtime", "legend"]
|
| 740 |
for row in ds_en_story:
|
| 741 |
+
if count >= 1000:
|
| 742 |
break
|
| 743 |
+
inst, resp = extract_qa(row)
|
| 744 |
+
if not inst or not resp or len(resp) < 80:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
continue
|
| 746 |
lower_inst = inst.lower()
|
| 747 |
if any(kw in lower_inst for kw in EN_STORY_KEYWORDS):
|
|
|
|
| 755 |
except Exception as e:
|
| 756 |
print(f" ⚠️ English Story load error: {e}")
|
| 757 |
|
| 758 |
+
# 8. Clean Workplace & Daily Tasks (Target: 1,500 samples)
|
| 759 |
+
print("\n📦 Step 8/9: Loading Practical Everyday Tasks & Emails...")
|
| 760 |
try:
|
| 761 |
ds_alp = load_dataset("yahma/alpaca-cleaned", split="train")
|
| 762 |
count = 0
|
| 763 |
for row in ds_alp:
|
| 764 |
+
if count >= 1500:
|
| 765 |
break
|
| 766 |
+
inst, resp = extract_qa(row)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
if not inst or not resp or len(resp) < 20:
|
| 768 |
continue
|
| 769 |
if is_duplicate(inst):
|