Update app.py
Browse files
app.py
CHANGED
@@ -107,8 +107,15 @@ def log_system_usage(tag=""):
|
|
107 |
|
108 |
def clean_response(text: str) -> str:
|
109 |
text = sanitize_utf8(text)
|
|
|
110 |
text = re.sub(r"\[TOOL_CALLS\].*", "", text, flags=re.DOTALL)
|
|
|
|
|
|
|
111 |
text = re.sub(r"\n{3,}", "\n\n", text).strip()
|
|
|
|
|
|
|
112 |
return text
|
113 |
|
114 |
def init_agent():
|
@@ -125,7 +132,7 @@ def init_agent():
|
|
125 |
tool_files_dict={"new_tool": target_tool_path},
|
126 |
force_finish=True,
|
127 |
enable_checker=True,
|
128 |
-
step_rag_num=
|
129 |
seed=100,
|
130 |
additional_default_tools=[],
|
131 |
)
|
@@ -157,8 +164,8 @@ def create_ui(agent):
|
|
157 |
extracted = "\n".join(results)
|
158 |
file_hash_value = file_hash(files[0].name) if files else ""
|
159 |
|
160 |
-
# Split extracted text into chunks of ~
|
161 |
-
chunk_size =
|
162 |
chunks = [extracted[i:i + chunk_size] for i in range(0, len(extracted), chunk_size)]
|
163 |
combined_response = ""
|
164 |
|
@@ -190,7 +197,7 @@ Begin analysis:
|
|
190 |
if history and history[-1]["content"].startswith("β³"):
|
191 |
history.pop()
|
192 |
|
193 |
-
# Process each chunk and stream results
|
194 |
for chunk_idx, chunk in enumerate(chunks, 1):
|
195 |
# Update UI with progress
|
196 |
history.append({"role": "assistant", "content": f"π Processing Chunk {chunk_idx} of {len(chunks)}..."})
|
@@ -215,7 +222,7 @@ Begin analysis:
|
|
215 |
cleaned = clean_response(m.content)
|
216 |
if cleaned:
|
217 |
chunk_response += cleaned + "\n"
|
218 |
-
#
|
219 |
if history[-1]["content"].startswith("π"):
|
220 |
history[-1] = {"role": "assistant", "content": f"--- Analysis for Chunk {chunk_idx} ---\n{chunk_response.strip()}"}
|
221 |
else:
|
@@ -225,7 +232,7 @@ Begin analysis:
|
|
225 |
cleaned = clean_response(chunk_output)
|
226 |
if cleaned:
|
227 |
chunk_response += cleaned + "\n"
|
228 |
-
#
|
229 |
if history[-1]["content"].startswith("π"):
|
230 |
history[-1] = {"role": "assistant", "content": f"--- Analysis for Chunk {chunk_idx} ---\n{chunk_response.strip()}"}
|
231 |
else:
|
@@ -233,7 +240,8 @@ Begin analysis:
|
|
233 |
yield history, None
|
234 |
|
235 |
# Append completed chunk response to combined response
|
236 |
-
|
|
|
237 |
|
238 |
# Finalize UI with complete response
|
239 |
if combined_response:
|
@@ -241,7 +249,7 @@ Begin analysis:
|
|
241 |
else:
|
242 |
history.append({"role": "assistant", "content": "No oversights identified."})
|
243 |
|
244 |
-
# Generate report file
|
245 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
246 |
if report_path:
|
247 |
with open(report_path, "w", encoding="utf-8") as f:
|
|
|
107 |
|
108 |
def clean_response(text: str) -> str:
|
109 |
text = sanitize_utf8(text)
|
110 |
+
# Remove tool calls, JSON data, and repetitive phrases
|
111 |
text = re.sub(r"\[TOOL_CALLS\].*", "", text, flags=re.DOTALL)
|
112 |
+
text = re.sub(r"\['get_[^\]]+\']\n?", "", text) # Remove tool names
|
113 |
+
text = re.sub(r"\{'meta':\s*\{.*?\}\s*,\s*'results':\s*\[.*?\]\}\n?", "", text, flags=re.DOTALL) # Remove JSON
|
114 |
+
text = re.sub(r"To analyze the medical records for clinical oversights.*?begin by reviewing.*?\n", "", text, flags=re.DOTALL)
|
115 |
text = re.sub(r"\n{3,}", "\n\n", text).strip()
|
116 |
+
# Only keep text under analysis headings or relevant content
|
117 |
+
if not re.search(r"(Missed Diagnoses|Medication Conflicts|Incomplete Assessments|Urgent Follow-up)", text):
|
118 |
+
return ""
|
119 |
return text
|
120 |
|
121 |
def init_agent():
|
|
|
132 |
tool_files_dict={"new_tool": target_tool_path},
|
133 |
force_finish=True,
|
134 |
enable_checker=True,
|
135 |
+
step_rag_num=2,
|
136 |
seed=100,
|
137 |
additional_default_tools=[],
|
138 |
)
|
|
|
164 |
extracted = "\n".join(results)
|
165 |
file_hash_value = file_hash(files[0].name) if files else ""
|
166 |
|
167 |
+
# Split extracted text into chunks of ~4,000 characters
|
168 |
+
chunk_size = 4000
|
169 |
chunks = [extracted[i:i + chunk_size] for i in range(0, len(extracted), chunk_size)]
|
170 |
combined_response = ""
|
171 |
|
|
|
197 |
if history and history[-1]["content"].startswith("β³"):
|
198 |
history.pop()
|
199 |
|
200 |
+
# Process each chunk and stream cleaned results
|
201 |
for chunk_idx, chunk in enumerate(chunks, 1):
|
202 |
# Update UI with progress
|
203 |
history.append({"role": "assistant", "content": f"π Processing Chunk {chunk_idx} of {len(chunks)}..."})
|
|
|
222 |
cleaned = clean_response(m.content)
|
223 |
if cleaned:
|
224 |
chunk_response += cleaned + "\n"
|
225 |
+
# Stream partial response to UI
|
226 |
if history[-1]["content"].startswith("π"):
|
227 |
history[-1] = {"role": "assistant", "content": f"--- Analysis for Chunk {chunk_idx} ---\n{chunk_response.strip()}"}
|
228 |
else:
|
|
|
232 |
cleaned = clean_response(chunk_output)
|
233 |
if cleaned:
|
234 |
chunk_response += cleaned + "\n"
|
235 |
+
# Stream partial response to UI
|
236 |
if history[-1]["content"].startswith("π"):
|
237 |
history[-1] = {"role": "assistant", "content": f"--- Analysis for Chunk {chunk_idx} ---\n{chunk_response.strip()}"}
|
238 |
else:
|
|
|
240 |
yield history, None
|
241 |
|
242 |
# Append completed chunk response to combined response
|
243 |
+
if chunk_response:
|
244 |
+
combined_response += f"--- Analysis for Chunk {chunk_idx} ---\n{chunk_response}\n"
|
245 |
|
246 |
# Finalize UI with complete response
|
247 |
if combined_response:
|
|
|
249 |
else:
|
250 |
history.append({"role": "assistant", "content": "No oversights identified."})
|
251 |
|
252 |
+
# Generate report file with cleaned response
|
253 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
254 |
if report_path:
|
255 |
with open(report_path, "w", encoding="utf-8") as f:
|