-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingestion.py
32 lines (26 loc) · 916 Bytes
/
ingestion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
from langchain.document_loaders import PyPDFDirectoryLoader
from langchain.chat_models import ChatOpenAI
from langchain.chains.summarize import load_summarize_chain
from fpdf import FPDF
from pypdf import PdfReader
def get_summary() -> None:
if os.path.isfile("pdfs/summary.pdf"):
reader = PdfReader("pdfs/summary.pdf")
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
return text
pdf_path = "pdfs/"
loader = PyPDFDirectoryLoader(path=pdf_path)
documents = loader.load()
print(len(documents))
llm = ChatOpenAI(temperature=0, model_name="gpt-4")
chain = load_summarize_chain(llm, chain_type="refine", verbose=True)
resutl = chain.run(documents)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=15)
pdf.multi_cell(0, 10, resutl)
pdf.output("pdfs/summary.pdf")
return resutl