plutoa386 commited on
Commit
41bfda0
·
1 Parent(s): 6d08b29

updated crew prediction json output structure

Browse files
Files changed (1) hide show
  1. crew.py +157 -72
crew.py CHANGED
@@ -442,56 +442,169 @@ get_news_task_doctor = Task(
442
  agent=doctor_news
443
  )
444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
445
  def run_crewai_lifestyle(docID: str) -> dict:
446
  try:
447
  lifestyle_data = pull_lifestyle(docID)
448
  if not lifestyle_data:
449
  return {"status": "error", "message": f"No lifestyle data for {docID}"}
450
 
451
- # Keep manual tool calls — they work
452
  risk_result = calculate_dementia_risk_lifestyle.run(docID)
453
  if risk_result["status"] != "success":
454
  return risk_result
455
- shap_explanation = explain_dementia_risk_lifestyle.run(docID)
456
 
457
- # Try crew for summary — but *don’t fail* if it errors
458
- crew_summary = "Summary unavailable (LLM error)."
 
 
 
459
  try:
460
  risk_assessment_task_lifestyle.description = f"Calculate dementia risk for patient using lifestyle data for ID: {docID}"
461
  explanation_task_lifestyle.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
462
-
463
- crew_lifestyle = Crew(
464
  agents=[risk_calculator_lifestyle],
465
  tasks=[risk_assessment_task_lifestyle, explanation_task_lifestyle],
466
- verbose=False, # reduce noise
467
- function_calling=False, # avoid tool-call JSON
468
- prevent_prompt_injection=True
469
  )
470
- raw_output = crew_lifestyle.kickoff()
471
- if raw_output and str(raw_output).strip():
472
- crew_summary = str(raw_output).strip()
473
- except Exception as e:
474
- # Log quietly if needed, but don’t break
475
- crew_summary = f"Summary failed: {type(e).__name__}"
476
 
 
477
  return {
478
  "status": "success",
479
- **risk_result,
480
- "shap_explanation": shap_explanation,
481
- "crew_summary": crew_summary
 
 
 
 
482
  }
483
 
484
  except Exception as e:
485
  return {"status": "error", "message": str(e)}
486
 
487
-
488
  def run_crewai_audio(docID: str) -> dict:
489
- """
490
- Runs the full CrewAI pipeline for audio risk + SHAP + LLM summary.
491
- Returns a JSON-serializable dict (success/error).
492
- """
493
  try:
494
- # ✅ First, get structured data (these work reliably)
495
  audio_data = pull_audio(docID)
496
  if audio_data is None:
497
  return {"status": "error", "message": f"No audio data for {docID}"}
@@ -499,69 +612,41 @@ def run_crewai_audio(docID: str) -> dict:
499
  risk_result = calculate_dementia_risk_audio.run(docID)
500
  if risk_result["status"] != "success":
501
  return risk_result
502
- shap_explanation = explain_dementia_risk_audio.run(docID)
503
 
504
- # ✅ Then, try crew for natural-language summary — but don't fail if it errors
505
- crew_summary = "Summary unavailable (LLM error)."
 
 
 
506
  try:
507
- # Update tasks with dynamic docID
508
  risk_assessment_task_audio.description = f"Calculate dementia risk for patient using audio data for ID: {docID}"
509
  explanation_task_audio.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
510
-
511
- crew_audio = Crew(
512
  agents=[risk_calculator_audio],
513
  tasks=[risk_assessment_task_audio, explanation_task_audio],
514
- verbose=False, # reduce log spam on retry/fallback
515
- function_calling=False, # avoid tool-call JSON → prevents 400
516
- tracing=False # optional: disable tracing to reduce overhead
517
  )
518
- raw_output = crew_audio.kickoff()
519
- if raw_output and str(raw_output).strip():
520
- crew_summary = str(raw_output).strip()
521
- except Exception as e:
522
- crew_summary = f"Summary failed: {type(e).__name__}"
523
 
524
  return {
525
  "status": "success",
526
- **risk_result,
527
- "shap_explanation": shap_explanation,
528
- "crew_summary": crew_summary
 
 
 
 
529
  }
530
 
531
  except Exception as e:
532
  return {"status": "error", "message": str(e)}
533
 
534
-
535
-
536
-
537
-
538
-
539
- def _parse_json_output(text: str) -> dict:
540
- """Robustly extract JSON from LLM output (handles markdown/code blocks)."""
541
- try:
542
- # Try direct parse first
543
- return json.loads(text)
544
- except json.JSONDecodeError:
545
- pass
546
-
547
- # Try to extract JSON from code block
548
- match = re.search(r"```(?:json)?\s*({.*})\s*```", text, re.DOTALL)
549
- if match:
550
- try:
551
- return json.loads(match.group(1))
552
- except:
553
- pass
554
-
555
- # Try to find first { ... } block
556
- match = re.search(r"\{.*\}", text, re.DOTALL)
557
- if match:
558
- try:
559
- return json.loads(match.group(0))
560
- except:
561
- pass
562
-
563
- raise ValueError(f"Could not parse JSON from: {text[:200]}...")
564
-
565
  def run_crewai_news_patient(count: int = 5) -> dict:
566
  try:
567
  # Inject count into task description dynamically
 
442
  agent=doctor_news
443
  )
444
 
445
+
446
+ # Helper functions
447
+ def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
448
+ """
449
+ Converts raw string SHAP explanation (from tool) into structured JSON:
450
+ {
451
+ "topfeatures": ["SleepQuality", "EducationLevel", ...],
452
+ "explanations": [
453
+ "SleepQuality = 4.5 increases risk (+0.650). Consider improving sleep hygiene with consistent bedtime and reduced screen time before bed.",
454
+ ...
455
+ ]
456
+ }
457
+ """
458
+ lines = shap_text.strip().split("\n")
459
+ features = []
460
+ explanations = []
461
+
462
+ # Skip header line(s)
463
+ start_idx = 0
464
+ for i, line in enumerate(lines):
465
+ if line.startswith(" •") or "→" in line:
466
+ start_idx = i
467
+ break
468
+
469
+ for line in lines[start_idx:]:
470
+ line = line.strip()
471
+ if not line.startswith("•") and not line.startswith(" •"):
472
+ continue
473
+ # Clean bullet
474
+ line = line.lstrip("• *→↑↓").strip()
475
+ # Example: "SleepQuality = 4.5 → ↑ risk (+0.650)"
476
+ try:
477
+ # Split on " → " or " = "
478
+ if " → " in line:
479
+ feature_part, impact_part = line.split(" → ", 1)
480
+ else:
481
+ feature_part, impact_part = line, ""
482
+
483
+ # Extract feature name and value
484
+ if " = " in feature_part:
485
+ feat_name, feat_val = feature_part.split(" = ", 1)
486
+ feat_name = feat_name.strip()
487
+ feat_val = feat_val.strip()
488
+ else:
489
+ feat_name = feature_part.strip()
490
+ feat_val = "unknown"
491
+
492
+ # Extract impact magnitude
493
+ impact_val = "unknown impact"
494
+ if "(" in impact_part and ")" in impact_part:
495
+ impact_val = impact_part.split("(")[-1].split(")")[0].strip()
496
+
497
+ features.append(feat_name)
498
+
499
+ if is_lifestyle:
500
+ # Lifestyle: add mitigation tip (short)
501
+ tips = {
502
+ "SleepQuality": "Consider improving sleep hygiene: consistent bedtime, reduce screens before bed, and limit caffeine after noon.",
503
+ "EducationLevel": "Engaging in lifelong learning (e.g., courses, puzzles, reading) may help build cognitive reserve.",
504
+ "CardiovascularDisease": "Managing heart health through diet, exercise, and medication (as advised by your doctor) supports brain health.",
505
+ "AlcoholConsumption": "Moderate or abstinent alcohol use is linked to lower dementia risk; discuss safe limits with your clinician.",
506
+ "Diabetes": "Keeping blood sugar well-controlled reduces vascular damage to the brain.",
507
+ "PhysicalActivity": "Aim for 150 mins/week of moderate activity (e.g., brisk walking) to support brain blood flow.",
508
+ "DietQuality": "A Mediterranean-style diet (vegetables, fish, olive oil, nuts) is associated with lower risk.",
509
+ "Depression": "Treating depression (therapy/medication) may reduce long-term cognitive decline.",
510
+ "HeadInjury": "Prevent falls and use helmets during high-risk activities.",
511
+ "Hypertension": "Regular BP monitoring and management (salt reduction, medication) protect small brain vessels.",
512
+ "Smoking": "Quitting smoking at any age improves circulation and reduces inflammation.",
513
+ "BMI": "Maintaining a healthy weight supports metabolic and vascular brain health.",
514
+ "FamilyHistoryAlzheimers": "While not modifiable, early lifestyle changes can delay onset even with genetic risk.",
515
+ }
516
+ tip = tips.get(feat_name, "Discuss personalized strategies with your healthcare provider.")
517
+ explanation = f"{feat_name} = {feat_val} increases risk ({impact_val}). {tip}"
518
+ else:
519
+ # Audio: no advice, just factual
520
+ explanation = f"{feat_name} = {feat_val}, contributing {impact_val} to risk."
521
+
522
+ explanations.append(explanation[:300]) # truncate to keep short
523
+
524
+ except Exception:
525
+ # Fallback
526
+ features.append("Unknown")
527
+ explanations.append(line)
528
+
529
+ # Keep only top 5
530
+ return {
531
+ "topfeatures": features[:5],
532
+ "explanations": explanations[:5]
533
+ }
534
+
535
+ def _parse_json_output(text: str) -> dict:
536
+ """Robustly extract JSON from LLM output (handles markdown/code blocks)."""
537
+ try:
538
+ # Try direct parse first
539
+ return json.loads(text)
540
+ except json.JSONDecodeError:
541
+ pass
542
+
543
+ # Try to extract JSON from code block
544
+ match = re.search(r"```(?:json)?\s*({.*})\s*```", text, re.DOTALL)
545
+ if match:
546
+ try:
547
+ return json.loads(match.group(1))
548
+ except:
549
+ pass
550
+
551
+ # Try to find first { ... } block
552
+ match = re.search(r"\{.*\}", text, re.DOTALL)
553
+ if match:
554
+ try:
555
+ return json.loads(match.group(0))
556
+ except:
557
+ pass
558
+
559
+ raise ValueError(f"Could not parse JSON from: {text[:200]}...")
560
+
561
  def run_crewai_lifestyle(docID: str) -> dict:
562
  try:
563
  lifestyle_data = pull_lifestyle(docID)
564
  if not lifestyle_data:
565
  return {"status": "error", "message": f"No lifestyle data for {docID}"}
566
 
 
567
  risk_result = calculate_dementia_risk_lifestyle.run(docID)
568
  if risk_result["status"] != "success":
569
  return risk_result
 
570
 
571
+ shap_raw = explain_dementia_risk_lifestyle.run(docID)
572
+ shap_structured = parse_shap_explanation(shap_raw, is_lifestyle=True)
573
+
574
+ # Optional: try crew summary (fallback only)
575
+ crew_summary = "Summary unavailable."
576
  try:
577
  risk_assessment_task_lifestyle.description = f"Calculate dementia risk for patient using lifestyle data for ID: {docID}"
578
  explanation_task_lifestyle.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
579
+ crew = Crew(
 
580
  agents=[risk_calculator_lifestyle],
581
  tasks=[risk_assessment_task_lifestyle, explanation_task_lifestyle],
582
+ verbose=False,
583
+ function_calling=False
 
584
  )
585
+ out = crew.kickoff()
586
+ if out and str(out).strip():
587
+ crew_summary = str(out).strip()
588
+ except:
589
+ pass
 
590
 
591
+ # Build final output — with nested `shap`
592
  return {
593
  "status": "success",
594
+ "risk_level": risk_result["risk_level"],
595
+ "prediction": risk_result["prediction"],
596
+ "confidence": risk_result["confidence"],
597
+ "age": risk_result["age"],
598
+ "gender": risk_result["gender"],
599
+ "docID": risk_result["docID"],
600
+ "shap": shap_structured
601
  }
602
 
603
  except Exception as e:
604
  return {"status": "error", "message": str(e)}
605
 
 
606
  def run_crewai_audio(docID: str) -> dict:
 
 
 
 
607
  try:
 
608
  audio_data = pull_audio(docID)
609
  if audio_data is None:
610
  return {"status": "error", "message": f"No audio data for {docID}"}
 
612
  risk_result = calculate_dementia_risk_audio.run(docID)
613
  if risk_result["status"] != "success":
614
  return risk_result
 
615
 
616
+ shap_raw = explain_dementia_risk_audio.run(docID)
617
+ shap_structured = parse_shap_explanation(shap_raw, is_lifestyle=False)
618
+
619
+ # Optional crew summary (fallback)
620
+ crew_summary = "Summary unavailable."
621
  try:
 
622
  risk_assessment_task_audio.description = f"Calculate dementia risk for patient using audio data for ID: {docID}"
623
  explanation_task_audio.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
624
+ crew = Crew(
 
625
  agents=[risk_calculator_audio],
626
  tasks=[risk_assessment_task_audio, explanation_task_audio],
627
+ verbose=False,
628
+ function_calling=False
 
629
  )
630
+ out = crew.kickoff()
631
+ if out and str(out).strip():
632
+ crew_summary = str(out).strip()
633
+ except:
634
+ pass
635
 
636
  return {
637
  "status": "success",
638
+ "risk_level": risk_result["risk_level"],
639
+ "prediction": risk_result["prediction"],
640
+ "confidence": risk_result["confidence"],
641
+ "age": risk_result["age"],
642
+ "gender": risk_result["gender"],
643
+ "docID": risk_result["docID"],
644
+ "shap": shap_structured
645
  }
646
 
647
  except Exception as e:
648
  return {"status": "error", "message": str(e)}
649
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
650
  def run_crewai_news_patient(count: int = 5) -> dict:
651
  try:
652
  # Inject count into task description dynamically