Spaces:
Sleeping
Sleeping
Eason Lu
commited on
Commit
·
92d2ddc
1
Parent(s):
1312451
fix translation initial
Browse filesFormer-commit-id: 408726bb34863a12a013fd7343df57456fc175a7
SRT.py
CHANGED
@@ -184,27 +184,15 @@ class SRT_script():
|
|
184 |
def inner_func(target, input_str):
|
185 |
response = openai.ChatCompletion.create(
|
186 |
# model=model,
|
187 |
-
model="gpt-
|
188 |
messages=[
|
189 |
-
# {"role": "system", "content": "You are a helpful assistant that help calibrates English to Chinese subtitle translations in starcraft2."},
|
190 |
-
# {"role": "system", "content": "You are provided with a translated Chinese transcript; you must modify or split the Chinese sentence to match the meaning and the number of the English transcript exactly one by one. You must not merge ANY Chinese lines, you can only split them but the total Chinese lines MUST equals to number of English lines."},
|
191 |
-
# {"role": "system", "content": "There is no need for you to add any comments or notes, and do not modify the English transcript."},
|
192 |
-
# {"role": "user", "content": 'You are given the English transcript and line number, your task is to merge or split the Chinese to match the exact number of lines in English transcript, no more no less. For example, if there are more Chinese lines than English lines, merge some the Chinese lines to match the number of English lines. If Chinese lines is less than English lines, split some Chinese lines to match the english lines: "{}"'.format(input_str)}
|
193 |
-
|
194 |
{"role": "system",
|
195 |
"content": "你的任务是按照要求合并或拆分句子到指定行数,你需要尽可能保证句意,但必要时可以将一句话分为两行输出"},
|
196 |
{"role": "system", "content": "注意:你只需要输出处理过的中文句子,如果你要输出序号,请使用冒号隔开"},
|
197 |
{"role": "user", "content": '请将下面的句子拆分或组合为{}句:\n{}'.format(target, input_str)}
|
198 |
-
# {"role": "system", "content": "请将以下中文与其英文句子一一对应并输出:"},
|
199 |
-
# {"role": "system", "content": "英文:{}".format(src_text)},
|
200 |
-
# {"role": "user", "content": "中文:{}\n\n".format(input_str)},
|
201 |
],
|
202 |
temperature=0.15
|
203 |
)
|
204 |
-
# print(src_text)
|
205 |
-
# print(input_str)
|
206 |
-
# print(response['choices'][0]['message']['content'].strip())
|
207 |
-
# exit()
|
208 |
return response['choices'][0]['message']['content'].strip()
|
209 |
|
210 |
lines = translate.split('\n\n')
|
@@ -214,14 +202,7 @@ class SRT_script():
|
|
214 |
while count < 5 and len(lines) != (end_seg_id - start_seg_id + 1):
|
215 |
count += 1
|
216 |
print("Solving Unmatched Lines|iteration {}".format(count))
|
217 |
-
|
218 |
-
# initialize GPT input
|
219 |
-
# for i, seg in enumerate(self.segments[start_seg_id-1:end_seg_id]):
|
220 |
-
# input_str += 'Sentence %d: ' %(i+1)+ seg.source_text + '\n'
|
221 |
-
# #Append to prompt string
|
222 |
-
# #Adds sentence index let GPT keep track of sentence breaks
|
223 |
-
# input_str += translate
|
224 |
-
# append translate to prompt
|
225 |
flag = True
|
226 |
while flag:
|
227 |
flag = False
|
@@ -261,12 +242,7 @@ class SRT_script():
|
|
261 |
log.write("range_of_text,iterations_solving,solved,file_length,video_name" + "\n")
|
262 |
log.write(str(id_range) + ',' + str(count) + ',' + str(solved) + ',' + str(
|
263 |
len(self.segments)) + ',' + video_name + "\n")
|
264 |
-
|
265 |
print(lines)
|
266 |
-
# print(id_range)
|
267 |
-
# for i, seg in enumerate(self.segments[start_seg_id-1:end_seg_id]):
|
268 |
-
# print(seg.source_text)
|
269 |
-
# print(translate)
|
270 |
|
271 |
for i, seg in enumerate(self.segments[start_seg_id - 1:end_seg_id]):
|
272 |
# naive way to due with merge translation problem
|
@@ -278,10 +254,10 @@ class SRT_script():
|
|
278 |
max_num -= 1
|
279 |
if i == len(lines) - 1:
|
280 |
break
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
|
286 |
def split_seg(self, seg, text_threshold, time_threshold):
|
287 |
# evenly split seg to 2 parts and add new seg into self.segments
|
|
|
184 |
def inner_func(target, input_str):
|
185 |
response = openai.ChatCompletion.create(
|
186 |
# model=model,
|
187 |
+
model="gpt-4",
|
188 |
messages=[
|
|
|
|
|
|
|
|
|
|
|
189 |
{"role": "system",
|
190 |
"content": "你的任务是按照要求合并或拆分句子到指定行数,你需要尽可能保证句意,但必要时可以将一句话分为两行输出"},
|
191 |
{"role": "system", "content": "注意:你只需要输出处理过的中文句子,如果你要输出序号,请使用冒号隔开"},
|
192 |
{"role": "user", "content": '请将下面的句子拆分或组合为{}句:\n{}'.format(target, input_str)}
|
|
|
|
|
|
|
193 |
],
|
194 |
temperature=0.15
|
195 |
)
|
|
|
|
|
|
|
|
|
196 |
return response['choices'][0]['message']['content'].strip()
|
197 |
|
198 |
lines = translate.split('\n\n')
|
|
|
202 |
while count < 5 and len(lines) != (end_seg_id - start_seg_id + 1):
|
203 |
count += 1
|
204 |
print("Solving Unmatched Lines|iteration {}".format(count))
|
205 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
flag = True
|
207 |
while flag:
|
208 |
flag = False
|
|
|
242 |
log.write("range_of_text,iterations_solving,solved,file_length,video_name" + "\n")
|
243 |
log.write(str(id_range) + ',' + str(count) + ',' + str(solved) + ',' + str(
|
244 |
len(self.segments)) + ',' + video_name + "\n")
|
|
|
245 |
print(lines)
|
|
|
|
|
|
|
|
|
246 |
|
247 |
for i, seg in enumerate(self.segments[start_seg_id - 1:end_seg_id]):
|
248 |
# naive way to due with merge translation problem
|
|
|
254 |
max_num -= 1
|
255 |
if i == len(lines) - 1:
|
256 |
break
|
257 |
+
if lines[i][0] in [' ', '\n']:
|
258 |
+
lines[i] = lines[i][1:]
|
259 |
+
seg.translation = lines[i]
|
260 |
+
|
261 |
|
262 |
def split_seg(self, seg, text_threshold, time_threshold):
|
263 |
# evenly split seg to 2 parts and add new seg into self.segments
|