Spaces:
Runtime error
Runtime error
Download crew.py from plutoa386/NMS-AI-DEV: direct link, hf CLI and curl.
- Browser
- Download file 33.1 kB
-
https://huggingface.co/spaces/plutoa386/NMS-AI-DEV/resolve/main/crew.py
- Command line
-
hf download hf://spaces/plutoa386/NMS-AI-DEV/crew.py
-
curl -L -o crew.py https://huggingface.co/spaces/plutoa386/NMS-AI-DEV/resolve/main/crew.py
33.1 kB
| 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 | |
| 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 | |
| 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)}" | |
| 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} | |
| 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)}" | |
| 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": [] | |
| } | |
| 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} |