Dineshpopuri commited on
Commit
c4b931f
·
verified ·
1 Parent(s): 416b9b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -36
app.py CHANGED
@@ -5,6 +5,7 @@ from reportlab.pdfgen import canvas
5
  import bleach
6
  import logging
7
  import os
 
8
  from huggingface_hub import InferenceClient
9
  from retry import retry
10
  import time
@@ -275,63 +276,59 @@ def generate_pdf(score, checklist_summary, missing_summary, checklist_status, lo
275
  qa_report = sanitize_input(qa_report) if qa_report else "No QA report provided"
276
  punch_list_text = sanitize_input(punch_list_text) if punch_list_text else "No punch list provided"
277
 
278
- # Define the temporary file path
279
- pdf_path = "readiness_report.pdf"
 
280
  logging.info(f"Attempting to create PDF at {pdf_path}")
281
 
282
- # Ensure the directory is writable
283
- pdf_dir = os.path.dirname(pdf_path) or "."
284
- if not os.access(pdf_dir, os.W_OK):
285
- logging.error(f"No write permissions in directory: {pdf_dir}")
286
- raise PermissionError(f"No write permissions in directory: {pdf_dir}")
287
-
288
  # Create the PDF using reportlab
289
  c = canvas.Canvas(pdf_path, pagesize=letter)
290
-
291
  # Set initial font and draw title
292
- logging.info("Setting font to Times-Bold for title")
293
  try:
294
- c.setFont("Times-Bold", 16)
295
  except Exception as e:
296
- logging.error(f"Font error: {str(e)}. Falling back to default font.")
297
- c.setFont("Helvetica-Bold", 16) # Fallback font
298
  c.drawString(50, 750, "Project Closure Readiness Report")
299
-
300
  # Draw basic fields
301
- logging.info("Drawing basic fields")
302
  try:
303
- c.setFont("Times-Roman", 12)
304
  except Exception as e:
305
- logging.error(f"Font error: {str(e)}. Falling back to default font.")
306
- c.setFont("Helvetica", 12) # Fallback font
307
  c.drawString(50, 720, f"Readiness Score: {score}%")
308
  c.drawString(50, 700, f"Status: {checklist_status}")
309
- c.drawString(50, 680, "Checklist Summary:")
310
 
311
- # Draw checklist summary with Y-position overflow handling
312
  logging.info("Drawing checklist summary")
313
- y = 660
314
- BOTTOM_MARGIN = 50
315
  LINE_HEIGHT = 20
 
 
 
 
316
  lines = checklist_summary.split("\n")
317
- if not lines:
318
  lines = ["No checklist summary available"]
319
  for line in lines:
320
  # Truncate long lines to prevent overflow
321
- if len(line) > 80: # Approximate character limit per line
322
  line = line[:77] + "..."
323
  if y < BOTTOM_MARGIN:
324
  logging.info("Y position below margin, creating new page")
325
  c.showPage()
326
  try:
327
- c.setFont("Times-Roman", 12)
328
- except:
329
  c.setFont("Helvetica", 12)
 
 
330
  y = 750
331
  c.drawString(50, y, line)
332
  y -= LINE_HEIGHT
333
 
334
- # Draw additional sections
335
  logging.info("Drawing missing summary")
336
  y -= 20
337
  c.drawString(50, y, "Missing Items:")
@@ -343,9 +340,9 @@ def generate_pdf(score, checklist_summary, missing_summary, checklist_status, lo
343
  if y < BOTTOM_MARGIN:
344
  c.showPage()
345
  try:
346
- c.setFont("Times-Roman", 12)
347
- except:
348
  c.setFont("Helvetica", 12)
 
 
349
  y = 750
350
  c.drawString(50, y, line)
351
  y -= LINE_HEIGHT
@@ -354,23 +351,21 @@ def generate_pdf(score, checklist_summary, missing_summary, checklist_status, lo
354
  logging.info("Drawing signature slots")
355
  y -= 20
356
  if y - 40 < BOTTOM_MARGIN:
357
- logging.info("Y position below margin, creating new page")
358
  c.showPage()
359
  try:
360
- c.setFont("Times-Roman", 12)
361
- except:
362
  c.setFont("Helvetica", 12)
 
 
363
  y = 750
364
  c.drawString(50, y - 20, "Stakeholder Signature: ____________________")
365
 
366
  y -= 20
367
  if y - 60 < BOTTOM_MARGIN:
368
- logging.info("Y position below margin, creating new page")
369
  c.showPage()
370
  try:
371
- c.setFont("Times-Roman", 12)
372
- except:
373
  c.setFont("Helvetica", 12)
 
 
374
  y = 750
375
  c.drawString(50, y - 40, "Date: ____________________")
376
 
@@ -388,7 +383,7 @@ def generate_pdf(score, checklist_summary, missing_summary, checklist_status, lo
388
  logging.info(f"Simulated public URL for PDF: {simulated_public_url}")
389
 
390
  logging.info(f"PDF generated successfully at {pdf_path}")
391
- return simulated_public_url, "PDF generation completed. Click the link to download."
392
  except Exception as e:
393
  logging.error(f"Error in generate_pdf: {str(e)}")
394
  raise
 
5
  import bleach
6
  import logging
7
  import os
8
+ import tempfile
9
  from huggingface_hub import InferenceClient
10
  from retry import retry
11
  import time
 
276
  qa_report = sanitize_input(qa_report) if qa_report else "No QA report provided"
277
  punch_list_text = sanitize_input(punch_list_text) if punch_list_text else "No punch list provided"
278
 
279
+ # Use a temporary directory for PDF generation
280
+ with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False, dir="/tmp") as temp_file:
281
+ pdf_path = temp_file.name
282
  logging.info(f"Attempting to create PDF at {pdf_path}")
283
 
 
 
 
 
 
 
284
  # Create the PDF using reportlab
285
  c = canvas.Canvas(pdf_path, pagesize=letter)
286
+
287
  # Set initial font and draw title
288
+ logging.info("Setting font to Helvetica-Bold for title (fallback)")
289
  try:
290
+ c.setFont("Helvetica-Bold", 16)
291
  except Exception as e:
292
+ logging.error(f"Font error: {str(e)}. Using default font.")
293
+ c.setFont("Helvetica", 16)
294
  c.drawString(50, 750, "Project Closure Readiness Report")
295
+
296
  # Draw basic fields
297
+ logging.info("Drawing basic fields with Helvetica")
298
  try:
299
+ c.setFont("Helvetica", 12)
300
  except Exception as e:
301
+ logging.error(f"Font error: {str(e)}. Continuing with default font.")
 
302
  c.drawString(50, 720, f"Readiness Score: {score}%")
303
  c.drawString(50, 700, f"Status: {checklist_status}")
 
304
 
305
+ # Draw checklist summary
306
  logging.info("Drawing checklist summary")
307
+ y = 680
 
308
  LINE_HEIGHT = 20
309
+ BOTTOM_MARGIN = 50
310
+ c.drawString(50, y, "Checklist Summary:")
311
+ y -= LINE_HEIGHT
312
+
313
  lines = checklist_summary.split("\n")
314
+ if not lines or lines == [""]:
315
  lines = ["No checklist summary available"]
316
  for line in lines:
317
  # Truncate long lines to prevent overflow
318
+ if len(line) > 80:
319
  line = line[:77] + "..."
320
  if y < BOTTOM_MARGIN:
321
  logging.info("Y position below margin, creating new page")
322
  c.showPage()
323
  try:
 
 
324
  c.setFont("Helvetica", 12)
325
+ except:
326
+ pass
327
  y = 750
328
  c.drawString(50, y, line)
329
  y -= LINE_HEIGHT
330
 
331
+ # Draw missing summary
332
  logging.info("Drawing missing summary")
333
  y -= 20
334
  c.drawString(50, y, "Missing Items:")
 
340
  if y < BOTTOM_MARGIN:
341
  c.showPage()
342
  try:
 
 
343
  c.setFont("Helvetica", 12)
344
+ except:
345
+ pass
346
  y = 750
347
  c.drawString(50, y, line)
348
  y -= LINE_HEIGHT
 
351
  logging.info("Drawing signature slots")
352
  y -= 20
353
  if y - 40 < BOTTOM_MARGIN:
 
354
  c.showPage()
355
  try:
 
 
356
  c.setFont("Helvetica", 12)
357
+ except:
358
+ pass
359
  y = 750
360
  c.drawString(50, y - 20, "Stakeholder Signature: ____________________")
361
 
362
  y -= 20
363
  if y - 60 < BOTTOM_MARGIN:
 
364
  c.showPage()
365
  try:
 
 
366
  c.setFont("Helvetica", 12)
367
+ except:
368
+ pass
369
  y = 750
370
  c.drawString(50, y - 40, "Date: ____________________")
371
 
 
383
  logging.info(f"Simulated public URL for PDF: {simulated_public_url}")
384
 
385
  logging.info(f"PDF generated successfully at {pdf_path}")
386
+ return simulated_public_url, f"PDF generation completed. File saved at {pdf_path}. Click the link to download."
387
  except Exception as e:
388
  logging.error(f"Error in generate_pdf: {str(e)}")
389
  raise