-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_pdf.py
184 lines (159 loc) · 6.85 KB
/
clean_pdf.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
import os
import textract
from PyPDF2 import PdfReader, PdfWriter
import openai,os,sys
# read in the pdf file
def clean_pdf(configs):
pdf_file = configs['input_file']
tmpdir = configs['tmpdir']
start_page = configs['start_page']
if start_page == -1:
start_page = 0
end_page = configs['end_page']
if tmpdir == '':
tmpdir = pdf_file.split('.')[0] + '_tmp'
# split into pages, save
if not os.path.exists(tmpdir):
os.mkdir(tmpdir)
# get the number of pages
pdf = PdfReader(open(pdf_file, 'rb'))
num_pages = len(pdf.pages)
assert end_page <= num_pages, 'end_page is greater than the number of pages in the pdf'
if end_page == -1:
end_page = num_pages
def pdf_to_text():
# write each page to tmpdir
for i in range(start_page, end_page):
pdf_writer = PdfWriter()
pdf_writer.add_page(pdf.pages[i])
output_filename = '{}/page_{}.pdf'.format(tmpdir, i)
# page_text = textract.process('{}/page_{}.pdf'.format(tmpdir, ii))
with open(output_filename, 'wb') as out:
pdf_writer.write(out)
page_text = textract.process(output_filename)
output_filename_txt = '{}/page_{}.txt'.format(tmpdir, i)
with open(output_filename_txt, 'wb') as out:
out.write(page_text)
# remove pdf page
os.remove(output_filename)
def text_to_cleaned():
# now extract only the text from the first page using textract
pre_prompt = configs['pre_prompt']
for ii in range(start_page, end_page):
print("Cleaning page {}".format(ii))
output_filename_txt = '{}/page_{}.txt'.format(tmpdir, ii)
text = textract.process(output_filename_txt).decode('utf-8')
prompt = f'{pre_prompt}\nSTART\n{text}'
completions = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.05,
max_tokens=2000,
)
message = completions['choices'][0]['text']
# if 'pre_prompt_2' in configs:
# pre_prompt_2 = configs['pre_prompt_2']
# prompt = f'{pre_prompt_2}\nSTART\n{message}'
# completions = openai.Completion.create(
# model="text-davinci-003",
# prompt=prompt,
# temperature=0.05,
# max_tokens=2000,
# )
# message = completions['choices'][0]['text']
output_filename = '{}/page_{}_cleaned{}.txt'.format(tmpdir, ii, configs['suffix'])
#print(prompt)
#print(message)
#print("\n\n\n\n\n\n")
with open(output_filename, 'w') as out:
out.write(message)
def cleaned_to_merged():
# now merge all the pages into one file
with open(f"{configs['output_file']}", 'w') as out:
for ii in range(start_page, end_page):
output_filename = '{}/page_{}_cleaned{}.txt'.format(tmpdir, ii, configs['suffix'])
with open(output_filename, 'r') as f:
if configs['remove_newlines']:
cleaned_text = f.read().replace('\n', ' ')
else:
cleaned_text = f.read()
out.write(cleaned_text)
if configs['print_page_breaks']:
out.write(f'\n\n\n\n\n\n\n PAGE {ii} ENDS\n\n\n\n\n\n\n')
if configs['output_to_pdf']:
# write the contents of configs['output_file'] to configs['output_file'].pdf in pdf format
# use pandoc
pdf_output_file = configs['output_file'].split('.')[0] + '.pdf'
os.system(f'pandoc {configs["output_file"]} -o {pdf_output_file} --pdf-engine=xelatex')
def raw_to_merged():
# now merge all the pages into one file
with open(configs['output_file_raw'], 'w') as out:
for ii in range(start_page, end_page):
output_filename = '{}/page_{}.txt'.format(tmpdir, ii)
with open(output_filename, 'r') as f:
out.write(f.read())
out.write(f'\n\n\n\n\n\n\n PAGE {ii} ENDS\n\n\n\n\n\n\n')
pdf_to_text()
raw_to_merged()
text_to_cleaned()
cleaned_to_merged()
if __name__ == '__main__':
from run_config import configs
clean_pdf(configs)
'''
# Validate OPENAI_API_KEY
if 'OPENAI_API_KEY' not in os.environ:
print('OPENAI_API_KEY not found in environment variables')
print('Please set the OPENAI_API_KEY environment variable')
print('Aborting...')
sys.exit(1)
else:
try:
openai.Engine.list()
print('OPENAI_API_KEY is valid')
except:
print('OPENAI_API_KEY is invalid')
print('Please check the OPENAI_API_KEY environment variable')
print('Aborting...')
sys.exit(1)
print('Enter input file path:')
configs['input_file'] = input()
configs['output_file'] = f"{configs['input_file'].split('.')[0]}_cleaned{configs['suffix']}.txt"
print(f'Enter output file path (Press enter to default to {configs["output_file"]}):')
configs['output_file'] = input()
if configs['output_file'] == '':
configs['output_file'] = f"{configs['input_file'].split('.')[0]}_cleaned{configs['suffix']}.txt"
configs['output_file_raw'] = f"{configs['input_file'].split('.')[0]}_raw.txt"
if configs['output_file'] == '':
configs['output_file_raw'] = f"{configs['input_file'].split('.')[0]}_raw.txt"
configs['tmpdir'] = f"{configs['input_file'].split('.')[0]}_tmp"
print('Enter start page (Press enter to default to 0):')
configs['start_page'] = input()
if configs['start_page'] == '':
configs['start_page'] = 0
else:
configs['start_page'] = int(configs['start_page'])
print('Enter end page (Press enter to default to -1):')
configs['end_page'] = input()
if configs['end_page'] == '':
configs['end_page'] = -1
else:
configs['end_page'] = int(configs['end_page'])
print('Enter tmpdir (Press enter to default to {}):'.format(configs['tmpdir']))
configs['tmpdir'] = input()
if configs['tmpdir'] == '':
configs['tmpdir'] = f"{configs['input_file'].split('.')[0]}_tmp"
print('Enter if you want output to pdf (Press enter to default to False): (True/False)')
print('Make sure pandoc is installed if you want to output to pdf')
configs['output_to_pdf'] = input()
if configs['output_to_pdf'] == '':
configs['output_to_pdf'] = False
else:
if configs['output_to_pdf'] == 'True':
configs['output_to_pdf'] = True
else:
configs['output_to_pdf'] = False
print('Running:')
clean_pdf(configs)
'''