umangchaudhry commited on
Commit
d094df2
·
verified ·
1 Parent(s): 17ba104

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -11,7 +11,8 @@ from langchain_community.document_loaders import PyPDFLoader
11
  from langchain.vectorstores import FAISS
12
  from langchain_openai import OpenAIEmbeddings
13
  from langchain_text_splitters import RecursiveCharacterTextSplitter
14
- import pdfkit # Added import for pdfkit
 
15
 
16
  # Function to process PDF, run Q&A, and return results
17
  def process_pdf(api_key, uploaded_file, questions_path, prompt_path, display_placeholder):
@@ -79,6 +80,25 @@ def process_pdf(api_key, uploaded_file, questions_path, prompt_path, display_pla
79
 
80
  return qa_results
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  # Streamlit app layout
83
  st.title("Climate Policy Summary Tool")
84
 
@@ -106,9 +126,8 @@ if st.button("Generate") and api_key and uploaded_file:
106
  st.download_button("Download as Markdown", markdown_output, file_name="results.md")
107
 
108
  # Create a PDF file for the user to download
109
- html_content = markdown_output.replace("### ", "<h3>").replace("**", "<b>").replace("\n", "<br>")
110
- pdf_output = pdfkit.from_string(html_content, False)
111
- st.download_button("Download as PDF", pdf_output, file_name="results.pdf")
112
 
113
  except Exception as e:
114
  st.error(f"An error occurred: {e}")
 
11
  from langchain.vectorstores import FAISS
12
  from langchain_openai import OpenAIEmbeddings
13
  from langchain_text_splitters import RecursiveCharacterTextSplitter
14
+ from reportlab.lib.pagesizes import letter
15
+ from reportlab.pdfgen import canvas
16
 
17
  # Function to process PDF, run Q&A, and return results
18
  def process_pdf(api_key, uploaded_file, questions_path, prompt_path, display_placeholder):
 
80
 
81
  return qa_results
82
 
83
+ # Function to create a PDF using reportlab
84
+ def create_pdf(content):
85
+ buffer = BytesIO()
86
+ pdf = canvas.Canvas(buffer, pagesize=letter)
87
+ pdf.setFont("Helvetica", 10)
88
+
89
+ # Start position for writing text
90
+ text = pdf.beginText(40, 750)
91
+
92
+ for line in content.split("\n"):
93
+ text.textLine(line)
94
+
95
+ pdf.drawText(text)
96
+ pdf.showPage()
97
+ pdf.save()
98
+
99
+ buffer.seek(0)
100
+ return buffer
101
+
102
  # Streamlit app layout
103
  st.title("Climate Policy Summary Tool")
104
 
 
126
  st.download_button("Download as Markdown", markdown_output, file_name="results.md")
127
 
128
  # Create a PDF file for the user to download
129
+ pdf_output = create_pdf(markdown_output)
130
+ st.download_button("Download as PDF", data=pdf_output, file_name="results.pdf")
 
131
 
132
  except Exception as e:
133
  st.error(f"An error occurred: {e}")