changed report UI
Browse files- src/report_generator.py +181 -51
src/report_generator.py
CHANGED
@@ -6,9 +6,11 @@ from datetime import datetime
|
|
6 |
import streamlit as st
|
7 |
from reportlab.lib.pagesizes import A4
|
8 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
9 |
-
from reportlab.lib.enums import TA_LEFT
|
10 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
|
11 |
from reportlab.lib import colors
|
|
|
|
|
12 |
|
13 |
from text_processors import format_conversation_history
|
14 |
from model import orchestrator_chat
|
@@ -78,82 +80,210 @@ def build_medical_report(data: dict) -> bytes:
|
|
78 |
fd, temp_path = tempfile.mkstemp(suffix='.pdf')
|
79 |
os.close(fd)
|
80 |
|
|
|
81 |
doc = SimpleDocTemplate(
|
82 |
temp_path,
|
83 |
pagesize=A4,
|
84 |
-
rightMargin=
|
85 |
-
leftMargin=
|
86 |
-
topMargin=
|
87 |
-
bottomMargin=
|
88 |
)
|
89 |
|
|
|
90 |
styles = getSampleStyleSheet()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
styles.add(ParagraphStyle(
|
92 |
name='SectionHeading',
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
96 |
alignment=TA_LEFT,
|
97 |
-
textColor=colors.
|
98 |
))
|
99 |
|
|
|
100 |
styles.add(ParagraphStyle(
|
101 |
-
name='
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
105 |
alignment=TA_LEFT
|
106 |
))
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
elems = []
|
109 |
|
110 |
-
#
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
patient = data.get('patient', {})
|
116 |
-
|
|
|
|
|
|
|
117 |
['Name:', patient.get('name', 'β')],
|
118 |
['Age:', patient.get('age', 'β')],
|
119 |
['Gender:', patient.get('gender', 'β')],
|
120 |
-
['Date:', data.get('visit_date', datetime.now().strftime("%Y-%m-%d"))]
|
121 |
]
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
('BACKGROUND', (0, 0), (1, 0), colors.
|
126 |
-
('
|
127 |
-
('
|
|
|
|
|
128 |
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
|
|
|
|
129 |
]))
|
130 |
|
131 |
-
elems
|
|
|
132 |
|
133 |
-
# Helper to add sections
|
134 |
-
def
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
text = str(text)
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
doc.build(elems)
|
158 |
|
159 |
# Read PDF bytes
|
@@ -219,7 +349,7 @@ def generate_and_download_report():
|
|
219 |
st.session_state.pdf_data = pdf_bytes # Store PDF data for email
|
220 |
|
221 |
# Display success message
|
222 |
-
st.success("
|
223 |
|
224 |
# Create two columns for the download and email buttons
|
225 |
col1, col2 = st.columns(2)
|
@@ -227,7 +357,7 @@ def generate_and_download_report():
|
|
227 |
with col1:
|
228 |
# Offer download
|
229 |
st.download_button(
|
230 |
-
label="
|
231 |
data=pdf_bytes,
|
232 |
file_name=f"medical_report_{datetime.now().strftime('%Y%m%d')}.pdf",
|
233 |
mime="application/pdf",
|
@@ -236,7 +366,7 @@ def generate_and_download_report():
|
|
236 |
|
237 |
with col2:
|
238 |
# Email button that shows email form when clicked
|
239 |
-
if st.button("
|
240 |
st.session_state.show_email_form = True
|
241 |
st.rerun()
|
242 |
|
|
|
6 |
import streamlit as st
|
7 |
from reportlab.lib.pagesizes import A4
|
8 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
9 |
+
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT
|
10 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image
|
11 |
from reportlab.lib import colors
|
12 |
+
from reportlab.lib.units import inch, cm
|
13 |
+
from reportlab.platypus import PageBreak, KeepTogether
|
14 |
|
15 |
from text_processors import format_conversation_history
|
16 |
from model import orchestrator_chat
|
|
|
80 |
fd, temp_path = tempfile.mkstemp(suffix='.pdf')
|
81 |
os.close(fd)
|
82 |
|
83 |
+
# Set up the document with margins
|
84 |
doc = SimpleDocTemplate(
|
85 |
temp_path,
|
86 |
pagesize=A4,
|
87 |
+
rightMargin=1.5*cm,
|
88 |
+
leftMargin=1.5*cm,
|
89 |
+
topMargin=1.5*cm,
|
90 |
+
bottomMargin=2*cm
|
91 |
)
|
92 |
|
93 |
+
# Set up styles with more professional fonts
|
94 |
styles = getSampleStyleSheet()
|
95 |
+
|
96 |
+
# Title style
|
97 |
+
styles.add(ParagraphStyle(
|
98 |
+
name='ReportTitle',
|
99 |
+
fontName='Helvetica-Bold',
|
100 |
+
fontSize=16,
|
101 |
+
leading=20,
|
102 |
+
alignment=TA_CENTER,
|
103 |
+
spaceAfter=0.2*inch,
|
104 |
+
textColor=colors.navy
|
105 |
+
))
|
106 |
+
|
107 |
+
# Section heading style
|
108 |
styles.add(ParagraphStyle(
|
109 |
name='SectionHeading',
|
110 |
+
fontName='Helvetica-Bold',
|
111 |
+
fontSize=12,
|
112 |
+
leading=14,
|
113 |
+
spaceAfter=0.1*inch,
|
114 |
alignment=TA_LEFT,
|
115 |
+
textColor=colors.navy
|
116 |
))
|
117 |
|
118 |
+
# Normal text style
|
119 |
styles.add(ParagraphStyle(
|
120 |
+
name='NormalText',
|
121 |
+
fontName='Helvetica',
|
122 |
+
fontSize=10,
|
123 |
+
leading=12,
|
124 |
+
spaceAfter=0.1*inch,
|
125 |
alignment=TA_LEFT
|
126 |
))
|
127 |
|
128 |
+
# Company info style
|
129 |
+
styles.add(ParagraphStyle(
|
130 |
+
name='CompanyInfo',
|
131 |
+
fontName='Helvetica',
|
132 |
+
fontSize=9,
|
133 |
+
leading=11,
|
134 |
+
alignment=TA_RIGHT,
|
135 |
+
textColor=colors.darkblue
|
136 |
+
))
|
137 |
+
|
138 |
+
# Initialize elements list
|
139 |
elems = []
|
140 |
|
141 |
+
# Add logo and company header
|
142 |
+
logo_path = "src/assets/logo.png" # Adjust path as needed
|
143 |
+
|
144 |
+
# Create a table for the header (logo on left, company info on right)
|
145 |
+
logo_img = Image(logo_path, width=1.5*inch, height=1.0*inch)
|
146 |
+
|
147 |
+
company_info = Paragraph(
|
148 |
+
"""
|
149 |
+
<b>Daease</b><br/>
|
150 |
+
Website: http://daease.com/<br/>
|
151 |
+
Email: [email protected]
|
152 |
+
""",
|
153 |
+
styles['CompanyInfo']
|
154 |
+
)
|
155 |
+
|
156 |
+
header_table = Table([[logo_img, company_info]], colWidths=[doc.width/2.0]*2)
|
157 |
+
header_table.setStyle(TableStyle([
|
158 |
+
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
159 |
+
('ALIGN', (0, 0), (0, 0), 'LEFT'),
|
160 |
+
('ALIGN', (1, 0), (1, 0), 'RIGHT'),
|
161 |
+
('BOTTOMPADDING', (0, 0), (-1, -1), 10),
|
162 |
+
]))
|
163 |
+
|
164 |
+
elems.append(header_table)
|
165 |
+
elems.append(Spacer(1, 0.3*inch))
|
166 |
|
167 |
+
# Add title
|
168 |
+
elems.append(Paragraph("Daease Medical Consultation Report", styles['ReportTitle']))
|
169 |
+
elems.append(Spacer(1, 0.2*inch))
|
170 |
+
|
171 |
+
# Add a horizontal line
|
172 |
+
tbl_line = Table([['']], colWidths=[doc.width], rowHeights=[1])
|
173 |
+
tbl_line.setStyle(TableStyle([
|
174 |
+
('LINEABOVE', (0, 0), (-1, -1), 1, colors.navy),
|
175 |
+
]))
|
176 |
+
elems.append(tbl_line)
|
177 |
+
elems.append(Spacer(1, 0.2*inch))
|
178 |
+
|
179 |
+
# Patient info in a clean table
|
180 |
patient = data.get('patient', {})
|
181 |
+
|
182 |
+
# Format the patient table data
|
183 |
+
patient_data = [
|
184 |
+
['Patient Information', ''],
|
185 |
['Name:', patient.get('name', 'β')],
|
186 |
['Age:', patient.get('age', 'β')],
|
187 |
['Gender:', patient.get('gender', 'β')],
|
188 |
+
['Visit Date:', data.get('visit_date', datetime.now().strftime("%Y-%m-%d"))]
|
189 |
]
|
190 |
|
191 |
+
patient_table = Table(patient_data, colWidths=[doc.width * 0.3, doc.width * 0.7])
|
192 |
+
patient_table.setStyle(TableStyle([
|
193 |
+
('BACKGROUND', (0, 0), (1, 0), colors.navy),
|
194 |
+
('TEXTCOLOR', (0, 0), (1, 0), colors.white),
|
195 |
+
('FONTNAME', (0, 0), (1, 0), 'Helvetica-Bold'),
|
196 |
+
('SPAN', (0, 0), (1, 0)),
|
197 |
+
('ALIGN', (0, 0), (1, 0), 'CENTER'),
|
198 |
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
199 |
+
('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
|
200 |
+
('BOX', (0, 0), (-1, -1), 1, colors.darkblue),
|
201 |
]))
|
202 |
|
203 |
+
elems.append(patient_table)
|
204 |
+
elems.append(Spacer(1, 0.3*inch))
|
205 |
|
206 |
+
# Helper to add sections as tables
|
207 |
+
def add_section_table(title, content):
|
208 |
+
if isinstance(content, list):
|
209 |
+
content = '\n'.join(map(str, content))
|
210 |
+
elif content is not None and not isinstance(content, str):
|
211 |
+
content = str(content)
|
|
|
212 |
|
213 |
+
if not content or content.strip() == '':
|
214 |
+
content = 'β'
|
215 |
+
|
216 |
+
section_data = [
|
217 |
+
[title, ''],
|
218 |
+
[Paragraph(content, styles['NormalText']), '']
|
219 |
+
]
|
220 |
+
|
221 |
+
# Create table and span the content cell
|
222 |
+
table = Table(section_data, colWidths=[doc.width * 0.97, doc.width * 0.03])
|
223 |
+
table.setStyle(TableStyle([
|
224 |
+
('BACKGROUND', (0, 0), (1, 0), colors.lightblue),
|
225 |
+
('TEXTCOLOR', (0, 0), (1, 0), colors.navy),
|
226 |
+
('FONTNAME', (0, 0), (1, 0), 'Helvetica-Bold'),
|
227 |
+
('SPAN', (0, 0), (1, 0)), # Span the header row
|
228 |
+
('SPAN', (0, 1), (1, 1)), # Span the content row
|
229 |
+
('ALIGN', (0, 0), (0, 0), 'LEFT'),
|
230 |
+
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
231 |
+
('BOX', (0, 0), (-1, -1), 1, colors.lightgrey),
|
232 |
+
('LINEBELOW', (0, 0), (1, 0), 1, colors.navy),
|
233 |
+
]))
|
234 |
+
|
235 |
+
elems.append(KeepTogether([table, Spacer(1, 0.1*inch)]))
|
236 |
|
237 |
+
# Add all medical information sections as tables
|
238 |
+
add_section_table("Chief Complaint", data.get('chief_complaint'))
|
239 |
+
add_section_table("History of Present Illness", data.get('history_of_present_illness'))
|
240 |
+
add_section_table("Past Medical History", data.get('past_medical_history'))
|
241 |
+
add_section_table("Medications", data.get('medications'))
|
242 |
+
add_section_table("Allergies", data.get('allergies'))
|
243 |
+
add_section_table("Examination Findings", data.get('examination'))
|
244 |
+
add_section_table("Diagnosis", data.get('diagnosis'))
|
245 |
+
add_section_table("Recommendations", data.get('recommendations'))
|
246 |
+
add_section_table("Reasoning", data.get('reasoning'))
|
247 |
+
add_section_table("Sources", data.get('sources'))
|
248 |
+
|
249 |
+
# Add AI signature section
|
250 |
+
elems.append(Spacer(1, 0.4*inch))
|
251 |
+
|
252 |
+
# Try to add AI signature image if available
|
253 |
+
signature_path = "assets/Logo/LOGO.png" # Fallback to logo if no signature available
|
254 |
+
try:
|
255 |
+
signature_img = Image(signature_path, width=1.2*inch, height=0.8*inch)
|
256 |
+
signature_table = Table([
|
257 |
+
[Paragraph("<i>Generated by</i>", styles['NormalText'])],
|
258 |
+
[signature_img],
|
259 |
+
[Paragraph("<b>Daease AI Medical Assistant</b>", styles['NormalText'])]
|
260 |
+
], colWidths=[doc.width * 0.3])
|
261 |
+
|
262 |
+
signature_table.setStyle(TableStyle([
|
263 |
+
('ALIGN', (0, 0), (0, -1), 'CENTER'),
|
264 |
+
('VALIGN', (0, 0), (0, -1), 'MIDDLE'),
|
265 |
+
]))
|
266 |
+
elems.append(signature_table)
|
267 |
+
except:
|
268 |
+
# Fallback if image can't be loaded
|
269 |
+
elems.append(Paragraph("<i>Generated by Daease AI Medical Assistant</i>", styles['NormalText']))
|
270 |
+
|
271 |
+
# Add disclaimer
|
272 |
+
elems.append(Spacer(1, 0.2*inch))
|
273 |
+
disclaimer = Paragraph(
|
274 |
+
"<i>Disclaimer: This report is generated by AI for informational purposes only. "
|
275 |
+
"It should not replace professional medical advice, diagnosis, or treatment. "
|
276 |
+
"Always consult with a qualified healthcare provider for medical concerns.</i>",
|
277 |
+
ParagraphStyle(
|
278 |
+
name='Disclaimer',
|
279 |
+
fontName='Helvetica-Oblique',
|
280 |
+
fontSize=8,
|
281 |
+
textColor=colors.grey
|
282 |
+
)
|
283 |
+
)
|
284 |
+
elems.append(disclaimer)
|
285 |
+
|
286 |
+
# Build the PDF document
|
287 |
doc.build(elems)
|
288 |
|
289 |
# Read PDF bytes
|
|
|
349 |
st.session_state.pdf_data = pdf_bytes # Store PDF data for email
|
350 |
|
351 |
# Display success message
|
352 |
+
st.success("Report is ready!")
|
353 |
|
354 |
# Create two columns for the download and email buttons
|
355 |
col1, col2 = st.columns(2)
|
|
|
357 |
with col1:
|
358 |
# Offer download
|
359 |
st.download_button(
|
360 |
+
label="Download Report",
|
361 |
data=pdf_bytes,
|
362 |
file_name=f"medical_report_{datetime.now().strftime('%Y%m%d')}.pdf",
|
363 |
mime="application/pdf",
|
|
|
366 |
|
367 |
with col2:
|
368 |
# Email button that shows email form when clicked
|
369 |
+
if st.button("Email Report"):
|
370 |
st.session_state.show_email_form = True
|
371 |
st.rerun()
|
372 |
|