Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,6 @@ import re
|
|
10 |
import psutil
|
11 |
import subprocess
|
12 |
from collections import defaultdict
|
13 |
-
import torch
|
14 |
|
15 |
# Persistent directory for Hugging Face Space
|
16 |
persistent_dir = os.getenv("HF_HOME", "/data/hf_cache")
|
@@ -90,18 +89,18 @@ def log_system_usage(tag=""):
|
|
90 |
|
91 |
def clean_response(text: str) -> str:
|
92 |
text = sanitize_utf8(text)
|
93 |
-
#
|
94 |
text = re.sub(r"\[TOOL_CALLS\].*|(?:get_|tool\s|retrieve\s|use\s).*?\n", "", text, flags=re.DOTALL | re.IGNORECASE)
|
95 |
text = re.sub(r"\{'meta':\s*\{.*?\}\s*,\s*'results':\s*\[.*?\]\}\n?", "", text, flags=re.DOTALL)
|
96 |
text = re.sub(
|
97 |
-
r"(?i)(to
|
98 |
r"start|look|use|focus|retrieve|tool|based\s|overall|indicate|mention|consider|ensure|need\s|"
|
99 |
r"provide|review|assess|identify|potential|records|patient|history|symptoms|medication|"
|
100 |
-
r"conflict|assessment|follow-up|issue|reasoning|step).*?\n",
|
101 |
"", text, flags=re.DOTALL
|
102 |
)
|
103 |
text = re.sub(r"\n{3,}", "\n\n", text).strip()
|
104 |
-
# Only keep lines
|
105 |
lines = []
|
106 |
valid_heading = False
|
107 |
for line in text.split("\n"):
|
@@ -116,7 +115,7 @@ def clean_response(text: str) -> str:
|
|
116 |
return "\n".join(lines).strip()
|
117 |
|
118 |
def consolidate_findings(responses: List[str]) -> str:
|
119 |
-
# Merge
|
120 |
findings = defaultdict(set)
|
121 |
headings = ["Missed Diagnoses", "Medication Conflicts", "Incomplete Assessments", "Urgent Follow-up"]
|
122 |
|
@@ -133,7 +132,7 @@ def consolidate_findings(responses: List[str]) -> str:
|
|
133 |
elif current_heading and line.startswith("-"):
|
134 |
findings[current_heading].add(line)
|
135 |
|
136 |
-
# Format
|
137 |
output = []
|
138 |
for heading in headings:
|
139 |
if findings[heading]:
|
@@ -149,11 +148,10 @@ def init_agent():
|
|
149 |
rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
|
150 |
force_finish=True,
|
151 |
enable_checker=True,
|
152 |
-
step_rag_num=
|
153 |
seed=100,
|
154 |
)
|
155 |
-
#
|
156 |
-
agent.init_model(dtype=torch.float16)
|
157 |
log_system_usage("After Load")
|
158 |
print("✅ Agent Ready")
|
159 |
return agent
|
@@ -181,14 +179,14 @@ def create_ui(agent):
|
|
181 |
extracted = "\n".join(results)
|
182 |
file_hash_value = file_hash(files[0].name) if files else ""
|
183 |
|
184 |
-
#
|
185 |
chunk_size = 1000
|
186 |
chunks = [extracted[i:i + chunk_size] for i in range(0, len(extracted), chunk_size)]
|
187 |
chunk_responses = []
|
188 |
-
batch_size = 4 #
|
189 |
|
190 |
prompt_template = """
|
191 |
-
Output only oversights under these headings, one
|
192 |
|
193 |
**Missed Diagnoses**:
|
194 |
**Medication Conflicts**:
|
@@ -200,7 +198,7 @@ Records:
|
|
200 |
"""
|
201 |
|
202 |
try:
|
203 |
-
#
|
204 |
for i in range(0, len(chunks), batch_size):
|
205 |
batch = chunks[i:i + batch_size]
|
206 |
batch_responses = []
|
@@ -212,7 +210,7 @@ Records:
|
|
212 |
history=[],
|
213 |
temperature=0.1,
|
214 |
max_new_tokens=128,
|
215 |
-
max_token=8192
|
216 |
call_agent=False,
|
217 |
conversation=[],
|
218 |
):
|
@@ -232,12 +230,12 @@ Records:
|
|
232 |
batch_responses.append(chunk_response)
|
233 |
chunk_responses.extend(batch_responses)
|
234 |
|
235 |
-
#
|
236 |
final_response = consolidate_findings(chunk_responses)
|
237 |
history[-1]["content"] = final_response
|
238 |
yield history, None
|
239 |
|
240 |
-
#
|
241 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
242 |
if report_path and final_response != "No oversights identified.":
|
243 |
with open(report_path, "w", encoding="utf-8") as f:
|
|
|
10 |
import psutil
|
11 |
import subprocess
|
12 |
from collections import defaultdict
|
|
|
13 |
|
14 |
# Persistent directory for Hugging Face Space
|
15 |
persistent_dir = os.getenv("HF_HOME", "/data/hf_cache")
|
|
|
89 |
|
90 |
def clean_response(text: str) -> str:
|
91 |
text = sanitize_utf8(text)
|
92 |
+
# Remove all tool and reasoning text
|
93 |
text = re.sub(r"\[TOOL_CALLS\].*|(?:get_|tool\s|retrieve\s|use\s).*?\n", "", text, flags=re.DOTALL | re.IGNORECASE)
|
94 |
text = re.sub(r"\{'meta':\s*\{.*?\}\s*,\s*'results':\s*\[.*?\]\}\n?", "", text, flags=re.DOTALL)
|
95 |
text = re.sub(
|
96 |
+
r"(?i)(to\s|analyze|will\s|since\s|no\s|none|previous|attempt|involve|check\s|explore|manually|"
|
97 |
r"start|look|use|focus|retrieve|tool|based\s|overall|indicate|mention|consider|ensure|need\s|"
|
98 |
r"provide|review|assess|identify|potential|records|patient|history|symptoms|medication|"
|
99 |
+
r"conflict|assessment|follow-up|issue|reasoning|step|prompt|address).*?\n",
|
100 |
"", text, flags=re.DOTALL
|
101 |
)
|
102 |
text = re.sub(r"\n{3,}", "\n\n", text).strip()
|
103 |
+
# Only keep heading lines and bullets
|
104 |
lines = []
|
105 |
valid_heading = False
|
106 |
for line in text.split("\n"):
|
|
|
115 |
return "\n".join(lines).strip()
|
116 |
|
117 |
def consolidate_findings(responses: List[str]) -> str:
|
118 |
+
# Merge unique findings
|
119 |
findings = defaultdict(set)
|
120 |
headings = ["Missed Diagnoses", "Medication Conflicts", "Incomplete Assessments", "Urgent Follow-up"]
|
121 |
|
|
|
132 |
elif current_heading and line.startswith("-"):
|
133 |
findings[current_heading].add(line)
|
134 |
|
135 |
+
# Format output
|
136 |
output = []
|
137 |
for heading in headings:
|
138 |
if findings[heading]:
|
|
|
148 |
rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
|
149 |
force_finish=True,
|
150 |
enable_checker=True,
|
151 |
+
step_rag_num=0, # Disable RAG to prevent tool artifacts
|
152 |
seed=100,
|
153 |
)
|
154 |
+
agent.init_model() # No dtype argument
|
|
|
155 |
log_system_usage("After Load")
|
156 |
print("✅ Agent Ready")
|
157 |
return agent
|
|
|
179 |
extracted = "\n".join(results)
|
180 |
file_hash_value = file_hash(files[0].name) if files else ""
|
181 |
|
182 |
+
# Tiny chunks for speed
|
183 |
chunk_size = 1000
|
184 |
chunks = [extracted[i:i + chunk_size] for i in range(0, len(extracted), chunk_size)]
|
185 |
chunk_responses = []
|
186 |
+
batch_size = 4 # Batch for A100
|
187 |
|
188 |
prompt_template = """
|
189 |
+
Output only oversights under these headings, one point each. No tools, reasoning, or text beyond headings and bullets.
|
190 |
|
191 |
**Missed Diagnoses**:
|
192 |
**Medication Conflicts**:
|
|
|
198 |
"""
|
199 |
|
200 |
try:
|
201 |
+
# Batch process chunks
|
202 |
for i in range(0, len(chunks), batch_size):
|
203 |
batch = chunks[i:i + batch_size]
|
204 |
batch_responses = []
|
|
|
210 |
history=[],
|
211 |
temperature=0.1,
|
212 |
max_new_tokens=128,
|
213 |
+
max_token=4096, # Revert to 4096 as 8192 may not be supported
|
214 |
call_agent=False,
|
215 |
conversation=[],
|
216 |
):
|
|
|
230 |
batch_responses.append(chunk_response)
|
231 |
chunk_responses.extend(batch_responses)
|
232 |
|
233 |
+
# Single final output
|
234 |
final_response = consolidate_findings(chunk_responses)
|
235 |
history[-1]["content"] = final_response
|
236 |
yield history, None
|
237 |
|
238 |
+
# Report file
|
239 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
240 |
if report_path and final_response != "No oversights identified.":
|
241 |
with open(report_path, "w", encoding="utf-8") as f:
|