Spaces:
Sleeping
Sleeping
Eason Lu
commited on
Commit
•
0d268b7
1
Parent(s):
4890407
change default model; short sentence merge; edit translation prompt
Browse files- SRT.py +14 -3
- pipeline.py +4 -4
SRT.py
CHANGED
@@ -105,7 +105,7 @@ class SRT_script():
|
|
105 |
merge_list = [] # a list of indices that should be merged e.g. [[0], [1, 2, 3, 4], [5, 6], [7]]
|
106 |
sentence = []
|
107 |
for i, seg in enumerate(self.segments):
|
108 |
-
if seg.source_text[-1] in ['.', '!', '?']:
|
109 |
sentence.append(i)
|
110 |
merge_list.append(sentence)
|
111 |
sentence = []
|
@@ -127,6 +127,11 @@ class SRT_script():
|
|
127 |
start_seg_id = id_range[0]
|
128 |
end_seg_id = id_range[1]
|
129 |
|
|
|
|
|
|
|
|
|
|
|
130 |
def inner_func(target,input_str):
|
131 |
response = openai.ChatCompletion.create(
|
132 |
#model=model,
|
@@ -136,13 +141,20 @@ class SRT_script():
|
|
136 |
#{"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."},
|
137 |
#{"role": "system", "content": "There is no need for you to add any comments or notes, and do not modify the English transcript."},
|
138 |
#{"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)}
|
|
|
139 |
{"role": "system", "content": "你的任务是按照要求合并或拆分句子到指定行数,你需要尽可能保证句意,但必要时可以将一句话分为两行输出"},
|
140 |
{"role": "system", "content": "注意:你只需要输出处理过的中文句子,如果你要输出序号,请使用冒号隔开"},
|
141 |
{"role": "user", "content": '请将下面的句子拆分或组合为{}句:\n{}'.format(target,input_str)}
|
|
|
|
|
|
|
142 |
],
|
143 |
-
#temperature=0.7
|
144 |
temperature = 0.15
|
145 |
)
|
|
|
|
|
|
|
|
|
146 |
return response['choices'][0]['message']['content'].strip()
|
147 |
|
148 |
|
@@ -206,7 +218,6 @@ class SRT_script():
|
|
206 |
#print(translate)
|
207 |
|
208 |
|
209 |
-
|
210 |
for i, seg in enumerate(self.segments[start_seg_id-1:end_seg_id]):
|
211 |
# naive way to due with merge translation problem
|
212 |
# TODO: need a smarter solution
|
|
|
105 |
merge_list = [] # a list of indices that should be merged e.g. [[0], [1, 2, 3, 4], [5, 6], [7]]
|
106 |
sentence = []
|
107 |
for i, seg in enumerate(self.segments):
|
108 |
+
if seg.source_text[-1] in ['.', '!', '?'] and len(seg.source_text) > 10:
|
109 |
sentence.append(i)
|
110 |
merge_list.append(sentence)
|
111 |
sentence = []
|
|
|
127 |
start_seg_id = id_range[0]
|
128 |
end_seg_id = id_range[1]
|
129 |
|
130 |
+
src_text = ""
|
131 |
+
for i, seg in enumerate(self.segments[start_seg_id-1:end_seg_id]):
|
132 |
+
src_text+=seg.source_text
|
133 |
+
src_text+='\n\n'
|
134 |
+
|
135 |
def inner_func(target,input_str):
|
136 |
response = openai.ChatCompletion.create(
|
137 |
#model=model,
|
|
|
141 |
#{"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."},
|
142 |
#{"role": "system", "content": "There is no need for you to add any comments or notes, and do not modify the English transcript."},
|
143 |
#{"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)}
|
144 |
+
|
145 |
{"role": "system", "content": "你的任务是按照要求合并或拆分句子到指定行数,你需要尽可能保证句意,但必要时可以将一句话分为两行输出"},
|
146 |
{"role": "system", "content": "注意:你只需要输出处理过的中文句子,如果你要输出序号,请使用冒号隔开"},
|
147 |
{"role": "user", "content": '请将下面的句子拆分或组合为{}句:\n{}'.format(target,input_str)}
|
148 |
+
# {"role": "system", "content": "请将以下中文与其英文句子一一对应并输出:"},
|
149 |
+
# {"role": "system", "content": "英文:{}".format(src_text)},
|
150 |
+
# {"role": "user", "content": "中文:{}\n\n".format(input_str)},
|
151 |
],
|
|
|
152 |
temperature = 0.15
|
153 |
)
|
154 |
+
# print(src_text)
|
155 |
+
# print(input_str)
|
156 |
+
# print(response['choices'][0]['message']['content'].strip())
|
157 |
+
# exit()
|
158 |
return response['choices'][0]['message']['content'].strip()
|
159 |
|
160 |
|
|
|
218 |
#print(translate)
|
219 |
|
220 |
|
|
|
221 |
for i, seg in enumerate(self.segments[start_seg_id-1:end_seg_id]):
|
222 |
# naive way to due with merge translation problem
|
223 |
# TODO: need a smarter solution
|
pipeline.py
CHANGED
@@ -19,7 +19,7 @@ parser.add_argument("--srt_file", help="srt file input path here", default=None,
|
|
19 |
parser.add_argument("--download", help="download path", default='./downloads', type=str, required=False)
|
20 |
parser.add_argument("--output_dir", help="translate result path", default='./results', type=str, required=False)
|
21 |
parser.add_argument("--video_name", help="video name, if use video link as input, the name will auto-filled by youtube video name", default='placeholder', type=str, required=False)
|
22 |
-
parser.add_argument("--model_name", help="model name only support gpt-4 and gpt-3.5-turbo", type=str, required=False, default="gpt-
|
23 |
parser.add_argument("-only_srt", help="set script output to only .srt file", action='store_true')
|
24 |
parser.add_argument("-v", help="auto encode script with video", action='store_true')
|
25 |
args = parser.parse_args()
|
@@ -136,9 +136,8 @@ else:
|
|
136 |
.merge_by_punctuation([' '])
|
137 |
.split_by_punctuation(['.', '。', '?'])
|
138 |
)
|
139 |
-
|
140 |
transcript = transcript.to_dict()
|
141 |
-
# print(transcript)
|
142 |
srt = SRT_script(transcript['segments']) # read segments to SRT class
|
143 |
|
144 |
else:
|
@@ -158,7 +157,7 @@ if not args.only_srt:
|
|
158 |
|
159 |
|
160 |
# Split the video script by sentences and create chunks within the token limit
|
161 |
-
def script_split(script_in, chunk_size =
|
162 |
script_split = script_in.split('\n\n')
|
163 |
script_arr = []
|
164 |
range_arr = []
|
@@ -224,6 +223,7 @@ def get_response(model_name, sentence):
|
|
224 |
messages = [
|
225 |
{"role": "system", "content": "You are a helpful assistant that translates English to Chinese and have decent background in starcraft2."},
|
226 |
{"role": "system", "content": "Your translation has to keep the orginal format and be as accurate as possible."},
|
|
|
227 |
{"role": "system", "content": "There is no need for you to add any comments or notes."},
|
228 |
{"role": "user", "content": 'Translate the following English text to Chinese: "{}"'.format(sentence)}
|
229 |
],
|
|
|
19 |
parser.add_argument("--download", help="download path", default='./downloads', type=str, required=False)
|
20 |
parser.add_argument("--output_dir", help="translate result path", default='./results', type=str, required=False)
|
21 |
parser.add_argument("--video_name", help="video name, if use video link as input, the name will auto-filled by youtube video name", default='placeholder', type=str, required=False)
|
22 |
+
parser.add_argument("--model_name", help="model name only support gpt-4 and gpt-3.5-turbo", type=str, required=False, default="gpt-4") # default change to gpt-4
|
23 |
parser.add_argument("-only_srt", help="set script output to only .srt file", action='store_true')
|
24 |
parser.add_argument("-v", help="auto encode script with video", action='store_true')
|
25 |
args = parser.parse_args()
|
|
|
136 |
.merge_by_punctuation([' '])
|
137 |
.split_by_punctuation(['.', '。', '?'])
|
138 |
)
|
139 |
+
|
140 |
transcript = transcript.to_dict()
|
|
|
141 |
srt = SRT_script(transcript['segments']) # read segments to SRT class
|
142 |
|
143 |
else:
|
|
|
157 |
|
158 |
|
159 |
# Split the video script by sentences and create chunks within the token limit
|
160 |
+
def script_split(script_in, chunk_size = 500):
|
161 |
script_split = script_in.split('\n\n')
|
162 |
script_arr = []
|
163 |
range_arr = []
|
|
|
223 |
messages = [
|
224 |
{"role": "system", "content": "You are a helpful assistant that translates English to Chinese and have decent background in starcraft2."},
|
225 |
{"role": "system", "content": "Your translation has to keep the orginal format and be as accurate as possible."},
|
226 |
+
{"role": "system", "content": "Your translation needs to be consistent with the number of sentences in the original."},
|
227 |
{"role": "system", "content": "There is no need for you to add any comments or notes."},
|
228 |
{"role": "user", "content": 'Translate the following English text to Chinese: "{}"'.format(sentence)}
|
229 |
],
|