File size: 5,399 Bytes
9fca47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import re
import json
import random

SCRAPED_DIR = r"c:\Risu Solutions\ByteAstra\backend\data\ayurveda\scraped"
OUTPUT_FILE = r"c:\Risu Solutions\ByteAstra\backend\data\finetune\ayurveda_train_real.jsonl"

def parse_md_file(filepath):
    """
    Parses a markdown file to extract frontmatter and body.
    """
    with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
        content = f.read()

    # Match frontmatter
    fm_match = re.match(r"^---\s*\n(.*?)\n---\s*\n", content, re.DOTALL)
    if not fm_match:
        return None, content

    fm_text = fm_match.group(1)
    body = content[fm_match.end():]

    # Parse YAML-like fields
    metadata = {}
    for line in fm_text.split("\n"):
        if ":" in line:
            k, v = line.split(":", 1)
            metadata[k.strip().lower()] = v.strip()

    return metadata, body

def chunk_text(text, min_words=100, max_words=300):
    """
    Splits text by double newlines, merging short paragraphs to form stable chunks.
    """
    paragraphs = [p.strip() for p in text.split("\n\n") if p.strip()]
    chunks = []
    current_chunk = []
    current_word_count = 0

    for p in paragraphs:
        word_count = len(p.split())
        if current_word_count + word_count <= max_words:
            current_chunk.append(p)
            current_word_count += word_count
        else:
            if current_chunk and current_word_count >= min_words:
                chunks.append("\n\n".join(current_chunk))
                current_chunk = [p]
                current_word_count = word_count
            elif not current_chunk:
                # Paragraph itself is larger than max_words, add it directly
                chunks.append(p)
            else:
                chunks.append("\n\n".join(current_chunk))
                current_chunk = [p]
                current_word_count = word_count

    if current_chunk:
        chunks.append("\n\n".join(current_chunk))

    return chunks

def extract_topic(chunk):
    """
    Extracts a topic name from the chunk (first heading or bolded word, or first few words).
    """
    lines = chunk.split("\n")
    # Check for short heading-like lines
    for line in lines[:3]:
        line_clean = line.strip()
        if line_clean and len(line_clean) < 60 and not line_clean.startswith("-") and not re.match(r"^\d", line_clean):
            return line_clean.replace("**", "").replace("##", "").strip()
            
    # Check for bolded word
    bold_match = re.search(r"\*\*(.*?)\*\*", chunk)
    if bold_match:
        return bold_match.group(1)

    # Fallback to first 5 words
    words = chunk.split()
    if len(words) > 5:
        return " ".join(words[:5]) + "..."
    return "Ayurvedic Concepts"

def build_dataset():
    random.seed(42)
    entries = []

    print("Scanning scraped Ayurveda files...")
    for root, dirs, files in os.walk(SCRAPED_DIR):
        for file in files:
            if file.endswith(".md"):
                filepath = os.path.join(root, file)
                metadata, body = parse_md_file(filepath)
                if not metadata:
                    continue

                source = metadata.get("source", "Classical Ayurveda Texts")
                section = metadata.get("section", "General Chapters")
                chapter = metadata.get("chapter", "Core Syllabus")

                chunks = chunk_text(body)
                for chunk in chunks:
                    word_count = len(chunk.split())
                    if word_count < 40:
                        continue # Skip extremely short items/noise
                        
                    topic = extract_topic(chunk)
                    
                    # Generate structural queries
                    queries = [
                        f"Show me the verbatim text from {source}, {section}, {chapter} concerning '{topic}'.",
                        f"Provide the classical verses/text describing '{topic}' in {source} ({chapter}, {section}).",
                        f"What does {source} say in {chapter} ({section}) about '{topic}'?",
                        f"Give the textual reference for '{topic}' from {source}, {section}, {chapter}.",
                        f"Explain the topic of '{topic}' as documented in {source} ({section}, {chapter})."
                    ]
                    query = random.choice(queries)

                    system_prompt = "You are ByteAstra, a precise BAMS tutor. Answer questions using only the provided classical textbooks context. Quote the textbook exactly when requested."
                    
                    entry = {
                        "conversations": [
                            {"role": "system", "content": system_prompt},
                            {"role": "user", "content": query},
                            {"role": "assistant", "content": chunk}
                        ]
                    }
                    entries.append(entry)

    # Shuffle the dataset
    random.shuffle(entries)
    
    # Save JSONL
    os.makedirs(os.path.dirname(OUTPUT_FILE), exist_ok=True)
    with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
        for entry in entries:
            f.write(json.dumps(entry, ensure_ascii=False) + "\n")

    print(f"Dataset compiled successfully! Generated {len(entries)} training samples in {OUTPUT_FILE}.")

if __name__ == "__main__":
    build_dataset()