Ali2206 commited on
Commit
72f7a1f
Β·
verified Β·
1 Parent(s): 8d40b58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -11,6 +11,8 @@ import pandas as pd
11
  import pdfplumber
12
  import gradio as gr
13
  import torch
 
 
14
 
15
  # === Configuration ===
16
  persistent_dir = "/data/hf_cache"
@@ -59,6 +61,21 @@ def remove_duplicate_paragraphs(text: str) -> str:
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)
@@ -249,12 +266,11 @@ def process_report(agent, file, messages: List[Dict[str, str]]) -> Tuple[List[Di
249
  return messages, None
250
 
251
  summary = generate_final_summary(agent, "\n\n".join(valid))
252
- report_path = os.path.join(report_dir, f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md")
253
- with open(report_path, 'w', encoding='utf-8') as f:
254
- f.write(f"# 🧠 Final Medical Report\n\n{summary}")
255
 
256
- messages.append({"role": "assistant", "content": f"πŸ“Š Final Report:\n\n{summary}"})
257
- messages.append({"role": "assistant", "content": f"βœ… Report saved: {os.path.basename(report_path)}"})
 
 
258
  return messages, report_path
259
 
260
  except Exception as e:
@@ -277,7 +293,7 @@ def create_ui(agent):
277
  chatbot = gr.Chatbot(label="🧠 CPS Assistant", height=480, type="messages")
278
  upload = gr.File(label="πŸ“‚ Upload Medical File", file_types=[".xlsx", ".csv", ".pdf"])
279
  analyze = gr.Button("🧠 Analyze")
280
- download = gr.File(label="πŸ“₯ Download Report", visible=False, interactive=False)
281
 
282
  state = gr.State(value=[])
283
 
@@ -293,4 +309,4 @@ def create_ui(agent):
293
  if __name__ == "__main__":
294
  agent = init_agent()
295
  ui = create_ui(agent)
296
- 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
+ from reportlab.lib.pagesizes import letter
15
+ from reportlab.pdfgen import canvas
16
 
17
  # === Configuration ===
18
  persistent_dir = "/data/hf_cache"
 
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)
 
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
  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
  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)