Spaces:
Runtime error
Runtime error
updated audio feature names in shap explanation
Browse files
crew.py
CHANGED
|
@@ -442,24 +442,53 @@ get_news_task_doctor = Task(
|
|
| 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
|
| 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
|
| 463 |
start_idx = 0
|
| 464 |
for i, line in enumerate(lines):
|
| 465 |
if line.startswith(" β’") or "β" in line:
|
|
@@ -468,36 +497,39 @@ def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
|
|
| 468 |
|
| 469 |
for line in lines[start_idx:]:
|
| 470 |
line = line.strip()
|
| 471 |
-
if not line.startswith("β’")
|
| 472 |
continue
|
| 473 |
-
# Clean bullet
|
| 474 |
line = line.lstrip("β’ *βββ").strip()
|
| 475 |
-
|
| 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 |
-
|
| 486 |
-
|
| 487 |
feat_val = feat_val.strip()
|
| 488 |
else:
|
| 489 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.",
|
|
@@ -513,20 +545,18 @@ def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
|
|
| 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(
|
| 517 |
-
explanation = f"{
|
| 518 |
else:
|
| 519 |
-
|
| 520 |
-
explanation = f"{
|
| 521 |
-
|
| 522 |
-
explanations.append(explanation[:
|
| 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]
|
|
|
|
| 442 |
agent=doctor_news
|
| 443 |
)
|
| 444 |
|
| 445 |
+
# Human-readable mapping for audio features
|
| 446 |
+
AUDIO_FEATURE_DESCRIPTIONS = {
|
| 447 |
+
# MFCC means (13 coefficients)
|
| 448 |
+
"mfcc_mean_0": "Overall speech energy (loudness stability)",
|
| 449 |
+
"mfcc_mean_1": "Voice pitch stability (fundamental frequency variation)",
|
| 450 |
+
"mfcc_mean_2": "Vocal tract shape β openness of mouth/jaw",
|
| 451 |
+
"mfcc_mean_3": "Tongue position β front vs. back articulation",
|
| 452 |
+
"mfcc_mean_4": "Nasality (airflow through nose during speech)",
|
| 453 |
+
"mfcc_mean_5": "Spectral tilt β balance of high/low frequencies",
|
| 454 |
+
"mfcc_mean_6": "Vocal effort / breathiness",
|
| 455 |
+
"mfcc_mean_7": "Plosive clarity (e.g., 'p', 't', 'k' sounds)",
|
| 456 |
+
"mfcc_mean_8": "Fricative precision (e.g., 's', 'f', 'sh' sounds)",
|
| 457 |
+
"mfcc_mean_9": "Vowel space contraction (reduced vowel distinctness)",
|
| 458 |
+
"mfcc_mean_10": "Speech rate consistency",
|
| 459 |
+
"mfcc_mean_11": "Pause frequency during speech",
|
| 460 |
+
"mfcc_mean_12": "Prosodic flatness (monotone speech tendency)",
|
| 461 |
+
|
| 462 |
+
# MFCC stds (variability)
|
| 463 |
+
"mfcc_std_0": "Loudness variability",
|
| 464 |
+
"mfcc_std_1": "Pitch variability (intonation range)",
|
| 465 |
+
"mfcc_std_2": "Vocal tract flexibility",
|
| 466 |
+
"mfcc_std_3": "Articulatory precision variability",
|
| 467 |
+
"mfcc_std_4": "Nasal resonance variability",
|
| 468 |
+
"mfcc_std_5": "Spectral balance variability",
|
| 469 |
+
"mfcc_std_6": "Breath support consistency",
|
| 470 |
+
"mfcc_std_7": "Consonant burst strength variation",
|
| 471 |
+
"mfcc_std_8": "Fricative noise stability",
|
| 472 |
+
"mfcc_std_9": "Vowel distinctness consistency",
|
| 473 |
+
"mfcc_std_10": "Speech rhythm stability",
|
| 474 |
+
"mfcc_std_11": "Pause placement consistency",
|
| 475 |
+
"mfcc_std_12": "Emotional prosody variation",
|
| 476 |
+
|
| 477 |
+
# Other features
|
| 478 |
+
"zcr": "Zero-crossing rate β measures speech turbulence/noisiness (e.g., breathiness, hoarseness)",
|
| 479 |
+
"rms": "Root-mean-square energy β average speech loudness"
|
| 480 |
+
}
|
| 481 |
|
| 482 |
# Helper functions
|
| 483 |
def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
|
| 484 |
"""
|
| 485 |
+
Converts raw string SHAP explanation into structured JSON with human-readable feature names.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
"""
|
| 487 |
lines = shap_text.strip().split("\n")
|
| 488 |
features = []
|
| 489 |
explanations = []
|
| 490 |
|
| 491 |
+
# Skip header
|
| 492 |
start_idx = 0
|
| 493 |
for i, line in enumerate(lines):
|
| 494 |
if line.startswith(" β’") or "β" in line:
|
|
|
|
| 497 |
|
| 498 |
for line in lines[start_idx:]:
|
| 499 |
line = line.strip()
|
| 500 |
+
if not (line.startswith("β’") or line.startswith(" β’")):
|
| 501 |
continue
|
|
|
|
| 502 |
line = line.lstrip("β’ *βββ").strip()
|
| 503 |
+
|
| 504 |
try:
|
|
|
|
| 505 |
if " β " in line:
|
| 506 |
feature_part, impact_part = line.split(" β ", 1)
|
| 507 |
else:
|
| 508 |
feature_part, impact_part = line, ""
|
| 509 |
|
|
|
|
| 510 |
if " = " in feature_part:
|
| 511 |
+
feat_name_raw, feat_val = feature_part.split(" = ", 1)
|
| 512 |
+
feat_name_raw = feat_name_raw.strip()
|
| 513 |
feat_val = feat_val.strip()
|
| 514 |
else:
|
| 515 |
+
feat_name_raw = feature_part.strip()
|
| 516 |
feat_val = "unknown"
|
| 517 |
|
|
|
|
| 518 |
impact_val = "unknown impact"
|
| 519 |
if "(" in impact_part and ")" in impact_part:
|
| 520 |
impact_val = impact_part.split("(")[-1].split(")")[0].strip()
|
| 521 |
|
| 522 |
+
# Map to human-readable name
|
| 523 |
+
if is_lifestyle:
|
| 524 |
+
feat_name_human = feat_name_raw
|
| 525 |
+
else:
|
| 526 |
+
# Audio: map mfcc_mean_1 β "Voice pitch stability", etc.
|
| 527 |
+
feat_name_human = AUDIO_FEATURE_DESCRIPTIONS.get(feat_name_raw, feat_name_raw).split(" (")[0].split(" β")[0].split(" -")[0].strip()
|
| 528 |
+
# e.g., from "Voice pitch stability (fundamental frequency variation)" β "Voice pitch stability"
|
| 529 |
+
|
| 530 |
+
features.append(feat_name_human)
|
| 531 |
|
| 532 |
if is_lifestyle:
|
|
|
|
| 533 |
tips = {
|
| 534 |
"SleepQuality": "Consider improving sleep hygiene: consistent bedtime, reduce screens before bed, and limit caffeine after noon.",
|
| 535 |
"EducationLevel": "Engaging in lifelong learning (e.g., courses, puzzles, reading) may help build cognitive reserve.",
|
|
|
|
| 545 |
"BMI": "Maintaining a healthy weight supports metabolic and vascular brain health.",
|
| 546 |
"FamilyHistoryAlzheimers": "While not modifiable, early lifestyle changes can delay onset even with genetic risk.",
|
| 547 |
}
|
| 548 |
+
tip = tips.get(feat_name_raw, "Discuss personalized strategies with your healthcare provider.")
|
| 549 |
+
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}"
|
| 550 |
else:
|
| 551 |
+
desc = AUDIO_FEATURE_DESCRIPTIONS.get(feat_name_raw, "an acoustic speech feature")
|
| 552 |
+
explanation = f"{desc} (value = {feat_val}) contributed {impact_val} to the risk prediction."
|
| 553 |
+
|
| 554 |
+
explanations.append(explanation[:350])
|
| 555 |
+
|
| 556 |
except Exception:
|
|
|
|
| 557 |
features.append("Unknown")
|
| 558 |
explanations.append(line)
|
| 559 |
|
|
|
|
| 560 |
return {
|
| 561 |
"topfeatures": features[:5],
|
| 562 |
"explanations": explanations[:5]
|