Update app.py
Browse files
app.py
CHANGED
|
@@ -108,6 +108,12 @@ def log_system_usage(tag=""):
|
|
| 108 |
except Exception as e:
|
| 109 |
print(f"[{tag}] GPU/CPU monitor failed: {e}")
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
def init_agent():
|
| 112 |
print("🔁 Initializing model...")
|
| 113 |
log_system_usage("Before Load")
|
|
@@ -141,7 +147,6 @@ def create_ui(agent):
|
|
| 141 |
download_output = gr.File(label="Download Full Report")
|
| 142 |
|
| 143 |
def analyze(message: str, history: List[dict], files: List):
|
| 144 |
-
# Append user message
|
| 145 |
history.append({"role": "user", "content": message})
|
| 146 |
history.append({"role": "assistant", "content": "⏳ Analyzing records for potential oversights..."})
|
| 147 |
yield history, None
|
|
@@ -166,11 +171,9 @@ Medical Records:
|
|
| 166 |
"""
|
| 167 |
|
| 168 |
try:
|
| 169 |
-
# Remove the temporary "Analyzing..." message
|
| 170 |
if history and history[-1]["content"].startswith("⏳"):
|
| 171 |
history.pop()
|
| 172 |
|
| 173 |
-
# Process agent response
|
| 174 |
for chunk in agent.run_gradio_chat(
|
| 175 |
message=prompt,
|
| 176 |
history=[],
|
|
@@ -186,17 +189,19 @@ Medical Records:
|
|
| 186 |
if isinstance(chunk, list):
|
| 187 |
for m in chunk:
|
| 188 |
if hasattr(m, 'content') and m.content:
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
| 191 |
elif isinstance(chunk, str) and chunk.strip():
|
| 192 |
-
|
| 193 |
-
if
|
| 194 |
-
history[-1]["
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
| 201 |
yield history, report_path if report_path and os.path.exists(report_path) else None
|
| 202 |
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
print(f"[{tag}] GPU/CPU monitor failed: {e}")
|
| 110 |
|
| 111 |
+
def clean_response(text: str) -> str:
|
| 112 |
+
text = sanitize_utf8(text)
|
| 113 |
+
text = re.sub(r"\[TOOL_CALLS\].*", "", text, flags=re.DOTALL)
|
| 114 |
+
text = re.sub(r"\n{3,}", "\n\n", text).strip()
|
| 115 |
+
return text
|
| 116 |
+
|
| 117 |
def init_agent():
|
| 118 |
print("🔁 Initializing model...")
|
| 119 |
log_system_usage("Before Load")
|
|
|
|
| 147 |
download_output = gr.File(label="Download Full Report")
|
| 148 |
|
| 149 |
def analyze(message: str, history: List[dict], files: List):
|
|
|
|
| 150 |
history.append({"role": "user", "content": message})
|
| 151 |
history.append({"role": "assistant", "content": "⏳ Analyzing records for potential oversights..."})
|
| 152 |
yield history, None
|
|
|
|
| 171 |
"""
|
| 172 |
|
| 173 |
try:
|
|
|
|
| 174 |
if history and history[-1]["content"].startswith("⏳"):
|
| 175 |
history.pop()
|
| 176 |
|
|
|
|
| 177 |
for chunk in agent.run_gradio_chat(
|
| 178 |
message=prompt,
|
| 179 |
history=[],
|
|
|
|
| 189 |
if isinstance(chunk, list):
|
| 190 |
for m in chunk:
|
| 191 |
if hasattr(m, 'content') and m.content:
|
| 192 |
+
cleaned = clean_response(m.content)
|
| 193 |
+
if cleaned:
|
| 194 |
+
history.append({"role": m.role, "content": cleaned})
|
| 195 |
+
yield history, None
|
| 196 |
elif isinstance(chunk, str) and chunk.strip():
|
| 197 |
+
cleaned = clean_response(chunk)
|
| 198 |
+
if cleaned:
|
| 199 |
+
if history and history[-1]["role"] == "assistant":
|
| 200 |
+
history[-1]["content"] += cleaned
|
| 201 |
+
else:
|
| 202 |
+
history.append({"role": "assistant", "content": cleaned})
|
| 203 |
+
yield history, None
|
| 204 |
+
|
| 205 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
| 206 |
yield history, report_path if report_path and os.path.exists(report_path) else None
|
| 207 |
|