from PyPDF2 import PdfReader
from langchain.docstore.document import Document
from langchain import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain.chains.summarize import load_summarize_chain
from dotenv import load_dotenv
import streamlit as st
from fpdf import FPDF
def get_pdf_text(pdf_docs):
text = ""
pdf_reader = PdfReader(pdf_docs)
for page in pdf_reader.pages:
text += page.extract_text()
return text
def generate_pdf(summary_text):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.write(1, summary_text)
pdf_output = pdf.output(dest='S').encode('latin1')
return pdf_output
def main():
load_dotenv()
st.set_page_config(page_title="Upload PDF to get summary", page_icon=":books:")
st.header("Upload your PDF to get summary :books:")
pdf_docs = st.file_uploader("Upload your PDF:")
if pdf_docs is None or pdf_docs == "":
st.info("Please upload a PDF file.")
else:
if st.button("Process"):
with st.spinner("Processing..."):
try:
pdf_text = get_pdf_text(pdf_docs)
documents = [Document(page_content=pdf_text)]
llm = ChatOpenAI(temperature=0, model='gpt-3.5-turbo')
template = '''As an expert in reverse analyzing resumes, your task is to reverse the order of skills provided in the resume. For example, if the uploaded PDF lists skills as:
- HTML
- CSS
- Java
You will reverse them to output:
- Java
- CSS
- HTML
Similarly, if the skills in the uploaded PDF are:
- SQL
- Flask
- Python
You will reverse them to output:
- Python
- Flask
- SQL
You should keep the rest of the text as it is, only reversing the order of the skills.
Resume : `{text}`
'''
prompt = PromptTemplate(
input_variables=['text'],
template=template
)
chain = load_summarize_chain(
llm,
chain_type='stuff',
prompt=prompt,
verbose=False
)
# response = chain.run(documents)
# st.text_area("Summary:", value=response, height=400)
# Add button to generate PDF
if st.button("Download PDF"):
response = chain.run(documents)
pdf_output = generate_pdf(response)
st.download_button(label="Download Summary PDF",
data=pdf_output,
file_name="summary.pdf",
mime="application/pdf")
except Exception as e:
st.error(f"An error occurred: {str(e)}")
if __name__ == "__main__":
main()