plutoa386 commited on
Commit
adcb5fd
·
1 Parent(s): 1ab4436

cleaned up

Browse files
Files changed (8) hide show
  1. .gitignore +1 -1
  2. README.md +0 -2
  3. crew.py +58 -40
  4. crew.txt +0 -471
  5. firebaseLoader.py +52 -36
  6. modelAudiov7_ML.pk1 +0 -3
  7. modelLifestyleV2_ML.pk1 +0 -3
  8. webscraper.py +15 -3
.gitignore CHANGED
@@ -1 +1 @@
1
- crew.txt
 
1
+ hellothere
README.md CHANGED
@@ -8,5 +8,3 @@ sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
8
  app_file: app.py
9
  pinned: false
10
  ---
 
 
crew.py CHANGED
@@ -5,7 +5,7 @@ import joblib
5
  import numpy as np
6
  from crewai import Agent, Task, Crew, LLM
7
  from crewai.tools import tool
8
- from firebaseLoader import *
9
  from webscraper import *
10
  from typing import Union
11
  import shap
@@ -13,6 +13,7 @@ import xgboost as xgb
13
  from xgboost import DMatrix
14
  from math import exp
15
 
 
16
  # ================================================================
17
  # Loading in the prediction models
18
  # ================================================================
@@ -48,7 +49,6 @@ llm_predict = LLM(model="huggingface/openai/gpt-oss-120b")
48
  llm_news = LLM(model="huggingface/Qwen/Qwen3-4B-Thinking-2507")
49
 
50
 
51
-
52
  # ================================================================
53
  # Defining the tools for the agents to make use of
54
  # ================================================================
@@ -132,7 +132,7 @@ def calculate_dementia_risk_audio(docID: str) -> Union[str, dict]:
132
  error_msg = f"Error calculating audio-based risk: {str(e)}"
133
  return {"status": "error", "message": error_msg}
134
 
135
- # Prediction tool using the lifestyle model
136
  @tool("explain_dementia_risk_audio")
137
  def explain_dementia_risk_audio(docID: str) -> str:
138
  """Provides SHAP-based explanation for audio risk prediction."""
@@ -156,7 +156,8 @@ def explain_dementia_risk_audio(docID: str) -> str:
156
  shap_vals = shap_values[0] # for class 1
157
  top_indices = np.argsort(-shap_vals)[:5] # descending by impact
158
 
159
- explanation_lines = ["🔍 Top factors increasing dementia risk (audio analysis):"]
 
160
  for i in top_indices:
161
  feat = feature_names[i]
162
  val = features[i]
@@ -168,6 +169,7 @@ def explain_dementia_risk_audio(docID: str) -> str:
168
 
169
  return "\n".join(explanation_lines)
170
 
 
171
  except Exception as e:
172
  return f"SHAP explanation failed: {str(e)}"
173
 
@@ -190,11 +192,13 @@ def calculate_dementia_risk_lifestyle(docID: str) -> Union[str, dict]:
190
  except json.JSONDecodeError:
191
  pass
192
 
 
193
  lifestyle_data = pull_lifestyle(docID)
194
  if not lifestyle_data:
195
  error = f"Error: No lifestyle data found for document ID '{docID}'"
196
  return {"status": "error", "message": error}
197
 
 
198
  FEATURE_MAPPING = {
199
  'Age': 'age',
200
  'Gender': 'gender',
@@ -247,10 +251,11 @@ def calculate_dementia_risk_lifestyle(docID: str) -> Union[str, dict]:
247
  gender_val = lifestyle_data.get("gender")
248
  age = lifestyle_data.get("age")
249
 
 
250
  risk_level = "High" if prediction == 1 else "Low"
251
  gender_str = "Male" if gender_val == 1 else "Female"
252
 
253
- # Structured output
254
  result_dict = {
255
  "risk_level": risk_level,
256
  "prediction": int(prediction),
@@ -262,6 +267,7 @@ def calculate_dementia_risk_lifestyle(docID: str) -> Union[str, dict]:
262
 
263
  return {"status": "success", **result_dict}
264
 
 
265
  except Exception as e:
266
  error_msg = f"Error calculating risk: {str(e)}"
267
  return {"status": "error", "message": error_msg}
@@ -270,10 +276,14 @@ def calculate_dementia_risk_lifestyle(docID: str) -> Union[str, dict]:
270
  def explain_dementia_risk_lifestyle(docID: str) -> str:
271
  """Provides SHAP-based explanation for lifestyle risk prediction."""
272
  try:
 
273
  lifestyle_data = pull_lifestyle(docID)
 
 
274
  if not lifestyle_data:
275
  return f"Error: No lifestyle data for {docID}"
276
 
 
277
  FEATURE_MAPPING = {
278
  'Age': 'age', 'Gender': 'gender', 'Ethnicity': 'ethnicity',
279
  'EducationLevel': 'education_level', 'BMI': 'bmi', 'Smoking': 'smoking',
@@ -291,11 +301,14 @@ def explain_dementia_risk_lifestyle(docID: str) -> str:
291
  return f"Missing feature: {feat_name}"
292
  features.append(val)
293
 
 
294
  X_instance = np.array(features).reshape(1, -1)
295
- shap_vals = lifestyle_explainer.shap_values(X_instance)[0] # class 1
296
 
297
- top_indices = np.argsort(-shap_vals)[:5]
 
 
298
 
 
299
  explanation_lines = ["Top factors increasing dementia risk (lifestyle analysis):"]
300
  for i in top_indices:
301
  feat = lifestyle_feature_names[i]
@@ -319,6 +332,7 @@ def explain_dementia_risk_lifestyle(docID: str) -> str:
319
 
320
  return "\n".join(explanation_lines)
321
 
 
322
  except Exception as e:
323
  return f"SHAP explanation failed: {str(e)}"
324
 
@@ -330,6 +344,7 @@ def gather_news_patient(count: int = 5) -> dict:
330
  Returns a json object with the format: {"status": "success", "articles": [{"title", "summary", "url"}, ...]}.
331
  """
332
  try:
 
333
  raw_articles = get_patient_news_raw(count=count)
334
  results = []
335
 
@@ -350,21 +365,25 @@ def gather_news_patient(count: int = 5) -> dict:
350
 
351
  Article:
352
  {full_text[:3000]}"""
353
-
 
354
  response = llm_news.invoke(prompt)
355
  summary = str(response).strip()
356
 
 
357
  results.append({
358
  "title": title,
359
  "summary": summary,
360
  "url": url
361
  })
362
 
 
363
  return {
364
  "status": "success",
365
  "articles": results
366
  }
367
 
 
368
  except Exception as e:
369
  return {
370
  "status": "error",
@@ -380,6 +399,7 @@ def gather_news_doctor(count: int = 5) -> dict:
380
  Returns a json object with the format: {"status": "success", "articles": [{"title", "summary", "url"}, ...]}.
381
  """
382
  try:
 
383
  raw_articles = get_doctor_news_raw(count=count)
384
  results = []
385
 
@@ -388,9 +408,11 @@ def gather_news_doctor(count: int = 5) -> dict:
388
  title = item["title"]
389
  full_text = scrape_full_text(url)
390
 
 
391
  if not full_text.strip():
392
  summary = "Summary unavailable."
393
  else:
 
394
  prompt = f"""You are a clinical expert.
395
  Summarize the following article in 2–3 precise, evidence-based sentences for physicians.
396
  Include: key findings, methods (if applicable), clinical relevance.
@@ -398,6 +420,7 @@ def gather_news_doctor(count: int = 5) -> dict:
398
  Article:
399
  {full_text[:3000]}"""
400
 
 
401
  response = llm_news.invoke(prompt)
402
  summary = str(response).strip()
403
 
@@ -407,11 +430,13 @@ def gather_news_doctor(count: int = 5) -> dict:
407
  "url": url
408
  })
409
 
 
410
  return {
411
  "status": "success",
412
  "articles": results
413
  }
414
 
 
415
  except Exception as e:
416
  return {
417
  "status": "error",
@@ -456,31 +481,36 @@ doctor_news_agent = Agent(
456
  verbose=True
457
  )
458
 
 
 
459
  # Define tasks for agents
 
 
 
460
  risk_assessment_task_audio = Task(
461
  description=f"Calculate dementia risk for patient",
462
  expected_output="Risk level, confidence score, and plain-language explanation.",
463
  agent=risk_calculator_audio,
464
  )
465
-
466
  explanation_task_audio = Task(
467
  description="Explain the dementia risk prediction for patient using SHAP",
468
  expected_output="Top 5 features increasing risk from audio analysis.",
469
  agent=risk_calculator_audio
470
  )
471
 
 
472
  risk_assessment_task_lifestyle = Task(
473
  description=f"Calculate dementia risk for patient using the given patient id: {placeholder_lifestyle_ID}",
474
  expected_output="Risk level, confidence score, and plain-language explanation.",
475
  agent=risk_calculator_lifestyle,
476
  )
477
-
478
  explanation_task_lifestyle = Task(
479
  description=f"Explain the dementia risk prediction for patient {placeholder_lifestyle_ID} using SHAP",
480
  expected_output="Top 5 features increasing risk, with impact direction and magnitude.",
481
  agent=risk_calculator_lifestyle
482
  )
483
 
 
484
  get_news_task_patient = Task(
485
  description=(
486
  "Use the `gather_news_patient` tool to fetch {count} recent dementia/caregiving news articles "
@@ -491,7 +521,6 @@ get_news_task_patient = Task(
491
  expected_output="A JSON object with 'status' and 'articles': list of {title, summary, url}.",
492
  agent=patient_news_agent
493
  )
494
-
495
  get_news_task_doctor = Task(
496
  description=(
497
  "Use the `gather_news_doctor` tool to fetch {count} recent dementia research articles "
@@ -503,7 +532,7 @@ get_news_task_doctor = Task(
503
  agent=doctor_news_agent
504
  )
505
 
506
- # Human-readable mapping for audio features
507
  AUDIO_FEATURE_DESCRIPTIONS = {
508
  # MFCC means (13 coefficients)
509
  "mfcc_mean_0": "Overall speech energy (loudness stability)",
@@ -540,7 +569,12 @@ AUDIO_FEATURE_DESCRIPTIONS = {
540
  "rms": "Root-mean-square energy — average speech loudness"
541
  }
542
 
 
 
543
  # Helper functions
 
 
 
544
  def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
545
  """
546
  Converts raw string SHAP explanation into structured JSON with human-readable feature names.
@@ -584,9 +618,9 @@ def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
584
  if is_lifestyle:
585
  feat_name_human = feat_name_raw
586
  else:
587
- # Audio: map mfcc_mean_1 → "Voice pitch stability", etc.
588
  feat_name_human = AUDIO_FEATURE_DESCRIPTIONS.get(feat_name_raw, feat_name_raw).split(" (")[0].split(" —")[0].split(" -")[0].strip()
589
- # e.g., from "Voice pitch stability (fundamental frequency variation)" → "Voice pitch stability"
590
 
591
  features.append(feat_name_human)
592
 
@@ -623,32 +657,7 @@ def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
623
  "explanations": explanations[:5]
624
  }
625
 
626
- def _parse_json_output(text: str) -> dict:
627
- """Robustly extract JSON from LLM output (handles markdown/code blocks)."""
628
- try:
629
- # Try direct parse first
630
- return json.loads(text)
631
- except json.JSONDecodeError:
632
- pass
633
-
634
- # Try to extract JSON from code block
635
- match = re.search(r"```(?:json)?\s*({.*})\s*```", text, re.DOTALL)
636
- if match:
637
- try:
638
- return json.loads(match.group(1))
639
- except:
640
- pass
641
-
642
- # Try to find first { ... } block
643
- match = re.search(r"\{.*\}", text, re.DOTALL)
644
- if match:
645
- try:
646
- return json.loads(match.group(0))
647
- except:
648
- pass
649
-
650
- raise ValueError(f"Could not parse JSON from: {text[:200]}...")
651
-
652
  def parse_json_from_markdown(s: str) -> dict:
653
  """
654
  Extracts and parses JSON from a string that may be wrapped in ``` or ```json.
@@ -665,6 +674,12 @@ def parse_json_from_markdown(s: str) -> dict:
665
  # Parse
666
  return json.loads(json_str)
667
 
 
 
 
 
 
 
668
  def run_crewai_lifestyle(docID: str) -> dict:
669
  try:
670
  lifestyle_data = pull_lifestyle(docID)
@@ -710,6 +725,7 @@ def run_crewai_lifestyle(docID: str) -> dict:
710
  except Exception as e:
711
  return {"status": "error", "message": str(e)}
712
 
 
713
  def run_crewai_audio(docID: str) -> dict:
714
  try:
715
  audio_data = pull_audio(docID)
@@ -754,6 +770,7 @@ def run_crewai_audio(docID: str) -> dict:
754
  except Exception as e:
755
  return {"status": "error", "message": str(e)}
756
 
 
757
  def run_crewai_news_patient(count: int = 5) -> dict:
758
  crew = Crew(
759
  agents=[patient_news_agent],
@@ -769,6 +786,7 @@ def run_crewai_news_patient(count: int = 5) -> dict:
769
  except Exception as e:
770
  return {"status": "error", "message": f"Failed to parse crew output, <{e}>", "raw": result}
771
 
 
772
  def run_crewai_news_doctor(count: int = 5) -> dict:
773
  crew = Crew(
774
  agents=[doctor_news_agent],
 
5
  import numpy as np
6
  from crewai import Agent, Task, Crew, LLM
7
  from crewai.tools import tool
8
+ from firebaseLoader import pull_audio, pull_lifestyle
9
  from webscraper import *
10
  from typing import Union
11
  import shap
 
13
  from xgboost import DMatrix
14
  from math import exp
15
 
16
+
17
  # ================================================================
18
  # Loading in the prediction models
19
  # ================================================================
 
49
  llm_news = LLM(model="huggingface/Qwen/Qwen3-4B-Thinking-2507")
50
 
51
 
 
52
  # ================================================================
53
  # Defining the tools for the agents to make use of
54
  # ================================================================
 
132
  error_msg = f"Error calculating audio-based risk: {str(e)}"
133
  return {"status": "error", "message": error_msg}
134
 
135
+ # SHAP explainer tool for audio prediction results
136
  @tool("explain_dementia_risk_audio")
137
  def explain_dementia_risk_audio(docID: str) -> str:
138
  """Provides SHAP-based explanation for audio risk prediction."""
 
156
  shap_vals = shap_values[0] # for class 1
157
  top_indices = np.argsort(-shap_vals)[:5] # descending by impact
158
 
159
+ # list of shap explanations
160
+ explanation_lines = ["Top factors increasing dementia risk (audio analysis):"]
161
  for i in top_indices:
162
  feat = feature_names[i]
163
  val = features[i]
 
169
 
170
  return "\n".join(explanation_lines)
171
 
172
+ # Error handling
173
  except Exception as e:
174
  return f"SHAP explanation failed: {str(e)}"
175
 
 
192
  except json.JSONDecodeError:
193
  pass
194
 
195
+ # Pulls data from firebase using the patientID (docID)
196
  lifestyle_data = pull_lifestyle(docID)
197
  if not lifestyle_data:
198
  error = f"Error: No lifestyle data found for document ID '{docID}'"
199
  return {"status": "error", "message": error}
200
 
201
+ # feature names in firebase dont match feature names in lifestyle model, so those names need to be changed
202
  FEATURE_MAPPING = {
203
  'Age': 'age',
204
  'Gender': 'gender',
 
251
  gender_val = lifestyle_data.get("gender")
252
  age = lifestyle_data.get("age")
253
 
254
+ # Setting the risk level
255
  risk_level = "High" if prediction == 1 else "Low"
256
  gender_str = "Male" if gender_val == 1 else "Female"
257
 
258
+ # Structured output for easy parsing
259
  result_dict = {
260
  "risk_level": risk_level,
261
  "prediction": int(prediction),
 
267
 
268
  return {"status": "success", **result_dict}
269
 
270
+ # Handles failure to predict
271
  except Exception as e:
272
  error_msg = f"Error calculating risk: {str(e)}"
273
  return {"status": "error", "message": error_msg}
 
276
  def explain_dementia_risk_lifestyle(docID: str) -> str:
277
  """Provides SHAP-based explanation for lifestyle risk prediction."""
278
  try:
279
+ # Pulls data from firebase using the patientID (docID)
280
  lifestyle_data = pull_lifestyle(docID)
281
+
282
+ # Handles missing patient entry
283
  if not lifestyle_data:
284
  return f"Error: No lifestyle data for {docID}"
285
 
286
+ # Remapping the feature names to work with lifestyle model
287
  FEATURE_MAPPING = {
288
  'Age': 'age', 'Gender': 'gender', 'Ethnicity': 'ethnicity',
289
  'EducationLevel': 'education_level', 'BMI': 'bmi', 'Smoking': 'smoking',
 
301
  return f"Missing feature: {feat_name}"
302
  features.append(val)
303
 
304
+
305
  X_instance = np.array(features).reshape(1, -1)
 
306
 
307
+ # Get top 5 positive contributors (pushing toward Dementia = class 1)
308
+ shap_vals = lifestyle_explainer.shap_values(X_instance)[0]
309
+ top_indices = np.argsort(-shap_vals)[:5] # descending by impact to prediction result
310
 
311
+ # list of shap explanations
312
  explanation_lines = ["Top factors increasing dementia risk (lifestyle analysis):"]
313
  for i in top_indices:
314
  feat = lifestyle_feature_names[i]
 
332
 
333
  return "\n".join(explanation_lines)
334
 
335
+ # Error handling
336
  except Exception as e:
337
  return f"SHAP explanation failed: {str(e)}"
338
 
 
344
  Returns a json object with the format: {"status": "success", "articles": [{"title", "summary", "url"}, ...]}.
345
  """
346
  try:
347
+ # gets the article data from webscraper
348
  raw_articles = get_patient_news_raw(count=count)
349
  results = []
350
 
 
365
 
366
  Article:
367
  {full_text[:3000]}"""
368
+
369
+ # Gets the llm_news the create a summary
370
  response = llm_news.invoke(prompt)
371
  summary = str(response).strip()
372
 
373
+
374
  results.append({
375
  "title": title,
376
  "summary": summary,
377
  "url": url
378
  })
379
 
380
+ # Structured output for easy parsing
381
  return {
382
  "status": "success",
383
  "articles": results
384
  }
385
 
386
+ # Error handling
387
  except Exception as e:
388
  return {
389
  "status": "error",
 
399
  Returns a json object with the format: {"status": "success", "articles": [{"title", "summary", "url"}, ...]}.
400
  """
401
  try:
402
+ # gets the article data from webscraper
403
  raw_articles = get_doctor_news_raw(count=count)
404
  results = []
405
 
 
408
  title = item["title"]
409
  full_text = scrape_full_text(url)
410
 
411
+ # LM summarization happens using llm_news
412
  if not full_text.strip():
413
  summary = "Summary unavailable."
414
  else:
415
+ # Use llm_news — ensure it's in scope
416
  prompt = f"""You are a clinical expert.
417
  Summarize the following article in 2–3 precise, evidence-based sentences for physicians.
418
  Include: key findings, methods (if applicable), clinical relevance.
 
420
  Article:
421
  {full_text[:3000]}"""
422
 
423
+ # Gets the llm_news the create a summary
424
  response = llm_news.invoke(prompt)
425
  summary = str(response).strip()
426
 
 
430
  "url": url
431
  })
432
 
433
+ # Structured output for easy parsing
434
  return {
435
  "status": "success",
436
  "articles": results
437
  }
438
 
439
+ # Error handling
440
  except Exception as e:
441
  return {
442
  "status": "error",
 
481
  verbose=True
482
  )
483
 
484
+
485
+ # ================================================================
486
  # Define tasks for agents
487
+ # ================================================================
488
+
489
+ # Audio prediction
490
  risk_assessment_task_audio = Task(
491
  description=f"Calculate dementia risk for patient",
492
  expected_output="Risk level, confidence score, and plain-language explanation.",
493
  agent=risk_calculator_audio,
494
  )
 
495
  explanation_task_audio = Task(
496
  description="Explain the dementia risk prediction for patient using SHAP",
497
  expected_output="Top 5 features increasing risk from audio analysis.",
498
  agent=risk_calculator_audio
499
  )
500
 
501
+ # Lifestyle prediction
502
  risk_assessment_task_lifestyle = Task(
503
  description=f"Calculate dementia risk for patient using the given patient id: {placeholder_lifestyle_ID}",
504
  expected_output="Risk level, confidence score, and plain-language explanation.",
505
  agent=risk_calculator_lifestyle,
506
  )
 
507
  explanation_task_lifestyle = Task(
508
  description=f"Explain the dementia risk prediction for patient {placeholder_lifestyle_ID} using SHAP",
509
  expected_output="Top 5 features increasing risk, with impact direction and magnitude.",
510
  agent=risk_calculator_lifestyle
511
  )
512
 
513
+ # News gathering
514
  get_news_task_patient = Task(
515
  description=(
516
  "Use the `gather_news_patient` tool to fetch {count} recent dementia/caregiving news articles "
 
521
  expected_output="A JSON object with 'status' and 'articles': list of {title, summary, url}.",
522
  agent=patient_news_agent
523
  )
 
524
  get_news_task_doctor = Task(
525
  description=(
526
  "Use the `gather_news_doctor` tool to fetch {count} recent dementia research articles "
 
532
  agent=doctor_news_agent
533
  )
534
 
535
+ # Human-readable mapping for audio features, as the names are not very understandable.
536
  AUDIO_FEATURE_DESCRIPTIONS = {
537
  # MFCC means (13 coefficients)
538
  "mfcc_mean_0": "Overall speech energy (loudness stability)",
 
569
  "rms": "Root-mean-square energy — average speech loudness"
570
  }
571
 
572
+
573
+ # ================================================================
574
  # Helper functions
575
+ # ================================================================
576
+
577
+ # Converts the raw explanations to something more understandable for patients/doctors
578
  def parse_shap_explanation(shap_text: str, is_lifestyle: bool = True) -> dict:
579
  """
580
  Converts raw string SHAP explanation into structured JSON with human-readable feature names.
 
618
  if is_lifestyle:
619
  feat_name_human = feat_name_raw
620
  else:
621
+ # Audio: map mfcc_mean_1 to "Voice pitch stability", etc.
622
  feat_name_human = AUDIO_FEATURE_DESCRIPTIONS.get(feat_name_raw, feat_name_raw).split(" (")[0].split(" —")[0].split(" -")[0].strip()
623
+ # e.g., from "Voice pitch stability (fundamental frequency variation)" to "Voice pitch stability"
624
 
625
  features.append(feat_name_human)
626
 
 
657
  "explanations": explanations[:5]
658
  }
659
 
660
+ # Extract the json from the returned string that the llm gives, as the llm is stupid.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
661
  def parse_json_from_markdown(s: str) -> dict:
662
  """
663
  Extracts and parses JSON from a string that may be wrapped in ``` or ```json.
 
674
  # Parse
675
  return json.loads(json_str)
676
 
677
+
678
+ # ================================================================
679
+ # Crews
680
+ # ================================================================
681
+
682
+ # Lifestyle predicton crew
683
  def run_crewai_lifestyle(docID: str) -> dict:
684
  try:
685
  lifestyle_data = pull_lifestyle(docID)
 
725
  except Exception as e:
726
  return {"status": "error", "message": str(e)}
727
 
728
+ # Audio prediction crew
729
  def run_crewai_audio(docID: str) -> dict:
730
  try:
731
  audio_data = pull_audio(docID)
 
770
  except Exception as e:
771
  return {"status": "error", "message": str(e)}
772
 
773
+ # Patient news crew
774
  def run_crewai_news_patient(count: int = 5) -> dict:
775
  crew = Crew(
776
  agents=[patient_news_agent],
 
786
  except Exception as e:
787
  return {"status": "error", "message": f"Failed to parse crew output, <{e}>", "raw": result}
788
 
789
+ # Doctor news crew
790
  def run_crewai_news_doctor(count: int = 5) -> dict:
791
  crew = Crew(
792
  agents=[doctor_news_agent],
crew.txt DELETED
@@ -1,471 +0,0 @@
1
- import os
2
- import re
3
- import json
4
- import joblib
5
- import numpy as np
6
- from crewai import Agent, Task, Crew, LLM
7
- from crewai.tools import tool
8
- from dataloaderImp import *
9
- from typing import Union
10
- import shap
11
- import xgboost as xgb
12
- from huggingface_hub import InferenceClient
13
-
14
- # Load audio model & metadata
15
- audio_model = xgb.Booster()
16
- audio_model.load_model('modelAudiov8_ML.json')
17
-
18
- metadata = joblib.load('modelAudiov8_metadata.pk1')
19
- audio_feature_names = metadata['feature_names']
20
- audio_mmse_index = metadata['mmse_index']
21
-
22
- # Load lifestyle model & metadata
23
- lifestyle_model = xgb.Booster()
24
- lifestyle_model.load_model('modelLifestyleV3_ML.json')
25
-
26
- metadata = joblib.load('modelLifestyleV3_metadata.pk1')
27
- lifestyle_feature_names = metadata['feature_names']
28
-
29
- # Load model explainers
30
- audio_explainer = shap.TreeExplainer(audio_model)
31
- lifestyle_explainer = shap.TreeExplainer(lifestyle_model)
32
-
33
- placeholder_lifestyle_ID = "umWSG13rLSfHqqJlq1WluPzjLB93"
34
-
35
-
36
- # Define tools for the agents
37
- @tool("calculate_dementia_risk_audio")
38
- def calculate_dementia_risk_audio(docID: str, return_json: bool = False) -> Union[str, dict]:
39
- """
40
- Calculates dementia risk for a specific patient using audio and transcript data from Firebase.
41
- If return_json=True, returns a structured dict. Otherwise, returns a formatted report string.
42
- """
43
- try:
44
- # Handle JSON-wrapped input (from CrewAI)
45
- if docID.strip().startswith('{'):
46
- try:
47
- parsed = json.loads(docID)
48
- if isinstance(parsed, dict):
49
- if "Audio" in parsed and isinstance(parsed["Audio"], dict):
50
- docID = parsed["Audio"].get("docID", docID)
51
- elif "docID" in parsed:
52
- docID = parsed["docID"]
53
- except json.JSONDecodeError:
54
- pass
55
-
56
- audio_data = pull_audio(docID)
57
-
58
- if audio_data is None:
59
- error_msg = f"Error: Could not retrieve or process audio data for patient '{docID}'"
60
- if return_json:
61
- return {"status": "error", "message": error_msg}
62
- return error_msg
63
-
64
- # Extract values
65
- age = audio_data.get("age")
66
- gender_val = audio_data.get("gender")
67
- mmse = audio_data.get("mmse") # Always None in current implementation
68
- combined_features = audio_data.get("full_features")
69
-
70
- if combined_features is None:
71
- error_msg = f"Error: Feature extraction failed for patient '{docID}'"
72
- if return_json:
73
- return {"status": "error", "message": error_msg}
74
- return error_msg
75
-
76
- # Predict
77
- prediction = audio_model.predict(combined_features.reshape(1, -1))[0]
78
- probability = audio_model.predict_proba(combined_features.reshape(1, -1))[0]
79
- confidence = float(max(probability))
80
- risk_level = "High" if prediction == 1 else "Low"
81
- gender_str = "Male" if gender_val == 1 else "Female"
82
-
83
- # Structured output
84
- result_dict = {
85
- "risk_level": risk_level,
86
- "prediction": int(prediction),
87
- "confidence": confidence,
88
- "age": age,
89
- "gender": gender_str,
90
- "docID": docID
91
- }
92
-
93
- if return_json:
94
- return {"status": "success", **result_dict}
95
-
96
- # Fallback: formatted string report (for CrewAI agents)
97
- explanation = (
98
- f"Based on speech analysis and linguistic patterns, "
99
- f"the AI model detected {'concerning patterns' if prediction == 1 else 'normal patterns'} "
100
- f"in speech fluency, vocabulary diversity, and cognitive response time."
101
- )
102
-
103
- mmse_note = "\n[Note: MMSE score not provided. Assessment based on speech and demographics only. Accuracy may be reduced.]"
104
-
105
- report = f"""
106
- DEMENTIA RISK ASSESSMENT (AUDIO)
107
- ================================
108
- Risk Level: {risk_level}
109
- Confidence: {confidence:.2%}
110
- Prediction: {'Positive' if prediction == 1 else 'Negative'}
111
- Age: {age}
112
- Gender: {gender_str}
113
- {mmse_note}
114
-
115
- EXPLANATION:
116
- {explanation}
117
- """
118
- return report
119
-
120
- except Exception as e:
121
- error_msg = f"Error calculating audio-based risk: {str(e)}"
122
- if return_json:
123
- return {"status": "error", "message": error_msg}
124
- return error_msg
125
-
126
- @tool("explain_dementia_risk_audio")
127
- def explain_dementia_risk_audio(docID: str) -> str:
128
- """Provides SHAP-based explanation for audio risk prediction."""
129
- try:
130
- # Reuse feature extraction logic (DRY: refactor if needed later)
131
- audio_data = pull_audio(docID)
132
- if audio_data is None:
133
- return f"Error: No data for {docID}"
134
-
135
- features = audio_data["full_features"]
136
- feature_names = audio_feature_names
137
-
138
- # SHAP values for this instance
139
- shap_values = audio_explainer.shap_values(features.reshape(1, -1))
140
- # shap_values is array of shape (1, n_features) for class 1 (Dementia)
141
-
142
- # Get top 5 positive contributors (pushing toward Dementia = class 1)
143
- shap_vals = shap_values[0] # for class 1
144
- top_indices = np.argsort(-shap_vals)[:5] # descending by impact
145
-
146
- explanation_lines = ["🔍 Top factors increasing dementia risk (audio analysis):"]
147
- for i in top_indices:
148
- feat = feature_names[i]
149
- val = features[i]
150
- impact = shap_vals[i]
151
- if abs(impact) < 1e-4:
152
- continue
153
- direction = "↑" if impact > 0 else "↓"
154
- explanation_lines.append(f" • {feat} = {val:.3f} → {direction} risk ({impact:+.3f})")
155
-
156
- return "\n".join(explanation_lines)
157
-
158
- except Exception as e:
159
- return f"SHAP explanation failed: {str(e)}"
160
-
161
- @tool("calculate_dementia_risk_lifestyle")
162
- def calculate_dementia_risk_lifestyle(docID: str, return_json: bool = False) -> Union[str, dict]:
163
- """
164
- Calculates dementia risk for a specific patient using lifestyle data.
165
- If return_json=True, returns a structured dict. Otherwise, returns a formatted report string.
166
- """
167
- try:
168
- # Handle JSON-wrapped input (from CrewAI)
169
- if docID.strip().startswith('{'):
170
- try:
171
- parsed = json.loads(docID)
172
- if isinstance(parsed, dict):
173
- if "Lifestyle" in parsed and isinstance(parsed["Lifestyle"], dict):
174
- docID = parsed["Lifestyle"].get("docID", docID)
175
- elif "docID" in parsed:
176
- docID = parsed["docID"]
177
- except json.JSONDecodeError:
178
- pass
179
-
180
- lifestyle_data = pull_lifestyle(docID)
181
- if not lifestyle_data:
182
- error = f"Error: No lifestyle data found for document ID '{docID}'"
183
- if return_json:
184
- return {"status": "error", "message": error}
185
- return error
186
-
187
- FEATURE_MAPPING = {
188
- 'Age': 'age',
189
- 'Gender': 'gender',
190
- 'Ethnicity': 'ethnicity',
191
- 'EducationLevel': 'education_level',
192
- 'BMI': 'bmi',
193
- 'Smoking': 'smoking',
194
- 'AlcoholConsumption': 'alcohol_consumption',
195
- 'PhysicalActivity': 'physical_activity',
196
- 'DietQuality': 'diet_quality',
197
- 'SleepQuality': 'sleep_quality',
198
- 'FamilyHistoryAlzheimers': 'family_history_dementia',
199
- 'CardiovascularDisease': 'cardiovascular_disease',
200
- 'Diabetes': 'diabetes',
201
- 'Depression': 'depression',
202
- 'HeadInjury': 'head_injury',
203
- 'Hypertension': 'hypertension'
204
- }
205
-
206
- features = []
207
- for feat_name in lifestyle_feature_names:
208
- firestore_key = FEATURE_MAPPING.get(feat_name)
209
- if firestore_key is None:
210
- error = f"Error: No mapping defined for feature '{feat_name}'"
211
- if return_json:
212
- return {"status": "error", "message": error}
213
- return error
214
- val = lifestyle_data.get(firestore_key)
215
- if val is None:
216
- error = f"Error: Missing required lifestyle feature '{feat_name}' (Firestore key: '{firestore_key}')"
217
- if return_json:
218
- return {"status": "error", "message": error}
219
- return error
220
- features.append(val)
221
-
222
- life = np.array(features).reshape(1, -1)
223
- age = lifestyle_data.get("age")
224
- gender_val = lifestyle_data.get("gender")
225
-
226
- prediction = lifestyle_model.predict(life)[0]
227
- probability = lifestyle_model.predict_proba(life)[0]
228
- confidence = float(max(probability))
229
- risk_level = "High" if prediction == 1 else "Low"
230
- gender_str = "Male" if gender_val == 1 else "Female"
231
-
232
- # Structured output
233
- result_dict = {
234
- "risk_level": risk_level,
235
- "prediction": int(prediction),
236
- "confidence": confidence,
237
- "age": age,
238
- "gender": gender_str,
239
- "docID": docID
240
- }
241
-
242
- if return_json:
243
- return result_dict
244
-
245
- # Original string report
246
- explanation = (
247
- f"Based on lifestyle factors including diet, physical activity, and medical history, "
248
- f"the AI model detected {'concerning patterns' if prediction == 1 else 'normal patterns'} "
249
- f"associated with increased dementia risk."
250
- )
251
-
252
- report = f"""
253
- DEMENTIA RISK ASSESSMENT (LIFESTYLE)
254
- ===================================
255
- Risk Level: {risk_level}
256
- Confidence: {confidence:.2%}
257
- Prediction: {'Positive' if prediction == 1 else 'Negative'}
258
- Age: {age}
259
- Gender: {gender_str}
260
-
261
- EXPLANATION:
262
- {explanation}
263
- """
264
- return report
265
-
266
- except Exception as e:
267
- error_msg = f"Error calculating risk: {str(e)}"
268
- if return_json:
269
- return {"status": "error", "message": error_msg}
270
- return error_msg
271
-
272
- @tool("explain_dementia_risk_lifestyle")
273
- def explain_dementia_risk_lifestyle(docID: str) -> str:
274
- """Provides SHAP-based explanation for lifestyle risk prediction."""
275
- try:
276
- lifestyle_data = pull_lifestyle(docID)
277
- if not lifestyle_data:
278
- return f"Error: No lifestyle data for {docID}"
279
-
280
- FEATURE_MAPPING = {
281
- 'Age': 'age', 'Gender': 'gender', 'Ethnicity': 'ethnicity',
282
- 'EducationLevel': 'education_level', 'BMI': 'bmi', 'Smoking': 'smoking',
283
- 'AlcoholConsumption': 'alcohol_consumption', 'PhysicalActivity': 'physical_activity',
284
- 'DietQuality': 'diet_quality', 'SleepQuality': 'sleep_quality',
285
- 'FamilyHistoryAlzheimers': 'family_history_dementia',
286
- 'CardiovascularDisease': 'cardiovascular_disease', 'Diabetes': 'diabetes',
287
- 'Depression': 'depression', 'HeadInjury': 'head_injury', 'Hypertension': 'hypertension'
288
- }
289
-
290
- features = []
291
- for feat_name in lifestyle_feature_names:
292
- val = lifestyle_data.get(FEATURE_MAPPING[feat_name])
293
- if val is None:
294
- return f"Missing feature: {feat_name}"
295
- features.append(val)
296
-
297
- X_instance = np.array(features).reshape(1, -1)
298
- shap_vals = lifestyle_explainer.shap_values(X_instance)[0] # class 1
299
-
300
- top_indices = np.argsort(-shap_vals)[:5]
301
-
302
- explanation_lines = ["🔍 Top factors increasing dementia risk (lifestyle analysis):"]
303
- for i in top_indices:
304
- feat = lifestyle_feature_names[i]
305
- val = features[i]
306
- impact = shap_vals[i]
307
- if abs(impact) < 1e-4:
308
- continue
309
- # Map categorical to readable (optional but nice)
310
- readable_val = val
311
- if feat == "Gender":
312
- readable_val = "Male" if val == 1 else "Female"
313
- elif feat in ["Smoking", "AlcoholConsumption", "FamilyHistoryAlzheimers",
314
- "CardiovascularDisease", "Diabetes", "Depression", "HeadInjury", "Hypertension"]:
315
- readable_val = "Yes" if val == 1 else "No"
316
- elif feat == "EducationLevel":
317
- edu_map = {1: "Primary", 2: "Secondary", 3: "Bachelor", 4: "Postgrad"}
318
- readable_val = edu_map.get(val, val)
319
-
320
- direction = "↑" if impact > 0 else "↓"
321
- explanation_lines.append(f" • {feat} = {readable_val} → {direction} risk ({impact:+.3f})")
322
-
323
- return "\n".join(explanation_lines)
324
-
325
- except Exception as e:
326
- return f"SHAP explanation failed: {str(e)}"
327
-
328
-
329
- # Load LLM from huggingface repos
330
- client = InferenceClient(token=os.getenv("HF_TOKEN"))
331
- def llm_invoke(prompt: str) -> str:
332
- return client.text_generation(prompt, model="mistralai/Mistral-7B-Instruct-v0.2", max_new_tokens=512, temperature=0.7)
333
-
334
- llm = type("SimpleLLM", (), {"invoke": llm_invoke})()
335
-
336
- # Define agents for Alzheimer's detection system
337
- risk_calculator_audio = Agent(
338
- role="Risk Calculator Audio Agent",
339
- goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
340
- backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
341
- verbose=True,
342
- llm=llm,
343
- tools=[calculate_dementia_risk_audio, explain_dementia_risk_audio]
344
- )
345
-
346
- risk_calculator_lifestyle = Agent(
347
- role="Risk Calculator Lifestyle Agent",
348
- goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
349
- backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
350
- verbose=True,
351
- llm=llm,
352
- tools=[calculate_dementia_risk_lifestyle, explain_dementia_risk_lifestyle]
353
- )
354
-
355
-
356
- # Define tasks for agents
357
- risk_assessment_task_audio = Task(
358
- description=f"Calculate dementia risk for patient",
359
- expected_output="Risk level, confidence score, and plain-language explanation.",
360
- agent=risk_calculator_audio,
361
- )
362
-
363
- explanation_task_audio = Task(
364
- description="Explain the dementia risk prediction for patient using SHAP",
365
- expected_output="Top 5 features increasing risk from audio analysis.",
366
- agent=risk_calculator_audio
367
- )
368
-
369
- risk_assessment_task_lifestyle = Task(
370
- description=f"Calculate dementia risk for patient using the given patient id: {placeholder_lifestyle_ID}",
371
- expected_output="Risk level, confidence score, and plain-language explanation.",
372
- agent=risk_calculator_lifestyle,
373
- )
374
-
375
- explanation_task_lifestyle = Task(
376
- description=f"Explain the dementia risk prediction for patient {placeholder_lifestyle_ID} using SHAP",
377
- expected_output="Top 5 features increasing risk, with impact direction and magnitude.",
378
- agent=risk_calculator_lifestyle
379
- )
380
-
381
- # Create crew of agents
382
- crew = Crew(
383
- agents=[risk_calculator_audio, risk_calculator_lifestyle],
384
- tasks=[risk_assessment_task_lifestyle],
385
- verbose=True
386
- )
387
-
388
- def run_crewai_lifestyle(docID: str) -> dict:
389
- """
390
- Runs the full CrewAI pipeline for lifestyle risk + SHAP + LLM summary.
391
- Returns a JSON-serializable dict (success/error).
392
- """
393
- try:
394
- # Update task with dynamic docID
395
- risk_assessment_task_lifestyle.description = f"Calculate dementia risk for patient using lifestyle data for ID: {docID}"
396
- explanation_task_lifestyle.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
397
-
398
- # Rebuild crew with updated tasks
399
- crew_lifestyle = Crew(
400
- agents=[risk_calculator_lifestyle],
401
- tasks=[risk_assessment_task_lifestyle, explanation_task_lifestyle],
402
- verbose=False # Keep logs clean for API
403
- )
404
-
405
- # Run
406
- raw_output = crew_lifestyle.kickoff()
407
-
408
- # Parse raw_output (usually a string) → try to extract JSON or format
409
- # Since agents return natural language, we'll wrap it + add structured fields
410
- # First, get raw model output for structured data
411
- lifestyle_data = pull_lifestyle(docID)
412
- if not lifestyle_data:
413
- return {"status": "error", "message": f"No lifestyle data for {docID}"}
414
-
415
- # Reuse function logic just for prediction (fast, reliable)
416
- risk_result = calculate_dementia_risk_lifestyle(docID, return_json=True)
417
- if risk_result["status"] != "success":
418
- return risk_result
419
-
420
- # Add explanation
421
- shap_explanation = explain_dementia_risk_lifestyle(docID)
422
-
423
- return {
424
- "status": "success",
425
- **risk_result,
426
- "shap_explanation": shap_explanation,
427
- "crew_summary": str(raw_output).strip()
428
- }
429
-
430
- except Exception as e:
431
- return {"status": "error", "message": str(e)}
432
-
433
-
434
- def run_crewai_audio(docID: str) -> dict:
435
- """Same for audio."""
436
- try:
437
- risk_assessment_task_audio.description = f"Calculate dementia risk for patient using audio data for ID: {docID}"
438
- explanation_task_audio.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
439
-
440
- crew_audio = Crew(
441
- agents=[risk_calculator_audio],
442
- tasks=[risk_assessment_task_audio, explanation_task_audio],
443
- verbose=False
444
- )
445
-
446
- raw_output = crew_audio.kickoff()
447
-
448
- # Get structured result
449
- risk_result = calculate_dementia_risk_audio(docID, return_json=True)
450
- if risk_result["status"] != "success":
451
- return risk_result
452
-
453
- shap_explanation = explain_dementia_risk_audio(docID)
454
-
455
- return {
456
- "status": "success",
457
- **risk_result,
458
- "shap_explanation": shap_explanation,
459
- "crew_summary": str(raw_output).strip()
460
- }
461
-
462
- except Exception as e:
463
- return {"status": "error", "message": str(e)}
464
-
465
-
466
- # Run crew
467
- if __name__ == "__main__":
468
- print("Starting Alzheimer's Detection Crew with Llama3.1:8b...")
469
- result = crew.kickoff()
470
- print("\n=== FINAL REPORT ===\n")
471
- print(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
firebaseLoader.py CHANGED
@@ -9,7 +9,11 @@ import numpy as np
9
  import firebase_admin
10
  from firebase_admin import credentials, firestore, storage
11
 
12
- # --- Firebase Initialization ---
 
 
 
 
13
  if not firebase_admin._apps:
14
  firebase_key = os.getenv("FIREBASE_SERVICE_ACCOUNT_JSON")
15
  if firebase_key:
@@ -28,10 +32,13 @@ bucket = storage.bucket()
28
  _whisper_model = None
29
  _nlp = None
30
 
 
 
 
 
31
  def _load_whisper():
32
  global _whisper_model
33
  if _whisper_model is None:
34
- import whisper
35
  print("Loading Whisper model...")
36
  _whisper_model = whisper.load_model("medium")
37
  return _whisper_model
@@ -39,44 +46,10 @@ def _load_whisper():
39
  def _load_spacy():
40
  global _nlp
41
  if _nlp is None:
42
- import spacy
43
  print("Loading spaCy model...")
44
  _nlp = spacy.load("en_core_web_sm")
45
  return _nlp
46
 
47
- def extract_audio_features(wav_path):
48
- """Extract MFCC, ZCR, RMS from a single .wav file"""
49
- try:
50
- y, sr = librosa.load(wav_path, sr=None)
51
- mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
52
- mfccs_mean = np.mean(mfccs, axis=1)
53
- mfccs_std = np.std(mfccs, axis=1)
54
- zcr = np.mean(librosa.feature.zero_crossing_rate(y))
55
- rms = np.mean(librosa.feature.rms(y=y))
56
- return np.concatenate([mfccs_mean, mfccs_std, [zcr, rms]])
57
- except Exception as e:
58
- print(f"Audio extraction failed for {wav_path}: {e}")
59
- return None
60
-
61
- def extract_text_features(cha_path):
62
- """Extract linguistic features from .cha file"""
63
- try:
64
- with open(cha_path, 'r', encoding='utf-8') as f:
65
- lines = f.readlines()
66
- utterances = [line.strip() for line in lines if line.startswith('*')]
67
- if not utterances:
68
- return None
69
- word_count = sum(len(line.split()) for line in utterances)
70
- sentence_count = len(utterances)
71
- avg_words_per_sentence = word_count / sentence_count if sentence_count > 0 else 0
72
- unique_words = len(set(word.lower().strip('.,!?;:') for line in utterances for word in line.split()))
73
- lexical_diversity = unique_words / word_count if word_count > 0 else 0
74
- pause_count = sum(line.count('<') + line.count('...') for line in utterances)
75
- return np.array([word_count, sentence_count, avg_words_per_sentence, lexical_diversity, pause_count])
76
- except Exception as e:
77
- print(f"Text extraction failed for {cha_path}: {e}")
78
- return None
79
-
80
  def text_to_cha_lines(text):
81
  text = text.replace("...", " ELLIPSIS_MARKER ")
82
  sentences = []
@@ -112,6 +85,48 @@ def generate_mor_gra_line(doc):
112
  return " ".join(mor_tokens), " ".join(gra_tokens)
113
 
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  def pull_audio(docID: str):
116
  """
117
  Downloads .wav from Firebase Storage, fetches age/gender from Firestore,
@@ -254,6 +269,7 @@ def pull_audio(docID: str):
254
 
255
  def pull_lifestyle(document_id: str):
256
  """Fetches lifestyle data from Firestore."""
 
257
  try:
258
  doc_ref = db.collection("Questionnaire").document(document_id)
259
  doc = doc_ref.get()
 
9
  import firebase_admin
10
  from firebase_admin import credentials, firestore, storage
11
 
12
+
13
+ # ================================================================
14
+ # Firebase Initialization
15
+ # ================================================================
16
+
17
  if not firebase_admin._apps:
18
  firebase_key = os.getenv("FIREBASE_SERVICE_ACCOUNT_JSON")
19
  if firebase_key:
 
32
  _whisper_model = None
33
  _nlp = None
34
 
35
+ # ================================================================
36
+ # Helper Fuctions for creating transcripts from audio files (.wav)
37
+ # ================================================================
38
+
39
  def _load_whisper():
40
  global _whisper_model
41
  if _whisper_model is None:
 
42
  print("Loading Whisper model...")
43
  _whisper_model = whisper.load_model("medium")
44
  return _whisper_model
 
46
  def _load_spacy():
47
  global _nlp
48
  if _nlp is None:
 
49
  print("Loading spaCy model...")
50
  _nlp = spacy.load("en_core_web_sm")
51
  return _nlp
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  def text_to_cha_lines(text):
54
  text = text.replace("...", " ELLIPSIS_MARKER ")
55
  sentences = []
 
85
  return " ".join(mor_tokens), " ".join(gra_tokens)
86
 
87
 
88
+ # ================================================================
89
+ # Helper Functions for audio data pulling
90
+ # ================================================================
91
+
92
+ def extract_audio_features(wav_path):
93
+ """Extract MFCC, ZCR, RMS from a single .wav file"""
94
+ try:
95
+ y, sr = librosa.load(wav_path, sr=None)
96
+ mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
97
+ mfccs_mean = np.mean(mfccs, axis=1)
98
+ mfccs_std = np.std(mfccs, axis=1)
99
+ zcr = np.mean(librosa.feature.zero_crossing_rate(y))
100
+ rms = np.mean(librosa.feature.rms(y=y))
101
+ return np.concatenate([mfccs_mean, mfccs_std, [zcr, rms]])
102
+ except Exception as e:
103
+ print(f"Audio extraction failed for {wav_path}: {e}")
104
+ return None
105
+
106
+ def extract_text_features(cha_path):
107
+ """Extract linguistic features from .cha file"""
108
+ try:
109
+ with open(cha_path, 'r', encoding='utf-8') as f:
110
+ lines = f.readlines()
111
+ utterances = [line.strip() for line in lines if line.startswith('*')]
112
+ if not utterances:
113
+ return None
114
+ word_count = sum(len(line.split()) for line in utterances)
115
+ sentence_count = len(utterances)
116
+ avg_words_per_sentence = word_count / sentence_count if sentence_count > 0 else 0
117
+ unique_words = len(set(word.lower().strip('.,!?;:') for line in utterances for word in line.split()))
118
+ lexical_diversity = unique_words / word_count if word_count > 0 else 0
119
+ pause_count = sum(line.count('<') + line.count('...') for line in utterances)
120
+ return np.array([word_count, sentence_count, avg_words_per_sentence, lexical_diversity, pause_count])
121
+ except Exception as e:
122
+ print(f"Text extraction failed for {cha_path}: {e}")
123
+ return None
124
+
125
+
126
+ # ================================================================
127
+ # Data loading functions
128
+ # ================================================================
129
+
130
  def pull_audio(docID: str):
131
  """
132
  Downloads .wav from Firebase Storage, fetches age/gender from Firestore,
 
269
 
270
  def pull_lifestyle(document_id: str):
271
  """Fetches lifestyle data from Firestore."""
272
+ # Just gets all the fields and there data from firebase
273
  try:
274
  doc_ref = db.collection("Questionnaire").document(document_id)
275
  doc = doc_ref.get()
modelAudiov7_ML.pk1 DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:5df642a21b54b684a1f5bdb50f687329652be0e35763c260338ec34b5b46c202
3
- size 123297
 
 
 
 
modelLifestyleV2_ML.pk1 DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:d77641897ca44426851e0fba91af99d96e68795b487e473978fb7048a5dae880
3
- size 436054
 
 
 
 
webscraper.py CHANGED
@@ -1,9 +1,13 @@
1
- # webscraper.py
2
  import requests
3
  from bs4 import BeautifulSoup
4
  import time
5
  import os
6
 
 
 
 
 
 
7
  def clean_text(text):
8
  return " ".join(text.split())
9
 
@@ -48,9 +52,10 @@ def scrape_article_links(base_url: str, max_links=10) -> list[dict]:
48
  return items
49
 
50
  except Exception as e:
51
- print(f"⚠️ scrape_article_links({base_url}) failed: {e}")
52
  return []
53
 
 
54
  def scrape_full_text(url: str) -> str:
55
  """Return raw full article text (no summarization)."""
56
  headers = {
@@ -72,9 +77,15 @@ def scrape_full_text(url: str) -> str:
72
  return text[:8000] # Cap for token efficiency
73
 
74
  except Exception as e:
75
- print(f"⚠️ scrape_full_text({url}) failed: {e}")
76
  return ""
77
 
 
 
 
 
 
 
78
  def get_patient_news_raw(count: int = 5) -> list[dict]:
79
  base_urls = [
80
  "https://www.caregiving.org/?s=dementia",
@@ -92,6 +103,7 @@ def get_patient_news_raw(count: int = 5) -> list[dict]:
92
  articles.append(item)
93
  return articles[:count]
94
 
 
95
  def get_doctor_news_raw(count: int = 5) -> list[dict]:
96
  base_urls = [
97
  "https://www.nature.com/search?q=dementia&order=date",
 
 
1
  import requests
2
  from bs4 import BeautifulSoup
3
  import time
4
  import os
5
 
6
+
7
+ # ================================================================
8
+ # Helper Functions
9
+ # ================================================================
10
+
11
  def clean_text(text):
12
  return " ".join(text.split())
13
 
 
52
  return items
53
 
54
  except Exception as e:
55
+ print(f"scrape_article_links({base_url}) failed: {e}")
56
  return []
57
 
58
+ # Attempts to get the full raw article text
59
  def scrape_full_text(url: str) -> str:
60
  """Return raw full article text (no summarization)."""
61
  headers = {
 
77
  return text[:8000] # Cap for token efficiency
78
 
79
  except Exception as e:
80
+ print(f"scrape_full_text({url}) failed: {e}")
81
  return ""
82
 
83
+
84
+ # ================================================================
85
+ # News webscraping
86
+ # ================================================================
87
+
88
+ # Gets new articles for patients
89
  def get_patient_news_raw(count: int = 5) -> list[dict]:
90
  base_urls = [
91
  "https://www.caregiving.org/?s=dementia",
 
103
  articles.append(item)
104
  return articles[:count]
105
 
106
+ # Gets new articles for doctors
107
  def get_doctor_news_raw(count: int = 5) -> list[dict]:
108
  base_urls = [
109
  "https://www.nature.com/search?q=dementia&order=date",