Spaces:
Runtime error
Runtime error
updated new gathering logics
Browse files
crew.py
CHANGED
|
@@ -299,10 +299,9 @@ def explain_dementia_risk_lifestyle(docID: str) -> str:
|
|
| 299 |
return f"SHAP explanation failed: {str(e)}"
|
| 300 |
|
| 301 |
@tool("gather_news_patient")
|
| 302 |
-
def gather_news_patient(count: int = 5) ->
|
| 303 |
"""
|
| 304 |
-
Gathers recent dementia news articles for patients. Returns
|
| 305 |
-
Args: count (int): Number of articles to fetch (max 10).
|
| 306 |
"""
|
| 307 |
from webscraper import search_dementia_news, scrape_article_content
|
| 308 |
articles = search_dementia_news(audience="patient", max_results=min(count, 10))
|
|
@@ -314,18 +313,14 @@ def gather_news_patient(count: int = 5) -> str:
|
|
| 314 |
clean_articles.append({
|
| 315 |
"title": art["title"],
|
| 316 |
"url": art["url"],
|
| 317 |
-
"content": content[:3000]
|
| 318 |
})
|
| 319 |
except Exception:
|
| 320 |
continue
|
| 321 |
-
return
|
| 322 |
-
|
| 323 |
|
| 324 |
@tool("gather_news_doctor")
|
| 325 |
-
def gather_news_doctor(count: int = 5) ->
|
| 326 |
-
"""
|
| 327 |
-
Gathers recent dementia news articles for doctors. Returns JSON-like string of raw results.
|
| 328 |
-
"""
|
| 329 |
from webscraper import search_dementia_news, scrape_article_content
|
| 330 |
articles = search_dementia_news(audience="doctor", max_results=min(count, 10))
|
| 331 |
clean_articles = []
|
|
@@ -340,7 +335,7 @@ def gather_news_doctor(count: int = 5) -> str:
|
|
| 340 |
})
|
| 341 |
except Exception:
|
| 342 |
continue
|
| 343 |
-
return
|
| 344 |
|
| 345 |
# Load 2 different llms for different purposes.
|
| 346 |
llm_predict = LLM(model="huggingface/openai/gpt-oss-120b")
|
|
@@ -410,26 +405,38 @@ explanation_task_lifestyle = Task(
|
|
| 410 |
|
| 411 |
get_news_task_patient = Task(
|
| 412 |
description=(
|
| 413 |
-
"1. Use gather_news_patient tool to fetch recent dementia news
|
| 414 |
-
"2.
|
| 415 |
-
"3.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
),
|
| 417 |
expected_output=(
|
| 418 |
"A JSON object with keys: 'titles', 'snippets', 'urls'.\n"
|
| 419 |
-
"- 'titles': list of article titles (strings).\n"
|
| 420 |
"- 'snippets': list of 2β3 line plain-language summaries (strings), one per article.\n"
|
| 421 |
"- 'urls': list of original article URLs (strings).\n"
|
| 422 |
"All lists must be same length and aligned by index.\n"
|
| 423 |
-
"Return ONLY valid JSON β no
|
| 424 |
),
|
| 425 |
agent=patient_news
|
| 426 |
)
|
| 427 |
|
| 428 |
get_news_task_doctor = Task(
|
| 429 |
description=(
|
| 430 |
-
"1. Use gather_news_doctor tool to fetch recent dementia
|
| 431 |
-
"2.
|
| 432 |
-
"3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
),
|
| 434 |
expected_output=(
|
| 435 |
"A JSON object with keys: 'titles', 'snippets', 'urls'.\n"
|
|
@@ -679,16 +686,10 @@ def run_crewai_audio(docID: str) -> dict:
|
|
| 679 |
|
| 680 |
def run_crewai_news_patient(count: int = 5) -> dict:
|
| 681 |
try:
|
| 682 |
-
#
|
| 683 |
-
desc = get_news_task_patient.description.replace(
|
| 684 |
-
"gather_news_patient tool",
|
| 685 |
-
f"gather_news_patient tool with count={count}"
|
| 686 |
-
)
|
| 687 |
-
task = Task(description=desc, expected_output=get_news_task_patient.expected_output, agent=patient_news)
|
| 688 |
-
|
| 689 |
crew = Crew(
|
| 690 |
agents=[patient_news],
|
| 691 |
-
tasks=[
|
| 692 |
verbose=True,
|
| 693 |
function_calling=False
|
| 694 |
)
|
|
@@ -701,7 +702,7 @@ def run_crewai_news_patient(count: int = 5) -> dict:
|
|
| 701 |
raise ValueError(f"Missing or invalid '{key}' in result")
|
| 702 |
if not (len(result["titles"]) == len(result["snippets"]) == len(result["urls"])):
|
| 703 |
raise ValueError("Mismatched list lengths")
|
| 704 |
-
|
| 705 |
return {"status": "success", **result}
|
| 706 |
|
| 707 |
except Exception as e:
|
|
@@ -709,29 +710,38 @@ def run_crewai_news_patient(count: int = 5) -> dict:
|
|
| 709 |
|
| 710 |
def run_crewai_news_doctor(count: int = 5) -> dict:
|
| 711 |
try:
|
| 712 |
-
|
| 713 |
-
"gather_news_doctor tool",
|
| 714 |
-
f"gather_news_doctor tool with count={count}"
|
| 715 |
-
)
|
| 716 |
-
task = Task(description=desc, expected_output=get_news_task_doctor.expected_output, agent=doctor_news)
|
| 717 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
crew = Crew(
|
| 719 |
agents=[doctor_news],
|
| 720 |
-
tasks=[
|
| 721 |
verbose=True,
|
| 722 |
function_calling=False
|
| 723 |
)
|
| 724 |
-
|
| 725 |
|
| 726 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 727 |
for key in ["titles", "snippets", "urls"]:
|
| 728 |
if key not in result or not isinstance(result[key], list):
|
| 729 |
-
raise ValueError(f"Missing or invalid '{key}'")
|
| 730 |
if not (len(result["titles"]) == len(result["snippets"]) == len(result["urls"])):
|
| 731 |
-
raise ValueError("Mismatched list lengths")
|
| 732 |
|
| 733 |
return {"status": "success", **result}
|
| 734 |
|
| 735 |
except Exception as e:
|
| 736 |
return {"status": "error", "message": str(e)}
|
| 737 |
-
return {"type": "placeholder", "value": "jharden13 is tha GOAT..........doctor"}
|
|
|
|
| 299 |
return f"SHAP explanation failed: {str(e)}"
|
| 300 |
|
| 301 |
@tool("gather_news_patient")
|
| 302 |
+
def gather_news_patient(count: int = 5) -> dict: # β Change return type to dict
|
| 303 |
"""
|
| 304 |
+
Gathers recent dementia news articles for patients. Returns structured dict.
|
|
|
|
| 305 |
"""
|
| 306 |
from webscraper import search_dementia_news, scrape_article_content
|
| 307 |
articles = search_dementia_news(audience="patient", max_results=min(count, 10))
|
|
|
|
| 313 |
clean_articles.append({
|
| 314 |
"title": art["title"],
|
| 315 |
"url": art["url"],
|
| 316 |
+
"content": content[:3000] # Keep full content for LLM to summarize
|
| 317 |
})
|
| 318 |
except Exception:
|
| 319 |
continue
|
| 320 |
+
return {"articles": clean_articles} # β Return dict, not JSON string
|
|
|
|
| 321 |
|
| 322 |
@tool("gather_news_doctor")
|
| 323 |
+
def gather_news_doctor(count: int = 5) -> dict: # β Same here
|
|
|
|
|
|
|
|
|
|
| 324 |
from webscraper import search_dementia_news, scrape_article_content
|
| 325 |
articles = search_dementia_news(audience="doctor", max_results=min(count, 10))
|
| 326 |
clean_articles = []
|
|
|
|
| 335 |
})
|
| 336 |
except Exception:
|
| 337 |
continue
|
| 338 |
+
return {"articles": clean_articles}
|
| 339 |
|
| 340 |
# Load 2 different llms for different purposes.
|
| 341 |
llm_predict = LLM(model="huggingface/openai/gpt-oss-120b")
|
|
|
|
| 405 |
|
| 406 |
get_news_task_patient = Task(
|
| 407 |
description=(
|
| 408 |
+
"1. Use the 'gather_news_patient' tool to fetch recent dementia news articles.\n"
|
| 409 |
+
"2. You will receive a list of articles, each with 'title', 'url', and 'content'.\n"
|
| 410 |
+
"3. For EACH article, read its 'content' field and generate a concise 2β3 line summary that highlights key takeaways for patients/families.\n"
|
| 411 |
+
"4. Ensure summaries are empathetic, jargon-free, and actionable.\n"
|
| 412 |
+
"5. Return a JSON object with keys: 'titles', 'snippets', 'urls' β matching the order of the fetched articles.\n"
|
| 413 |
+
"6. DO NOT invent titles or URLs β use only what was returned by the tool.\n"
|
| 414 |
+
"7. Return ONLY valid JSON β no markdown, no extra text."
|
| 415 |
),
|
| 416 |
expected_output=(
|
| 417 |
"A JSON object with keys: 'titles', 'snippets', 'urls'.\n"
|
| 418 |
+
"- 'titles': list of original article titles (strings).\n"
|
| 419 |
"- 'snippets': list of 2β3 line plain-language summaries (strings), one per article.\n"
|
| 420 |
"- 'urls': list of original article URLs (strings).\n"
|
| 421 |
"All lists must be same length and aligned by index.\n"
|
| 422 |
+
"Return ONLY valid JSON β no extra text."
|
| 423 |
),
|
| 424 |
agent=patient_news
|
| 425 |
)
|
| 426 |
|
| 427 |
get_news_task_doctor = Task(
|
| 428 |
description=(
|
| 429 |
+
"1. Use the 'gather_news_doctor' tool to fetch recent dementia-related articles for medical professionals.\n"
|
| 430 |
+
"2. You will receive a list of articles, each with 'title', 'url', and 'content' (scraped full text).\n"
|
| 431 |
+
"3. For EACH article, read its 'content' field and generate a concise 2β3 line summary.\n"
|
| 432 |
+
"4. Summaries must be clinical, evidence-focused, and highlight:\n"
|
| 433 |
+
" - Trial design/outcomes (if applicable)\n"
|
| 434 |
+
" - Diagnostic/therapeutic implications\n"
|
| 435 |
+
" - Guideline updates or expert consensus changes\n"
|
| 436 |
+
" - Biomarker or mechanistic insights\n"
|
| 437 |
+
"5. DO NOT invent titles, URLs, or content β use only what the tool returns.\n"
|
| 438 |
+
"6. Return a JSON object with keys: 'titles', 'snippets', 'urls', in matching order.\n"
|
| 439 |
+
"7. Return ONLY valid JSON β no markdown, no commentary."
|
| 440 |
),
|
| 441 |
expected_output=(
|
| 442 |
"A JSON object with keys: 'titles', 'snippets', 'urls'.\n"
|
|
|
|
| 686 |
|
| 687 |
def run_crewai_news_patient(count: int = 5) -> dict:
|
| 688 |
try:
|
| 689 |
+
# Just use the pre-defined task β it already knows to use gather_news_patient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
crew = Crew(
|
| 691 |
agents=[patient_news],
|
| 692 |
+
tasks=[get_news_task_patient],
|
| 693 |
verbose=True,
|
| 694 |
function_calling=False
|
| 695 |
)
|
|
|
|
| 702 |
raise ValueError(f"Missing or invalid '{key}' in result")
|
| 703 |
if not (len(result["titles"]) == len(result["snippets"]) == len(result["urls"])):
|
| 704 |
raise ValueError("Mismatched list lengths")
|
| 705 |
+
|
| 706 |
return {"status": "success", **result}
|
| 707 |
|
| 708 |
except Exception as e:
|
|
|
|
| 710 |
|
| 711 |
def run_crewai_news_doctor(count: int = 5) -> dict:
|
| 712 |
try:
|
| 713 |
+
original_gather = doctor_news.tools[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 714 |
|
| 715 |
+
# Wrap tool to inject count
|
| 716 |
+
def gather_with_count(query=None):
|
| 717 |
+
return gather_news_doctor(count=count)
|
| 718 |
+
|
| 719 |
+
# Replace tool temporarily
|
| 720 |
+
doctor_news.tools = [gather_with_count]
|
| 721 |
+
|
| 722 |
+
# Run crew with static task
|
| 723 |
crew = Crew(
|
| 724 |
agents=[doctor_news],
|
| 725 |
+
tasks=[get_news_task_doctor],
|
| 726 |
verbose=True,
|
| 727 |
function_calling=False
|
| 728 |
)
|
| 729 |
+
raw_output = str(crew.kickoff()).strip()
|
| 730 |
|
| 731 |
+
# Restore original tool
|
| 732 |
+
doctor_news.tools = [original_gather]
|
| 733 |
+
|
| 734 |
+
# Parse JSON
|
| 735 |
+
result = _parse_json_output(raw_output)
|
| 736 |
+
|
| 737 |
+
# Validate structure
|
| 738 |
for key in ["titles", "snippets", "urls"]:
|
| 739 |
if key not in result or not isinstance(result[key], list):
|
| 740 |
+
raise ValueError(f"Missing or invalid '{key}' in result")
|
| 741 |
if not (len(result["titles"]) == len(result["snippets"]) == len(result["urls"])):
|
| 742 |
+
raise ValueError("Mismatched list lengths in doctor news result")
|
| 743 |
|
| 744 |
return {"status": "success", **result}
|
| 745 |
|
| 746 |
except Exception as e:
|
| 747 |
return {"status": "error", "message": str(e)}
|
|
|