-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_to_jsonl-en.py
151 lines (110 loc) · 5 KB
/
convert_to_jsonl-en.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
import os
import json
import csv
from PyPDF2 import PdfFileReader
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QPushButton, QFileDialog, QCheckBox
class ConverterWindow(QMainWindow):
def __init__(self):
super().__init__()
self.input_folder = None
self.output_file = None
self.output_folder = None
self.single_output = False
self.init_ui()
def init_ui(self):
self.setWindowTitle('File Converter')
# Input folder
self.input_folder_label = QLabel('Input Folder:')
self.input_folder_button = QPushButton('Browse')
self.input_folder_button.clicked.connect(self.browse_input_folder)
# Output file
self.output_file_label = QLabel('Output File (Single):')
self.output_file_button = QPushButton('Browse')
self.output_file_button.clicked.connect(self.browse_output_file)
# Output folder
self.output_folder_label = QLabel('Output Folder (Multiple):')
self.output_folder_button = QPushButton('Browse')
self.output_folder_button.clicked.connect(self.browse_output_folder)
# Single output checkbox
self.single_output_checkbox = QCheckBox('Merge into Single File')
# Convert button
self.convert_button = QPushButton('Convert')
self.convert_button.clicked.connect(self.convert_files)
# Layout
central_widget = QWidget()
layout = QVBoxLayout()
layout.addWidget(self.input_folder_label)
layout.addWidget(self.input_folder_button)
layout.addWidget(self.output_file_label)
layout.addWidget(self.output_file_button)
layout.addWidget(self.output_folder_label)
layout.addWidget(self.output_folder_button)
layout.addWidget(self.single_output_checkbox)
layout.addWidget(self.convert_button)
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
def browse_input_folder(self):
self.input_folder = QFileDialog.getExistingDirectory(self, 'Select Input Folder')
def browse_output_file(self):
self.output_file, _ = QFileDialog.getSaveFileName(self, 'Select Output File', '', 'JSONL Files (*.jsonl)')
def browse_output_folder(self):
self.output_folder = QFileDialog.getExistingDirectory(self, 'Select Output Folder')
def convert_files(self):
if self.single_output and self.output_file:
convert_files(self.input_folder, self.output_file, single_output=True)
elif self.output_folder:
convert_files(self.input_folder, self.output_folder)
def set_single_output(self, checked):
self.single_output = checked
def txt_to_jsonl(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
data = [{'text': line.strip()} for line in lines]
with open(output_file, 'a', encoding='utf-8') as f:
for item in data:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
def csv_to_jsonl(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
data = [row for row in reader]
with open(output_file, 'a', encoding='utf-8') as f:
for item in data:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
def json_to_jsonl(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as f:
data = json.load(f)
with open(output_file, 'a', encoding='utf-8') as f:
for item in data:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
def pdf_to_jsonl(input_file, output_file):
pdf = PdfFileReader(input_file)
data = []
for page_num in range(pdf.numPages):
page = pdf.getPage(page_num)
text = page.extractText().strip()
data.append({'text': text})
with open(output_file, 'a', encoding='utf-8') as f:
for item in data:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
def convert_file(input_file, output_file, single_output=False):
file_ext = os.path.splitext(input_file)[1].lower()
if file_ext == '.txt':
txt_to_jsonl(input_file, output_file)
elif file_ext == '.csv':
csv_to_jsonl(input_file, output_file)
elif file_ext == '.json':
json_to_jsonl(input_file, output_file)
elif file_ext == '.pdf':
pdf_to_jsonl(input_file, output_file)
if single_output:
os.remove(input_file)
def convert_files(input_folder, output_file, single_output=False):
os.makedirs(os.path.dirname(output_file), exist_ok=True)
for file_name in os.listdir(input_folder):
input_file = os.path.join(input_folder, file_name)
convert_file(input_file, output_file, single_output)
if __name__ == '__main__':
app = QApplication([])
window = ConverterWindow()
window.show()
app.exec_()