Dhruv-Ty commited on
Commit
c83a7f9
Β·
verified Β·
1 Parent(s): 0a6ef9c

Update src/report_generator.py

Browse files
Files changed (1) hide show
  1. src/report_generator.py +62 -5
src/report_generator.py CHANGED
@@ -12,12 +12,60 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, Tabl
12
  from reportlab.lib import colors
13
  from reportlab.lib.units import inch, cm
14
  from reportlab.platypus import PageBreak, KeepTogether
 
15
 
16
  from model import orchestrator_chat
17
  from utils import format_conversation_history
18
  from session_state import get_full_history
19
 
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # Function to encode images to base64 (similar to app.py)
22
  def get_image_base64(image_path):
23
  try:
@@ -257,19 +305,23 @@ def build_medical_report(data: dict) -> bytes:
257
  # --- TABLE 1: Patient Information ---
258
  patient = data.get('patient', {})
259
 
 
 
 
260
  # Create a cleaner patient info table with proper Paragraph objects
261
  patient_data = [
262
  [Paragraph('<font color="#BA55D3"><b>Patient Information</b></font>', styles['SectionHeading']), ''],
263
  [Paragraph('Name:', styles['NormalText']), Paragraph(f'{patient.get("name", "–")}', styles['NormalText'])],
264
  [Paragraph('Age:', styles['NormalText']), Paragraph(f'{patient.get("age", "–")}', styles['NormalText'])],
265
  [Paragraph('Gender:', styles['NormalText']), Paragraph(f'{patient.get("gender", "–")}', styles['NormalText'])],
266
- [Paragraph('Visit Date:', styles['NormalText']), Paragraph(f'{data.get("visit_date", datetime.now().strftime("%Y-%m-%d"))}', styles['NormalText'])]
 
267
  ]
268
 
269
  patient_table = Table(
270
  patient_data,
271
  colWidths=[doc.width * 0.3, doc.width * 0.7],
272
- rowHeights=[0.4*inch, 0.25*inch, 0.25*inch, 0.25*inch, 0.25*inch]
273
  )
274
 
275
  patient_table.setStyle(TableStyle([
@@ -478,9 +530,14 @@ def generate_and_download_report():
478
  # Update with patient info from the form
479
  medical_data["patient"].update(st.session_state.patient_info)
480
 
481
- # Set the visit date to today if not present
482
- if not medical_data.get("visit_date"):
483
- medical_data["visit_date"] = datetime.now().strftime("%Y-%m-%d")
 
 
 
 
 
484
 
485
  # Generate PDF
486
  pdf_data = build_medical_report(medical_data)
 
12
  from reportlab.lib import colors
13
  from reportlab.lib.units import inch, cm
14
  from reportlab.platypus import PageBreak, KeepTogether
15
+ import requests
16
 
17
  from model import orchestrator_chat
18
  from utils import format_conversation_history
19
  from session_state import get_full_history
20
 
21
 
22
+ # Function to fetch current date and time from an API
23
+ def fetch_current_datetime():
24
+ """
25
+ Fetch the current date and time from a public API.
26
+ Returns a dictionary with date, time, and formatted timestamp.
27
+ If the API call fails, falls back to system time.
28
+ """
29
+ try:
30
+ # Call the WorldTimeAPI to get current date and time
31
+ response = requests.get("http://worldtimeapi.org/api/ip")
32
+ if response.status_code == 200:
33
+ data = response.json()
34
+ datetime_str = data.get('datetime')
35
+ if datetime_str:
36
+ # Parse the datetime string
37
+ dt = datetime.fromisoformat(datetime_str.replace('Z', '+00:00'))
38
+
39
+ # Format the date and time
40
+ formatted_date = dt.strftime("%Y-%m-%d")
41
+ formatted_time = dt.strftime("%H:%M:%S")
42
+ full_timestamp = dt.strftime("%Y-%m-%d %H:%M:%S")
43
+
44
+ return {
45
+ "date": formatted_date,
46
+ "time": formatted_time,
47
+ "timestamp": full_timestamp
48
+ }
49
+
50
+ # Fallback to system time if API fails
51
+ print("API call failed, using system time instead")
52
+ now = datetime.now()
53
+ return {
54
+ "date": now.strftime("%Y-%m-%d"),
55
+ "time": now.strftime("%H:%M:%S"),
56
+ "timestamp": now.strftime("%Y-%m-%d %H:%M:%S")
57
+ }
58
+ except Exception as e:
59
+ # Handle any exceptions and fall back to system time
60
+ print(f"Error fetching time from API: {str(e)}")
61
+ now = datetime.now()
62
+ return {
63
+ "date": now.strftime("%Y-%m-%d"),
64
+ "time": now.strftime("%H:%M:%S"),
65
+ "timestamp": now.strftime("%Y-%m-%d %H:%M:%S")
66
+ }
67
+
68
+
69
  # Function to encode images to base64 (similar to app.py)
70
  def get_image_base64(image_path):
71
  try:
 
305
  # --- TABLE 1: Patient Information ---
306
  patient = data.get('patient', {})
307
 
308
+ # Get report generation timestamp
309
+ generation_time = data.get('generation_timestamp', datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
310
+
311
  # Create a cleaner patient info table with proper Paragraph objects
312
  patient_data = [
313
  [Paragraph('<font color="#BA55D3"><b>Patient Information</b></font>', styles['SectionHeading']), ''],
314
  [Paragraph('Name:', styles['NormalText']), Paragraph(f'{patient.get("name", "–")}', styles['NormalText'])],
315
  [Paragraph('Age:', styles['NormalText']), Paragraph(f'{patient.get("age", "–")}', styles['NormalText'])],
316
  [Paragraph('Gender:', styles['NormalText']), Paragraph(f'{patient.get("gender", "–")}', styles['NormalText'])],
317
+ [Paragraph('Visit Date:', styles['NormalText']), Paragraph(f'{data.get("visit_date", datetime.now().strftime("%Y-%m-%d"))}', styles['NormalText'])],
318
+ [Paragraph('Report Generated:', styles['NormalText']), Paragraph(f'{generation_time}', styles['NormalText'])]
319
  ]
320
 
321
  patient_table = Table(
322
  patient_data,
323
  colWidths=[doc.width * 0.3, doc.width * 0.7],
324
+ rowHeights=[0.4*inch, 0.25*inch, 0.25*inch, 0.25*inch, 0.25*inch, 0.25*inch]
325
  )
326
 
327
  patient_table.setStyle(TableStyle([
 
530
  # Update with patient info from the form
531
  medical_data["patient"].update(st.session_state.patient_info)
532
 
533
+ # Fetch current date and time from the API
534
+ datetime_data = fetch_current_datetime()
535
+
536
+ # Set the visit date to today's date from API
537
+ medical_data["visit_date"] = datetime_data["date"]
538
+
539
+ # Set the report generation timestamp
540
+ medical_data["generation_timestamp"] = datetime_data["timestamp"]
541
 
542
  # Generate PDF
543
  pdf_data = build_medical_report(medical_data)