-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_to_jsonl-ru.py
149 lines (110 loc) · 5.29 KB
/
convert_to_jsonl-ru.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
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('Конвертер файлов')
# Папка ввода
self.input_folder_label = QLabel('Входная папка:')
self.input_folder_button = QPushButton('Обзор')
self.input_folder_button.clicked.connect(self.browse_input_folder)
# Выходной файл
self.output_file_label = QLabel('Выходной файл (один):')
self.output_file_button = QPushButton('Обзор')
self.output_file_button.clicked.connect(self.browse_output_file)
# Папка вывода
self.output_folder_label = QLabel('Папка вывода (несколько файлов):')
self.output_folder_button = QPushButton('Обзор')
self.output_folder_button.clicked.connect(self.browse_output_folder)
# Флажок для одного файла вывода
self.single_output_checkbox = QCheckBox('Объединить в один файл')
# Кнопка конвертации
self.convert_button = QPushButton('Конвертировать')
self.convert_button.clicked.connect(self.convert_files)
# Размещение
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, 'Выберите входную папку')
def browse_output_file(self):
self.output_file, _ = QFileDialog.getSaveFileName(self, 'Выберите выходной файл', '', 'Файлы JSONL (*.jsonl)')
def browse_output_folder(self):
self.output_folder = QFileDialog.getExistingDirectory(self, 'Выберите папку вывода')
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_()