-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_processor.py
More file actions
50 lines (37 loc) · 1.46 KB
/
Copy pathpdf_processor.py
File metadata and controls
50 lines (37 loc) · 1.46 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import shutil
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.docstore.document import Document
def process_pdf(pdf_path):
if not os.path.exists('pdfs'):
os.makedirs('pdfs')
original_filename = os.path.basename(pdf_path)
new_filename = input(f"Enter a new name for {original_filename} (without .pdf extension): ")
new_filename = f"{new_filename}.pdf"
new_path = os.path.join('pdfs', new_filename)
shutil.copy2(pdf_path, new_path)
print(f"File copied and renamed to: {new_path}")
return new_path
def file_processing(file_path):
loader = PyPDFLoader(file_path)
data = loader.load()
question_gen = ''
for page in data:
question_gen += page.page_content
splitter_ques_gen = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=100
)
chunks_ques_gen = splitter_ques_gen.split_text(question_gen)
document_ques_gen = [Document(page_content=t) for t in chunks_ques_gen]
splitter_ans_gen = RecursiveCharacterTextSplitter(
chunk_size=300,
chunk_overlap=30
)
document_answer_gen = splitter_ans_gen.split_documents(document_ques_gen)
return document_ques_gen, document_answer_gen
if __name__ == "__main__":
pdf_file = input("Enter the path to your PDF file: ")
processed_file = process_pdf(pdf_file)
print(f"PDF processed: {processed_file}")