Update app.py
Browse files
app.py
CHANGED
|
@@ -175,10 +175,12 @@ def clean_response(text: str) -> str:
|
|
| 175 |
cleaned.append(f"### {heading}\n" + "\n".join(findings))
|
| 176 |
|
| 177 |
text = "\n\n".join(cleaned).strip()
|
|
|
|
| 178 |
return text if text else ""
|
| 179 |
|
| 180 |
def summarize_findings(combined_response: str) -> str:
|
| 181 |
if not combined_response or all("No oversights identified" in chunk for chunk in combined_response.split("--- Analysis for Chunk")):
|
|
|
|
| 182 |
return "### Summary of Clinical Oversights\nNo critical oversights identified in the provided records."
|
| 183 |
|
| 184 |
sections = {}
|
|
@@ -205,9 +207,12 @@ def summarize_findings(combined_response: str) -> str:
|
|
| 205 |
summary_lines.append(summary)
|
| 206 |
|
| 207 |
if not summary_lines:
|
|
|
|
| 208 |
return "### Summary of Clinical Oversights\nNo critical oversights identified."
|
| 209 |
|
| 210 |
-
|
|
|
|
|
|
|
| 211 |
|
| 212 |
def init_agent():
|
| 213 |
logger.info("Initializing model...")
|
|
@@ -252,10 +257,12 @@ Patient Record Excerpt (Chunk {0} of {1}):
|
|
| 252 |
def analyze(message: str, history: List[dict], files: List, progress=gr.Progress()):
|
| 253 |
history.append({"role": "user", "content": message})
|
| 254 |
yield history, None, ""
|
|
|
|
| 255 |
|
| 256 |
extracted = ""
|
| 257 |
file_hash_value = ""
|
| 258 |
if files:
|
|
|
|
| 259 |
def update_extraction_progress(current, total):
|
| 260 |
progress(current / total, desc=f"Extracting text... Page {current}/{total}")
|
| 261 |
return history, None, ""
|
|
@@ -265,11 +272,13 @@ Patient Record Excerpt (Chunk {0} of {1}):
|
|
| 265 |
results = [sanitize_utf8(f.result()) for f in as_completed(futures)]
|
| 266 |
extracted = "\n".join(results)
|
| 267 |
file_hash_value = file_hash(files[0].name) if files else ""
|
| 268 |
-
|
| 269 |
history.append({"role": "assistant", "content": "✅ Text extraction complete."})
|
| 270 |
yield history, None, ""
|
| 271 |
-
|
|
|
|
| 272 |
|
|
|
|
| 273 |
chunk_size = 6000
|
| 274 |
chunks = [extracted[i:i + chunk_size] for i in range(0, len(extracted), chunk_size)]
|
| 275 |
logger.info("Created %d chunks", len(chunks))
|
|
@@ -304,6 +313,7 @@ Patient Record Excerpt (Chunk {0} of {1}):
|
|
| 304 |
batch_responses.append(chunk_response)
|
| 305 |
torch.cuda.empty_cache()
|
| 306 |
gc.collect()
|
|
|
|
| 307 |
|
| 308 |
for chunk_idx, chunk_response in enumerate(batch_responses, batch_idx + 1):
|
| 309 |
if chunk_response:
|
|
@@ -323,6 +333,7 @@ Patient Record Excerpt (Chunk {0} of {1}):
|
|
| 323 |
if report_path:
|
| 324 |
with open(report_path, "w", encoding="utf-8") as f:
|
| 325 |
f.write(combined_response + "\n\n" + summary)
|
|
|
|
| 326 |
yield history, report_path if report_path and os.path.exists(report_path) else None, summary
|
| 327 |
|
| 328 |
except Exception as e:
|
|
|
|
| 175 |
cleaned.append(f"### {heading}\n" + "\n".join(findings))
|
| 176 |
|
| 177 |
text = "\n\n".join(cleaned).strip()
|
| 178 |
+
logger.debug("Cleaned response length: %d chars", len(text))
|
| 179 |
return text if text else ""
|
| 180 |
|
| 181 |
def summarize_findings(combined_response: str) -> str:
|
| 182 |
if not combined_response or all("No oversights identified" in chunk for chunk in combined_response.split("--- Analysis for Chunk")):
|
| 183 |
+
logger.info("No clinical oversights identified in analysis")
|
| 184 |
return "### Summary of Clinical Oversights\nNo critical oversights identified in the provided records."
|
| 185 |
|
| 186 |
sections = {}
|
|
|
|
| 207 |
summary_lines.append(summary)
|
| 208 |
|
| 209 |
if not summary_lines:
|
| 210 |
+
logger.info("No clinical oversights identified after summarization")
|
| 211 |
return "### Summary of Clinical Oversights\nNo critical oversights identified."
|
| 212 |
|
| 213 |
+
summary = "### Summary of Clinical Oversights\n" + "\n".join(summary_lines)
|
| 214 |
+
logger.info("Summarized findings: %s", summary[:100])
|
| 215 |
+
return summary
|
| 216 |
|
| 217 |
def init_agent():
|
| 218 |
logger.info("Initializing model...")
|
|
|
|
| 257 |
def analyze(message: str, history: List[dict], files: List, progress=gr.Progress()):
|
| 258 |
history.append({"role": "user", "content": message})
|
| 259 |
yield history, None, ""
|
| 260 |
+
logger.info("Starting analysis for message: %s", message[:100])
|
| 261 |
|
| 262 |
extracted = ""
|
| 263 |
file_hash_value = ""
|
| 264 |
if files:
|
| 265 |
+
logger.info("Processing %d uploaded files", len(files))
|
| 266 |
def update_extraction_progress(current, total):
|
| 267 |
progress(current / total, desc=f"Extracting text... Page {current}/{total}")
|
| 268 |
return history, None, ""
|
|
|
|
| 272 |
results = [sanitize_utf8(f.result()) for f in as_completed(futures)]
|
| 273 |
extracted = "\n".join(results)
|
| 274 |
file_hash_value = file_hash(files[0].name) if files else ""
|
| 275 |
+
logger.info("Extraction complete for %d files", len(files))
|
| 276 |
history.append({"role": "assistant", "content": "✅ Text extraction complete."})
|
| 277 |
yield history, None, ""
|
| 278 |
+
else:
|
| 279 |
+
logger.warning("No files uploaded for analysis")
|
| 280 |
|
| 281 |
+
logger.info("Extracted text length: %d chars", len(extracted))
|
| 282 |
chunk_size = 6000
|
| 283 |
chunks = [extracted[i:i + chunk_size] for i in range(0, len(extracted), chunk_size)]
|
| 284 |
logger.info("Created %d chunks", len(chunks))
|
|
|
|
| 313 |
batch_responses.append(chunk_response)
|
| 314 |
torch.cuda.empty_cache()
|
| 315 |
gc.collect()
|
| 316 |
+
logger.debug("Processed chunk response length: %d chars", len(chunk_response))
|
| 317 |
|
| 318 |
for chunk_idx, chunk_response in enumerate(batch_responses, batch_idx + 1):
|
| 319 |
if chunk_response:
|
|
|
|
| 333 |
if report_path:
|
| 334 |
with open(report_path, "w", encoding="utf-8") as f:
|
| 335 |
f.write(combined_response + "\n\n" + summary)
|
| 336 |
+
logger.info("Analysis complete, report saved at: %s", report_path if report_path else "None")
|
| 337 |
yield history, report_path if report_path and os.path.exists(report_path) else None, summary
|
| 338 |
|
| 339 |
except Exception as e:
|