Update app.py
Browse files
app.py
CHANGED
@@ -203,6 +203,43 @@ def analyze_with_agent(agent, prompt: str) -> str:
|
|
203 |
except Exception as e:
|
204 |
return f"Error in analysis: {str(e)}"
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
def create_ui(agent):
|
207 |
with gr.Blocks(theme=gr.themes.Soft(), title="Patient History Analyzer") as demo:
|
208 |
gr.Markdown("# 🏥 Patient History Analyzer")
|
@@ -240,37 +277,6 @@ def create_ui(agent):
|
|
240 |
- Description
|
241 |
""")
|
242 |
|
243 |
-
def analyze(file):
|
244 |
-
if not file:
|
245 |
-
raise gr.Error("Please upload a file")
|
246 |
-
|
247 |
-
try:
|
248 |
-
df = pd.read_excel(file.name)
|
249 |
-
patient_data = process_patient_data(df)
|
250 |
-
chunks = chunk_bookings(patient_data)
|
251 |
-
full_report = []
|
252 |
-
|
253 |
-
for i, bookings in enumerate(chunks, 1):
|
254 |
-
prompt = generate_analysis_prompt(patient_data, bookings)
|
255 |
-
response = analyze_with_agent(agent, prompt)
|
256 |
-
full_report.append(f"## Chunk {i}\n{response}\n")
|
257 |
-
yield "\n".join(full_report), None
|
258 |
-
|
259 |
-
# Final summary
|
260 |
-
if len(chunks) > 1:
|
261 |
-
summary_prompt = "Create final summary combining all chunks"
|
262 |
-
summary = analyze_with_agent(agent, summary_prompt)
|
263 |
-
full_report.append(f"## Final Summary\n{summary}\n")
|
264 |
-
|
265 |
-
report_path = os.path.join(report_dir, f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md")
|
266 |
-
with open(report_path, 'w') as f:
|
267 |
-
f.write("\n".join(full_report))
|
268 |
-
|
269 |
-
yield "\n".join(full_report), report_path
|
270 |
-
|
271 |
-
except Exception as e:
|
272 |
-
raise gr.Error(f"Error: {str(e)}")
|
273 |
-
|
274 |
analyze_btn.click(
|
275 |
analyze,
|
276 |
inputs=file_upload,
|
|
|
203 |
except Exception as e:
|
204 |
return f"Error in analysis: {str(e)}"
|
205 |
|
206 |
+
def analyze(file):
|
207 |
+
if not file:
|
208 |
+
raise gr.Error("Please upload a file")
|
209 |
+
|
210 |
+
try:
|
211 |
+
df = pd.read_excel(file.name)
|
212 |
+
patient_data = process_patient_data(df)
|
213 |
+
all_bookings = list(patient_data['bookings'].keys())
|
214 |
+
|
215 |
+
# ✅ Build one full prompt with all bookings
|
216 |
+
prompt = generate_analysis_prompt(patient_data, all_bookings)
|
217 |
+
|
218 |
+
# ✅ Add holistic reasoning instruction to prompt
|
219 |
+
prompt += "\n\n" + "\n".join([
|
220 |
+
"**Please analyze the entire patient history across all bookings.**",
|
221 |
+
"Look for missed diagnoses, inconsistent notes across different doctors,",
|
222 |
+
"missing follow-ups, or any gaps in care delivery.",
|
223 |
+
"Provide detailed insight into what may have been overlooked."
|
224 |
+
])
|
225 |
+
|
226 |
+
# ✅ Run the agent once over the full patient timeline
|
227 |
+
response = analyze_with_agent(agent, prompt)
|
228 |
+
|
229 |
+
# ✅ Wrap and save the result
|
230 |
+
full_report = f"# 🧠 Full Patient History Analysis\n\n{response}"
|
231 |
+
|
232 |
+
report_path = os.path.join(
|
233 |
+
report_dir, f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
|
234 |
+
)
|
235 |
+
with open(report_path, 'w') as f:
|
236 |
+
f.write(full_report)
|
237 |
+
|
238 |
+
yield full_report, report_path
|
239 |
+
|
240 |
+
except Exception as e:
|
241 |
+
raise gr.Error(f"Error: {str(e)}")
|
242 |
+
|
243 |
def create_ui(agent):
|
244 |
with gr.Blocks(theme=gr.themes.Soft(), title="Patient History Analyzer") as demo:
|
245 |
gr.Markdown("# 🏥 Patient History Analyzer")
|
|
|
277 |
- Description
|
278 |
""")
|
279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
analyze_btn.click(
|
281 |
analyze,
|
282 |
inputs=file_upload,
|