forked from Raychanan/ChatGPT-for-Translation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatGPT-translate.py
359 lines (320 loc) · 13.7 KB
/
ChatGPT-translate.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
import os
import re
from tqdm import tqdm
import argparse
import time
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
import openai
import trafilatura
import requests
ALLOWED_FILE_TYPES = [".txt", ".md", ".rtf", ".html"]
class ChatGPT:
def __init__(self, key, target_language, not_to_translate_people_names):
self.key = key
self.target_language = target_language
self.last_request_time = 0
self.request_interval = 1 # seconds
self.max_backoff_time = 60 # seconds
self.not_to_translate_people_names = not_to_translate_people_names
def translate(self, text):
# Set up OpenAI API key
openai.api_key = self.key
if not text:
return ""
# lang
while True:
try:
# Check if enough time has passed since the last request
elapsed_time = time.monotonic() - self.last_request_time
if elapsed_time < self.request_interval:
time.sleep(self.request_interval - elapsed_time)
self.last_request_time = time.monotonic()
# change prompt based on not_to_translate_people_names
if self.not_to_translate_people_names:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{
'role': 'system',
'content': 'You are a translator assistant.'
}, {
"role":
"user",
"content":
f"Translate the following text into {self.target_language} in a way that is faithful to the original text. But do not translate people and authors' names and surnames. Return only the translation and nothing else:\n{text}",
}],
)
else:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{
'role': 'system',
'content': 'You are a translator assistant.'
}, {
"role":
"user",
"content":
f"Translate the following text into {self.target_language} in a way that is faithful to the original text. Return only the translation and nothing else:\n{text}",
}],
)
t_text = (completion["choices"][0].get("message").get(
"content").encode("utf8").decode())
break
except Exception as e:
print(str(e))
# Exponential backoff if rate limit is hit
self.request_interval *= 2
if self.request_interval > self.max_backoff_time:
self.request_interval = self.max_backoff_time
print(
f"Rate limit hit. Sleeping for {self.request_interval} seconds."
)
time.sleep(self.request_interval)
continue
return t_text
def translate_text_file(text_filepath_or_url, options):
OPENAI_API_KEY = options.openai_key or os.environ.get("OPENAI_API_KEY")
translator = ChatGPT(OPENAI_API_KEY, options.target_language,
options.not_to_translate_people_names)
paragraphs = read_and_preprocess_data(text_filepath_or_url)
# keep first three paragraphs
first_three_paragraphs = paragraphs[:2]
# if users require to ignore References, we then take out all paragraphs after the one starting with "References"
if options.not_to_translate_references:
ignore_strings = ["Acknowledgment", "Notes", "NOTES", "disclosure statement", "References", "Funding", "declaration of conflicting interest", "acknowledgment", "supplementary material", "Acknowledgements"]
ignore_indices = []
for i, p in enumerate(paragraphs):
for ignore_str in ignore_strings:
if (p.startswith(ignore_str) or p.lower().startswith(ignore_str.lower())) and len(p) < 30:
ignore_indices.append(i)
break
if ignore_indices:
print("References will not be translated.")
ref_paragraphs = paragraphs[min(ignore_indices):]
paragraphs = paragraphs[:min(ignore_indices)]
else:
print(paragraphs[-3:])
raise Exception("No References found.")
def split_and_translate(paragraph):
import nltk
try:
nltk.data.find('tokenizers/punkt')
except:
nltk.download('punkt')
from nltk.tokenize import sent_tokenize
words = paragraph.split()
if len(words) > 4000:
sentences = sent_tokenize(paragraph)
half = len(sentences) // 2
first_half = " ".join(sentences[:half])
second_half = " ".join(sentences[half:])
translated_first_half = translator.translate(first_half).strip()
translated_second_half = translator.translate(second_half).strip()
return translated_first_half + " " + translated_second_half
else:
return translator.translate(paragraph).strip()
with ThreadPoolExecutor(max_workers=options.num_threads) as executor:
translated_paragraphs = list(
tqdm(executor.map(split_and_translate, paragraphs),
total=len(paragraphs),
desc="Translating paragraphs",
unit="paragraph"))
translated_text = "\n".join(translated_paragraphs)
if options.bilingual:
bilingual_text = "\n".join(f"{paragraph}\n{translation}"
for paragraph, translation in zip(
paragraphs, translated_paragraphs))
# add first three paragraphs if required
if options.keep_first_two_paragraphs:
bilingual_text = "\n".join(
first_three_paragraphs) + "\n" + bilingual_text
# append References
if options.not_to_translate_references:
bilingual_text += "\n".join(ref_paragraphs)
output_file = f"{Path(text_filepath_or_url).parent}/{Path(text_filepath_or_url).stem}_bilingual.txt"
with open(output_file, "w") as f:
f.write(bilingual_text)
print(f"Bilingual text saved to {f.name}.")
else:
# remove extra newlines
translated_text = re.sub(r"\n{2,}", "\n", translated_text)
# add first three paragraphs if required
if options.keep_first_two_paragraphs:
translated_text = "\n".join(
first_three_paragraphs) + "\n" + translated_text
# append References
if options.not_to_translate_references:
translated_text += "\n" + "\n".join(ref_paragraphs)
output_file = f"{Path(text_filepath_or_url).parent}/{Path(text_filepath_or_url).stem}_translated.txt"
with open(output_file, "w", encoding="utf-8") as f:
f.write(translated_text)
print(f"Translated text saved to {f.name}.")
def download_html(url):
response = requests.get(url)
return response.text
def read_and_preprocess_data(text_filepath_or_url):
if text_filepath_or_url.startswith('http'):
# replace "https:/www" with "https://www"
text_filepath_or_url = text_filepath_or_url.replace(":/", "://")
# download and extract text from URL
print("Downloading and extracting text from URL...")
downloaded = trafilatura.fetch_url(text_filepath_or_url)
print("Downloaded text:")
print(downloaded)
text = trafilatura.extract(downloaded)
else:
with open(text_filepath_or_url, "r", encoding='utf-8') as f:
text = f.read()
if text_filepath_or_url.endswith('.html'):
# extract text from HTML file
print("Extracting text from HTML file...")
text = trafilatura.extract(text)
# write to a txt file ended with "_extracted"
with open(
f"{Path(text_filepath_or_url).parent}/{Path(text_filepath_or_url).stem}_extracted.txt",
"w") as f:
f.write(text)
print(f"Extracted text saved to {f.name}.")
paragraphs = [p.strip() for p in text.split("\n") if p.strip() != ""]
return paragraphs
def parse_arguments():
"""Parse command-line arguments"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--input_path",
dest="input_path",
type=str,
help="input file or folder to translate",
)
parser.add_argument(
"--openai_key",
dest="openai_key",
type=str,
default="",
help="OpenAI API key",
)
parser.add_argument(
"--num_threads",
dest="num_threads",
type=int,
default=10,
help="number of threads to use for translation",
)
parser.add_argument(
"--bilingual",
dest="bilingual",
action="store_true",
default=False,
help=
"output bilingual txt file with original and translated text side by side",
)
parser.add_argument(
"--target_language",
dest="target_language",
type=str,
default="Simplified Chinese",
help="target language to translate to",
)
parser.add_argument(
"--not_to_translate_people_names",
dest="not_to_translate_people_names",
action="store_true",
default=False,
help="whether or not to translate names in the text",
)
parser.add_argument(
"--not_to_translate_references",
dest="not_to_translate_references",
action="store_true",
default=False,
help="not to translate references",
)
parser.add_argument(
"--keep_first_two_paragraphs",
dest="keep_first_two_paragraphs",
action="store_true",
default=False,
help="keep the first three paragraphs of the original text",
)
# add arg: only_process_this_file_extension
parser.add_argument(
"--only_process_this_file_extension",
dest="only_process_this_file_extension",
type=str,
default="",
help="only process files with this extension",
)
options = parser.parse_args()
OPENAI_API_KEY = options.openai_key or os.environ.get("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise Exception("Please provide your OpenAI API key")
return options
def check_file_path(file_path: Path, options=None):
"""
Ensure file extension is in ALLOWED_FILE_TYPES or is a URL.
If file ends with _translated.txt or _bilingual.txt, skip it.
If there is any txt file ending with _translated.txt or _bilingual.txt, skip it.
"""
if not file_path.suffix.lower() in ALLOWED_FILE_TYPES and not str(
file_path).startswith('http'):
raise Exception("Please use a txt file or URL")
if file_path.stem.endswith("_translated") or file_path.stem.endswith(
"extracted_translated"):
print(
f"You already have a translated file for {file_path}, skipping...")
return False
elif file_path.stem.endswith("_bilingual") or file_path.stem.endswith(
"extracted_bilingual"):
print(
f"You already have a bilingual file for {file_path}, skipping...")
return False
if (file_path.with_name(f"{file_path.stem}_translated.txt").exists() or
file_path.with_name(f"{file_path.stem}_extracted_translated.txt").
exists()) and not getattr(options, 'bilingual', False):
print(
f"You already have a translated file for {file_path}, skipping...")
return False
elif (file_path.with_name(f"{file_path.stem}_bilingual.txt").exists()
or file_path.with_name(f"{file_path.stem}_extracted_bilingual.txt").
exists()) and getattr(options, 'bilingual', False):
print(
f"You already have a bilingual file for {file_path}, skipping...")
return False
return True
def process_file(file_path, options):
"""Translate a single text file"""
if not check_file_path(file_path, options):
return
print(f"Translating {file_path}...")
translate_text_file(str(file_path), options)
def process_folder(folder_path, options):
"""Translate all text files in a folder"""
# if only_process_this_file_extension is set, only process files with this extension
if options.only_process_this_file_extension:
files_to_process = list(
folder_path.rglob(f"*.{options.only_process_this_file_extension}"))
print(
f"Only processing files with extension {options.only_process_this_file_extension}"
)
print(f"Found {len(files_to_process)} files to process")
else:
files_to_process = list(folder_path.rglob("*"))
total_files = len(files_to_process)
for index, file_path in enumerate(files_to_process):
if file_path.is_file() and file_path.suffix.lower(
) in ALLOWED_FILE_TYPES:
process_file(file_path, options)
print(
f"Processed file {index + 1} of {total_files}. Only {total_files - index - 1} files left to process."
)
def main():
"""Main function"""
options = parse_arguments()
input_path = Path(options.input_path)
if input_path.is_dir():
# input path is a folder, scan and process all allowed file types
process_folder(input_path, options)
elif input_path.is_file:
process_file(input_path, options)
if __name__ == "__main__":
main()