vikramvasudevan commited on
Commit
0248207
·
verified ·
1 Parent(s): 35d82ad

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. home.py +31 -1
  2. modules/db.py +48 -1
home.py CHANGED
@@ -157,6 +157,29 @@ def flatten_reports(reports: List[Dict[str, Any]]) -> pd.DataFrame:
157
  ]
158
  return pd.DataFrame(rows)
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  def trends_index(trends: List[Dict[str, Any]]) -> List[str]:
162
  names = sorted({t.get("test_name", "") for t in trends if t.get("test_name")})
@@ -435,7 +458,7 @@ with gr.Blocks(
435
  choices=[], label="Select Test", interactive=True
436
  )
437
  trend_plot = gr.Plot(label="Trend Chart")
438
- with gr.Tab("✅ Final Reports"):
439
  final_df = gr.DataFrame(
440
  headers=[
441
  "final_report_id",
@@ -447,6 +470,13 @@ with gr.Blocks(
447
  wrap=True,
448
  interactive=False,
449
  )
 
 
 
 
 
 
 
450
  with gr.Tab("📄 Upload History"):
451
  gr.Markdown("## here is your upload history")
452
 
 
157
  ]
158
  return pd.DataFrame(rows)
159
 
160
+ import tempfile
161
+ import os
162
+
163
+ def download_final_report(report_id: str) -> str | None:
164
+ """
165
+ Fetch the PDF for a given final_report and return a temporary file path.
166
+ """
167
+ if not report_id:
168
+ return None
169
+
170
+ db = get_db()
171
+ pdf_bytes = db.get_final_report_pdf(report_id)
172
+ if not pdf_bytes:
173
+ return None
174
+
175
+ # Write to a temporary file
176
+ tmpdir = tempfile.mkdtemp()
177
+ file_path = os.path.join(tmpdir, f"{report_id}.pdf")
178
+ with open(file_path, "wb") as f:
179
+ f.write(pdf_bytes)
180
+
181
+ return file_path
182
+
183
 
184
  def trends_index(trends: List[Dict[str, Any]]) -> List[str]:
185
  names = sorted({t.get("test_name", "") for t in trends if t.get("test_name")})
 
458
  choices=[], label="Select Test", interactive=True
459
  )
460
  trend_plot = gr.Plot(label="Trend Chart")
461
+ with gr.TabItem("✅ Final Reports"):
462
  final_df = gr.DataFrame(
463
  headers=[
464
  "final_report_id",
 
470
  wrap=True,
471
  interactive=False,
472
  )
473
+
474
+ with gr.Row():
475
+ report_id_in = gr.Textbox(label="Report ID", placeholder="Paste Report ID to download")
476
+ download_btn = gr.Button("⬇️ Download PDF", variant="primary")
477
+ download_file = gr.File(label="Your PDF", type="filepath", file_types=[".pdf"])
478
+
479
+ gr.Markdown("Tip: Select another patient to refresh final reports.")
480
  with gr.Tab("📄 Upload History"):
481
  gr.Markdown("## here is your upload history")
482
 
modules/db.py CHANGED
@@ -6,7 +6,7 @@ from bson import ObjectId
6
  from dotenv import load_dotenv
7
 
8
  from modules.models import StandardizedReport
9
-
10
 
11
  class SheamiDB:
12
  def __init__(self, uri: str, db_name: str = "sheami"):
@@ -24,6 +24,7 @@ class SheamiDB:
24
  self.trends = self.db["trends"]
25
  self.final_reports = self.db["final_reports"]
26
  self.run_stats = self.db["run_stats"]
 
27
 
28
  # ---------------------------
29
  # USER FUNCTIONS
@@ -372,3 +373,49 @@ class SheamiDB:
372
 
373
  # print("updated/inserted", updated, "trends")
374
  return updated
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from dotenv import load_dotenv
7
 
8
  from modules.models import StandardizedReport
9
+ import gridfs
10
 
11
  class SheamiDB:
12
  def __init__(self, uri: str, db_name: str = "sheami"):
 
24
  self.trends = self.db["trends"]
25
  self.final_reports = self.db["final_reports"]
26
  self.run_stats = self.db["run_stats"]
27
+ self.fs = gridfs.GridFS(self.db)
28
 
29
  # ---------------------------
30
  # USER FUNCTIONS
 
373
 
374
  # print("updated/inserted", updated, "trends")
375
  return updated
376
+
377
+ # ---------------------------
378
+ # FINAL REPORT FUNCTIONS
379
+ # ---------------------------
380
+ def add_final_report_v2(
381
+ self,
382
+ patient_id: str,
383
+ summary: str,
384
+ recommendations: list,
385
+ trend_snapshots: list,
386
+ pdf_bytes: bytes = None,
387
+ file_name: str = None,
388
+ ) -> str:
389
+ """
390
+ Insert a final report.
391
+ If pdf_bytes is provided, stores it in GridFS and saves file_id in metadata.
392
+ """
393
+ pdf_file_id = None
394
+ if pdf_bytes:
395
+ pdf_file_id = self.fs.put(
396
+ pdf_bytes,
397
+ filename=file_name or f"final_report_{patient_id}.pdf",
398
+ patient_id=ObjectId(patient_id),
399
+ uploaded_at=datetime.now(timezone.utc),
400
+ )
401
+
402
+ final_report = {
403
+ "patient_id": ObjectId(patient_id),
404
+ "generated_at": datetime.now(timezone.utc),
405
+ "summary": summary,
406
+ "recommendations": recommendations,
407
+ "trend_snapshots": trend_snapshots,
408
+ "pdf_file_id": pdf_file_id, # Reference to GridFS file
409
+ }
410
+
411
+ result = self.final_reports.insert_one(final_report)
412
+ return str(result.inserted_id)
413
+
414
+ def get_final_report_pdf(self, report_id: str) -> bytes | None:
415
+ """
416
+ Fetch the PDF bytes for a given final_report (if stored).
417
+ """
418
+ doc = self.final_reports.find_one({"_id": ObjectId(report_id)})
419
+ if not doc or not doc.get("pdf_file_id"):
420
+ return None
421
+ return self.fs.get(doc["pdf_file_id"]).read()