File size: 33,060 Bytes
4745c27
 
 
 
 
 
 
adcb5fd
e887f35
80f4ce3
e4e7be6
f8b3f71
b9ca991
bbc1725
4745c27
adcb5fd
1ab4436
 
 
 
3bcd85a
 
2226758
4745c27
2226758
3bcd85a
 
4745c27
3bcd85a
 
2226758
b4da82f
2226758
3bcd85a
b4da82f
1ab4436
 
 
 
 
e4e7be6
 
 
 
b98a0f7
4745c27
e887f35
 
832d762
4745c27
1ab4436
 
 
 
 
 
4745c27
79aae02
4745c27
6bc5f56
79aae02
4745c27
 
6bc5f56
 
 
 
 
 
 
 
 
 
 
 
1ab4436
6069fd3
6bc5f56
1ab4436
6bc5f56
 
79aae02
6bc5f56
1ab4436
4745c27
6bc5f56
1ab4436
4745c27
 
6bc5f56
 
79aae02
6bc5f56
1ab4436
 
bbc1725
 
 
1ab4436
d100b13
1ab4436
 
bbc1725
 
 
 
 
1ab4436
bbc1725
 
 
 
 
1ab4436
4745c27
6bc5f56
 
1ab4436
6bc5f56
 
 
 
 
 
 
 
 
79aae02
6bc5f56
1ab4436
4745c27
6bc5f56
79aae02
4745c27
adcb5fd
e4e7be6
 
 
 
1ab4436
e4e7be6
1ab4436
 
e4e7be6
 
 
1ab4436
e4e7be6
 
 
 
 
 
 
 
 
 
 
adcb5fd
 
e4e7be6
 
 
 
 
 
 
 
 
 
 
adcb5fd
e4e7be6
 
 
4745c27
79aae02
4745c27
 
79aae02
4745c27
 
b736967
4745c27
 
 
 
 
 
 
 
 
b736967
4745c27
adcb5fd
4745c27
 
b736967
79aae02
4745c27
adcb5fd
4745c27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b736967
79aae02
4745c27
 
b736967
79aae02
4745c27
 
00c2c38
 
 
 
 
 
bbc1725
00c2c38
 
 
 
 
 
 
 
 
 
025891c
 
920924a
4745c27
adcb5fd
4745c27
b736967
 
adcb5fd
b736967
 
 
 
 
 
 
 
 
79aae02
4745c27
adcb5fd
4745c27
b736967
79aae02
4745c27
e4e7be6
 
 
 
adcb5fd
e4e7be6
adcb5fd
 
e4e7be6
 
 
adcb5fd
e4e7be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4745c27
adcb5fd
e4e7be6
 
adcb5fd
 
 
e4e7be6
adcb5fd
bbc1725
e4e7be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adcb5fd
e4e7be6
 
4745c27
c736f26
f81db3a
4141c3a
e887f35
 
3932934
4141c3a
e887f35
adcb5fd
e887f35
 
 
 
 
 
 
 
3932934
e887f35
 
 
 
 
 
 
 
 
 
 
adcb5fd
 
3932934
e887f35
 
adcb5fd
e887f35
 
 
 
 
 
adcb5fd
e887f35
 
 
 
 
adcb5fd
e887f35
 
 
 
 
 
79aae02
c736f26
fc6b5b4
 
e887f35
 
3932934
fc6b5b4
e887f35
adcb5fd
e887f35
 
a7555f1
e887f35
 
 
 
 
adcb5fd
e887f35
 
 
adcb5fd
e887f35
 
 
 
 
 
 
adcb5fd
e887f35
 
 
 
 
 
 
 
 
adcb5fd
e887f35
 
 
 
 
adcb5fd
e887f35
 
 
 
 
 
4745c27
 
 
 
 
 
 
a7555f1
e4e7be6
4745c27
 
 
 
 
 
 
a7555f1
e4e7be6
4745c27
 
e887f35
a7555f1
e887f35
 
 
a7555f1
e887f35
a7555f1
 
e887f35
a7555f1
e887f35
 
 
a7555f1
e887f35
a7555f1
4745c27
adcb5fd
 
4745c27
adcb5fd
 
 
4745c27
 
 
 
 
e4e7be6
 
 
 
 
 
adcb5fd
4745c27
 
 
 
 
e4e7be6
 
 
 
4745c27
 
adcb5fd
a7555f1
dc9781b
e887f35
 
bfa3b45
cfccfda
dc9781b
e887f35
 
a7555f1
 
dc9781b
e887f35
bfa3b45
 
cfccfda
dc9781b
e887f35
 
4745c27
 
adcb5fd
26b71da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41bfda0
adcb5fd
 
41bfda0
adcb5fd
 
 
41bfda0
 
26b71da
41bfda0
 
 
 
 
26b71da
41bfda0
 
 
 
 
 
 
 
26b71da
41bfda0
 
26b71da
41bfda0
 
 
 
 
 
 
26b71da
 
41bfda0
 
26b71da
41bfda0
 
 
 
 
 
26b71da
 
 
 
adcb5fd
26b71da
adcb5fd
26b71da
 
41bfda0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26b71da
 
41bfda0
26b71da
 
 
 
 
41bfda0
 
 
 
 
 
 
 
 
adcb5fd
b8f2e67
 
 
 
 
 
7e97447
b8f2e67
 
 
 
 
 
 
 
 
adcb5fd
 
 
 
 
 
e4e7be6
 
 
 
 
 
79aae02
e4e7be6
 
 
41bfda0
 
 
 
 
263b60c
 
 
41bfda0
263b60c
 
41bfda0
 
263b60c
41bfda0
 
 
 
 
263b60c
41bfda0
e4e7be6
 
41bfda0
 
 
 
 
 
 
e4e7be6
 
 
 
 
adcb5fd
e4e7be6
 
6d08b29
 
 
e4e7be6
79aae02
e4e7be6
 
 
41bfda0
 
 
 
 
6d08b29
 
 
41bfda0
6d08b29
 
41bfda0
 
6d08b29
41bfda0
 
 
 
 
6d08b29
e4e7be6
 
41bfda0
 
 
 
 
 
 
e4e7be6
 
 
 
 
adcb5fd
dc9781b
e887f35
 
 
 
7950f1b
e887f35
b8f2e67
83f2727
531ed6f
b8f2e67
 
 
 
dc9781b
adcb5fd
6e92835
e887f35
 
 
 
 
 
 
531ed6f
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
import os
import re
import json
import joblib
import numpy as np
from crewai import Agent, Task, Crew, LLM
from crewai.tools import tool
from firebaseLoader import pull_audio, pull_lifestyle
from webscraper import *
from typing import Union
import shap
import xgboost as xgb
from xgboost import DMatrix
from math import exp


# ================================================================
# Loading in the prediction models
# ================================================================

# Load audio model & metadata
audio_model = xgb.Booster()
audio_model.load_model('models/modelAudiov8_ML.json')

metadata = joblib.load('models/modelAudiov8_metadata.pk1')
audio_feature_names = metadata['feature_names']
audio_mmse_index = metadata['mmse_index']

# Load lifestyle model & metadata
lifestyle_model = xgb.Booster()
lifestyle_model.load_model('models/modelLifestyleV3_ML.json')

metadata = joblib.load('models/modelLifestyleV3_metadata.pk1')
lifestyle_feature_names = metadata['feature_names']


# ================================================================
# Loading in the SHAP explainers
# ================================================================

# Load model explainers
audio_explainer = shap.TreeExplainer(audio_model)
lifestyle_explainer = shap.TreeExplainer(lifestyle_model)

placeholder_lifestyle_ID = "umWSG13rLSfHqqJlq1WluPzjLB93"

# Load 2 different llms for different purposes.
llm_predict = LLM(model="huggingface/openai/gpt-oss-120b")
llm_news = LLM(model="huggingface/Qwen/Qwen3-4B-Thinking-2507")


# ================================================================
# Defining the tools for the agents to make use of
# ================================================================

# Prediction tool using the audio model
@tool("calculate_dementia_risk_audio")
def calculate_dementia_risk_audio(docID: str) -> Union[str, dict]:
    """
    Calculates dementia risk for a specific patient using audio and transcript data from Firebase.
    Returns a structured dict.
    """
    try:
        # Handle JSON-wrapped input (from CrewAI)
        if docID.strip().startswith('{'):
            try:
                parsed = json.loads(docID)
                if isinstance(parsed, dict):
                    if "Audio" in parsed and isinstance(parsed["Audio"], dict):
                        docID = parsed["Audio"].get("docID", docID)
                    elif "docID" in parsed:
                        docID = parsed["docID"]
            except json.JSONDecodeError:
                pass

        # Pulls data from firebase using the patientID (docID)
        audio_data = pull_audio(docID)

        # Error handling for missing patient entry
        if audio_data is None:
            error_msg = f"Error: Could not retrieve or process audio data for patient '{docID}'"
            return {"status": "error", "message": error_msg}

        # Extract values 
        age = audio_data.get("age")
        gender_val = audio_data.get("gender")
        mmse = audio_data.get("mmse")  # This feature was defined as optional during the audio model training
        combined_features = audio_data.get("full_features")

        if combined_features is None:
            error_msg = f"Error: Feature extraction failed for patient '{docID}'"
            return {"status": "error", "message": error_msg}

        
        # Convert the numpy array to DMatrix for compatability
        audio_array = np.array(combined_features).reshape(1, -1)
        dmat = DMatrix(audio_array)

        # Predict using the patient data, returns a 1 or 0
        prediction = audio_model.predict(dmat)[0]

        # Getting the confidence (probability) of the prediction
        try:
            # If model supports predict_proba (e.g., scikit-learn wrapper), use it
            proba = audio_model.predict_proba(dmat)[0]
            confidence = float(max(proba))
        except AttributeError:
            # Fallback, use raw prediction scores (if binary classification)
            raw_score = audio_model.predict(dmat, output_margin=True)[0]
            # Convert to probability via sigmoid (for binary classification)
            proba_pos = 1 / (1 + exp(-raw_score))
            confidence = proba_pos if prediction == 1 else (1 - proba_pos)

        # Setting the risk level
        risk_level = "High" if prediction == 1 else "Low"
        gender_str = "Male" if gender_val == 1 else "Female"

        # Structured output for easy parsing
        result_dict = {
            "risk_level": risk_level,
            "prediction": int(prediction),
            "confidence": confidence,
            "age": age,
            "gender": gender_str,
            "docID": docID
        }

        return {"status": "success", **result_dict}

    # Handles failure to predict
    except Exception as e:
        error_msg = f"Error calculating audio-based risk: {str(e)}"
        return {"status": "error", "message": error_msg}

# SHAP explainer tool for audio prediction results
@tool("explain_dementia_risk_audio")
def explain_dementia_risk_audio(docID: str) -> str:
    """Provides SHAP-based explanation for audio risk prediction."""
    try:
        # Pulls data from firebase using the patientID (docID)
        audio_data = pull_audio(docID)
        
        # Handles missing patient entry
        if audio_data is None:
            return f"Error: No data for {docID}"

        # Get the patients data
        features = audio_data["full_features"]
        feature_names = audio_feature_names

        # SHAP values for this instance
        shap_values = audio_explainer.shap_values(features.reshape(1, -1))
        # shap_values is array of shape (1, n_features) for class 1 (Dementia)

        # Get top 5 positive contributors (pushing toward Dementia = class 1)
        shap_vals = shap_values[0]  # for class 1
        top_indices = np.argsort(-shap_vals)[:5]  # descending by impact

        # list of shap explanations
        explanation_lines = ["Top factors increasing dementia risk (audio analysis):"]
        for i in top_indices:
            feat = feature_names[i]
            val = features[i]
            impact = shap_vals[i]
            if abs(impact) < 1e-4:
                continue
            direction = "↑" if impact > 0 else "↓"
            explanation_lines.append(f"  β€’ {feat} = {val:.3f} β†’ {direction} risk ({impact:+.3f})")

        return "\n".join(explanation_lines)

    # Error handling
    except Exception as e:
        return f"SHAP explanation failed: {str(e)}"

@tool("calculate_dementia_risk_lifestyle")
def calculate_dementia_risk_lifestyle(docID: str) -> Union[str, dict]:
    """
    Calculates dementia risk for a specific patient using lifestyle data.
    Returns a structured dict
    """
    try:
        # Handle JSON-wrapped input (from CrewAI)
        if docID.strip().startswith('{'):
            try:
                parsed = json.loads(docID)
                if isinstance(parsed, dict):
                    if "Lifestyle" in parsed and isinstance(parsed["Lifestyle"], dict):
                        docID = parsed["Lifestyle"].get("docID", docID)
                    elif "docID" in parsed:
                        docID = parsed["docID"]
            except json.JSONDecodeError:
                pass

        # Pulls data from firebase using the patientID (docID)
        lifestyle_data = pull_lifestyle(docID)
        if not lifestyle_data:
            error = f"Error: No lifestyle data found for document ID '{docID}'"
            return {"status": "error", "message": error}

        # feature names in firebase dont match feature names in lifestyle model, so those names need to be changed
        FEATURE_MAPPING = {
            'Age': 'age',
            'Gender': 'gender',
            'Ethnicity': 'ethnicity',
            'EducationLevel': 'education_level',
            'BMI': 'bmi',
            'Smoking': 'smoking',
            'AlcoholConsumption': 'alcohol_consumption',
            'PhysicalActivity': 'physical_activity',
            'DietQuality': 'diet_quality',
            'SleepQuality': 'sleep_quality',
            'FamilyHistoryAlzheimers': 'family_history_dementia',
            'CardiovascularDisease': 'cardiovascular_disease',
            'Diabetes': 'diabetes',
            'Depression': 'depression',
            'HeadInjury': 'head_injury',
            'Hypertension': 'hypertension'
        }

        features = []
        for feat_name in lifestyle_feature_names:
            firestore_key = FEATURE_MAPPING.get(feat_name)
            if firestore_key is None:
                error = f"Error: No mapping defined for feature '{feat_name}'"
                return {"status": "error", "message": error}
            val = lifestyle_data.get(firestore_key)
            if val is None:
                error = f"Error: Missing required lifestyle feature '{feat_name}' (Firestore key: '{firestore_key}')"
                return {"status": "error", "message": error}
            features.append(val)

        # Convert to DMatrix
        life_array = np.array(features).reshape(1, -1)
        dmat = DMatrix(life_array)

        # Predict
        prediction = lifestyle_model.predict(dmat)[0]
        # For confidence
        try:
            # If model supports predict_proba (e.g., scikit-learn wrapper), use it
            proba = lifestyle_model.predict_proba(dmat)[0]
            confidence = float(max(proba))
        except AttributeError:
            # Fallback: use raw prediction scores (if binary classification)
            raw_score = lifestyle_model.predict(dmat, output_margin=True)[0]
            # Convert to probability via sigmoid (for binary classification)
            proba_pos = 1 / (1 + exp(-raw_score))
            confidence = proba_pos if prediction == 1 else (1 - proba_pos)
        
        gender_val = lifestyle_data.get("gender")
        age = lifestyle_data.get("age")

        # Setting the risk level
        risk_level = "High" if prediction == 1 else "Low"
        gender_str = "Male" if gender_val == 1 else "Female"

        # Structured output for easy parsing
        result_dict = {
            "risk_level": risk_level,
            "prediction": int(prediction),
            "confidence": confidence,
            "age": age,
            "gender": gender_str,
            "docID": docID
        }

        return {"status": "success", **result_dict}

    # Handles failure to predict
    except Exception as e:
        error_msg = f"Error calculating risk: {str(e)}"
        return {"status": "error", "message": error_msg}

@tool("explain_dementia_risk_lifestyle")
def explain_dementia_risk_lifestyle(docID: str) -> str:
    """Provides SHAP-based explanation for lifestyle risk prediction."""
    try:
        # Pulls data from firebase using the patientID (docID)
        lifestyle_data = pull_lifestyle(docID)

        # Handles missing patient entry
        if not lifestyle_data:
            return f"Error: No lifestyle data for {docID}"

        # Remapping the feature names to work with lifestyle model
        FEATURE_MAPPING = {
            'Age': 'age', 'Gender': 'gender', 'Ethnicity': 'ethnicity',
            'EducationLevel': 'education_level', 'BMI': 'bmi', 'Smoking': 'smoking',
            'AlcoholConsumption': 'alcohol_consumption', 'PhysicalActivity': 'physical_activity',
            'DietQuality': 'diet_quality', 'SleepQuality': 'sleep_quality',
            'FamilyHistoryAlzheimers': 'family_history_dementia',
            'CardiovascularDisease': 'cardiovascular_disease', 'Diabetes': 'diabetes',
            'Depression': 'depression', 'HeadInjury': 'head_injury', 'Hypertension': 'hypertension'
        }

        features = []
        for feat_name in lifestyle_feature_names:
            val = lifestyle_data.get(FEATURE_MAPPING[feat_name])
            if val is None:
                return f"Missing feature: {feat_name}"
            features.append(val)

        
        X_instance = np.array(features).reshape(1, -1)

        # Get top 5 positive contributors (pushing toward Dementia = class 1)
        shap_vals = lifestyle_explainer.shap_values(X_instance)[0]
        top_indices = np.argsort(-shap_vals)[:5] # descending by impact to prediction result

        # list of shap explanations
        explanation_lines = ["Top factors increasing dementia risk (lifestyle analysis):"]
        for i in top_indices:
            feat = lifestyle_feature_names[i]
            val = features[i]
            impact = shap_vals[i]
            if abs(impact) < 1e-4:
                continue
            # Map categorical to readable (optional but nice)
            readable_val = val
            if feat == "Gender":
                readable_val = "Male" if val == 1 else "Female"
            elif feat in ["Smoking", "AlcoholConsumption", "FamilyHistoryAlzheimers",
                          "CardiovascularDisease", "Diabetes", "Depression", "HeadInjury", "Hypertension"]:
                readable_val = "Yes" if val == 1 else "No"
            elif feat == "EducationLevel":
                edu_map = {1: "Primary", 2: "Secondary", 3: "Bachelor", 4: "Postgrad"}
                readable_val = edu_map.get(val, val)

            direction = "↑" if impact > 0 else "↓"
            explanation_lines.append(f"  β€’ {feat} = {readable_val} β†’ {direction} risk ({impact:+.3f})")

        return "\n".join(explanation_lines)

    # Error handling
    except Exception as e:
        return f"SHAP explanation failed: {str(e)}"

@tool("gather_news_patient")
def gather_news_patient(count: int = 5) -> dict:
    """
    Fetches count recent dementia news items for patients, retrieves full text,
    and generates patient-friendly summaries using the LLM.
    Returns a json object with the format: {"status": "success", "articles": [{"title", "summary", "url"}, ...]}.
    """
    try:
        # gets the article data from webscraper
        raw_articles = get_patient_news_raw(count=count)
        results = []

        for item in raw_articles:
            url = item["url"]
            title = item["title"]
            full_text = scrape_full_text(url)

            # LM summarization happens using llm_news
            if not full_text.strip():
                summary = "Summary unavailable."
            else:
                # Use llm_news β€” ensure it's in scope
                prompt = f"""You are a compassionate health educator.
                Summarize the following article in 2–3 clear, empathetic sentences for patients/families affected by dementia.
                Focus on: What does this mean? What can they do? Hope & support.
                Avoid jargon.

                Article:
                {full_text[:3000]}"""
                
                # Gets the llm_news the create a summary
                response = llm_news.invoke(prompt)
                summary = str(response).strip()


            results.append({
                "title": title,
                "summary": summary,
                "url": url
            })

        # Structured output for easy parsing
        return {
            "status": "success",
            "articles": results
        }

    # Error handling
    except Exception as e:
        return {
            "status": "error",
            "message": str(e),
            "articles": []
        }

@tool("gather_news_doctor")
def gather_news_doctor(count: int = 5) -> dict:
    """
    Fetches count recent dementia news items for doctors, retrieves full text,
    and generates clinical summaries using the LLM.
    Returns a json object with the format: {"status": "success", "articles": [{"title", "summary", "url"}, ...]}.
    """
    try:
        # gets the article data from webscraper
        raw_articles = get_doctor_news_raw(count=count)
        results = []

        for item in raw_articles:
            url = item["url"]
            title = item["title"]
            full_text = scrape_full_text(url)

            # LM summarization happens using llm_news
            if not full_text.strip():
                summary = "Summary unavailable."
            else:
                # Use llm_news β€” ensure it's in scope
                prompt = f"""You are a clinical expert.
                Summarize the following article in 2–3 precise, evidence-based sentences for physicians.
                Include: key findings, methods (if applicable), clinical relevance.

                Article:
                {full_text[:3000]}"""

                # Gets the llm_news the create a summary
                response = llm_news.invoke(prompt)
                summary = str(response).strip()

            results.append({
                "title": title,
                "summary": summary,
                "url": url
            })

        # Structured output for easy parsing
        return {
            "status": "success",
            "articles": results
        }

    # Error handling
    except Exception as e:
        return {
            "status": "error",
            "message": str(e),
            "articles": []
        }

# Define agents for Alzheimer's detection system
risk_calculator_audio = Agent(
    role="Risk Calculator Audio Agent",
    goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
    backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
    verbose=True,
    llm=llm_predict,
    tools=[calculate_dementia_risk_audio, explain_dementia_risk_audio]
)

risk_calculator_lifestyle = Agent(
    role="Risk Calculator Lifestyle Agent",
    goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
    backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
    verbose=True,
    llm=llm_predict,
    tools=[calculate_dementia_risk_lifestyle, explain_dementia_risk_lifestyle]
)

patient_news_agent = Agent(
    role="Patient News Agent",
    goal="Gather and summarize dementia/caregiving news for patients and families in plain, supportive language.",
    backstory="You find real, current articles from trusted sites like caregiving.org, extract full content, and use your medical communication expertise to make it clear and kind for non-experts.",
    tools=[gather_news_patient],
    llm=llm_news,
    verbose=True
)

doctor_news_agent = Agent(
    role="Doctor News Agent",
    goal="Gather and summarize high-impact dementia research for clinicians and scientists.",
    backstory="You retrieve peer-reviewed research (e.g., from Europe PMC), extract key findings, and distill them into clinically actionable summaries for time-pressed professionals.",
    tools=[gather_news_doctor],
    llm=llm_news,
    verbose=True
)


# ================================================================
# Define tasks for agents
# ================================================================

# Audio prediction
risk_assessment_task_audio = Task(
    description=f"Calculate dementia risk for patient",
    expected_output="Risk level, confidence score, and plain-language explanation.",
    agent=risk_calculator_audio,
)
explanation_task_audio = Task(
    description="Explain the dementia risk prediction for patient using SHAP",
    expected_output="Top 5 features increasing risk from audio analysis.",
    agent=risk_calculator_audio
)

# Lifestyle prediction
risk_assessment_task_lifestyle = Task(
    description=f"Calculate dementia risk for patient using the given patient id: {placeholder_lifestyle_ID}",
    expected_output="Risk level, confidence score, and plain-language explanation.",
    agent=risk_calculator_lifestyle,
)
explanation_task_lifestyle = Task(
    description=f"Explain the dementia risk prediction for patient {placeholder_lifestyle_ID} using SHAP",
    expected_output="Top 5 features increasing risk, with impact direction and magnitude.",
    agent=risk_calculator_lifestyle
)

# News gathering
get_news_task_patient = Task(
    description=(
        "Use the `gather_news_patient` tool to fetch {count} recent dementia/caregiving news articles "
        "and return the raw JSON result. Do not generate or modify content."
        "do not add any formatting to the result, such as backticks, only return the raw json dict"
        "do not modify the given urls or titles"
    ),
    expected_output="A JSON object with 'status' and 'articles': list of {title, summary, url}.",
    agent=patient_news_agent
)
get_news_task_doctor = Task(
    description=(
        "Use the `gather_news_doctor` tool to fetch {count} recent dementia research articles "
        "and return the raw JSON result. Do not generate or modify content."
        "do not add any formatting to the result, such as backticks, only return the raw json dict"
        "do not modify the given urls or titles"
    ),
    expected_output="A JSON object with 'status' and 'articles': list of {title, summary, url}.",
    agent=doctor_news_agent
)

# Human-readable mapping for audio features, as the names are not very understandable.
AUDIO_FEATURE_DESCRIPTIONS = {
    # MFCC means (13 coefficients)
    "mfcc_mean_0": "Overall speech energy (loudness stability)",
    "mfcc_mean_1": "Voice pitch stability (fundamental frequency variation)",
    "mfcc_mean_2": "Vocal tract shape β€” openness of mouth/jaw",
    "mfcc_mean_3": "Tongue position β€” front vs. back articulation",
    "mfcc_mean_4": "Nasality (airflow through nose during speech)",
    "mfcc_mean_5": "Spectral tilt β€” balance of high/low frequencies",
    "mfcc_mean_6": "Vocal effort / breathiness",
    "mfcc_mean_7": "Plosive clarity (e.g., 'p', 't', 'k' sounds)",
    "mfcc_mean_8": "Fricative precision (e.g., 's', 'f', 'sh' sounds)",
    "mfcc_mean_9": "Vowel space contraction (reduced vowel distinctness)",
    "mfcc_mean_10": "Speech rate consistency",
    "mfcc_mean_11": "Pause frequency during speech",
    "mfcc_mean_12": "Prosodic flatness (monotone speech tendency)",
    
    # MFCC stds (variability)
    "mfcc_std_0": "Loudness variability",
    "mfcc_std_1": "Pitch variability (intonation range)",
    "mfcc_std_2": "Vocal tract flexibility",
    "mfcc_std_3": "Articulatory precision variability",
    "mfcc_std_4": "Nasal resonance variability",
    "mfcc_std_5": "Spectral balance variability",
    "mfcc_std_6": "Breath support consistency",
    "mfcc_std_7": "Consonant burst strength variation",
    "mfcc_std_8": "Fricative noise stability",
    "mfcc_std_9": "Vowel distinctness consistency",
    "mfcc_std_10": "Speech rhythm stability",
    "mfcc_std_11": "Pause placement consistency",
    "mfcc_std_12": "Emotional prosody variation",
    
    # Other features
    "zcr": "Zero-crossing rate β€” measures speech turbulence/noisiness (e.g., breathiness, hoarseness)",
    "rms": "Root-mean-square energy β€” average speech loudness"
}


# ================================================================
# Helper functions
# ================================================================

# Converts the raw explanations to something more understandable for patients/doctors
def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
    """
    Converts raw string SHAP explanation into structured JSON with human-readable feature names.
    """
    lines = shap_text.strip().split("\n")
    features = []
    explanations = []
    
    # Skip header
    start_idx = 0
    for i, line in enumerate(lines):
        if line.startswith("  β€’") or "β†’" in line:
            start_idx = i
            break
    
    for line in lines[start_idx:]:
        line = line.strip()
        if not (line.startswith("β€’") or line.startswith("  β€’")):
            continue
        line = line.lstrip("β€’ *→↑↓").strip()
        
        try:
            if " β†’ " in line:
                feature_part, impact_part = line.split(" β†’ ", 1)
            else:
                feature_part, impact_part = line, ""
            
            if " = " in feature_part:
                feat_name_raw, feat_val = feature_part.split(" = ", 1)
                feat_name_raw = feat_name_raw.strip()
                feat_val = feat_val.strip()
            else:
                feat_name_raw = feature_part.strip()
                feat_val = "unknown"
            
            impact_val = "unknown impact"
            if "(" in impact_part and ")" in impact_part:
                impact_val = impact_part.split("(")[-1].split(")")[0].strip()
            
            # Map to human-readable name
            if is_lifestyle:
                feat_name_human = feat_name_raw
            else:
                # Audio: map mfcc_mean_1 to "Voice pitch stability", etc.
                feat_name_human = AUDIO_FEATURE_DESCRIPTIONS.get(feat_name_raw, feat_name_raw).split(" (")[0].split(" β€”")[0].split(" -")[0].strip()
                # e.g., from "Voice pitch stability (fundamental frequency variation)" to "Voice pitch stability"
            
            features.append(feat_name_human)
            
            if is_lifestyle:
                tips = {
                    "SleepQuality": "Consider improving sleep hygiene: consistent bedtime, reduce screens before bed, and limit caffeine after noon.",
                    "EducationLevel": "Engaging in lifelong learning (e.g., courses, puzzles, reading) may help build cognitive reserve.",
                    "CardiovascularDisease": "Managing heart health through diet, exercise, and medication (as advised by your doctor) supports brain health.",
                    "AlcoholConsumption": "Moderate or abstinent alcohol use is linked to lower dementia risk; discuss safe limits with your clinician.",
                    "Diabetes": "Keeping blood sugar well-controlled reduces vascular damage to the brain.",
                    "PhysicalActivity": "Aim for 150 mins/week of moderate activity (e.g., brisk walking) to support brain blood flow.",
                    "DietQuality": "A Mediterranean-style diet (vegetables, fish, olive oil, nuts) is associated with lower risk.",
                    "Depression": "Treating depression (therapy/medication) may reduce long-term cognitive decline.",
                    "HeadInjury": "Prevent falls and use helmets during high-risk activities.",
                    "Hypertension": "Regular BP monitoring and management (salt reduction, medication) protect small brain vessels.",
                    "Smoking": "Quitting smoking at any age improves circulation and reduces inflammation.",
                    "BMI": "Maintaining a healthy weight supports metabolic and vascular brain health.",
                    "FamilyHistoryAlzheimers": "While not modifiable, early lifestyle changes can delay onset even with genetic risk.",
                }
                tip = tips.get(feat_name_raw, "Discuss personalized strategies with your healthcare provider.")
                explanation = f"{AUDIO_FEATURE_DESCRIPTIONS.get(feat_name_raw, feat_name_raw) if not is_lifestyle else feat_name_raw} (value = {feat_val}) increased risk ({impact_val}). {tip}"
            else:
                desc = AUDIO_FEATURE_DESCRIPTIONS.get(feat_name_raw, "an acoustic speech feature")
                explanation = f"{desc} (value = {feat_val}) contributed {impact_val} to the risk prediction."
            
            explanations.append(explanation[:350])
            
        except Exception:
            features.append("Unknown")
            explanations.append(line)

    return {
        "topfeatures": features[:5],
        "explanations": explanations[:5]
    }

# Extract the json from the returned string that the llm gives, as the llm is stupid.
def parse_json_from_markdown(s: str) -> dict:
    """
    Extracts and parses JSON from a string that may be wrapped in ``` or ```json.
    Returns dict on success, or raises ValueError on failure.
    """
    # Try to find JSON inside ```json ... ``` or ``` ... ```
    match = re.search(r"```(?:json)?\s*```", s, re.DOTALL | re.IGNORECASE)
    if match:
        json_str = match.group(1)
    else:
        # Fallback: assume whole string is JSON (e.g., raw dict from function_calling=True)
        json_str = s.strip()

    # Parse
    return json.loads(json_str)


# ================================================================
# Crews
# ================================================================

# Lifestyle predicton crew
def run_crewai_lifestyle(docID: str) -> dict:
    try:
        lifestyle_data = pull_lifestyle(docID)
        if not lifestyle_data:
            return {"status": "error", "message": f"No lifestyle data for {docID}"}

        risk_result = calculate_dementia_risk_lifestyle.run(docID)
        if risk_result["status"] != "success":
            return risk_result

        shap_raw = explain_dementia_risk_lifestyle.run(docID)
        shap_structured = parse_shap_explanation(shap_raw, is_lifestyle=True)

        # Optional: try crew summary (fallback only)
        crew_summary = "Summary unavailable."
        try:
            risk_assessment_task_lifestyle.description = f"Calculate dementia risk for patient using lifestyle data for ID: {docID}"
            explanation_task_lifestyle.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
            crew = Crew(
                agents=[risk_calculator_lifestyle],
                tasks=[risk_assessment_task_lifestyle, explanation_task_lifestyle],
                verbose=False,
                function_calling=False
            )
            out = crew.kickoff()
            if out and str(out).strip():
                crew_summary = str(out).strip()
        except:
            pass

        # Build final output β€” with nested `shap`
        return {
            "status": "success",
            "risk_level": risk_result["risk_level"],
            "prediction": risk_result["prediction"],
            "confidence": risk_result["confidence"],
            "age": risk_result["age"],
            "gender": risk_result["gender"],
            "docID": risk_result["docID"],
            "shap": shap_structured
        }

    except Exception as e:
        return {"status": "error", "message": str(e)}

# Audio prediction crew
def run_crewai_audio(docID: str) -> dict:
    try:
        audio_data = pull_audio(docID)
        if audio_data is None:
            return {"status": "error", "message": f"No audio data for {docID}"}

        risk_result = calculate_dementia_risk_audio.run(docID)
        if risk_result["status"] != "success":
            return risk_result

        shap_raw = explain_dementia_risk_audio.run(docID)
        shap_structured = parse_shap_explanation(shap_raw, is_lifestyle=False)

        # Optional crew summary (fallback)
        crew_summary = "Summary unavailable."
        try:
            risk_assessment_task_audio.description = f"Calculate dementia risk for patient using audio data for ID: {docID}"
            explanation_task_audio.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
            crew = Crew(
                agents=[risk_calculator_audio],
                tasks=[risk_assessment_task_audio, explanation_task_audio],
                verbose=False,
                function_calling=False
            )
            out = crew.kickoff()
            if out and str(out).strip():
                crew_summary = str(out).strip()
        except:
            pass

        return {
            "status": "success",
            "risk_level": risk_result["risk_level"],
            "prediction": risk_result["prediction"],
            "confidence": risk_result["confidence"],
            "age": risk_result["age"],
            "gender": risk_result["gender"],
            "docID": risk_result["docID"],
            "shap": shap_structured
        }

    except Exception as e:
        return {"status": "error", "message": str(e)}

# Patient news crew
def run_crewai_news_patient(count: int = 5) -> dict:
    crew = Crew(
        agents=[patient_news_agent],
        tasks=[get_news_task_patient],
        verbose=True,
        function_calling=True
    )
    result = crew.kickoff(inputs={"count": count})

    # CrewAI returns ToolOutput β†’ .raw or str β€” parse if needed
    try:
        return parse_json_from_markdown(str(result))
    except Exception as e:
        return {"status": "error", "message": f"Failed to parse crew output, <{e}>", "raw": result}

# Doctor news crew
def run_crewai_news_doctor(count: int = 5) -> dict:
    crew = Crew(
        agents=[doctor_news_agent],
        tasks=[get_news_task_doctor],
        verbose=True,
        function_calling=False
    )
    result = crew.kickoff(inputs={"count": count})
    
    # CrewAI returns ToolOutput β†’ .raw or str β€” parse if needed
    try:
        return parse_json_from_markdown(str(result))
    except Exception as e:
        return {"status": "error", "message": f"Failed to parse crew output, <{e}>", "raw": result}