-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
364 lines (329 loc) · 12.7 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import csv
from PyQt6 import QtCore
from PyQt6.QtCore import QDir, Qt
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMainWindow, QLabel, QLineEdit, QVBoxLayout, \
QFileDialog, QHBoxLayout, QListWidget, QListWidgetItem, QRadioButton, QCheckBox, QGridLayout, QDialog, QMessageBox
import sys
from data import Data, OpenAIException
languages = [
{"Afrikaans": "af"},
{"Albanian": "sq"},
{"Amharic": "am"},
{"Arabic": "ar"},
{"Armenian": "hy"},
{"Assamese": "as"},
{"Aymara": "ay"},
{"Azerbaijani": "az"},
{"Bambara": "bm"},
{"Basque": "eu"},
{"Belarusian": "be"},
{"Bengali": "bn"},
{"Bhojpuri": "bho"},
{"Bosnian": "bs"},
{"Bulgarian": "bg"},
{"Catalan": "ca"},
{"Cebuano": "ceb"},
{"Chinese (Simplified)": "zh-CN"},
{"Chinese (Traditional)": "zh-TW"},
{"Corsican": "co"},
{"Croatian": "hr"},
{"Czech": "cs"},
{"Danish": "da"},
{"Dhivehi": "dv"},
{"Dogri": "doi"},
{"Dutch": "nl"},
{"English": "en"},
{"Esperanto": "eo"},
{"Estonian": "et"},
{"Ewe": "ee"},
{"Filipino (Tagalog)": "fil"},
{"Finnish": "fi"},
{"French": "fr"},
{"Frisian": "fy"},
{"Galician": "gl"},
{"Georgian": "ka"},
{"German": "de"},
{"Greek": "el"},
{"Guarani": "gn"},
{"Gujarati": "gu"},
{"Haitian Creole": "ht"},
{"Hausa": "ha"},
{"Hawaiian": "haw"},
{"Hebrew": "he or iw"},
{"Hindi": "hi"},
{"Hmong": "hmn"},
{"Hungarian": "hu"},
{"Icelandic": "is"},
{"Igbo": "ig"},
{"Ilocano": "ilo"},
{"Indonesian": "id"},
{"Irish": "ga"},
{"Italian": "it"},
{"Japanese": "ja"},
{"Javanese": "jv or jw"},
{"Kannada": "kn"},
{"Kazakh": "kk"},
{"Khmer": "km"},
{"Kinyarwanda": "rw"},
{"Konkani": "gom"},
{"Korean": "ko"},
{"Krio": "kri"},
{"Kurdish": "ku"},
{"Kurdish (Sorani)": "ckb"},
{"Kyrgyz": "ky"},
{"Lao": "lo"},
{"Latin": "la"},
{"Latvian": "lv"},
{"Lingala": "ln"},
{"Lithuanian": "lt"},
{"Luganda": "lg"},
{"Luxembourgish": "lb"},
{"Macedonian": "mk"},
{"Maithili": "mai"},
{"Malagasy": "mg"},
{"Malay": "ms"},
{"Malayalam": "ml"},
{"Maltese": "mt"},
{"Maori": "mi"},
{"Marathi": "mr"},
{"Meiteilon (Manipuri)": "mni-Mtei"},
{"Mizo": "lus"},
{"Mongolian": "mn"},
{"Myanmar (Burmese)": "my"},
{"Nepali": "ne"},
{"Norwegian": "no"},
{"Nyanja (Chichewa)": "ny"},
{"Odia (Oriya)": "or"},
{"Oromo": "om"},
{"Pashto": "ps"},
{"Persian": "fa"},
{"Polish": "pl"},
{"Portuguese (Portugal, Brazil)": "pt"},
{"Punjabi": "pa"},
{"Quechua": "qu"},
{"Romanian": "ro"},
{"Russian": "ru"},
{"Samoan": "sm"},
{"Sanskrit": "sa"},
{"Scots Gaelic": "gd"},
{"Sepedi": "nso"},
{"Serbian": "sr"},
{"Sesotho": "st"},
{"Shona": "sn"},
{"Sindhi": "sd"},
{"Sinhala (Sinhalese)": "si"},
{"Slovak": "sk"},
{"Slovenian": "sl"},
{"Somali": "so"},
{"Spanish": "es"},
{"Sundanese": "su"},
{"Swahili": "sw"},
{"Swedish": "sv"},
{"Tagalog (Filipino)": "tl"},
{"Tajik": "tg"},
{"Tamil": "ta"},
{"Tatar": "tt"},
{"Telugu": "te"},
{"Thai": "th"},
{"Tigrinya": "ti"},
{"Tsonga": "ts"},
{"Turkish": "tr"},
{"Turkmen": "tk"},
{"Twi (Akan)": "ak"},
{"Ukrainian": "uk"},
{"Urdu": "ur"},
{"Uyghur": "ug"},
{"Uzbek": "uz"},
{"Vietnamese": "vi"},
{"Welsh": "cy"},
{"Xhosa": "xh"},
{"Yiddish": "yi"},
{"Yoruba": "yo"},
{"Zulu": "zu"},
]
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.data = None
self.selected_headers = []
self.include_header_in_output = False
self.only_missing_langs = False;
self.gpt_mode_state = 'gpt-3.5-turbo'
self.setWindowTitle("Prepare your database for spatie/laravel-translatable")
self.resize(500, 500)
vbox = QVBoxLayout()
open_ai_hbox = QHBoxLayout()
open_ai_hbox.addWidget(QLabel('OpenAI Apikey:'))
self.open_ai_line_edit = QLineEdit()
open_ai_hbox.addWidget(self.open_ai_line_edit)
# choose open AI model
open_ai_model_hbox = QHBoxLayout()
self.gpt3_button = QRadioButton('GPT 3.5')
self.gpt3_button.setChecked(True)
self.gpt3_button.toggled.connect(lambda: self.select_gpt_mode(self.gpt3_button))
open_ai_model_hbox.addWidget(self.gpt3_button)
self.gpt4_button = QRadioButton('GPT 4')
self.gpt4_button.toggled.connect(lambda: self.select_gpt_mode(self.gpt4_button))
open_ai_model_hbox.addWidget(self.gpt4_button)
self.gpt_4_preview_button = QRadioButton('GPT 4 Turbo')
self.gpt_4_preview_button.toggled.connect(lambda: self.select_gpt_mode(self.gpt_4_preview_button))
open_ai_model_hbox.addWidget(self.gpt_4_preview_button)
vbox.addLayout(open_ai_hbox)
vbox.addLayout(open_ai_model_hbox)
file_hbox = QHBoxLayout()
# File Labal
file_label = QLabel("CSV File Path")
file_hbox.addWidget(file_label)
# File Line Edit
self.file_line_edit = QLineEdit()
file_hbox.addWidget(self.file_line_edit)
# Browse Button
browse_button = QPushButton('Browse')
browse_button.clicked.connect(self.open_file_dialog)
file_hbox.addWidget(browse_button)
vbox.addLayout(file_hbox)
# Header List
header_list_label = QLabel("Select the columns you want to translate")
self.header_list = QListWidget()
self.header_list.itemChanged.connect(self.on_header_list_item_selection_changed)
# set a fixed height for the list widget with scroll
self.header_list.setMaximumHeight(200)
vbox.addWidget(header_list_label)
vbox.addWidget(self.header_list)
# Select source language
source_language_label = QLabel("Select the source language")
self.source_language_list = QListWidget()
for language in languages:
item = QListWidgetItem(list(language.keys())[0])
self.source_language_list.addItem(item)
self.source_language_list.setMaximumHeight(200)
vbox.addWidget(source_language_label)
vbox.addWidget(self.source_language_list)
# Select destination languages
destination_languages_label = QLabel("Select the destination languages")
self.destination_languages_list = QListWidget()
for language in languages:
item = QListWidgetItem(list(language.keys())[0])
item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable)
item.setCheckState(Qt.CheckState.Unchecked)
self.destination_languages_list.addItem(item)
self.destination_languages_list.setMaximumHeight(200)
vbox.addWidget(destination_languages_label)
vbox.addWidget(self.destination_languages_list)
options_label = QLabel('Options')
vbox.addWidget(options_label)
options_grid = QGridLayout()
# Output with headers or not checkbox
all_columns_check = QCheckBox('Output with Headers')
all_columns_check.setChecked(self.include_header_in_output)
all_columns_check.setToolTip(
'If checked, the output will have headers. For databases like MySQL make sure this option is not checked!')
all_columns_check.stateChanged.connect(self.include_header_option_change)
options_grid.addWidget(all_columns_check, 0, 0)
# Only translate missing langs or all checkbox
only_translate_missing_langs_check = QCheckBox('Only Translate missing langs')
only_translate_missing_langs_check.setChecked(self.only_missing_langs)
only_translate_missing_langs_check.setToolTip(
'If checked, only missing translations will translate base on source language.'
)
only_translate_missing_langs_check.stateChanged.connect(self.only_missing_langs_change)
options_grid.addWidget(only_translate_missing_langs_check, 0, 1)
options_grid.setColumnStretch(1, 1)
vbox.addLayout(options_grid)
# Generate Button
self.generate_button = QPushButton('Translate')
self.generate_button.clicked.connect(self.generate)
vbox.addWidget(self.generate_button)
widget = QWidget()
widget.setLayout(vbox)
self.setCentralWidget(widget)
def open_file_dialog(self):
filename, _ = QFileDialog.getOpenFileName(self, "Choose your CSV", QDir().homePath(),
"CSV (*.csv);;All Files (*.*)")
if not filename:
return
self.file_line_edit.setText(filename)
# currently read only because due to the lack of action when line edit updates
self.file_line_edit.setReadOnly(True)
self.data = Data(filename)
self.header_list.clear()
self.header_list.setVisible(True)
for header in self.data.get_headers():
item = QListWidgetItem(header)
item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable)
item.setCheckState(Qt.CheckState.Unchecked)
self.header_list.addItem(item)
def on_header_list_item_selection_changed(self):
self.selected_headers = []
for index in range(self.header_list.count()):
item = self.header_list.item(index)
if item.checkState() == Qt.CheckState.Checked:
self.selected_headers.append(item.text())
def select_gpt_mode(self, button):
if button.text() == 'GPT 3.5':
self.gpt_mode_state = 'gpt-3.5-turbo'
elif button.text() == 'GPT 4':
self.gpt_mode_state = 'gpt-4'
elif button.text() == 'GPT 4 Turbo':
self.gpt_mode_state = 'gpt-4-1106-preview'
def include_header_option_change(self, state):
if state == 2:
self.include_header_in_output = True
else:
self.include_header_in_output = False
def only_missing_langs_change(self, state):
if state == 2:
self.only_missing_langs = True
else:
self.only_missing_langs = False
@QtCore.pyqtSlot()
def generate(self):
openai_key = self.open_ai_line_edit.text()
if not openai_key:
error_message_box = QMessageBox(self)
error_message_box.setWindowTitle('Error')
error_message_box.setText("Provide an OpenAI API key!")
error_message_box.setIcon(QMessageBox.Icon.Critical)
error_message_box.exec()
return
if not self.data:
error_message_box = QMessageBox(self)
error_message_box.setWindowTitle('Error')
error_message_box.setText("Provide your CSV file")
error_message_box.setIcon(QMessageBox.Icon.Critical)
error_message_box.exec()
return
# self.generate_button.setText('Generating...')
# self.generate_button.setEnabled(False)
source_language = list(languages[self.source_language_list.currentRow()].values())[0]
destination_languages = []
for index in range(self.destination_languages_list.count()):
item = self.destination_languages_list.item(index)
if item.checkState() == Qt.CheckState.Checked:
destination_languages.append(list(languages[index].values())[0])
try:
translations = self.data.translate_columns_with_open_ai(self.selected_headers, source_language,
destination_languages,
self.open_ai_line_edit.text(),
self.gpt_mode_state,
self.only_missing_langs)
except OpenAIException as e:
# self.generate_button.setEnabled(True)
# self.generate_button.setText('Generate')
# show error message
error_message_box = QMessageBox(self)
error_message_box.setWindowTitle('Error')
error_message_box.setText(str(e))
error_message_box.setIcon(QMessageBox.Icon.Critical)
error_message_box.exec()
return
# save data to a file same as the input file and end with _translated
translations.to_csv(self.file_line_edit.text().replace('.csv', '_translated.csv'), index=False,
quoting=csv.QUOTE_MINIMAL, header=self.include_header_in_output, na_rep='NULL')
# self.generate_button.setEnabled(True)
# self.generate_button.setText('Generate')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()