Spaces:
Sleeping
Sleeping
Download backend/pdf_generator.py from Presight-AI/app: direct link, hf CLI and curl.
- Browser
- Download file 4.78 kB
-
https://huggingface.co/spaces/Presight-AI/app/resolve/main/backend/pdf_generator.py
- Command line
-
hf download hf://spaces/Presight-AI/app/backend/pdf_generator.py
-
curl -L -o pdf_generator.py https://huggingface.co/spaces/Presight-AI/app/resolve/main/backend/pdf_generator.py
4.78 kB
| from reportlab.lib import colors | |
| from reportlab.lib.pagesizes import letter | |
| from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image | |
| from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | |
| from reportlab.lib.units import inch | |
| from io import BytesIO | |
| from datetime import datetime | |
| class PrescriptionPDFGenerator: | |
| def __init__(self): | |
| self.styles = getSampleStyleSheet() | |
| self._create_custom_styles() | |
| def _create_custom_styles(self): | |
| self.styles.add(ParagraphStyle( | |
| name='Header', | |
| parent=self.styles['Heading1'], | |
| fontSize=18, | |
| spaceAfter=20, | |
| textColor=colors.HexColor('#1B67E0') | |
| )) | |
| self.styles.add(ParagraphStyle( | |
| name='SubHeader', | |
| parent=self.styles['Heading2'], | |
| fontSize=14, | |
| spaceAfter=10, | |
| textColor=colors.HexColor('#2C3E50') | |
| )) | |
| self.styles.add(ParagraphStyle( | |
| name='NormalText', | |
| parent=self.styles['Normal'], | |
| fontSize=10, | |
| spaceAfter=6 | |
| )) | |
| self.styles.add(ParagraphStyle( | |
| name='Warning', | |
| parent=self.styles['Normal'], | |
| fontSize=10, | |
| textColor=colors.red, | |
| spaceAfter=6 | |
| )) | |
| def generate_report(self, data): | |
| buffer = BytesIO() | |
| doc = SimpleDocTemplate(buffer, pagesize=letter, | |
| rightMargin=72, leftMargin=72, | |
| topMargin=72, bottomMargin=72) | |
| story = [] | |
| # Header | |
| story.append(Paragraph("Prescription Analysis Report", self.styles['Header'])) | |
| story.append(Paragraph(f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M')}", self.styles['NormalText'])) | |
| story.append(Spacer(1, 12)) | |
| # Patient Info | |
| story.append(Paragraph("Patient Information", self.styles['SubHeader'])) | |
| patient_data = [ | |
| ["Patient Name:", data.get('patient_name', 'N/A')], | |
| ["Doctor Name:", data.get('doctor_name', 'N/A')], | |
| ["Date:", data.get('date', 'N/A')] | |
| ] | |
| t = Table(patient_data, colWidths=[1.5*inch, 4*inch]) | |
| t.setStyle(TableStyle([ | |
| ('FONTNAME', (0,0), (0,-1), 'Helvetica-Bold'), | |
| ('FONTNAME', (1,0), (1,-1), 'Helvetica'), | |
| ('TEXTCOLOR', (0,0), (-1,-1), colors.black), | |
| ('ALIGN', (0,0), (-1,-1), 'LEFT'), | |
| ('VALIGN', (0,0), (-1,-1), 'TOP'), | |
| ])) | |
| story.append(t) | |
| story.append(Spacer(1, 20)) | |
| # Medicines | |
| story.append(Paragraph("Prescribed Medicines", self.styles['SubHeader'])) | |
| medicines = data.get('medicines', []) | |
| if medicines: | |
| med_data = [["Medicine", "Dosage", "Instructions"]] | |
| for med in medicines: | |
| med_data.append([ | |
| med.get('name', 'Unknown'), | |
| f"{med.get('strength', '')} {med.get('frequency', '')}", | |
| med.get('instructions', '') | |
| ]) | |
| t = Table(med_data, colWidths=[2*inch, 1.5*inch, 3*inch]) | |
| t.setStyle(TableStyle([ | |
| ('BACKGROUND', (0,0), (-1,0), colors.HexColor('#E8F2FF')), | |
| ('TEXTCOLOR', (0,0), (-1,0), colors.HexColor('#1B67E0')), | |
| ('ALIGN', (0,0), (-1,-1), 'LEFT'), | |
| ('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'), | |
| ('FONTSIZE', (0,0), (-1,0), 10), | |
| ('BOTTOMPADDING', (0,0), (-1,0), 12), | |
| ('BACKGROUND', (0,1), (-1,-1), colors.white), | |
| ('TEXTCOLOR', (0,1), (-1,-1), colors.black), | |
| ('FONTNAME', (0,1), (-1,-1), 'Helvetica'), | |
| ('FONTSIZE', (0,1), (-1,-1), 9), | |
| ('GRID', (0,0), (-1,-1), 1, colors.HexColor('#EBECF0')), | |
| ('VALIGN', (0,0), (-1,-1), 'MIDDLE'), | |
| ])) | |
| story.append(t) | |
| else: | |
| story.append(Paragraph("No medicines detected.", self.styles['NormalText'])) | |
| story.append(Spacer(1, 20)) | |
| # Health Tip | |
| if data.get('health_tip'): | |
| story.append(Paragraph("Health Tip", self.styles['SubHeader'])) | |
| story.append(Paragraph(data['health_tip'], self.styles['NormalText'])) | |
| story.append(Spacer(1, 20)) | |
| # Disclaimer | |
| story.append(Spacer(1, 30)) | |
| story.append(Paragraph("DISCLAIMER: This report is generated by AI and may contain errors. Please consult a healthcare professional for medical advice.", self.styles['Warning'])) | |
| doc.build(story) | |
| buffer.seek(0) | |
| return buffer |