Update app.py
Browse files
app.py
CHANGED
|
@@ -97,29 +97,43 @@ def validate_tool_file(file_path):
|
|
| 97 |
with open(file_path, 'r') as f:
|
| 98 |
data = json.load(f)
|
| 99 |
if isinstance(data, list):
|
| 100 |
-
assert all(isinstance(t, dict) and "name" in t for t in data), "Invalid
|
| 101 |
elif isinstance(data, dict):
|
| 102 |
assert "tools" in data and isinstance(data["tools"], list), "'tools' field missing or invalid"
|
| 103 |
-
assert all(isinstance(t, dict) and "name" in t for t in data["tools"]), "Invalid
|
| 104 |
else:
|
| 105 |
-
raise ValueError("
|
| 106 |
return True
|
| 107 |
except Exception as e:
|
| 108 |
-
print(f"❌
|
| 109 |
return False
|
| 110 |
|
| 111 |
def init_agent():
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
if not
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
agent = TxAgent(
|
| 120 |
model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
|
| 121 |
rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
|
| 122 |
-
tool_files_dict=
|
| 123 |
force_finish=True,
|
| 124 |
enable_checker=True,
|
| 125 |
step_rag_num=4,
|
|
@@ -220,9 +234,8 @@ def create_ui(agent):
|
|
| 220 |
background: linear-gradient(135deg, #37b6e9, #4b4ced);
|
| 221 |
}
|
| 222 |
""") as demo:
|
| 223 |
-
gr.Markdown("""# 🧠 Clinical Reasoning Assistant
|
| 224 |
-
Upload clinical Excel records below and click **Analyze** to generate a medical summary.
|
| 225 |
-
""")
|
| 226 |
file_upload = gr.File(label="Upload Excel File", file_types=[".xlsx"])
|
| 227 |
analyze_btn = gr.Button("Analyze")
|
| 228 |
report_output_markdown = gr.Markdown(elem_classes="output-markdown")
|
|
|
|
| 97 |
with open(file_path, 'r') as f:
|
| 98 |
data = json.load(f)
|
| 99 |
if isinstance(data, list):
|
| 100 |
+
assert all(isinstance(t, dict) and "name" in t for t in data), "Invalid list format"
|
| 101 |
elif isinstance(data, dict):
|
| 102 |
assert "tools" in data and isinstance(data["tools"], list), "'tools' field missing or invalid"
|
| 103 |
+
assert all(isinstance(t, dict) and "name" in t for t in data["tools"]), "Invalid item in 'tools'"
|
| 104 |
else:
|
| 105 |
+
raise ValueError("Unexpected structure")
|
| 106 |
return True
|
| 107 |
except Exception as e:
|
| 108 |
+
print(f"❌ Tool validation failed for {file_path}: {e}")
|
| 109 |
return False
|
| 110 |
|
| 111 |
def init_agent():
|
| 112 |
+
all_tool_paths = {
|
| 113 |
+
"opentarget": "/home/user/.pyenv/versions/3.10.17/lib/python3.10/site-packages/tooluniverse/data/opentarget_tools.json",
|
| 114 |
+
"fda_drug_label": "/home/user/.pyenv/versions/3.10.17/lib/python3.10/site-packages/tooluniverse/data/fda_drug_labeling_tools.json",
|
| 115 |
+
"special_tools": "/home/user/.pyenv/versions/3.10.17/lib/python3.10/site-packages/tooluniverse/data/special_tools.json",
|
| 116 |
+
"monarch": "/home/user/.pyenv/versions/3.10.17/lib/python3.10/site-packages/tooluniverse/data/monarch_tools.json",
|
| 117 |
+
"new_tool": os.path.join(tool_cache_dir, "new_tool.json"),
|
| 118 |
+
}
|
| 119 |
|
| 120 |
+
if not os.path.exists(all_tool_paths["new_tool"]):
|
| 121 |
+
shutil.copy(os.path.abspath("data/new_tool.json"), all_tool_paths["new_tool"])
|
| 122 |
+
|
| 123 |
+
valid_tool_paths = {}
|
| 124 |
+
for key, path in all_tool_paths.items():
|
| 125 |
+
if validate_tool_file(path):
|
| 126 |
+
valid_tool_paths[key] = path
|
| 127 |
+
else:
|
| 128 |
+
print(f"⚠️ Skipping invalid tool file: {path}")
|
| 129 |
+
|
| 130 |
+
if not valid_tool_paths:
|
| 131 |
+
raise RuntimeError("❌ No valid tool files found to load into TxAgent.")
|
| 132 |
|
| 133 |
agent = TxAgent(
|
| 134 |
model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
|
| 135 |
rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
|
| 136 |
+
tool_files_dict=valid_tool_paths,
|
| 137 |
force_finish=True,
|
| 138 |
enable_checker=True,
|
| 139 |
step_rag_num=4,
|
|
|
|
| 234 |
background: linear-gradient(135deg, #37b6e9, #4b4ced);
|
| 235 |
}
|
| 236 |
""") as demo:
|
| 237 |
+
gr.Markdown("""# 🧠 Clinical Reasoning Assistant
|
| 238 |
+
Upload clinical Excel records below and click **Analyze** to generate a medical summary.""")
|
|
|
|
| 239 |
file_upload = gr.File(label="Upload Excel File", file_types=[".xlsx"])
|
| 240 |
analyze_btn = gr.Button("Analyze")
|
| 241 |
report_output_markdown = gr.Markdown(elem_classes="output-markdown")
|