Spaces:
Runtime error
Runtime error
updated lifestyle crew run logic
Browse files
crew.py
CHANGED
|
@@ -443,43 +443,42 @@ get_news_task_doctor = Task(
|
|
| 443 |
)
|
| 444 |
|
| 445 |
def run_crewai_lifestyle(docID: str) -> dict:
|
| 446 |
-
"""
|
| 447 |
-
Runs the full CrewAI pipeline for lifestyle risk + SHAP + LLM summary.
|
| 448 |
-
Returns a JSON-serializable dict (success/error).
|
| 449 |
-
"""
|
| 450 |
try:
|
| 451 |
-
# Update task with dynamic docID
|
| 452 |
-
risk_assessment_task_lifestyle.description = f"Calculate dementia risk for patient using lifestyle data for ID: {docID}"
|
| 453 |
-
explanation_task_lifestyle.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
|
| 454 |
-
|
| 455 |
-
# Rebuild crew with updated tasks
|
| 456 |
-
crew_lifestyle = Crew(
|
| 457 |
-
agents=[risk_calculator_lifestyle],
|
| 458 |
-
tasks=[risk_assessment_task_lifestyle, explanation_task_lifestyle],
|
| 459 |
-
verbose=True,
|
| 460 |
-
tracing=True
|
| 461 |
-
)
|
| 462 |
-
|
| 463 |
-
# Run
|
| 464 |
-
raw_output = crew_lifestyle.kickoff()
|
| 465 |
-
|
| 466 |
-
# Parse raw_output (usually a string) → try to extract JSON or format
|
| 467 |
-
# Since agents return natural language, we'll wrap it + add structured fields
|
| 468 |
-
# First, get raw model output for structured data
|
| 469 |
lifestyle_data = pull_lifestyle(docID)
|
| 470 |
if not lifestyle_data:
|
| 471 |
return {"status": "error", "message": f"No lifestyle data for {docID}"}
|
| 472 |
|
|
|
|
| 473 |
risk_result = calculate_dementia_risk_lifestyle.run(docID)
|
| 474 |
if risk_result["status"] != "success":
|
| 475 |
return risk_result
|
| 476 |
shap_explanation = explain_dementia_risk_lifestyle.run(docID)
|
| 477 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
return {
|
| 479 |
"status": "success",
|
| 480 |
**risk_result,
|
| 481 |
"shap_explanation": shap_explanation,
|
| 482 |
-
"crew_summary":
|
| 483 |
}
|
| 484 |
|
| 485 |
except Exception as e:
|
|
@@ -499,6 +498,7 @@ def run_crewai_audio(docID: str) -> dict:
|
|
| 499 |
agents=[risk_calculator_audio],
|
| 500 |
tasks=[risk_assessment_task_audio, explanation_task_audio],
|
| 501 |
verbose=True,
|
|
|
|
| 502 |
tracing=True
|
| 503 |
)
|
| 504 |
|
|
|
|
| 443 |
)
|
| 444 |
|
| 445 |
def run_crewai_lifestyle(docID: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
lifestyle_data = pull_lifestyle(docID)
|
| 448 |
if not lifestyle_data:
|
| 449 |
return {"status": "error", "message": f"No lifestyle data for {docID}"}
|
| 450 |
|
| 451 |
+
# Keep manual tool calls — they work
|
| 452 |
risk_result = calculate_dementia_risk_lifestyle.run(docID)
|
| 453 |
if risk_result["status"] != "success":
|
| 454 |
return risk_result
|
| 455 |
shap_explanation = explain_dementia_risk_lifestyle.run(docID)
|
| 456 |
|
| 457 |
+
# Try crew for summary — but *don’t fail* if it errors
|
| 458 |
+
crew_summary = "Summary unavailable (LLM error)."
|
| 459 |
+
try:
|
| 460 |
+
risk_assessment_task_lifestyle.description = f"Calculate dementia risk for patient using lifestyle data for ID: {docID}"
|
| 461 |
+
explanation_task_lifestyle.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
|
| 462 |
+
|
| 463 |
+
crew_lifestyle = Crew(
|
| 464 |
+
agents=[risk_calculator_lifestyle],
|
| 465 |
+
tasks=[risk_assessment_task_lifestyle, explanation_task_lifestyle],
|
| 466 |
+
verbose=False, # reduce noise
|
| 467 |
+
function_calling=False, # avoid tool-call JSON
|
| 468 |
+
prevent_prompt_injection=True
|
| 469 |
+
)
|
| 470 |
+
raw_output = crew_lifestyle.kickoff()
|
| 471 |
+
if raw_output and str(raw_output).strip():
|
| 472 |
+
crew_summary = str(raw_output).strip()
|
| 473 |
+
except Exception as e:
|
| 474 |
+
# Log quietly if needed, but don’t break
|
| 475 |
+
crew_summary = f"Summary failed: {type(e).__name__}"
|
| 476 |
+
|
| 477 |
return {
|
| 478 |
"status": "success",
|
| 479 |
**risk_result,
|
| 480 |
"shap_explanation": shap_explanation,
|
| 481 |
+
"crew_summary": crew_summary
|
| 482 |
}
|
| 483 |
|
| 484 |
except Exception as e:
|
|
|
|
| 498 |
agents=[risk_calculator_audio],
|
| 499 |
tasks=[risk_assessment_task_audio, explanation_task_audio],
|
| 500 |
verbose=True,
|
| 501 |
+
function_calling=False,
|
| 502 |
tracing=True
|
| 503 |
)
|
| 504 |
|