Spaces:
Runtime error
Runtime error
updated with base framework for news agents
Browse files
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title: NMS AI
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.49.1
|
| 8 |
app_file: app.py
|
|
|
|
| 1 |
---
|
| 2 |
+
title: NMS AI DEV
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.49.1
|
| 8 |
app_file: app.py
|
app.py
CHANGED
|
@@ -2,8 +2,7 @@ import os
|
|
| 2 |
import sys
|
| 3 |
sys.path.append(os.path.dirname(__file__))
|
| 4 |
|
| 5 |
-
|
| 6 |
-
from crew import run_crewai_lifestyle, run_crewai_audio
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
|
|
@@ -21,26 +20,69 @@ def assess_audio(docID: str):
|
|
| 21 |
return {"status": "error", "message": str(e)}
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def assess_risk(docID: str, mode: str):
|
| 25 |
if mode == "audio":
|
| 26 |
return assess_audio(docID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
-
return
|
| 29 |
-
|
| 30 |
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
-
gr.Markdown("# 🧠 NMS Dementia Risk Assessment API
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
demo.launch()
|
|
|
|
| 2 |
import sys
|
| 3 |
sys.path.append(os.path.dirname(__file__))
|
| 4 |
|
| 5 |
+
from crew import run_crewai_lifestyle, run_crewai_audio, run_crewai_news_patient, run_crewai_news_doctor
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
|
|
|
|
| 20 |
return {"status": "error", "message": str(e)}
|
| 21 |
|
| 22 |
|
| 23 |
+
def news_patient():
|
| 24 |
+
try:
|
| 25 |
+
return run_crewai_news_patient()
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return {"status": "error", "message": str(e)}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def news_doctor():
|
| 31 |
+
try:
|
| 32 |
+
return run_crewai_news_doctor()
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return {"status": "error", "message": str(e)}
|
| 35 |
+
|
| 36 |
def assess_risk(docID: str, mode: str):
|
| 37 |
if mode == "audio":
|
| 38 |
return assess_audio(docID)
|
| 39 |
+
elif mode == "lifestyle":
|
| 40 |
+
return news_doctor()
|
| 41 |
+
elif mode == "doctornews":
|
| 42 |
+
return news_doctor()
|
| 43 |
+
elif mode == "patientnews":
|
| 44 |
+
return news_patient()
|
| 45 |
else:
|
| 46 |
+
return {"status": "error", "message": f"option '{mode}' not found"}
|
|
|
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("# 🧠 NMS Dementia Risk Assessment API - Development Zone")
|
| 50 |
+
|
| 51 |
+
with gr.Tabs():
|
| 52 |
+
# ====== Predictions Tab ======
|
| 53 |
+
with gr.Tab("Predictions"):
|
| 54 |
+
gr.Markdown("### 🔮 Risk Assessment (Lifestyle or Audio)")
|
| 55 |
+
mode_pred = gr.Radio(
|
| 56 |
+
choices=["lifestyle", "audio"],
|
| 57 |
+
value="lifestyle",
|
| 58 |
+
label="Assessment Type"
|
| 59 |
+
)
|
| 60 |
+
doc_id_pred = gr.Textbox(
|
| 61 |
+
label="Patient Document ID",
|
| 62 |
+
placeholder="e.g., umWSG13rLSfHqqJlq1WluPzjLB93"
|
| 63 |
+
)
|
| 64 |
+
output_pred = gr.JSON()
|
| 65 |
+
btn_pred = gr.Button("Run Assessment")
|
| 66 |
+
btn_pred.click(
|
| 67 |
+
fn=lambda doc_id, mode: assess_lifestyle(doc_id) if mode == "lifestyle" else assess_audio(doc_id),
|
| 68 |
+
inputs=[doc_id_pred, mode_pred],
|
| 69 |
+
outputs=output_pred
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# ====== News Tab ======
|
| 73 |
+
with gr.Tab("News"):
|
| 74 |
+
gr.Markdown("### 📰 Dementia News for Different Audiences")
|
| 75 |
+
mode_news = gr.Radio(
|
| 76 |
+
choices=["patientnews", "doctornews"],
|
| 77 |
+
value="patientnews",
|
| 78 |
+
label="News Audience"
|
| 79 |
+
)
|
| 80 |
+
output_news = gr.JSON()
|
| 81 |
+
btn_news = gr.Button("Fetch News")
|
| 82 |
+
btn_news.click(
|
| 83 |
+
fn=lambda mode: news_patient() if mode == "patientnews" else news_doctor(),
|
| 84 |
+
inputs=[mode_news],
|
| 85 |
+
outputs=output_news
|
| 86 |
+
)
|
| 87 |
|
| 88 |
demo.launch()
|
crew.py
CHANGED
|
@@ -359,9 +359,10 @@ def explain_dementia_risk_lifestyle(docID: str) -> str:
|
|
| 359 |
return f"SHAP explanation failed: {str(e)}"
|
| 360 |
|
| 361 |
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
)
|
|
|
|
| 365 |
|
| 366 |
# Define agents for Alzheimer's detection system
|
| 367 |
risk_calculator_audio = Agent(
|
|
@@ -369,7 +370,7 @@ risk_calculator_audio = Agent(
|
|
| 369 |
goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
|
| 370 |
backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
|
| 371 |
verbose=True,
|
| 372 |
-
llm=
|
| 373 |
tools=[calculate_dementia_risk_audio, explain_dementia_risk_audio]
|
| 374 |
)
|
| 375 |
|
|
@@ -378,10 +379,27 @@ risk_calculator_lifestyle = Agent(
|
|
| 378 |
goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
|
| 379 |
backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
|
| 380 |
verbose=True,
|
| 381 |
-
llm=
|
| 382 |
tools=[calculate_dementia_risk_lifestyle, explain_dementia_risk_lifestyle]
|
| 383 |
)
|
| 384 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
# Define tasks for agents
|
| 387 |
risk_assessment_task_audio = Task(
|
|
@@ -408,11 +426,16 @@ explanation_task_lifestyle = Task(
|
|
| 408 |
agent=risk_calculator_lifestyle
|
| 409 |
)
|
| 410 |
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
)
|
| 417 |
|
| 418 |
def run_crewai_lifestyle(docID: str) -> dict:
|
|
@@ -460,7 +483,10 @@ def run_crewai_lifestyle(docID: str) -> dict:
|
|
| 460 |
|
| 461 |
|
| 462 |
def run_crewai_audio(docID: str) -> dict:
|
| 463 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 464 |
try:
|
| 465 |
risk_assessment_task_audio.description = f"Calculate dementia risk for patient using audio data for ID: {docID}"
|
| 466 |
explanation_task_audio.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
|
|
@@ -490,10 +516,8 @@ def run_crewai_audio(docID: str) -> dict:
|
|
| 490 |
except Exception as e:
|
| 491 |
return {"status": "error", "message": str(e)}
|
| 492 |
|
|
|
|
|
|
|
| 493 |
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
print("Starting Alzheimer's Detection Crew with Llama3.1:8b...")
|
| 497 |
-
result = crew.kickoff()
|
| 498 |
-
print("\n=== FINAL REPORT ===\n")
|
| 499 |
-
print(result)
|
|
|
|
| 359 |
return f"SHAP explanation failed: {str(e)}"
|
| 360 |
|
| 361 |
|
| 362 |
+
|
| 363 |
+
# Load 2 different llms for different purposes.
|
| 364 |
+
llm_predict = LLM(model="huggingface/openai/gpt-oss-120b")
|
| 365 |
+
llm_news = LLM(model="huggingface/meta-llama/Llama-3.1-8B")
|
| 366 |
|
| 367 |
# Define agents for Alzheimer's detection system
|
| 368 |
risk_calculator_audio = Agent(
|
|
|
|
| 370 |
goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
|
| 371 |
backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
|
| 372 |
verbose=True,
|
| 373 |
+
llm=llm_predict,
|
| 374 |
tools=[calculate_dementia_risk_audio, explain_dementia_risk_audio]
|
| 375 |
)
|
| 376 |
|
|
|
|
| 379 |
goal="Calculate dementia risk for a specific patient using ML model and LLM interpretation",
|
| 380 |
backstory="You combine machine learning predictions with natural language explanations to provide personalized risk assessments.",
|
| 381 |
verbose=True,
|
| 382 |
+
llm=llm_predict,
|
| 383 |
tools=[calculate_dementia_risk_lifestyle, explain_dementia_risk_lifestyle]
|
| 384 |
)
|
| 385 |
|
| 386 |
+
patient_news = Agent(
|
| 387 |
+
role="Patient News Agent",
|
| 388 |
+
goal="Gather dementia-related news articles that are helpful and easy to understand for patients and families",
|
| 389 |
+
backstory="You find and share clear, trustworthy news about dementia—like new treatments, daily care tips, and support resources—written for patients and caregivers.",
|
| 390 |
+
verbose=True,
|
| 391 |
+
llm=llm_news,
|
| 392 |
+
tools=[calculate_dementia_risk_lifestyle, explain_dementia_risk_lifestyle]
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
doctor_news = Agent(
|
| 396 |
+
role="Doctor News Agent",
|
| 397 |
+
goal="Gather dementia-related news and research updates that are useful for doctors and medical professionals",
|
| 398 |
+
backstory="You find and summarize the latest clinical studies, guidelines, and expert insights on dementia—focused on diagnosis, treatment, and research for healthcare providers.",
|
| 399 |
+
verbose=True,
|
| 400 |
+
llm=llm_news,
|
| 401 |
+
tools=[calculate_dementia_risk_lifestyle, explain_dementia_risk_lifestyle]
|
| 402 |
+
)
|
| 403 |
|
| 404 |
# Define tasks for agents
|
| 405 |
risk_assessment_task_audio = Task(
|
|
|
|
| 426 |
agent=risk_calculator_lifestyle
|
| 427 |
)
|
| 428 |
|
| 429 |
+
get_news_task_patient = Task(
|
| 430 |
+
description="Find recent dementia-related news articles helpful for patients and families.",
|
| 431 |
+
expected_output="5 article titles, sources, short summaries (2-3 sentence), and links — written in plain language.",
|
| 432 |
+
agent=patient_news
|
| 433 |
+
)
|
| 434 |
+
|
| 435 |
+
get_news_task_doctor = Task(
|
| 436 |
+
description="Find recent dementia-related news and research updates useful for doctors.",
|
| 437 |
+
expected_output="5 article titles, sources, short summaries (2-3 sentences), and links — focused on clinical or research relevance.",
|
| 438 |
+
agent=doctor_news
|
| 439 |
)
|
| 440 |
|
| 441 |
def run_crewai_lifestyle(docID: str) -> dict:
|
|
|
|
| 483 |
|
| 484 |
|
| 485 |
def run_crewai_audio(docID: str) -> dict:
|
| 486 |
+
"""
|
| 487 |
+
Runs the full CrewAI pipeline for audio risk + SHAP + LLM summary.
|
| 488 |
+
Returns a JSON-serializable dict (success/error).
|
| 489 |
+
"""
|
| 490 |
try:
|
| 491 |
risk_assessment_task_audio.description = f"Calculate dementia risk for patient using audio data for ID: {docID}"
|
| 492 |
explanation_task_audio.description = f"Explain the dementia risk prediction for patient {docID} using SHAP"
|
|
|
|
| 516 |
except Exception as e:
|
| 517 |
return {"status": "error", "message": str(e)}
|
| 518 |
|
| 519 |
+
def run_crewai_news_patient() -> dict:
|
| 520 |
+
return {"type": "placeholder", "value": "jharden13 is tha GOAT..........patient"}
|
| 521 |
|
| 522 |
+
def run_crewai_news_doctor() ->dict:
|
| 523 |
+
return {"type": "placeholder", "value": "jharden13 is tha GOAT..........doctor"}
|
|
|
|
|
|
|
|
|
|
|
|