Update app.py
Browse files
app.py
CHANGED
|
@@ -56,7 +56,7 @@ def extract_priority_pages(file_path: str, max_pages: int = 20) -> str:
|
|
| 56 |
text_chunks.append(f"=== Page {i+1} ===\n{text.strip()}")
|
| 57 |
for i, page in enumerate(pdf.pages[3:max_pages], start=4):
|
| 58 |
page_text = page.extract_text() or ""
|
| 59 |
-
if any(re.search(rf'
|
| 60 |
text_chunks.append(f"=== Page {i} ===\n{page_text.strip()}")
|
| 61 |
return "\n\n".join(text_chunks)
|
| 62 |
except Exception as e:
|
|
@@ -140,8 +140,9 @@ def create_ui(agent):
|
|
| 140 |
send_btn = gr.Button("Analyze", variant="primary")
|
| 141 |
download_output = gr.File(label="Download Full Report")
|
| 142 |
|
| 143 |
-
def analyze(message: str, history:
|
| 144 |
-
|
|
|
|
| 145 |
history.append({"role": "assistant", "content": "⏳ Analyzing records for potential oversights..."})
|
| 146 |
yield history, None
|
| 147 |
|
|
@@ -152,7 +153,7 @@ def create_ui(agent):
|
|
| 152 |
futures = [executor.submit(convert_file_to_json, f.name, f.name.split(".")[-1].lower()) for f in files]
|
| 153 |
results = [sanitize_utf8(f.result()) for f in as_completed(futures)]
|
| 154 |
extracted = "\n".join(results)
|
| 155 |
-
file_hash_value = file_hash(files[0].name)
|
| 156 |
|
| 157 |
prompt = f"""Review these medical records and identify EXACTLY what might have been missed:
|
| 158 |
1. List potential missed diagnoses
|
|
@@ -165,6 +166,11 @@ Medical Records:
|
|
| 165 |
"""
|
| 166 |
|
| 167 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
for chunk in agent.run_gradio_chat(
|
| 169 |
message=prompt,
|
| 170 |
history=[],
|
|
@@ -176,14 +182,21 @@ Medical Records:
|
|
| 176 |
):
|
| 177 |
if chunk is None:
|
| 178 |
continue
|
|
|
|
| 179 |
if isinstance(chunk, list):
|
| 180 |
for m in chunk:
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
yield history, None
|
| 186 |
|
|
|
|
| 187 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
| 188 |
yield history, report_path if report_path and os.path.exists(report_path) else None
|
| 189 |
|
|
@@ -206,4 +219,4 @@ if __name__ == "__main__":
|
|
| 206 |
show_error=True,
|
| 207 |
allowed_paths=[report_dir],
|
| 208 |
share=False
|
| 209 |
-
)
|
|
|
|
| 56 |
text_chunks.append(f"=== Page {i+1} ===\n{text.strip()}")
|
| 57 |
for i, page in enumerate(pdf.pages[3:max_pages], start=4):
|
| 58 |
page_text = page.extract_text() or ""
|
| 59 |
+
if any(re.search(rf'\b{kw}\b', page_text.lower()) for kw in MEDICAL_KEYWORDS):
|
| 60 |
text_chunks.append(f"=== Page {i} ===\n{page_text.strip()}")
|
| 61 |
return "\n\n".join(text_chunks)
|
| 62 |
except Exception as e:
|
|
|
|
| 140 |
send_btn = gr.Button("Analyze", variant="primary")
|
| 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
|
| 148 |
|
|
|
|
| 153 |
futures = [executor.submit(convert_file_to_json, f.name, f.name.split(".")[-1].lower()) for f in files]
|
| 154 |
results = [sanitize_utf8(f.result()) for f in as_completed(futures)]
|
| 155 |
extracted = "\n".join(results)
|
| 156 |
+
file_hash_value = file_hash(files[0].name) if files else ""
|
| 157 |
|
| 158 |
prompt = f"""Review these medical records and identify EXACTLY what might have been missed:
|
| 159 |
1. List potential missed diagnoses
|
|
|
|
| 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=[],
|
|
|
|
| 182 |
):
|
| 183 |
if chunk is None:
|
| 184 |
continue
|
| 185 |
+
|
| 186 |
if isinstance(chunk, list):
|
| 187 |
for m in chunk:
|
| 188 |
+
if hasattr(m, 'content') and m.content:
|
| 189 |
+
history.append({"role": m.role, "content": sanitize_utf8(m.content)})
|
| 190 |
+
yield history, None
|
| 191 |
+
elif isinstance(chunk, str) and chunk.strip():
|
| 192 |
+
# Append new assistant message or update the last one
|
| 193 |
+
if history and history[-1]["role"] == "assistant":
|
| 194 |
+
history[-1]["content"] += sanitize_utf8(chunk)
|
| 195 |
+
else:
|
| 196 |
+
history.append({"role": "assistant", "content": sanitize_utf8(chunk)})
|
| 197 |
yield history, None
|
| 198 |
|
| 199 |
+
# Provide report path if available
|
| 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 |
|
|
|
|
| 219 |
show_error=True,
|
| 220 |
allowed_paths=[report_dir],
|
| 221 |
share=False
|
| 222 |
+
)
|