KevinGeng commited on
Commit
af7ae63
1 Parent(s): 1ab88a9

upgrade gradio to 4.26, better PDF report support

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +16 -6
  3. local/pdf_generator.py +186 -21
  4. requirements.txt +1 -1
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 📊
4
  colorFrom: pink
5
  colorTo: gray
6
  sdk: gradio
7
- sdk_version: 4.9.1
8
  app_file: app.py
9
  license: apache-2.0
10
  ---
 
4
  colorFrom: pink
5
  colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 4.36.0
8
  app_file: app.py
9
  license: apache-2.0
10
  ---
app.py CHANGED
@@ -185,7 +185,8 @@ examples = [
185
  def change_button_visibility(list_of_conponents):
186
  for comp in list_of_conponents:
187
  comp.value is not None
188
-
 
189
  # add java script code for front right logo
190
 
191
  # Inputs
@@ -216,9 +217,10 @@ pitch_plot = gr.Plot(label="Pitch Contour and dB Analysis", show_label=False, co
216
  patient_name=gr.Textbox(placeholder="Patient Name", label="Patient Name", type="text")
217
  slp_name = gr.Textbox(placeholder="SLP Name", label="SLP Name", type="text")
218
 
219
- output_report = gr.File(interactive=False, type="filepath")
220
  # Report
221
- report_trigger = gr.Button(value="Download Report", elem_classes="primary", variant="primary", visible=True)
 
222
 
223
  # Analysis
224
  run_all = gr.Button(value="Submit", elem_classes="primary", variant="primary")
@@ -228,6 +230,13 @@ with gr.Blocks(theme="KevinGeng/Laronix_ver2") as demo:
228
  # gr.HTML(description, elem_classes="description")
229
  with gr.Row():
230
  with gr.Column(scale=1):
 
 
 
 
 
 
 
231
  gr.Markdown('''
232
  # 1. Choose a sentence to read.
233
  Copy & paste or type your sentence in the "Reference" field. \n
@@ -258,7 +267,7 @@ with gr.Blocks(theme="KevinGeng/Laronix_ver2") as demo:
258
  with gr.Row():
259
  clear = gr.ClearButton(components=[ref, input_audio, exp_id], value="Clear All")
260
  run_all.render()
261
- run_all.click(fn=calc_mos, inputs=[exp_id, input_audio, ref, patient_name, slp_name], outputs=[nat_score, nat_plot, int_score, int_plot, hyp, phonemes, spk_rate, pitch_plot, output_report, Mean_Pitch, Mean_DB])
262
  # once run button is clicked, show nat_score, nat_plot, int_score, int_plot, these are invisible by default
263
  with gr.Row():
264
  gr.HTML("<hr>")
@@ -306,11 +315,12 @@ with gr.Blocks(theme="KevinGeng/Laronix_ver2") as demo:
306
  # Once the Submit button is clicked, nat_plot.value , int_plot.value are not None, the Download Report button will be visible
307
 
308
  # generate report
309
- patient_name.render()
310
- slp_name.render()
311
  output_report.render()
312
  report_trigger.render()
313
 
 
314
 
315
  demo.launch(share=True, allowed_paths=["local"])
316
 
 
185
  def change_button_visibility(list_of_conponents):
186
  for comp in list_of_conponents:
187
  comp.value is not None
188
+
189
+
190
  # add java script code for front right logo
191
 
192
  # Inputs
 
217
  patient_name=gr.Textbox(placeholder="Patient Name", label="Patient Name", type="text")
218
  slp_name = gr.Textbox(placeholder="SLP Name", label="SLP Name", type="text")
219
 
220
+ output_report = gr.File(interactive=False, type="filepath", visible=False)
221
  # Report
222
+
223
+ report_trigger = gr.DownloadButton(label="Download Report", value="report.pdf", elem_classes="primary", variant="primary")
224
 
225
  # Analysis
226
  run_all = gr.Button(value="Submit", elem_classes="primary", variant="primary")
 
230
  # gr.HTML(description, elem_classes="description")
231
  with gr.Row():
232
  with gr.Column(scale=1):
233
+ gr.Markdown('''
234
+ # 0. Input User Information.
235
+ ''')
236
+ with gr.Row():
237
+ patient_name.render()
238
+ slp_name.render()
239
+
240
  gr.Markdown('''
241
  # 1. Choose a sentence to read.
242
  Copy & paste or type your sentence in the "Reference" field. \n
 
267
  with gr.Row():
268
  clear = gr.ClearButton(components=[ref, input_audio, exp_id], value="Clear All")
269
  run_all.render()
270
+ run_all.click(fn=calc_mos, inputs=[exp_id, input_audio, ref, patient_name, slp_name], outputs=[nat_score, nat_plot, int_score, int_plot, hyp, phonemes, spk_rate, pitch_plot, output_report, Mean_Pitch, Mean_DB], scroll_to_output=True)
271
  # once run button is clicked, show nat_score, nat_plot, int_score, int_plot, these are invisible by default
272
  with gr.Row():
273
  gr.HTML("<hr>")
 
315
  # Once the Submit button is clicked, nat_plot.value , int_plot.value are not None, the Download Report button will be visible
316
 
317
  # generate report
318
+ # patient_name.render()
319
+ # slp_name.render()
320
  output_report.render()
321
  report_trigger.render()
322
 
323
+
324
 
325
  demo.launch(share=True, allowed_paths=["local"])
326
 
local/pdf_generator.py CHANGED
@@ -6,8 +6,12 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Tabl
6
  from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
7
  # from indicator_plot import Intelligibility_Plot, Naturalness_Plot
8
  # from pitch_contour import draw_spec_db_pitch
 
 
 
9
  import io
10
 
 
11
  class MedicalReport:
12
  def __init__(self, patient_name, date, doctor_name, Nat_plot, Int_plot, ref, hyp, pitch_plot):
13
 
@@ -21,40 +25,59 @@ class MedicalReport:
21
  self.pitch_plot = Image(io.BytesIO(pitch_plot), width=500, height=250)
22
 
23
  def generate_report(self, filename):
24
- doc = SimpleDocTemplate(filename, pagesize=letter)
 
25
  styles = getSampleStyleSheet()
26
- right_logo = "./local/laronix_logo.jpg" # Change this to the path of your logo image
 
 
 
 
 
 
27
 
 
 
 
 
 
 
 
 
28
  elements = []
29
-
30
  # Add logo on the right side
31
- logo = Image(right_logo, width=100, height=35)
32
- logo.hAlign = 'RIGHT'
 
 
33
  elements.append(logo)
34
- # add html header
35
 
36
- # add split line
37
- elements.append(Paragraph("<br></br>", styles["Normal"]))
38
- # Add report content
39
- elements.append(Paragraph("<b>Laronix SLP Report</b>", styles["Title"]))
40
- elements.append(Paragraph(f"<b>Tester:</b> {self.patient_name}", styles["Normal"]))
41
- elements.append(Paragraph(f"<b>SLP:</b> {self.doctor_name}", styles["Normal"]))
42
- elements.append(Paragraph(f"<b>Date:</b> {self.date}", styles["Normal"]))
43
 
 
 
 
 
44
  elements.append(Paragraph("<br></br>", styles["Normal"]))
45
 
 
 
46
  # Add ref and hyp
47
- elements.append(Paragraph("<br></br><br/>", styles["Normal"])) # Add some space
48
  elements.append(Paragraph(f"<b>Reference:</b> {self.ref}", styles["Normal"]))
49
  elements.append(Paragraph(f"<b>Hypothesis:</b> {self.hyp}", styles["Normal"]))
50
- elements.append(Paragraph("<br></br><br/>", styles["Normal"]))
51
 
 
 
52
  # Create a table with two columns to organize the plots
53
  data = [
54
- [Paragraph(f"<b>Naturalness Score:</b>", styles["Normal"]), Paragraph(f"<b>Intelligibility Score:</b>", styles["Normal"])],
55
  [self.Nat_plot, self.Int_plot],
56
  ]
57
- table = Table(data, colWidths=[200, 200]) # Adjust colWidths as needed
58
 
59
  # Add some space between the plots
60
  table.setStyle(TableStyle([('VALIGN', (0, 0), (-1, -1), 'TOP')]))
@@ -62,14 +85,14 @@ class MedicalReport:
62
 
63
  # Add the table to the elements list
64
  elements.append(table)
65
- elements.append(Paragraph(f"<b>Pitch Plot:</b>", styles["Normal"]))
66
  elements.append(self.pitch_plot)
67
  # Build the PDF
68
  doc.build(elements)
69
 
70
  return filename
71
 
72
- # # Example usage:
73
  # patient_name = "John Doe"
74
  # AVA_history = 35
75
  # # Nat_plot = "./local/laronix_logo.jpg"
@@ -88,6 +111,148 @@ class MedicalReport:
88
  # pitch_plot = pitch_plot_buffer.read()
89
  # pitch_plot_buffer.close()
90
 
91
- # import pdb; pdb.set_trace()
92
  # report = MedicalReport(patient_name, date, doctor_name, Nat_plot, Int_plot, ref, hyp, pitch_plot)
93
- # x = report.generate_report("medical_report.pdf")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
7
  # from indicator_plot import Intelligibility_Plot, Naturalness_Plot
8
  # from pitch_contour import draw_spec_db_pitch
9
+ from reportlab.pdfbase import pdfmetrics
10
+ from reportlab.pdfbase.ttfonts import TTFont
11
+
12
  import io
13
 
14
+
15
  class MedicalReport:
16
  def __init__(self, patient_name, date, doctor_name, Nat_plot, Int_plot, ref, hyp, pitch_plot):
17
 
 
25
  self.pitch_plot = Image(io.BytesIO(pitch_plot), width=500, height=250)
26
 
27
  def generate_report(self, filename):
28
+ doc = SimpleDocTemplate(filename, pagesize=letter, topMargin=0, bottomMargin=0)
29
+
30
  styles = getSampleStyleSheet()
31
+ # remove top margin
32
+ styles["Normal"].spaceBefore = 0.02 * inch
33
+ # Change the color of heading2 to green
34
+ styles["Heading2"].textColor = colors.green
35
+ # Change the color of heading2 to green but lighter, using RGB
36
+ # make heading3 50% transparent
37
+ styles["Heading3"].textColor.alpha = 0.7
38
 
39
+ # add under line to heading2
40
+ right_logo = "local/laronix-ai-logo-header.jpg" # Change this to the path of your logo image
41
+ # load muli ttf and register it
42
+ pdfmetrics.registerFont(TTFont('Muli', 'local/Muli/Muli.ttf'))
43
+
44
+ # change default font to muli
45
+ styles["Normal"].fontName = "Muli"
46
+
47
  elements = []
 
48
  # Add logo on the right side
49
+ # add header, logo width == page width
50
+ logo = Image(right_logo, width=600, height=85)
51
+
52
+ # Align the logo in the center of the thw header
53
  elements.append(logo)
 
54
 
55
+ # add execute date on top right of the page
56
+ elements.append(Paragraph(f"Date: {self.date}", styles["BodyText"]))
57
+
58
+ # Patient name, doctor name, and date
59
+ elements.append(Paragraph("User Information", styles["Heading2"]))
 
 
60
 
61
+ elements.append(Paragraph("<br></br>", styles["Normal"]))
62
+ elements.append(Paragraph("<br></br>", styles["Normal"]))
63
+ elements.append(Paragraph(f"<b>Participant:</b> {self.patient_name}, <b>\t Instructor:</b> {self.doctor_name}", styles["Normal"]))
64
+
65
  elements.append(Paragraph("<br></br>", styles["Normal"]))
66
 
67
+ elements.append(Paragraph(f"<b>AI Analysis</b>", styles["Heading2"]))
68
+ elements.append(Paragraph("Speech Recognition", styles["Heading3"]))
69
  # Add ref and hyp
 
70
  elements.append(Paragraph(f"<b>Reference:</b> {self.ref}", styles["Normal"]))
71
  elements.append(Paragraph(f"<b>Hypothesis:</b> {self.hyp}", styles["Normal"]))
 
72
 
73
+
74
+ # AI Analysis
75
  # Create a table with two columns to organize the plots
76
  data = [
77
+ [Paragraph(f"<b>Naturalness Score:</b>", styles["Heading3"]), Paragraph(f"<b>Intelligibility Score:</b>", styles["Heading3"])],
78
  [self.Nat_plot, self.Int_plot],
79
  ]
80
+ table = Table(data, colWidths=[230, 230]) # Adjust colWidths as needed
81
 
82
  # Add some space between the plots
83
  table.setStyle(TableStyle([('VALIGN', (0, 0), (-1, -1), 'TOP')]))
 
85
 
86
  # Add the table to the elements list
87
  elements.append(table)
88
+ elements.append(Paragraph(f"<b>Acoustic Analysis</b>", styles["Heading2"]))
89
  elements.append(self.pitch_plot)
90
  # Build the PDF
91
  doc.build(elements)
92
 
93
  return filename
94
 
95
+ # Example usage:
96
  # patient_name = "John Doe"
97
  # AVA_history = 35
98
  # # Nat_plot = "./local/laronix_logo.jpg"
 
111
  # pitch_plot = pitch_plot_buffer.read()
112
  # pitch_plot_buffer.close()
113
 
114
+ # # import pdb; pdb.set_trace()
115
  # report = MedicalReport(patient_name, date, doctor_name, Nat_plot, Int_plot, ref, hyp, pitch_plot)
116
+ # x = report.generate_report("medical_report.pdf")
117
+
118
+
119
+ # from reportlab.pdfgen import canvas
120
+ # from reportlab.lib.pagesizes import letter
121
+ # from reportlab.lib.units import inch
122
+ # from reportlab.pdfbase.ttfonts import TTFont
123
+ # from reportlab.pdfbase import pdfmetrics
124
+ # from reportlab.lib.pagesizes import letter
125
+ # from reportlab.pdfgen import canvas
126
+ # from reportlab.lib import colors
127
+ # from reportlab.lib.units import inch
128
+ # from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle
129
+ # from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
130
+ # # from indicator_plot import Intelligibility_Plot, Naturalness_Plot
131
+ # # from pitch_contour import draw_spec_db_pitch
132
+ # import io
133
+
134
+
135
+ # # Paths to the font files
136
+ # poppins_font_path = 'local/Poppins/Poppins-Bold.ttf' # Replace with the path to the Poppins font file
137
+ # muli_font_path = 'local/Muli/Muli-Bold.ttf' # Replace with the path to the Muli font file
138
+
139
+ # # Register the fonts
140
+ # pdfmetrics.registerFont(TTFont('Poppins', poppins_font_path))
141
+ # pdfmetrics.registerFont(TTFont('Muli-Bold', muli_font_path))
142
+
143
+ # # The path to the header image file
144
+ # header_image_path = "/home/kevingeng/SSDisk/Laronix_voice_quality_checking_system_MICROPHONE/local/laronix_logo.jpg"
145
+
146
+ # # Details for the content
147
+ # user_name = "John Doe"
148
+ # instructor_name = "Jane Smith"
149
+ # testing_date = "2024-04-18"
150
+ # spectrogtam_path = "003.jpg"
151
+
152
+ # # Function to create a PDF with the new header and content
153
+ # def create_pdf_with_header_and_content(output_path, header_path, user, instructor, date, spectrogram_path, mean_pitch, mean_decibel, ref, hyp):
154
+
155
+ # c = canvas.Canvas(output_path, pagesize=letter)
156
+ # width, height = letter # Get the dimensions of the letter size
157
+
158
+ # # Header image details
159
+ # # Make Header image width 3 inches
160
+
161
+ # header_image_width = 3* inch
162
+ # header_image_height = 0.5 * inch
163
+
164
+ # # Calculate the position for the header
165
+ # x_position = (width - header_image_width) / 2
166
+ # y_position = height - header_image_height - 0.5 * inch # 0.5 inch from the top of the page
167
+
168
+ # # Draw the header image
169
+ # c.drawImage(header_path, x_position, y_position, width=header_image_width, height=header_image_height, preserveAspectRatio=True)
170
+
171
+ # # make smaller padding for the header
172
+
173
+ # # Title details
174
+ # title_text = "Voice Analysis Report"
175
+ # c.setFont("Poppins", 16)
176
+ # title_text_width = c.stringWidth(title_text, "Poppins", 16)
177
+ # title_x_position = (width - title_text_width) / 2
178
+ # title_y_position = y_position - 0.2 * inch # 0.5 inch below the header image
179
+ # # Draw the title
180
+ # c.drawString(title_x_position, title_y_position, title_text)
181
+
182
+ # # Content position details, move the content to up right next to the header
183
+ # name_x_position = x_position + header_image_width + 0.5 * inch
184
+ # content_x_position = 0.5 * inch
185
+ # content_y_position = y_position
186
+
187
+ # # User name details
188
+ # c.setFont("Muli-Bold", 12)
189
+ # c.drawString(name_x_position, content_y_position, f"User: {user}")
190
+ # # Instructor name details
191
+ # c.drawString(name_x_position, content_y_position - 0.3 * inch, f"Instructor: {instructor}")
192
+ # # Testing date details
193
+ # c.drawString(name_x_position, content_y_position - 0.6 * inch, f"Date: {date}")
194
+
195
+ # # # Draw a line to separate the header from the content
196
+ # # c.line(0, content_y_position - 0.8 * inch, width, content_y_position - 0.8 * inch)
197
+
198
+ # # # Add mean pitch and decibel level in one line, still in the center of the page
199
+ # # c.setFont("Muli-Bold", 12)
200
+ # # c.drawString((width - 7 * inch) / 2, content_y_position - 1 * inch, f"Mean Pitch: {mean_pitch} Hz, Mean Decibel: {mean_decibel} dB")
201
+
202
+ # # # Add reference and hypothesis in two line
203
+ # # c.setFont("Muli-Bold", 12)
204
+ # # c.drawString(content_x_position, content_y_position - 1.5 * inch, f"Reference: {ref}")
205
+ # # c.drawString(content_x_position, content_y_position - 2 * inch, f"Hypothesis: {hyp}")
206
+
207
+ # # # Spectrogram and pitch contour details
208
+ # # spectrogram_image_height = 4 * inch # Adjust as needed
209
+ # # # position the spectrogram image, under the split line, in the center of row
210
+ # # spectrogram_start_x_position = (width - 7* inch) / 2
211
+ # # spectrogram_start_y_position = content_y_position - 1 * inch - spectrogram_image_height
212
+
213
+ # # # Draw the spectrogram image
214
+ # # c.drawImage(spectrogram_path, spectrogram_start_x_position, spectrogram_start_y_position, width=7* inch, height=spectrogram_image_height, preserveAspectRatio=True)
215
+
216
+ # doc = SimpleDocTemplate(output_path, pagesize=letter)
217
+ # styles = getSampleStyleSheet()
218
+
219
+ # elements = []
220
+
221
+ # # Add report content
222
+ # elements.append(Paragraph(f"<b>Tester:</b> {user}", styles["Normal"]))
223
+ # elements.append(Paragraph(f"<b>SLP:</b> {self}", styles["Normal"]))
224
+ # elements.append(Paragraph(f"<b>Date:</b> {self.date}", styles["Normal"]))
225
+
226
+ # elements.append(Paragraph("<br></br>", styles["Normal"]))
227
+
228
+ # # Add ref and hyp
229
+ # elements.append(Paragraph("<br></br><br/>", styles["Normal"])) # Add some space
230
+ # elements.append(Paragraph(f"<b>Reference:</b> {self.ref}", styles["Normal"]))
231
+ # elements.append(Paragraph(f"<b>Hypothesis:</b> {self.hyp}", styles["Normal"]))
232
+ # elements.append(Paragraph("<br></br><br/>", styles["Normal"]))
233
+
234
+ # # Create a table with two columns to organize the plots
235
+ # data = [
236
+ # [Paragraph(f"<b>Naturalness Score:</b>", styles["Normal"]), Paragraph(f"<b>Intelligibility Score:</b>", styles["Normal"])],
237
+ # [self.Nat_plot, self.Int_plot],
238
+ # ]
239
+ # table = Table(data, colWidths=[200, 200]) # Adjust colWidths as needed
240
+
241
+ # # Add some space between the plots
242
+ # table.setStyle(TableStyle([('VALIGN', (0, 0), (-1, -1), 'TOP')]))
243
+ # elements.append(Spacer(1, 12))
244
+
245
+ # # Add the table to the elements list
246
+ # elements.append(table)
247
+ # elements.append(Paragraph(f"<b>Pitch Plot:</b>", styles["Normal"]))
248
+ # elements.append(self.pitch_plot)
249
+ # # Build the PDF
250
+ # doc.build(elements)
251
+ # # Save the canvas
252
+ # c.save()
253
+
254
+ # # Specify the path for the new PDF
255
+ # new_pdf_path = '/home/kevingeng/SSDisk/Laronix_voice_quality_checking_system_MICROPHONE/report.pdf'
256
+
257
+ # # Create the new PDF with the header and content
258
+ # create_pdf_with_header_and_content(new_pdf_path, header_image_path, user_name, instructor_name, testing_date, spectrogtam_path, 100, 100, "ref", "hyp")
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio==4.9.1
2
  pytorch_lightning==1.5.9
3
  torch==1.12.1
4
  jiwer
 
1
+ gradio==4.26.0
2
  pytorch_lightning==1.5.9
3
  torch==1.12.1
4
  jiwer