dwipper commited on
Commit
392a3e7
·
1 Parent(s): 16a006e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import openai
3
  import datetime
4
  import time
@@ -6,9 +7,9 @@ import gradio as gr
6
  import json
7
  from jinja2 import Template
8
  import requests
9
- #from pdfminer.high_level import extract_text
10
  import fitz
11
- import pdfkit
 
12
 
13
  # Initialize OpenAI
14
  openai.api_key = os.environ.get('OPENAI_API_KEY')
@@ -346,20 +347,33 @@ def pdf_to_text(contract_file_cmpt, contract_text_tbox, file_name_tbox):
346
 
347
  return file_text, redline_file_name
348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
  def download_pdf(compliance_comments,contract_redline_html,file_name_tbox):
350
-
351
- #config = pdfkit.configuration(wkhtmltopdf="/tmp/pip-ephem-wheel-cache-4svwmn1h/wheels/c4/fe/24/47a8dabc269dcd143551749339e4d8bbc2ca2002ee03943027")
352
- #contract_redline_comments = "<h2><u>Contract Redline:</u></h2><br>"+contract_redline_html + "<br><h2><u>Compliance Comments:</u></h2><br>"+compliance_comments
353
- global pdf_download
354
- #pdf_download = pdfkit.from_string(contract_redline_comments,None,configuration=config)
355
- with open('NILI Contract Redline.pdf', 'rb') as file:
356
- pdf_download = file.read()
357
-
358
- #with open("Champion Contract Test.pdf", 'wb') as file:
359
- #file.write(pdf_download)
360
-
361
- #return {pdf_download_file: file_name_tbox}
362
- return {pdf_download_file: "NILI Contract Redline.pdf"}
363
 
364
  # Gradio UI
365
  CIMStheme = gr.themes.Soft().set(
 
1
  import os
2
+ import io
3
  import openai
4
  import datetime
5
  import time
 
7
  import json
8
  from jinja2 import Template
9
  import requests
 
10
  import fitz
11
+ from xhtml2pdf import pisa
12
+ from io import BytesIO
13
 
14
  # Initialize OpenAI
15
  openai.api_key = os.environ.get('OPENAI_API_KEY')
 
347
 
348
  return file_text, redline_file_name
349
 
350
+ def convert_html_to_pdf(source_html, output_filename):
351
+ # Result file stream
352
+ result_file = open(output_filename, "w+b")
353
+
354
+ # Convert HTML to PDF
355
+ pisa_status = pisa.CreatePDF(
356
+ BytesIO(source_html.encode("UTF-8")), # HTML content
357
+ dest=result_file) # File handle to receive the result
358
+
359
+ # Close the result file
360
+ result_file.close()
361
+
362
+ # Return True on success and False on errors
363
+ return pisa_status.err
364
+
365
+
366
  def download_pdf(compliance_comments,contract_redline_html,file_name_tbox):
367
+ #config = pdfkit.configuration(wkhtmltopdf="/usr/local/bin/wkhtmltopdf")
368
+
369
+ contract_redline_comments = "<h2><u>Contract Redline:</u></h2><br>"+contract_redline_html + "<br><h2><u>Compliance Comments:</u></h2><br>"+compliance_comments
370
+
371
+ #global pdf_download
372
+ #pdf_download = pdfkit.from_string(contract_redline_comments,file_name_tbox,configuration=config)
373
+
374
+ convert_html_to_pdf(contract_redline_comments, file_name_tbox)
375
+
376
+ return {pdf_download_file: file_name_tbox}
 
 
 
377
 
378
  # Gradio UI
379
  CIMStheme = gr.themes.Soft().set(