jhj0517 commited on
Commit
ab2f482
·
1 Parent(s): 3b2080f

changed module name

Browse files
Files changed (2) hide show
  1. modules/deepl_api.py +198 -0
  2. modules/deepl_manager.py +0 -0
modules/deepl_api.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import time
3
+ import os
4
+ from datetime import datetime
5
+ import gradio as gr
6
+
7
+ from modules.subtitle_manager import *
8
+
9
+ """
10
+ This is written by the DeepL API documentation.
11
+ If you want to know the information of DeepL API, see here: https://www.deepl.com/docs-api/documents
12
+ """
13
+
14
+ DEEPL_AVAILABLE_TARGET_LANGS = {
15
+ 'Bulgarian': 'BG',
16
+ 'Czech': 'CS',
17
+ 'Danish': 'DA',
18
+ 'German': 'DE',
19
+ 'Greek': 'EL',
20
+ 'English': 'EN',
21
+ 'English (British)': 'EN-GB',
22
+ 'English (American)': 'EN-US',
23
+ 'Spanish': 'ES',
24
+ 'Estonian': 'ET',
25
+ 'Finnish': 'FI',
26
+ 'French': 'FR',
27
+ 'Hungarian': 'HU',
28
+ 'Indonesian': 'ID',
29
+ 'Italian': 'IT',
30
+ 'Japanese': 'JA',
31
+ 'Korean': 'KO',
32
+ 'Lithuanian': 'LT',
33
+ 'Latvian': 'LV',
34
+ 'Norwegian (Bokmål)': 'NB',
35
+ 'Dutch': 'NL',
36
+ 'Polish': 'PL',
37
+ 'Portuguese': 'PT',
38
+ 'Portuguese (Brazilian)': 'PT-BR',
39
+ 'Portuguese (all Portuguese varieties excluding Brazilian Portuguese)': 'PT-PT',
40
+ 'Romanian': 'RO',
41
+ 'Russian': 'RU',
42
+ 'Slovak': 'SK',
43
+ 'Slovenian': 'SL',
44
+ 'Swedish': 'SV',
45
+ 'Turkish': 'TR',
46
+ 'Ukrainian': 'UK',
47
+ 'Chinese (simplified)': 'ZH'
48
+ }
49
+
50
+ DEEPL_AVAILABLE_SOURCE_LANGS = {
51
+ 'Automatic Detection': None,
52
+ 'Bulgarian': 'BG',
53
+ 'Czech': 'CS',
54
+ 'Danish': 'DA',
55
+ 'German': 'DE',
56
+ 'Greek': 'EL',
57
+ 'English': 'EN',
58
+ 'Spanish': 'ES',
59
+ 'Estonian': 'ET',
60
+ 'Finnish': 'FI',
61
+ 'French': 'FR',
62
+ 'Hungarian': 'HU',
63
+ 'Indonesian': 'ID',
64
+ 'Italian': 'IT',
65
+ 'Japanese': 'JA',
66
+ 'Korean': 'KO',
67
+ 'Lithuanian': 'LT',
68
+ 'Latvian': 'LV',
69
+ 'Norwegian (Bokmål)': 'NB',
70
+ 'Dutch': 'NL',
71
+ 'Polish': 'PL',
72
+ 'Portuguese (all Portuguese varieties mixed)': 'PT',
73
+ 'Romanian': 'RO',
74
+ 'Russian': 'RU',
75
+ 'Slovak': 'SK',
76
+ 'Slovenian': 'SL',
77
+ 'Swedish': 'SV',
78
+ 'Turkish': 'TR',
79
+ 'Ukrainian': 'UK',
80
+ 'Chinese': 'ZH'
81
+ }
82
+
83
+
84
+ class DeepLAPI:
85
+ def __init__(self):
86
+ self.api_interval = 1
87
+ self.max_text_batch_size = 50
88
+ self.available_target_langs = DEEPL_AVAILABLE_TARGET_LANGS
89
+ self.available_source_langs = DEEPL_AVAILABLE_SOURCE_LANGS
90
+
91
+ def translate_deepl(self,
92
+ auth_key: str,
93
+ fileobjs: list,
94
+ source_lang: str,
95
+ target_lang: str,
96
+ is_pro: bool,
97
+ progress=gr.Progress()) -> list:
98
+ """
99
+ Translate subtitle files using DeepL API
100
+
101
+ Parameters
102
+ ----------
103
+ auth_key: str
104
+ API Key for DeepL from gr.Textbox()
105
+ fileobjs: list
106
+ List of files to transcribe from gr.Files()
107
+ source_lang: str
108
+ Source language of the file to transcribe from gr.Dropdown()
109
+ target_lang: str
110
+ Target language of the file to transcribe from gr.Dropdown()
111
+ is_pro: str
112
+ Boolean value that is about pro user or not from gr.Checkbox().
113
+ progress: gr.Progress
114
+ Indicator to show progress directly in gradio.
115
+
116
+ Returns
117
+ ----------
118
+ A List of
119
+ String to return to gr.Textbox()
120
+ Files to return to gr.Files()
121
+ """
122
+
123
+ files_info = {}
124
+ for fileobj in fileobjs:
125
+ file_path = fileobj.name
126
+ file_name, file_ext = os.path.splitext(os.path.basename(fileobj.name))
127
+
128
+ if file_ext == ".srt":
129
+ parsed_dicts = parse_srt(file_path=file_path)
130
+
131
+ batch_size = self.max_text_batch_size
132
+ for batch_start in range(0, len(parsed_dicts), batch_size):
133
+ batch_end = min(batch_start + batch_size, len(parsed_dicts))
134
+ sentences_to_translate = [dic["sentence"] for dic in parsed_dicts[batch_start:batch_end]]
135
+ translated_texts = self.request_deepl_translate(auth_key, sentences_to_translate, source_lang,
136
+ target_lang, is_pro)
137
+ for i, translated_text in enumerate(translated_texts):
138
+ parsed_dicts[batch_start + i]["sentence"] = translated_text["text"]
139
+ progress(batch_end / len(parsed_dicts), desc="Translating..")
140
+
141
+ subtitle = get_serialized_srt(parsed_dicts)
142
+ timestamp = datetime.now().strftime("%m%d%H%M%S")
143
+
144
+ file_name = file_name[:-9]
145
+ output_path = os.path.join("outputs", "translations", f"{file_name}-{timestamp}.srt")
146
+ write_file(subtitle, output_path)
147
+
148
+ elif file_ext == ".vtt":
149
+ parsed_dicts = parse_vtt(file_path=file_path)
150
+
151
+ batch_size = self.max_text_batch_size
152
+ for batch_start in range(0, len(parsed_dicts), batch_size):
153
+ batch_end = min(batch_start + batch_size, len(parsed_dicts))
154
+ sentences_to_translate = [dic["sentence"] for dic in parsed_dicts[batch_start:batch_end]]
155
+ translated_texts = self.request_deepl_translate(auth_key, sentences_to_translate, source_lang,
156
+ target_lang, is_pro)
157
+ for i, translated_text in enumerate(translated_texts):
158
+ parsed_dicts[batch_start + i]["sentence"] = translated_text["text"]
159
+ progress(batch_end / len(parsed_dicts), desc="Translating..")
160
+
161
+ subtitle = get_serialized_vtt(parsed_dicts)
162
+ timestamp = datetime.now().strftime("%m%d%H%M%S")
163
+
164
+ file_name = file_name[:-9]
165
+ output_path = os.path.join("outputs", "translations", f"{file_name}-{timestamp}.srt")
166
+
167
+ write_file(subtitle, output_path)
168
+
169
+ files_info[file_name] = subtitle
170
+ total_result = ''
171
+ for file_name, subtitle in files_info.items():
172
+ total_result += '------------------------------------\n'
173
+ total_result += f'{file_name}\n\n'
174
+ total_result += f'{subtitle}'
175
+
176
+ gr_str = f"Done! Subtitle is in the outputs/translation folder.\n\n{total_result}"
177
+ return [gr_str, output_path]
178
+
179
+ def request_deepl_translate(self,
180
+ auth_key: str,
181
+ text: list,
182
+ source_lang: str,
183
+ target_lang: str,
184
+ is_pro: bool):
185
+ """Request API response to DeepL server"""
186
+
187
+ url = 'https://api.deepl.com/v2/translate' if is_pro else 'https://api-free.deepl.com/v2/translate'
188
+ headers = {
189
+ 'Authorization': f'DeepL-Auth-Key {auth_key}'
190
+ }
191
+ data = {
192
+ 'text': text,
193
+ 'source_lang': DEEPL_AVAILABLE_SOURCE_LANGS[source_lang],
194
+ 'target_lang': DEEPL_AVAILABLE_TARGET_LANGS[target_lang]
195
+ }
196
+ response = requests.post(url, headers=headers, data=data).json()
197
+ time.sleep(self.api_interval)
198
+ return response["translations"]
modules/deepl_manager.py DELETED
File without changes