File size: 3,071 Bytes
5b4bf2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efc501a
5b4bf2d
efc501a
 
 
 
 
 
5b4bf2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efc501a
 
 
5b4bf2d
 
 
 
 
 
 
 
 
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
"""
Mobile Export Utility: Export 1,500 Cardiac Q&A Knowledge Base to JSON
======================================================================
Creates a standalone, clean JSON file (cardiac_knowledge_base.json) containing
all 1,500 questions and answers across the 10 cardiology pillars:
1. Medications (with official Medical Disclaimer)
2. Diet and Food
3. Exercise and Walking
4. Sleep and Rest
5. Demographics
6. Body Composition
7. Substances
8. Infections
9. Hydration
10. Genetics

This JSON asset is ready to be bundled directly into:
  - iOS app bundle (Assets / Bundle.main.url(forResource: "cardiac_knowledge_base", withExtension: "json"))
  - Android assets folder (assets/cardiac_knowledge_base.json)
"""

import json
import os
import re

DATASET_MD = "cardiac_health_dataset.md"
OUTPUT_JSON = "cardiac_knowledge_base.json"
EXACT_DISCLAIMER = (
    "⚠️ **Medical Disclaimer:** For educational purposes only, not a prescription or treatment plan. "
    "**Do not start, stop, or change any medication without your doctor’s approval.** "
)


def export_json():
    global DATASET_MD, OUTPUT_JSON
    if not os.path.exists(DATASET_MD):
        alt_path = os.path.join(os.path.dirname(__file__), DATASET_MD)
        if os.path.exists(alt_path):
            DATASET_MD = alt_path
        else:
            print(f"Error: {DATASET_MD} not found.")
            return

    with open(DATASET_MD, "r", encoding="utf-8") as f:
        text = f.read()

    pattern = r"### Question (\d+)\s*\((.*?)\)\s*\n+\*\*Q:\*\*\s*(.*?)\n+\*\*A:\*\*\s*(.*?)(?=\n+---|### Question|\Z)"
    matches = re.findall(pattern, text, re.DOTALL)

    records = []
    categories = set()

    for q_num, category, question, answer in matches:
        q_clean = question.strip()
        a_clean = answer.strip()
        cat_clean = category.strip()
        categories.add(cat_clean)

        is_medication = cat_clean.lower() == "medications" or any(
            kw in q_clean.lower()
            for kw in ["statin", "beta-blocker", "aspirin", "diuretic", "ace inhibitor", "nitrate", "anticoagulant", "antiarrhythmic", "pcsk9", "calcium channel"]
        )

        records.append({
            "id": int(q_num),
            "category": cat_clean,
            "question": q_clean,
            "answer": a_clean,
            "disclaimer_required": is_medication,
            "medical_disclaimer": EXACT_DISCLAIMER if is_medication else None,
        })

    metadata = {
        "dataset_name": "MedGemma-Micro Cardiac Health Dataset",
        "total_pairs": len(records),
        "categories": sorted(list(categories)),
        "disclaimer": EXACT_DISCLAIMER,
        "items": records,
    }

    if os.path.exists(OUTPUT_JSON):
        os.remove(OUTPUT_JSON)

    with open(OUTPUT_JSON, "w", encoding="utf-8") as f:
        json.dump(metadata, f, indent=2, ensure_ascii=False)

    size_kb = os.path.getsize(OUTPUT_JSON) / 1024.0
    print(f"Successfully exported {len(records)} cardiac Q&A pairs to '{OUTPUT_JSON}' ({size_kb:.1f} KB)")


if __name__ == "__main__":
    export_json()