Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ import re
|
|
12 |
import psutil
|
13 |
import subprocess
|
14 |
|
15 |
-
# Persistent directory
|
16 |
persistent_dir = "/data/hf_cache"
|
17 |
os.makedirs(persistent_dir, exist_ok=True)
|
18 |
|
@@ -132,20 +132,25 @@ def init_agent():
|
|
132 |
return agent
|
133 |
|
134 |
def clean_response(response: str) -> str:
|
135 |
-
"""
|
136 |
-
#
|
137 |
-
|
|
|
|
|
|
|
|
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
143 |
|
144 |
# Remove any remaining JSON artifacts
|
145 |
-
|
146 |
-
|
147 |
|
148 |
-
return
|
149 |
|
150 |
def create_ui(agent):
|
151 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
@@ -207,20 +212,20 @@ Medical Records:
|
|
207 |
finish_detected = True
|
208 |
|
209 |
# Display intermediate response
|
210 |
-
|
211 |
-
if
|
212 |
-
history[-1] = {"role": "assistant", "content":
|
213 |
yield history, None
|
214 |
|
215 |
-
# Final processing
|
216 |
final_cleaned = clean_response(full_response)
|
217 |
|
218 |
if not final_cleaned:
|
219 |
final_cleaned = "⚠️ No clear oversights identified or model output was invalid."
|
220 |
|
221 |
-
#
|
222 |
report_path = None
|
223 |
-
if
|
224 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt")
|
225 |
with open(report_path, "w", encoding="utf-8") as f:
|
226 |
f.write(final_cleaned)
|
|
|
12 |
import psutil
|
13 |
import subprocess
|
14 |
|
15 |
+
# Persistent directory setup
|
16 |
persistent_dir = "/data/hf_cache"
|
17 |
os.makedirs(persistent_dir, exist_ok=True)
|
18 |
|
|
|
132 |
return agent
|
133 |
|
134 |
def clean_response(response: str) -> str:
|
135 |
+
"""Enhanced response cleaner that handles duplicates and tool calls."""
|
136 |
+
# First extract the main analysis content
|
137 |
+
analysis_match = re.search(
|
138 |
+
r'(Based on the medical records provided.*?)(?=\[TOOL_CALLS\]|Based on|$)',
|
139 |
+
response,
|
140 |
+
flags=re.DOTALL
|
141 |
+
)
|
142 |
|
143 |
+
if analysis_match:
|
144 |
+
cleaned = analysis_match.group(1).strip()
|
145 |
+
else:
|
146 |
+
# Fallback if pattern not found
|
147 |
+
cleaned = re.sub(r'\[TOOL_CALLS\].*?$', '', response, flags=re.DOTALL).strip()
|
148 |
|
149 |
# Remove any remaining JSON artifacts
|
150 |
+
cleaned = re.sub(r'\{.*?\}', '', cleaned)
|
151 |
+
cleaned = re.sub(r'\[.*?\]', '', cleaned)
|
152 |
|
153 |
+
return cleaned
|
154 |
|
155 |
def create_ui(agent):
|
156 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
212 |
finish_detected = True
|
213 |
|
214 |
# Display intermediate response
|
215 |
+
current_cleaned = clean_response(full_response)
|
216 |
+
if current_cleaned:
|
217 |
+
history[-1] = {"role": "assistant", "content": current_cleaned}
|
218 |
yield history, None
|
219 |
|
220 |
+
# Final processing
|
221 |
final_cleaned = clean_response(full_response)
|
222 |
|
223 |
if not final_cleaned:
|
224 |
final_cleaned = "⚠️ No clear oversights identified or model output was invalid."
|
225 |
|
226 |
+
# Save report
|
227 |
report_path = None
|
228 |
+
if file_hash_value:
|
229 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt")
|
230 |
with open(report_path, "w", encoding="utf-8") as f:
|
231 |
f.write(final_cleaned)
|