Ali2206 commited on
Commit
2cf375a
Β·
verified Β·
1 Parent(s): b1deddd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -26
app.py CHANGED
@@ -11,8 +11,6 @@ import pandas as pd
11
  import pdfplumber
12
  import gradio as gr
13
  import torch
14
- from reportlab.lib.pagesizes import letter
15
- from reportlab.pdfgen import canvas
16
 
17
  # === Configuration ===
18
  persistent_dir = "/data/hf_cache"
@@ -61,21 +59,6 @@ def remove_duplicate_paragraphs(text: str) -> str:
61
  seen.add(clean_p)
62
  return "\n\n".join(unique_paragraphs)
63
 
64
- def save_report_as_pdf(text: str, path: str):
65
- c = canvas.Canvas(path, pagesize=letter)
66
- width, height = letter
67
- y = height - 50
68
-
69
- lines = text.split('\n')
70
- for line in lines:
71
- if y < 50:
72
- c.showPage()
73
- y = height - 50
74
- c.drawString(50, y, line.strip())
75
- y -= 15
76
-
77
- c.save()
78
-
79
  def extract_text_from_excel(path: str) -> str:
80
  all_text = []
81
  xls = pd.ExcelFile(path)
@@ -205,17 +188,14 @@ def generate_final_summary(agent, combined: str) -> str:
205
  combined = remove_duplicate_paragraphs(combined)
206
  final_prompt = f"""
207
  You are an expert clinical summarizer. Analyze the following summaries carefully and generate a **single final concise structured medical report**, avoiding any repetition or redundancy.
208
-
209
  Summaries:
210
  {combined}
211
-
212
  Respond with:
213
  - Diagnostic Patterns
214
  - Medication Issues
215
  - Missed Opportunities
216
  - Inconsistencies
217
  - Follow-up Recommendations
218
-
219
  Avoid repeating the same points multiple times.
220
  """.strip()
221
 
@@ -266,11 +246,12 @@ def process_report(agent, file, messages: List[Dict[str, str]]) -> Tuple[List[Di
266
  return messages, None
267
 
268
  summary = generate_final_summary(agent, "\n\n".join(valid))
 
 
 
269
 
270
- report_path = os.path.join(report_dir, f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf")
271
- save_report_as_pdf(summary, report_path)
272
-
273
- messages.append({"role": "assistant", "content": "βœ… Report generated successfully. Click below to download it."})
274
  return messages, report_path
275
 
276
  except Exception as e:
@@ -293,7 +274,7 @@ def create_ui(agent):
293
  chatbot = gr.Chatbot(label="🧠 CPS Assistant", height=480, type="messages")
294
  upload = gr.File(label="πŸ“‚ Upload Medical File", file_types=[".xlsx", ".csv", ".pdf"])
295
  analyze = gr.Button("🧠 Analyze")
296
- download = gr.File(label="πŸ“₯ Download Report", visible=False, interactive=True)
297
 
298
  state = gr.State(value=[])
299
 
@@ -309,4 +290,4 @@ def create_ui(agent):
309
  if __name__ == "__main__":
310
  agent = init_agent()
311
  ui = create_ui(agent)
312
- ui.launch(server_name="0.0.0.0", server_port=7860, allowed_paths=["/data/hf_cache/reports"], share=False)
 
11
  import pdfplumber
12
  import gradio as gr
13
  import torch
 
 
14
 
15
  # === Configuration ===
16
  persistent_dir = "/data/hf_cache"
 
59
  seen.add(clean_p)
60
  return "\n\n".join(unique_paragraphs)
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def extract_text_from_excel(path: str) -> str:
63
  all_text = []
64
  xls = pd.ExcelFile(path)
 
188
  combined = remove_duplicate_paragraphs(combined)
189
  final_prompt = f"""
190
  You are an expert clinical summarizer. Analyze the following summaries carefully and generate a **single final concise structured medical report**, avoiding any repetition or redundancy.
 
191
  Summaries:
192
  {combined}
 
193
  Respond with:
194
  - Diagnostic Patterns
195
  - Medication Issues
196
  - Missed Opportunities
197
  - Inconsistencies
198
  - Follow-up Recommendations
 
199
  Avoid repeating the same points multiple times.
200
  """.strip()
201
 
 
246
  return messages, None
247
 
248
  summary = generate_final_summary(agent, "\n\n".join(valid))
249
+ report_path = os.path.join(report_dir, f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md")
250
+ with open(report_path, 'w', encoding='utf-8') as f:
251
+ f.write(f"# 🧠 Final Medical Report\n\n{summary}")
252
 
253
+ messages.append({"role": "assistant", "content": f"πŸ“Š Final Report:\n\n{summary}"})
254
+ messages.append({"role": "assistant", "content": f"βœ… Report saved: {os.path.basename(report_path)}"})
 
 
255
  return messages, report_path
256
 
257
  except Exception as e:
 
274
  chatbot = gr.Chatbot(label="🧠 CPS Assistant", height=480, type="messages")
275
  upload = gr.File(label="πŸ“‚ Upload Medical File", file_types=[".xlsx", ".csv", ".pdf"])
276
  analyze = gr.Button("🧠 Analyze")
277
+ download = gr.File(label="πŸ“₯ Download Report", visible=False, interactive=False)
278
 
279
  state = gr.State(value=[])
280
 
 
290
  if __name__ == "__main__":
291
  agent = init_agent()
292
  ui = create_ui(agent)
293
+ ui.launch(server_name="0.0.0.0", server_port=7860, allowed_paths=["/data/hf_cache/reports"], share=False)