potential SHAP XAI integration
Browse files- .gitignore +1 -0
- app.py +13 -8
- crew.py +185 -94
- crew.txt +41 -0
- requirements.txt +2 -1
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
test.txt
|
app.py
CHANGED
|
@@ -2,40 +2,45 @@ import os
|
|
| 2 |
import sys
|
| 3 |
sys.path.append(os.path.dirname(__file__))
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
lifestyle_func = calculate_dementia_risk_lifestyle.func
|
| 9 |
-
audio_func = calculate_dementia_risk_audio.func
|
| 10 |
|
| 11 |
def assess_lifestyle(docID: str):
|
| 12 |
try:
|
| 13 |
-
return
|
| 14 |
except Exception as e:
|
| 15 |
return {"status": "error", "message": str(e)}
|
| 16 |
|
|
|
|
| 17 |
def assess_audio(docID: str):
|
| 18 |
try:
|
| 19 |
-
return
|
| 20 |
except Exception as e:
|
| 21 |
return {"status": "error", "message": str(e)}
|
| 22 |
|
|
|
|
| 23 |
def assess_risk(docID: str, mode: str):
|
| 24 |
if mode == "audio":
|
| 25 |
return assess_audio(docID)
|
| 26 |
else:
|
| 27 |
return assess_lifestyle(docID)
|
| 28 |
|
|
|
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
-
gr.Markdown("# 🧠 NMS Dementia Risk Assessment API")
|
| 31 |
mode = gr.Radio(
|
| 32 |
choices=["lifestyle", "audio"],
|
| 33 |
value="lifestyle",
|
| 34 |
label="Select Risk Assessment Type"
|
| 35 |
)
|
| 36 |
-
doc_id = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 37 |
output = gr.JSON()
|
| 38 |
-
btn = gr.Button("Get Risk Assessment")
|
| 39 |
btn.click(fn=assess_risk, inputs=[doc_id, mode], outputs=output)
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
| 2 |
import sys
|
| 3 |
sys.path.append(os.path.dirname(__file__))
|
| 4 |
|
| 5 |
+
# Import the new CrewAI orchestrators (not the raw tools)
|
| 6 |
+
from crew import run_crewai_lifestyle, run_crewai_audio
|
| 7 |
import gradio as gr
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def assess_lifestyle(docID: str):
|
| 11 |
try:
|
| 12 |
+
return run_crewai_lifestyle(docID)
|
| 13 |
except Exception as e:
|
| 14 |
return {"status": "error", "message": str(e)}
|
| 15 |
|
| 16 |
+
|
| 17 |
def assess_audio(docID: str):
|
| 18 |
try:
|
| 19 |
+
return run_crewai_audio(docID)
|
| 20 |
except Exception as e:
|
| 21 |
return {"status": "error", "message": str(e)}
|
| 22 |
|
| 23 |
+
|
| 24 |
def assess_risk(docID: str, mode: str):
|
| 25 |
if mode == "audio":
|
| 26 |
return assess_audio(docID)
|
| 27 |
else:
|
| 28 |
return assess_lifestyle(docID)
|
| 29 |
|
| 30 |
+
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("# 🧠 NMS Dementia Risk Assessment API (CrewAI + SHAP)")
|
| 33 |
mode = gr.Radio(
|
| 34 |
choices=["lifestyle", "audio"],
|
| 35 |
value="lifestyle",
|
| 36 |
label="Select Risk Assessment Type"
|
| 37 |
)
|
| 38 |
+
doc_id = gr.Textbox(
|
| 39 |
+
label="Patient Document ID",
|
| 40 |
+
placeholder="e.g., 265e9lboNaBH9bQE0CHR"
|
| 41 |
+
)
|
| 42 |
output = gr.JSON()
|
| 43 |
+
btn = gr.Button("Get Risk Assessment (CrewAI + SHAP)")
|
| 44 |
btn.click(fn=assess_risk, inputs=[doc_id, mode], outputs=output)
|
| 45 |
|
| 46 |
demo.launch()
|
crew.py
CHANGED
|
@@ -7,11 +7,7 @@ from crewai import Agent, Task, Crew, LLM
|
|
| 7 |
from crewai.tools import tool
|
| 8 |
from dataloaderImp import *
|
| 9 |
from typing import Union
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# For future web scraping
|
| 13 |
-
# import requests
|
| 14 |
-
# from bs4 import BeautifulSoup
|
| 15 |
|
| 16 |
|
| 17 |
# Load audio model
|
|
@@ -25,34 +21,14 @@ lifestyle_package = joblib.load('modelLifestyleV2_ML.pk1')
|
|
| 25 |
lifestyle_model = lifestyle_package['model']
|
| 26 |
lifestyle_feature_names = lifestyle_package['feature_names']
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
placeholder_lifestyle_ID = "265e9lboNaBH9bQE0CHR"
|
| 29 |
|
| 30 |
|
| 31 |
# Define tools for the agents
|
| 32 |
-
@tool("fetch_medical_news")
|
| 33 |
-
def fetch_medical_news(query: str = "dementia latest research") -> str:
|
| 34 |
-
"""Fetches recent medical news on dementia/Alzheimer's from trusted sources."""
|
| 35 |
-
# Placeholder for future web scraping
|
| 36 |
-
return """
|
| 37 |
-
1. New Study: Exercise may slow cognitive decline in early-stage dementia
|
| 38 |
-
2. Early Detection: AI tools show 90% accuracy in identifying early signs
|
| 39 |
-
3. Treatment: Clinical trials for new drug show promising results
|
| 40 |
-
4. Nutrition: Mediterranean diet linked to reduced risk of cognitive decline
|
| 41 |
-
5. Sleep: Poor sleep patterns identified as early warning signs
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
@tool("fetch_patient_news")
|
| 45 |
-
def fetch_patient_news(query: str = "dementia care tips") -> str:
|
| 46 |
-
"""Fetches patient-friendly dementia news from trusted sources."""
|
| 47 |
-
# Placeholder for future web scraping
|
| 48 |
-
return """
|
| 49 |
-
1. Caregiver Tips: 5 Ways to Reduce Anxiety in Dementia Patients
|
| 50 |
-
2. Activities: Music Therapy Shows Benefits for Memory Recall
|
| 51 |
-
3. Safety: Home Modifications to Prevent Falls and Accidents
|
| 52 |
-
4. Communication: How to Talk to Someone with Dementia
|
| 53 |
-
5. Support: Local Support Groups Now Available in Your Area
|
| 54 |
-
"""
|
| 55 |
-
|
| 56 |
@tool("calculate_dementia_risk_audio")
|
| 57 |
def calculate_dementia_risk_audio(docID: str, return_json: bool = False) -> Union[str, dict]:
|
| 58 |
"""
|
|
@@ -142,6 +118,41 @@ def calculate_dementia_risk_audio(docID: str, return_json: bool = False) -> Unio
|
|
| 142 |
return {"status": "error", "message": error_msg}
|
| 143 |
return error_msg
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
@tool("calculate_dementia_risk_lifestyle")
|
| 146 |
def calculate_dementia_risk_lifestyle(docID: str, return_json: bool = False) -> Union[str, dict]:
|
| 147 |
"""
|
|
@@ -253,27 +264,61 @@ def calculate_dementia_risk_lifestyle(docID: str, return_json: bool = False) ->
|
|
| 253 |
return {"status": "error", "message": error_msg}
|
| 254 |
return error_msg
|
| 255 |
|
| 256 |
-
@tool("
|
| 257 |
-
def
|
| 258 |
-
"""
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
# OLD (won't work on HF):
|
| 279 |
# llm = LLM(model="ollama/llama3.1:8b")
|
|
@@ -292,30 +337,13 @@ llm = LLM(
|
|
| 292 |
|
| 293 |
|
| 294 |
# Define agents for Alzheimer's detection system
|
| 295 |
-
medical_researcher = Agent(
|
| 296 |
-
role="Medical Researcher Agent",
|
| 297 |
-
goal="Check medical-specific news sources for dementia-related news of interest to medical professionals",
|
| 298 |
-
backstory="You are a specialist in neurology and dementia research, scanning peer-reviewed journals and medical databases for the latest findings.",
|
| 299 |
-
verbose=True,
|
| 300 |
-
llm=llm
|
| 301 |
-
)
|
| 302 |
-
|
| 303 |
-
patient_researcher = Agent(
|
| 304 |
-
role="Patient Researcher Agent",
|
| 305 |
-
goal="Check patient-specific news sources for dementia-related news of interest to carers and those suffering from dementia",
|
| 306 |
-
backstory="You focus on translating complex medical information into accessible, compassionate content for patients and caregivers.",
|
| 307 |
-
verbose=True,
|
| 308 |
-
llm=llm,
|
| 309 |
-
tools=[fetch_patient_news] # Use the tool directly, not the function
|
| 310 |
-
)
|
| 311 |
-
|
| 312 |
risk_calculator_audio = Agent(
|
| 313 |
role="Risk Calculator Audio Agent",
|
| 314 |
goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
|
| 315 |
backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
|
| 316 |
verbose=True,
|
| 317 |
llm=llm,
|
| 318 |
-
tools=[calculate_dementia_risk_audio]
|
| 319 |
)
|
| 320 |
|
| 321 |
risk_calculator_lifestyle = Agent(
|
|
@@ -324,57 +352,120 @@ risk_calculator_lifestyle = Agent(
|
|
| 324 |
backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
|
| 325 |
verbose=True,
|
| 326 |
llm=llm,
|
| 327 |
-
tools=[calculate_dementia_risk_lifestyle]
|
| 328 |
-
)
|
| 329 |
-
|
| 330 |
-
formatter = Agent(
|
| 331 |
-
role="Formatter Agent",
|
| 332 |
-
goal="Format results into a clear, user-friendly report for doctors or patients",
|
| 333 |
-
backstory="You ensure complex medical information is presented clearly, respectfully, and appropriately for the intended audience.",
|
| 334 |
-
verbose=True,
|
| 335 |
-
llm=llm,
|
| 336 |
-
tools=[format_report]
|
| 337 |
)
|
| 338 |
|
| 339 |
|
| 340 |
# Define tasks for agents
|
| 341 |
-
medical_news_task = Task(
|
| 342 |
-
description="Search for the latest medical research on dementia and Alzheimer's disease.",
|
| 343 |
-
expected_output="A summary of 3-5 key medical research updates with links.",
|
| 344 |
-
agent=medical_researcher
|
| 345 |
-
)
|
| 346 |
-
|
| 347 |
-
patient_news_task = Task(
|
| 348 |
-
description="Find patient-friendly articles on dementia care, support, and lifestyle tips.",
|
| 349 |
-
expected_output="A list of 3-5 helpful patient/caregiver resources with brief summaries.",
|
| 350 |
-
agent=patient_researcher
|
| 351 |
-
)
|
| 352 |
-
|
| 353 |
risk_assessment_task_audio = Task(
|
| 354 |
description=f"Calculate dementia risk for patient",
|
| 355 |
expected_output="Risk level, confidence score, and plain-language explanation.",
|
| 356 |
agent=risk_calculator_audio,
|
| 357 |
)
|
| 358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
risk_assessment_task_lifestyle = Task(
|
| 360 |
description=f"Calculate dementia risk for patient using the given patient id: {placeholder_lifestyle_ID}",
|
| 361 |
expected_output="Risk level, confidence score, and plain-language explanation.",
|
| 362 |
agent=risk_calculator_lifestyle,
|
| 363 |
)
|
| 364 |
|
| 365 |
-
|
| 366 |
-
description="
|
| 367 |
-
expected_output="
|
| 368 |
-
agent=
|
| 369 |
)
|
| 370 |
|
| 371 |
# Create crew of agents
|
| 372 |
crew = Crew(
|
| 373 |
-
agents=[
|
| 374 |
tasks=[risk_assessment_task_lifestyle],
|
| 375 |
verbose=True
|
| 376 |
)
|
| 377 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
# Run crew
|
| 379 |
if __name__ == "__main__":
|
| 380 |
print("Starting Alzheimer's Detection Crew with Llama3.1:8b...")
|
|
|
|
| 7 |
from crewai.tools import tool
|
| 8 |
from dataloaderImp import *
|
| 9 |
from typing import Union
|
| 10 |
+
import shap
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
# Load audio model
|
|
|
|
| 21 |
lifestyle_model = lifestyle_package['model']
|
| 22 |
lifestyle_feature_names = lifestyle_package['feature_names']
|
| 23 |
|
| 24 |
+
# Load model explainers
|
| 25 |
+
audio_explainer = shap.TreeExplainer(audio_model)
|
| 26 |
+
lifestyle_explainer = shap.TreeExplainer(lifestyle_model)
|
| 27 |
+
|
| 28 |
placeholder_lifestyle_ID = "265e9lboNaBH9bQE0CHR"
|
| 29 |
|
| 30 |
|
| 31 |
# Define tools for the agents
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
@tool("calculate_dementia_risk_audio")
|
| 33 |
def calculate_dementia_risk_audio(docID: str, return_json: bool = False) -> Union[str, dict]:
|
| 34 |
"""
|
|
|
|
| 118 |
return {"status": "error", "message": error_msg}
|
| 119 |
return error_msg
|
| 120 |
|
| 121 |
+
@tool("explain_dementia_risk_audio")
|
| 122 |
+
def explain_dementia_risk_audio(docID: str) -> str:
|
| 123 |
+
"""Provides SHAP-based explanation for audio risk prediction."""
|
| 124 |
+
try:
|
| 125 |
+
# Reuse feature extraction logic (DRY: refactor if needed later)
|
| 126 |
+
audio_data = pull_audio(docID)
|
| 127 |
+
if audio_data is None:
|
| 128 |
+
return f"Error: No data for {docID}"
|
| 129 |
+
|
| 130 |
+
features = audio_data["full_features"]
|
| 131 |
+
feature_names = audio_feature_names
|
| 132 |
+
|
| 133 |
+
# SHAP values for this instance
|
| 134 |
+
shap_values = audio_explainer.shap_values(features.reshape(1, -1))
|
| 135 |
+
# shap_values is array of shape (1, n_features) for class 1 (Dementia)
|
| 136 |
+
|
| 137 |
+
# Get top 5 positive contributors (pushing toward Dementia = class 1)
|
| 138 |
+
shap_vals = shap_values[0] # for class 1
|
| 139 |
+
top_indices = np.argsort(-shap_vals)[:5] # descending by impact
|
| 140 |
+
|
| 141 |
+
explanation_lines = ["🔍 Top factors increasing dementia risk (audio analysis):"]
|
| 142 |
+
for i in top_indices:
|
| 143 |
+
feat = feature_names[i]
|
| 144 |
+
val = features[i]
|
| 145 |
+
impact = shap_vals[i]
|
| 146 |
+
if abs(impact) < 1e-4:
|
| 147 |
+
continue
|
| 148 |
+
direction = "↑" if impact > 0 else "↓"
|
| 149 |
+
explanation_lines.append(f" • {feat} = {val:.3f} → {direction} risk ({impact:+.3f})")
|
| 150 |
+
|
| 151 |
+
return "\n".join(explanation_lines)
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
return f"SHAP explanation failed: {str(e)}"
|
| 155 |
+
|
| 156 |
@tool("calculate_dementia_risk_lifestyle")
|
| 157 |
def calculate_dementia_risk_lifestyle(docID: str, return_json: bool = False) -> Union[str, dict]:
|
| 158 |
"""
|
|
|
|
| 264 |
return {"status": "error", "message": error_msg}
|
| 265 |
return error_msg
|
| 266 |
|
| 267 |
+
@tool("explain_dementia_risk_lifestyle")
|
| 268 |
+
def explain_dementia_risk_lifestyle(docID: str) -> str:
|
| 269 |
+
"""Provides SHAP-based explanation for lifestyle risk prediction."""
|
| 270 |
+
try:
|
| 271 |
+
lifestyle_data = pull_lifestyle(docID)
|
| 272 |
+
if not lifestyle_data:
|
| 273 |
+
return f"Error: No lifestyle data for {docID}"
|
| 274 |
+
|
| 275 |
+
FEATURE_MAPPING = {
|
| 276 |
+
'Age': 'age', 'Gender': 'gender', 'Ethnicity': 'ethnicity',
|
| 277 |
+
'EducationLevel': 'education_level', 'BMI': 'bmi', 'Smoking': 'smoking',
|
| 278 |
+
'AlcoholConsumption': 'alcohol_consumption', 'PhysicalActivity': 'physical_activity',
|
| 279 |
+
'DietQuality': 'diet_quality', 'SleepQuality': 'sleep_quality',
|
| 280 |
+
'FamilyHistoryAlzheimers': 'family_history_dementia',
|
| 281 |
+
'CardiovascularDisease': 'cardiovascular_disease', 'Diabetes': 'diabetes',
|
| 282 |
+
'Depression': 'depression', 'HeadInjury': 'head_injury', 'Hypertension': 'hypertension'
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
features = []
|
| 286 |
+
for feat_name in lifestyle_feature_names:
|
| 287 |
+
val = lifestyle_data.get(FEATURE_MAPPING[feat_name])
|
| 288 |
+
if val is None:
|
| 289 |
+
return f"Missing feature: {feat_name}"
|
| 290 |
+
features.append(val)
|
| 291 |
|
| 292 |
+
X_instance = np.array(features).reshape(1, -1)
|
| 293 |
+
shap_vals = lifestyle_explainer.shap_values(X_instance)[0] # class 1
|
| 294 |
+
|
| 295 |
+
top_indices = np.argsort(-shap_vals)[:5]
|
| 296 |
+
|
| 297 |
+
explanation_lines = ["🔍 Top factors increasing dementia risk (lifestyle analysis):"]
|
| 298 |
+
for i in top_indices:
|
| 299 |
+
feat = lifestyle_feature_names[i]
|
| 300 |
+
val = features[i]
|
| 301 |
+
impact = shap_vals[i]
|
| 302 |
+
if abs(impact) < 1e-4:
|
| 303 |
+
continue
|
| 304 |
+
# Map categorical to readable (optional but nice)
|
| 305 |
+
readable_val = val
|
| 306 |
+
if feat == "Gender":
|
| 307 |
+
readable_val = "Male" if val == 1 else "Female"
|
| 308 |
+
elif feat in ["Smoking", "AlcoholConsumption", "FamilyHistoryAlzheimers",
|
| 309 |
+
"CardiovascularDisease", "Diabetes", "Depression", "HeadInjury", "Hypertension"]:
|
| 310 |
+
readable_val = "Yes" if val == 1 else "No"
|
| 311 |
+
elif feat == "EducationLevel":
|
| 312 |
+
edu_map = {1: "Primary", 2: "Secondary", 3: "Bachelor", 4: "Postgrad"}
|
| 313 |
+
readable_val = edu_map.get(val, val)
|
| 314 |
+
|
| 315 |
+
direction = "↑" if impact > 0 else "↓"
|
| 316 |
+
explanation_lines.append(f" • {feat} = {readable_val} → {direction} risk ({impact:+.3f})")
|
| 317 |
+
|
| 318 |
+
return "\n".join(explanation_lines)
|
| 319 |
+
|
| 320 |
+
except Exception as e:
|
| 321 |
+
return f"SHAP explanation failed: {str(e)}"
|
| 322 |
|
| 323 |
# OLD (won't work on HF):
|
| 324 |
# llm = LLM(model="ollama/llama3.1:8b")
|
|
|
|
| 337 |
|
| 338 |
|
| 339 |
# Define agents for Alzheimer's detection system
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
risk_calculator_audio = Agent(
|
| 341 |
role="Risk Calculator Audio Agent",
|
| 342 |
goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
|
| 343 |
backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
|
| 344 |
verbose=True,
|
| 345 |
llm=llm,
|
| 346 |
+
tools=[calculate_dementia_risk_audio, explain_dementia_risk_audio]
|
| 347 |
)
|
| 348 |
|
| 349 |
risk_calculator_lifestyle = Agent(
|
|
|
|
| 352 |
backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
|
| 353 |
verbose=True,
|
| 354 |
llm=llm,
|
| 355 |
+
tools=[calculate_dementia_risk_lifestyle, explain_dementia_risk_lifestyle]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
)
|
| 357 |
|
| 358 |
|
| 359 |
# Define tasks for agents
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
risk_assessment_task_audio = Task(
|
| 361 |
description=f"Calculate dementia risk for patient",
|
| 362 |
expected_output="Risk level, confidence score, and plain-language explanation.",
|
| 363 |
agent=risk_calculator_audio,
|
| 364 |
)
|
| 365 |
|
| 366 |
+
explanation_task_audio = Task(
|
| 367 |
+
description="Explain the dementia risk prediction for patient using SHAP",
|
| 368 |
+
expected_output="Top 5 features increasing risk from audio analysis.",
|
| 369 |
+
agent=risk_calculator_audio
|
| 370 |
+
)
|
| 371 |
+
|
| 372 |
risk_assessment_task_lifestyle = Task(
|
| 373 |
description=f"Calculate dementia risk for patient using the given patient id: {placeholder_lifestyle_ID}",
|
| 374 |
expected_output="Risk level, confidence score, and plain-language explanation.",
|
| 375 |
agent=risk_calculator_lifestyle,
|
| 376 |
)
|
| 377 |
|
| 378 |
+
explanation_task_lifestyle = Task(
|
| 379 |
+
description=f"Explain the dementia risk prediction for patient {placeholder_lifestyle_ID} using SHAP",
|
| 380 |
+
expected_output="Top 5 features increasing risk, with impact direction and magnitude.",
|
| 381 |
+
agent=risk_calculator_lifestyle
|
| 382 |
)
|
| 383 |
|
| 384 |
# Create crew of agents
|
| 385 |
crew = Crew(
|
| 386 |
+
agents=[risk_calculator_audio, risk_calculator_lifestyle],
|
| 387 |
tasks=[risk_assessment_task_lifestyle],
|
| 388 |
verbose=True
|
| 389 |
)
|
| 390 |
|
| 391 |
+
def run_crewai_lifestyle(docID: str) -> dict:
|
| 392 |
+
"""
|
| 393 |
+
Runs the full CrewAI pipeline for lifestyle risk + SHAP + LLM summary.
|
| 394 |
+
Returns a JSON-serializable dict (success/error).
|
| 395 |
+
"""
|
| 396 |
+
try:
|
| 397 |
+
# Update task with dynamic docID
|
| 398 |
+
risk_assessment_task_lifestyle.description = f"Calculate dementia risk for patient using lifestyle data for ID: {docID}"
|
| 399 |
+
explanation_task_lifestyle.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
|
| 400 |
+
|
| 401 |
+
# Rebuild crew with updated tasks
|
| 402 |
+
crew_lifestyle = Crew(
|
| 403 |
+
agents=[risk_calculator_lifestyle],
|
| 404 |
+
tasks=[risk_assessment_task_lifestyle, explanation_task_lifestyle],
|
| 405 |
+
verbose=False # Keep logs clean for API
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
# Run
|
| 409 |
+
raw_output = crew_lifestyle.kickoff()
|
| 410 |
+
|
| 411 |
+
# Parse raw_output (usually a string) → try to extract JSON or format
|
| 412 |
+
# Since agents return natural language, we'll wrap it + add structured fields
|
| 413 |
+
# First, get raw model output for structured data
|
| 414 |
+
lifestyle_data = pull_lifestyle(docID)
|
| 415 |
+
if not lifestyle_data:
|
| 416 |
+
return {"status": "error", "message": f"No lifestyle data for {docID}"}
|
| 417 |
+
|
| 418 |
+
# Reuse function logic just for prediction (fast, reliable)
|
| 419 |
+
risk_result = calculate_dementia_risk_lifestyle(docID, return_json=True)
|
| 420 |
+
if risk_result["status"] != "success":
|
| 421 |
+
return risk_result
|
| 422 |
+
|
| 423 |
+
# Add explanation
|
| 424 |
+
shap_explanation = explain_dementia_risk_lifestyle(docID)
|
| 425 |
+
|
| 426 |
+
return {
|
| 427 |
+
"status": "success",
|
| 428 |
+
**risk_result,
|
| 429 |
+
"shap_explanation": shap_explanation,
|
| 430 |
+
"crew_summary": str(raw_output).strip()
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
except Exception as e:
|
| 434 |
+
return {"status": "error", "message": str(e)}
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
def run_crewai_audio(docID: str) -> dict:
|
| 438 |
+
"""Same for audio."""
|
| 439 |
+
try:
|
| 440 |
+
risk_assessment_task_audio.description = f"Calculate dementia risk for patient using audio data for ID: {docID}"
|
| 441 |
+
explanation_task_audio.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
|
| 442 |
+
|
| 443 |
+
crew_audio = Crew(
|
| 444 |
+
agents=[risk_calculator_audio],
|
| 445 |
+
tasks=[risk_assessment_task_audio, explanation_task_audio],
|
| 446 |
+
verbose=False
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
raw_output = crew_audio.kickoff()
|
| 450 |
+
|
| 451 |
+
# Get structured result
|
| 452 |
+
risk_result = calculate_dementia_risk_audio(docID, return_json=True)
|
| 453 |
+
if risk_result["status"] != "success":
|
| 454 |
+
return risk_result
|
| 455 |
+
|
| 456 |
+
shap_explanation = explain_dementia_risk_audio(docID)
|
| 457 |
+
|
| 458 |
+
return {
|
| 459 |
+
"status": "success",
|
| 460 |
+
**risk_result,
|
| 461 |
+
"shap_explanation": shap_explanation,
|
| 462 |
+
"crew_summary": str(raw_output).strip()
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
except Exception as e:
|
| 466 |
+
return {"status": "error", "message": str(e)}
|
| 467 |
+
|
| 468 |
+
|
| 469 |
# Run crew
|
| 470 |
if __name__ == "__main__":
|
| 471 |
print("Starting Alzheimer's Detection Crew with Llama3.1:8b...")
|
crew.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
sys.path.append(os.path.dirname(__file__))
|
| 4 |
+
|
| 5 |
+
from crew import calculate_dementia_risk_lifestyle, calculate_dementia_risk_audio
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
lifestyle_func = calculate_dementia_risk_lifestyle.func
|
| 9 |
+
audio_func = calculate_dementia_risk_audio.func
|
| 10 |
+
|
| 11 |
+
def assess_lifestyle(docID: str):
|
| 12 |
+
try:
|
| 13 |
+
return lifestyle_func(docID, return_json=True)
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return {"status": "error", "message": str(e)}
|
| 16 |
+
|
| 17 |
+
def assess_audio(docID: str):
|
| 18 |
+
try:
|
| 19 |
+
return audio_func(docID, return_json=True)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return {"status": "error", "message": str(e)}
|
| 22 |
+
|
| 23 |
+
def assess_risk(docID: str, mode: str):
|
| 24 |
+
if mode == "audio":
|
| 25 |
+
return assess_audio(docID)
|
| 26 |
+
else:
|
| 27 |
+
return assess_lifestyle(docID)
|
| 28 |
+
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown("# 🧠 NMS Dementia Risk Assessment API")
|
| 31 |
+
mode = gr.Radio(
|
| 32 |
+
choices=["lifestyle", "audio"],
|
| 33 |
+
value="lifestyle",
|
| 34 |
+
label="Select Risk Assessment Type"
|
| 35 |
+
)
|
| 36 |
+
doc_id = gr.Textbox(label="Patient Document ID <265e9lboNaBH9bQE0CHR>", placeholder="e.g., 265e9lboNaBH9bQE0CHR")
|
| 37 |
+
output = gr.JSON()
|
| 38 |
+
btn = gr.Button("Get Risk Assessment")
|
| 39 |
+
btn.click(fn=assess_risk, inputs=[doc_id, mode], outputs=output)
|
| 40 |
+
|
| 41 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -11,4 +11,5 @@ langchain-huggingface
|
|
| 11 |
xgboost
|
| 12 |
spacy
|
| 13 |
openai-whisper
|
| 14 |
-
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
|
|
|
|
|
|
| 11 |
xgboost
|
| 12 |
spacy
|
| 13 |
openai-whisper
|
| 14 |
+
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
|
| 15 |
+
shap>=0.44.0
|