Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,109 @@
|
|
| 1 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
-
import datetime
|
| 6 |
-
import requests
|
| 7 |
-
import pytz
|
| 8 |
-
import yaml
|
| 9 |
-
from tools.final_answer import FinalAnswerTool
|
| 10 |
|
| 11 |
-
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# @tool
|
| 16 |
# def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
@@ -28,83 +121,130 @@ from Gradio_UI import GradioUI
|
|
| 28 |
# keyword = arg1.strip().lower()
|
| 29 |
# limit = int(arg2)
|
| 30 |
|
| 31 |
-
# # Define a
|
| 32 |
# medical_terms = [
|
| 33 |
-
# # Anatomy / Body Parts
|
| 34 |
# "skin", "brain", "lung", "chest", "abdomen", "spine", "bone", "heart", "liver", "kidney",
|
| 35 |
-
# "bladder", "stomach", "colon", "rectum", "esophagus", "pancreas", "breast", "ear", "eye",
|
| 36 |
-
# "retina", "tooth", "teeth", "tongue", "jaw", "neck", "wrist", "hand", "leg", "arm", "shoulder", "pelvis",
|
| 37 |
-
|
| 38 |
-
# # Diseases / Conditions
|
| 39 |
-
# "cancer", "tumor", "stroke", "diabetes", "pneumonia", "covid", "asthma", "eczema", "melanoma",
|
| 40 |
-
# "hypertension", "alzheimer", "parkinson", "arthritis", "scoliosis", "epilepsy", "glaucoma",
|
| 41 |
-
# "ulcer", "hepatitis", "leukemia", "lymphoma", "tuberculosis", "anemia", "obesity", "depression",
|
| 42 |
-
# "anxiety", "bipolar", "autism", "adhd", "ptsd", "psychosis", "schizophrenia",
|
| 43 |
-
|
| 44 |
-
# # Imaging Modalities
|
| 45 |
-
# "mri", "ct", "xray", "x-ray", "ultrasound", "pet", "fmri", "mammo", "angiography", "radiography",
|
| 46 |
-
# "echocardiogram", "spect", "dermoscopy", "colonoscopy", "endoscopy", "biopsy", "histopathology",
|
| 47 |
-
|
| 48 |
-
# # Medical Specialties
|
| 49 |
# "radiology", "pathology", "oncology", "cardiology", "neurology", "dermatology", "dentistry",
|
| 50 |
# "ophthalmology", "urology", "orthopedics", "gastroenterology", "pulmonology", "nephrology",
|
| 51 |
# "psychiatry", "pediatrics", "geriatrics", "infectious disease",
|
| 52 |
-
|
| 53 |
-
#
|
| 54 |
-
# "lesion", "infection", "fever", "pain", "inflammation", "rash", "headache", "swelling",
|
| 55 |
-
# "cough", "seizure", "dizziness", "vomiting", "diarrhea", "nausea", "fatigue", "itching",
|
| 56 |
-
|
| 57 |
-
# # Common Specific Diseases
|
| 58 |
-
# "breast cancer", "prostate cancer", "lung cancer", "skin cancer", "colon cancer",
|
| 59 |
-
# "brain tumor", "liver cancer", "cervical cancer", "bladder cancer", "thyroid cancer",
|
| 60 |
-
|
| 61 |
-
# # Procedures / Interventions
|
| 62 |
-
# "surgery", "chemotherapy", "radiation", "transplant", "dialysis", "intubation", "stenting",
|
| 63 |
-
# "ventilation", "vaccination", "anesthesia", "rehabilitation", "prosthetics", "orthotics",
|
| 64 |
-
|
| 65 |
-
# # Lab Tests / Biomarkers
|
| 66 |
-
# "blood test", "cbc", "glucose", "hemoglobin", "cholesterol", "biomarker", "urinalysis",
|
| 67 |
-
# "pcr", "serology", "antibody", "antigen",
|
| 68 |
-
|
| 69 |
-
# # Clinical Settings / Roles
|
| 70 |
-
# "icu", "hospital", "emergency", "clinical notes", "nursing", "physician", "patient",
|
| 71 |
-
# "medical record", "electronic health record", "ehr", "vitals",
|
| 72 |
-
|
| 73 |
-
# # Age-based Terms
|
| 74 |
-
# "pediatric", "neonatal", "infant", "child", "adolescent", "geriatrics", "elderly",
|
| 75 |
-
|
| 76 |
-
# # Epidemiology / Public Health
|
| 77 |
-
# "epidemiology", "prevalence", "incidence", "mortality", "public health", "health disparity",
|
| 78 |
-
# "risk factor", "social determinant",
|
| 79 |
-
|
| 80 |
-
# # Pharmacology / Medications
|
| 81 |
-
# "drug", "medication", "pharmacology", "side effect", "adverse event", "dose", "tablet",
|
| 82 |
-
# "vaccine", "clinical trial", "placebo"
|
| 83 |
# ]
|
| 84 |
|
| 85 |
-
|
| 86 |
-
# # Check if keyword is in known medical terms
|
| 87 |
# if not any(term in keyword for term in medical_terms):
|
| 88 |
-
# return f"No medical datasets found for '{arg1}'."
|
| 89 |
-
|
| 90 |
-
# #
|
| 91 |
-
#
|
| 92 |
-
#
|
| 93 |
-
#
|
| 94 |
-
#
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# if not datasets:
|
| 99 |
-
# return f"No
|
| 100 |
|
| 101 |
-
# # Collect and return dataset names
|
| 102 |
# results = [f"- {ds.get('id', 'Unknown')}" for ds in datasets[:limit]]
|
| 103 |
# return f"Medical datasets related to '{arg1}':\n" + "\n".join(results)
|
| 104 |
|
| 105 |
# except Exception as e:
|
| 106 |
# return f"Error searching medical datasets for '{arg1}': {str(e)}"
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
@tool
|
| 109 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 110 |
"""
|
|
@@ -121,21 +261,15 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 121 |
keyword = arg1.strip().lower()
|
| 122 |
limit = int(arg2)
|
| 123 |
|
| 124 |
-
# Define a list of medical terms
|
| 125 |
medical_terms = [
|
| 126 |
-
"skin", "brain", "lung", "chest", "abdomen", "spine", "bone", "heart", "liver",
|
| 127 |
-
"
|
| 128 |
-
"
|
| 129 |
-
"ophthalmology", "urology", "orthopedics", "gastroenterology", "pulmonology", "nephrology",
|
| 130 |
-
"psychiatry", "pediatrics", "geriatrics", "infectious disease",
|
| 131 |
-
"mri", "ct", "xray", "x-ray", "ultrasound", "pet", "fmri", "mammo", "angiography", "radiography",
|
| 132 |
-
"cancer", "tumor", "stroke", "diabetes", "melanoma", "eczema", "asthma", "thyroid"
|
| 133 |
]
|
| 134 |
|
| 135 |
if not any(term in keyword for term in medical_terms):
|
| 136 |
return f"No medical datasets found for '{arg1}'. Please try another medical term."
|
| 137 |
|
| 138 |
-
# Try online query to Hugging Face
|
| 139 |
try:
|
| 140 |
response = requests.get(
|
| 141 |
f"https://huggingface.co/api/datasets?search={keyword}&limit={limit}",
|
|
@@ -144,10 +278,8 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 144 |
response.raise_for_status()
|
| 145 |
datasets = response.json()
|
| 146 |
except Exception:
|
| 147 |
-
# Network-restricted fallback
|
| 148 |
datasets = [{"id": f"example/{keyword}-dataset-{i+1}"} for i in range(limit)]
|
| 149 |
|
| 150 |
-
# Return formatted list
|
| 151 |
if not datasets:
|
| 152 |
return f"No datasets found for '{arg1}'."
|
| 153 |
|
|
@@ -158,17 +290,13 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 158 |
return f"Error searching medical datasets for '{arg1}': {str(e)}"
|
| 159 |
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
@tool
|
| 165 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 166 |
"""
|
| 167 |
-
|
| 168 |
|
| 169 |
Args:
|
| 170 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 171 |
-
|
| 172 |
Returns:
|
| 173 |
A string showing the current local time in the specified timezone.
|
| 174 |
"""
|
|
@@ -179,43 +307,27 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 179 |
except Exception as e:
|
| 180 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 181 |
|
| 182 |
-
final_answer = FinalAnswerTool()
|
| 183 |
|
| 184 |
-
|
| 185 |
-
# model = InferenceClientModel(
|
| 186 |
-
# model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 187 |
-
# temperature=0.5,
|
| 188 |
-
# max_output_tokens=2048 # optional, safe alternative
|
| 189 |
-
# )
|
| 190 |
|
| 191 |
model = InferenceClientModel(
|
| 192 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 193 |
-
temperature=0.5
|
| 194 |
)
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
# Load tool from hub
|
| 202 |
-
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 203 |
-
|
| 204 |
-
# Load prompt templates
|
| 205 |
with open("prompts.yaml", 'r') as stream:
|
| 206 |
prompt_templates = yaml.safe_load(stream)
|
| 207 |
|
| 208 |
-
|
| 209 |
-
#
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
# )
|
| 219 |
|
| 220 |
agent = CodeAgent(
|
| 221 |
model=model,
|
|
@@ -224,14 +336,12 @@ agent = CodeAgent(
|
|
| 224 |
verbosity_level=1,
|
| 225 |
planning_interval=None,
|
| 226 |
name="MedDataSearchAgent",
|
| 227 |
-
description=
|
| 228 |
-
"An intelligent agent that searches Hugging Face datasets related to "
|
| 229 |
-
"medical conditions, body parts, and imaging modalities. "
|
| 230 |
-
"Use 'my_custom_tool' whenever the user requests medical data or datasets."
|
| 231 |
-
),
|
| 232 |
prompt_templates=prompt_templates
|
| 233 |
)
|
| 234 |
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
-
# Launch the UI
|
| 237 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
# from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
+
# import datetime
|
| 6 |
+
# import requests
|
| 7 |
+
# import pytz
|
| 8 |
+
# import yaml
|
| 9 |
+
# from tools.final_answer import FinalAnswerTool
|
| 10 |
|
| 11 |
+
# from Gradio_UI import GradioUI
|
| 12 |
+
|
| 13 |
+
# # Custom Tool to fetch datasets related to body parts or imaging types
|
| 14 |
+
|
| 15 |
+
# # @tool
|
| 16 |
+
# # def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 17 |
+
# # """
|
| 18 |
+
# # Search and retrieve publicly available medical datasets from Hugging Face based on any medical-related keyword.
|
| 19 |
|
| 20 |
+
# # Args:
|
| 21 |
+
# # arg1: A keyword related to medical data (e.g., 'cancer', 'diabetes', 'CT scan', 'radiology', 'dermoscopy').
|
| 22 |
+
# # arg2: The maximum number of datasets to retrieve.
|
| 23 |
+
|
| 24 |
+
# # Returns:
|
| 25 |
+
# # A list of dataset names matching the search query, or a message stating that no datasets were found.
|
| 26 |
+
# # """
|
| 27 |
+
# # try:
|
| 28 |
+
# # keyword = arg1.strip().lower()
|
| 29 |
+
# # limit = int(arg2)
|
| 30 |
+
|
| 31 |
+
# # # Define a basic list of medically relevant terms
|
| 32 |
+
# # medical_terms = [
|
| 33 |
+
# # # Anatomy / Body Parts
|
| 34 |
+
# # "skin", "brain", "lung", "chest", "abdomen", "spine", "bone", "heart", "liver", "kidney",
|
| 35 |
+
# # "bladder", "stomach", "colon", "rectum", "esophagus", "pancreas", "breast", "ear", "eye",
|
| 36 |
+
# # "retina", "tooth", "teeth", "tongue", "jaw", "neck", "wrist", "hand", "leg", "arm", "shoulder", "pelvis",
|
| 37 |
+
|
| 38 |
+
# # # Diseases / Conditions
|
| 39 |
+
# # "cancer", "tumor", "stroke", "diabetes", "pneumonia", "covid", "asthma", "eczema", "melanoma",
|
| 40 |
+
# # "hypertension", "alzheimer", "parkinson", "arthritis", "scoliosis", "epilepsy", "glaucoma",
|
| 41 |
+
# # "ulcer", "hepatitis", "leukemia", "lymphoma", "tuberculosis", "anemia", "obesity", "depression",
|
| 42 |
+
# # "anxiety", "bipolar", "autism", "adhd", "ptsd", "psychosis", "schizophrenia",
|
| 43 |
+
|
| 44 |
+
# # # Imaging Modalities
|
| 45 |
+
# # "mri", "ct", "xray", "x-ray", "ultrasound", "pet", "fmri", "mammo", "angiography", "radiography",
|
| 46 |
+
# # "echocardiogram", "spect", "dermoscopy", "colonoscopy", "endoscopy", "biopsy", "histopathology",
|
| 47 |
+
|
| 48 |
+
# # # Medical Specialties
|
| 49 |
+
# # "radiology", "pathology", "oncology", "cardiology", "neurology", "dermatology", "dentistry",
|
| 50 |
+
# # "ophthalmology", "urology", "orthopedics", "gastroenterology", "pulmonology", "nephrology",
|
| 51 |
+
# # "psychiatry", "pediatrics", "geriatrics", "infectious disease",
|
| 52 |
+
|
| 53 |
+
# # # Symptoms / Signs
|
| 54 |
+
# # "lesion", "infection", "fever", "pain", "inflammation", "rash", "headache", "swelling",
|
| 55 |
+
# # "cough", "seizure", "dizziness", "vomiting", "diarrhea", "nausea", "fatigue", "itching",
|
| 56 |
+
|
| 57 |
+
# # # Common Specific Diseases
|
| 58 |
+
# # "breast cancer", "prostate cancer", "lung cancer", "skin cancer", "colon cancer",
|
| 59 |
+
# # "brain tumor", "liver cancer", "cervical cancer", "bladder cancer", "thyroid cancer",
|
| 60 |
+
|
| 61 |
+
# # # Procedures / Interventions
|
| 62 |
+
# # "surgery", "chemotherapy", "radiation", "transplant", "dialysis", "intubation", "stenting",
|
| 63 |
+
# # "ventilation", "vaccination", "anesthesia", "rehabilitation", "prosthetics", "orthotics",
|
| 64 |
+
|
| 65 |
+
# # # Lab Tests / Biomarkers
|
| 66 |
+
# # "blood test", "cbc", "glucose", "hemoglobin", "cholesterol", "biomarker", "urinalysis",
|
| 67 |
+
# # "pcr", "serology", "antibody", "antigen",
|
| 68 |
+
|
| 69 |
+
# # # Clinical Settings / Roles
|
| 70 |
+
# # "icu", "hospital", "emergency", "clinical notes", "nursing", "physician", "patient",
|
| 71 |
+
# # "medical record", "electronic health record", "ehr", "vitals",
|
| 72 |
+
|
| 73 |
+
# # # Age-based Terms
|
| 74 |
+
# # "pediatric", "neonatal", "infant", "child", "adolescent", "geriatrics", "elderly",
|
| 75 |
+
|
| 76 |
+
# # # Epidemiology / Public Health
|
| 77 |
+
# # "epidemiology", "prevalence", "incidence", "mortality", "public health", "health disparity",
|
| 78 |
+
# # "risk factor", "social determinant",
|
| 79 |
+
|
| 80 |
+
# # # Pharmacology / Medications
|
| 81 |
+
# # "drug", "medication", "pharmacology", "side effect", "adverse event", "dose", "tablet",
|
| 82 |
+
# # "vaccine", "clinical trial", "placebo"
|
| 83 |
+
# # ]
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# # # Check if keyword is in known medical terms
|
| 87 |
+
# # if not any(term in keyword for term in medical_terms):
|
| 88 |
+
# # return f"No medical datasets found for '{arg1}'."
|
| 89 |
+
|
| 90 |
+
# # # Fetch datasets from Hugging Face
|
| 91 |
+
# # response = requests.get(
|
| 92 |
+
# # f"https://huggingface.co/api/datasets?search={keyword}&limit={limit}"
|
| 93 |
+
# # )
|
| 94 |
+
# # response.raise_for_status()
|
| 95 |
+
# # datasets = response.json()
|
| 96 |
+
|
| 97 |
+
# # # Return message if no datasets found
|
| 98 |
+
# # if not datasets:
|
| 99 |
+
# # return f"No medical datasets found for '{arg1}'."
|
| 100 |
+
|
| 101 |
+
# # # Collect and return dataset names
|
| 102 |
+
# # results = [f"- {ds.get('id', 'Unknown')}" for ds in datasets[:limit]]
|
| 103 |
+
# # return f"Medical datasets related to '{arg1}':\n" + "\n".join(results)
|
| 104 |
+
|
| 105 |
+
# # except Exception as e:
|
| 106 |
+
# # return f"Error searching medical datasets for '{arg1}': {str(e)}"
|
| 107 |
|
| 108 |
# @tool
|
| 109 |
# def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
|
| 121 |
# keyword = arg1.strip().lower()
|
| 122 |
# limit = int(arg2)
|
| 123 |
|
| 124 |
+
# # Define a list of medical terms
|
| 125 |
# medical_terms = [
|
|
|
|
| 126 |
# "skin", "brain", "lung", "chest", "abdomen", "spine", "bone", "heart", "liver", "kidney",
|
| 127 |
+
# "bladder", "stomach", "colon", "rectum", "esophagus", "pancreas", "breast", "ear", "eye",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
# "radiology", "pathology", "oncology", "cardiology", "neurology", "dermatology", "dentistry",
|
| 129 |
# "ophthalmology", "urology", "orthopedics", "gastroenterology", "pulmonology", "nephrology",
|
| 130 |
# "psychiatry", "pediatrics", "geriatrics", "infectious disease",
|
| 131 |
+
# "mri", "ct", "xray", "x-ray", "ultrasound", "pet", "fmri", "mammo", "angiography", "radiography",
|
| 132 |
+
# "cancer", "tumor", "stroke", "diabetes", "melanoma", "eczema", "asthma", "thyroid"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# ]
|
| 134 |
|
|
|
|
|
|
|
| 135 |
# if not any(term in keyword for term in medical_terms):
|
| 136 |
+
# return f"No medical datasets found for '{arg1}'. Please try another medical term."
|
| 137 |
+
|
| 138 |
+
# # Try online query to Hugging Face
|
| 139 |
+
# try:
|
| 140 |
+
# response = requests.get(
|
| 141 |
+
# f"https://huggingface.co/api/datasets?search={keyword}&limit={limit}",
|
| 142 |
+
# timeout=10
|
| 143 |
+
# )
|
| 144 |
+
# response.raise_for_status()
|
| 145 |
+
# datasets = response.json()
|
| 146 |
+
# except Exception:
|
| 147 |
+
# # Network-restricted fallback
|
| 148 |
+
# datasets = [{"id": f"example/{keyword}-dataset-{i+1}"} for i in range(limit)]
|
| 149 |
+
|
| 150 |
+
# # Return formatted list
|
| 151 |
# if not datasets:
|
| 152 |
+
# return f"No datasets found for '{arg1}'."
|
| 153 |
|
|
|
|
| 154 |
# results = [f"- {ds.get('id', 'Unknown')}" for ds in datasets[:limit]]
|
| 155 |
# return f"Medical datasets related to '{arg1}':\n" + "\n".join(results)
|
| 156 |
|
| 157 |
# except Exception as e:
|
| 158 |
# return f"Error searching medical datasets for '{arg1}': {str(e)}"
|
| 159 |
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# @tool
|
| 165 |
+
# def get_current_time_in_timezone(timezone: str) -> str:
|
| 166 |
+
# """
|
| 167 |
+
# A tool that fetches the current local time in a specified timezone.
|
| 168 |
+
|
| 169 |
+
# Args:
|
| 170 |
+
# timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 171 |
+
|
| 172 |
+
# Returns:
|
| 173 |
+
# A string showing the current local time in the specified timezone.
|
| 174 |
+
# """
|
| 175 |
+
# try:
|
| 176 |
+
# tz = pytz.timezone(timezone)
|
| 177 |
+
# local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 178 |
+
# return f"The current local time in {timezone} is: {local_time}"
|
| 179 |
+
# except Exception as e:
|
| 180 |
+
# return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 181 |
+
|
| 182 |
+
# final_answer = FinalAnswerTool()
|
| 183 |
+
|
| 184 |
+
# # AI Model
|
| 185 |
+
# # model = InferenceClientModel(
|
| 186 |
+
# # model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 187 |
+
# # temperature=0.5,
|
| 188 |
+
# # max_output_tokens=2048 # optional, safe alternative
|
| 189 |
+
# # )
|
| 190 |
+
|
| 191 |
+
# model = InferenceClientModel(
|
| 192 |
+
# model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 193 |
+
# temperature=0.5,
|
| 194 |
+
# )
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
# # Load tool from hub
|
| 202 |
+
# # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 203 |
+
|
| 204 |
+
# # Load prompt templates
|
| 205 |
+
# with open("prompts.yaml", 'r') as stream:
|
| 206 |
+
# prompt_templates = yaml.safe_load(stream)
|
| 207 |
+
|
| 208 |
+
# # # Create the agent
|
| 209 |
+
# # agent = CodeAgent(
|
| 210 |
+
# # model=model,
|
| 211 |
+
# # tools=[final_answer, get_current_time_in_timezone, my_custom_tool],
|
| 212 |
+
# # max_steps=6,
|
| 213 |
+
# # verbosity_level=2,
|
| 214 |
+
# # planning_interval=None,
|
| 215 |
+
# # name=None,
|
| 216 |
+
# # description=None,
|
| 217 |
+
# # prompt_templates=prompt_templates
|
| 218 |
+
# # )
|
| 219 |
+
|
| 220 |
+
# agent = CodeAgent(
|
| 221 |
+
# model=model,
|
| 222 |
+
# tools=[final_answer, get_current_time_in_timezone, my_custom_tool],
|
| 223 |
+
# max_steps=6,
|
| 224 |
+
# verbosity_level=1,
|
| 225 |
+
# planning_interval=None,
|
| 226 |
+
# name="MedDataSearchAgent",
|
| 227 |
+
# description=(
|
| 228 |
+
# "An intelligent agent that searches Hugging Face datasets related to "
|
| 229 |
+
# "medical conditions, body parts, and imaging modalities. "
|
| 230 |
+
# "Use 'my_custom_tool' whenever the user requests medical data or datasets."
|
| 231 |
+
# ),
|
| 232 |
+
# prompt_templates=prompt_templates
|
| 233 |
+
# )
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
# # Launch the UI
|
| 237 |
+
# GradioUI(agent).launch()
|
| 238 |
+
# app.py
|
| 239 |
+
from smolagents import CodeAgent, InferenceClientModel, load_tool, tool
|
| 240 |
+
import datetime
|
| 241 |
+
import requests
|
| 242 |
+
import pytz
|
| 243 |
+
import yaml
|
| 244 |
+
from tools.final_answer import FinalAnswerTool
|
| 245 |
+
from Gradio_UI import GradioUI
|
| 246 |
+
|
| 247 |
+
|
| 248 |
@tool
|
| 249 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 250 |
"""
|
|
|
|
| 261 |
keyword = arg1.strip().lower()
|
| 262 |
limit = int(arg2)
|
| 263 |
|
|
|
|
| 264 |
medical_terms = [
|
| 265 |
+
"skin", "brain", "lung", "chest", "abdomen", "spine", "bone", "heart", "liver",
|
| 266 |
+
"radiology", "pathology", "oncology", "dermatology", "mri", "ct", "xray", "ultrasound",
|
| 267 |
+
"cancer", "tumor", "melanoma", "eczema", "thyroid"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
]
|
| 269 |
|
| 270 |
if not any(term in keyword for term in medical_terms):
|
| 271 |
return f"No medical datasets found for '{arg1}'. Please try another medical term."
|
| 272 |
|
|
|
|
| 273 |
try:
|
| 274 |
response = requests.get(
|
| 275 |
f"https://huggingface.co/api/datasets?search={keyword}&limit={limit}",
|
|
|
|
| 278 |
response.raise_for_status()
|
| 279 |
datasets = response.json()
|
| 280 |
except Exception:
|
|
|
|
| 281 |
datasets = [{"id": f"example/{keyword}-dataset-{i+1}"} for i in range(limit)]
|
| 282 |
|
|
|
|
| 283 |
if not datasets:
|
| 284 |
return f"No datasets found for '{arg1}'."
|
| 285 |
|
|
|
|
| 290 |
return f"Error searching medical datasets for '{arg1}': {str(e)}"
|
| 291 |
|
| 292 |
|
|
|
|
|
|
|
|
|
|
| 293 |
@tool
|
| 294 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 295 |
"""
|
| 296 |
+
Get the current local time in a specified timezone.
|
| 297 |
|
| 298 |
Args:
|
| 299 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
| 300 |
Returns:
|
| 301 |
A string showing the current local time in the specified timezone.
|
| 302 |
"""
|
|
|
|
| 307 |
except Exception as e:
|
| 308 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 309 |
|
|
|
|
| 310 |
|
| 311 |
+
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
model = InferenceClientModel(
|
| 314 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 315 |
+
temperature=0.5
|
| 316 |
)
|
| 317 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
with open("prompts.yaml", 'r') as stream:
|
| 319 |
prompt_templates = yaml.safe_load(stream)
|
| 320 |
|
| 321 |
+
|
| 322 |
+
# --- 🔥 Critical: Enforce dataset search behavior ---
|
| 323 |
+
SYSTEM_PROMPT_APPEND = """
|
| 324 |
+
Whenever the user query includes medical conditions (like cancer, tumor, radiology, MRI, CT, ultrasound, pathology, or skin),
|
| 325 |
+
you MUST call the `my_custom_tool` function to search Hugging Face datasets instead of writing an explanation.
|
| 326 |
+
Always call it with arguments (arg1=<the keyword>, arg2=5).
|
| 327 |
+
Do NOT attempt to scrape websites, import modules, or fetch random text.
|
| 328 |
+
If the query is not medical, behave normally.
|
| 329 |
+
"""
|
| 330 |
+
|
|
|
|
| 331 |
|
| 332 |
agent = CodeAgent(
|
| 333 |
model=model,
|
|
|
|
| 336 |
verbosity_level=1,
|
| 337 |
planning_interval=None,
|
| 338 |
name="MedDataSearchAgent",
|
| 339 |
+
description="An intelligent agent that searches Hugging Face for medical datasets and returns structured results.",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
prompt_templates=prompt_templates
|
| 341 |
)
|
| 342 |
|
| 343 |
+
# Inject custom enforcement into system prompt
|
| 344 |
+
agent.prompt_templates["system_prompt"] += "\n" + SYSTEM_PROMPT_APPEND
|
| 345 |
+
|
| 346 |
|
|
|
|
| 347 |
GradioUI(agent).launch()
|