-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreport_generation.py
177 lines (124 loc) · 6.69 KB
/
report_generation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from docx import Document
import docx
from datetime import date
import datetime
from docx.shared import Inches
from docx.shared import Pt
from docx.enum.text import WD_TAB_ALIGNMENT
import os
import subprocess
import tempfile
import io
import pyrebase
import aspose.words as aw
import time
class ReportGeneration():
def generate_report(self, full_name, date_of_birth, email, lesion, risk_factor, processed_image_grid, classification_text, classification_subtext, report_type):
report_number = full_name.lower().replace(" ","")+datetime.datetime.now().strftime("%m%d%Y-%H%M%S")
disclaimer_text = "The results generated by this report using experimental AI (deep learning) and computer vision models developed by Mammory (org) under the supervision of Aga Khan University Hospital are not a substitute for a professional medical evaluation and should not be used to diagnose or treat any medical condition. The results of this test should be considered preliminary and subject to further evaluation by a medical professional. The models were trained on diverse datasets over time, but false predictions are still possible. The entire objective of this application is to assist radiologists, technicians, and doctors in the early detection of symptoms and to prevent delayed treatments. However, Mammory (org) cannot guarantee the accuracy of the results and accepts no liability for any inaccuracies or misinterpretations of test results. These results are not admissible in court and should not be relied upon as a sole source of information."
path = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))).replace("\\","/")
header = path+"/header.jpg"
footer = path+"/footer.jpg"
#create document object to call paragraphs
doc = docx.Document()
#adding header
doc.add_picture(header, width=Inches(5.9), height=Inches(0.75))
#create paragraph object for each section
paragraph1 = doc.add_paragraph()
paragraph1.add_run("Name: ").bold = True
paragraph1.add_run(full_name)
#paragraph 2
paragraph2 = doc.add_paragraph()
paragraph2.add_run("Date of Birth: ").bold = True
paragraph2.add_run(date_of_birth)
#add spacing
tab_stop = paragraph2.paragraph_format.tab_stops.add_tab_stop(Inches(6), WD_TAB_ALIGNMENT.RIGHT)
paragraph2.add_run("\tDate Generated: ").bold = True
paragraph2.add_run(str(date.today()))
#paragraph 3
paragraph3 = doc.add_paragraph()
paragraph3.add_run("Email Address: ").bold = True
paragraph3.add_run(email)
#add spacing
tab_stop = paragraph3.paragraph_format.tab_stops.add_tab_stop(Inches(6), WD_TAB_ALIGNMENT.RIGHT)
paragraph3.add_run("\tReport Number: ").bold = True
paragraph3.add_run(str(report_number))
#separator
doc.add_paragraph().add_run(" ___________________________________________________________________________________")
#paragraph for Lesion
paragraph4 = doc.add_paragraph()
paragraph4.add_run("Lesion Classification").bold = True
tab_stop = paragraph4.paragraph_format.tab_stops.add_tab_stop(Inches(6), WD_TAB_ALIGNMENT.RIGHT)
paragraph4.add_run("\t"+lesion).bold = True
#paragraph for Risk Factor
paragraph5 = doc.add_paragraph()
paragraph5.add_run("Risk Factor").bold = True
tab_stop = paragraph5.paragraph_format.tab_stops.add_tab_stop(Inches(6), WD_TAB_ALIGNMENT.RIGHT)
paragraph5.add_run("\t"+str(risk_factor)+"%").bold = True
#adding classification image
doc.add_picture(processed_image_grid, width=Inches(6.0), height=Inches(2.0))
#adding classification text and subtext
doc.add_paragraph().add_run(classification_text).bold = True
classificationPara = doc.add_paragraph()
classificationPara.add_run(classification_subtext)
#adding disclaimer
disclaimer = doc.add_paragraph()
run = disclaimer.add_run("DISCLAIMER:")
run.font.size = Pt(5)
run.bold = True
run = disclaimer.add_run(disclaimer_text)
run.font.size = Pt(5)
#create name for my docx file
file_name = report_number+".docx"
#adding footer
doc.add_picture(footer, width=Inches(5.9), height=Inches(0.75))
for section in doc.sections:
section.top_margin = docx.shared.Inches(0.5)
section.bottom_margin = docx.shared.Inches(0.5)
#save docx in system
doc.save(file_name)
aspose_doc = aw.Document(file_name)
pdf_file_name = report_number+".pdf"
aspose_doc.save(pdf_file_name, aw.SaveFormat.PDF)
#api_key = "your_api_key"
#client = Client(api_key)
#deleting the docx file
if os.path.exists(file_name):
os.remove(file_name)
data = {"Account ID": email, "Risk Factor": risk_factor, "Message": classification_text, "Lesion": lesion, "Class Type":report_type}
return pdf_file_name, data
def push_to_firebase(self, pdf_file_name, data):
#api configuration
config = {
"apiKey": "AIzaSyAI2y70MRlwzybPmzRsn0eYeNyyAWL34Gs",
"authDomain": "mammory.firebaseapp.com",
"databaseURL": "https://mammory-default-rtdb.firebaseio.com/",
"storageBucket": "mammory.appspot.com",
"projectId": "mammory",
"storageBucket": "mammory.appspot.com",
"messagingSenderId": "384004541434",
"appId": "1:384004541434:web:ff56b697112dbf634f7243",
"measurementId": "G-SS9LV9NW51"
}
#initialize app
firebase = pyrebase.initialize_app(config)
#create an instance for storage which will be used to put the file
storage = firebase.storage()
#select the file to upload, pdf_file_name is the path
pdf_file = open(pdf_file_name, "rb")
#place the file & give the path. PDFs/report-14.pdf is the path to my firebase STORAGE folder where I want to save report-14.pdf
temp_url = storage.child("PDFs/"+pdf_file_name).put(pdf_file)
pdf_file.close()
#to return file URL
url = storage.child("PDFs/"+pdf_file_name).get_url(temp_url['downloadTokens'])
print(url)
#deleting the pdf file from system
if os.path.exists(pdf_file_name):
os.remove(pdf_file_name)
#instance for database
database = firebase.database()
#adding the url to the data dictionary
data["Report_URL"] = str(url)
#To set the values in database
database.push(data)
return data