Spaces:
Sleeping
Sleeping
JiaenLiu
commited on
Commit
•
1e2d254
1
Parent(s):
03b6946
in progress
Browse filesFormer-commit-id: a417baf3dcafc23778cbd99c1315f892d9569822
- SRT.py +2 -2
- pipeline.py +22 -4
SRT.py
CHANGED
@@ -109,13 +109,13 @@ class SRT_script():
|
|
109 |
|
110 |
|
111 |
|
112 |
-
def set_translation(self, translate:str, id_range:tuple):
|
113 |
start_seg_id = id_range[0]
|
114 |
end_seg_id = id_range[1]
|
115 |
|
116 |
def inner_func(input_str):
|
117 |
response = openai.ChatCompletion.create(
|
118 |
-
model=
|
119 |
messages = [
|
120 |
{"role": "system", "content": "You are a helpful assistant that help calibrates English to Chinese subtitle translations in starcraft2."},
|
121 |
{"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."},
|
|
|
109 |
|
110 |
|
111 |
|
112 |
+
def set_translation(self, translate:str, id_range:tuple, model):
|
113 |
start_seg_id = id_range[0]
|
114 |
end_seg_id = id_range[1]
|
115 |
|
116 |
def inner_func(input_str):
|
117 |
response = openai.ChatCompletion.create(
|
118 |
+
model=model,
|
119 |
messages = [
|
120 |
{"role": "system", "content": "You are a helpful assistant that help calibrates English to Chinese subtitle translations in starcraft2."},
|
121 |
{"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."},
|
pipeline.py
CHANGED
@@ -6,6 +6,8 @@ from tqdm import tqdm
|
|
6 |
from SRT import SRT_script
|
7 |
import stable_whisper
|
8 |
|
|
|
|
|
9 |
parser = argparse.ArgumentParser()
|
10 |
parser.add_argument("--link", help="youtube video link here", default=None, type=str, required=False)
|
11 |
parser.add_argument("--video_file", help="local video path here", default=None, type=str, required=False)
|
@@ -14,7 +16,7 @@ parser.add_argument("--srt_file", help="srt file input path here", default=None,
|
|
14 |
parser.add_argument("--download", help="download path", default='./downloads', type=str, required=False)
|
15 |
parser.add_argument("--output_dir", help="translate result path", default='./results', type=str, required=False)
|
16 |
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)
|
17 |
-
parser.add_argument("--model_name", help="model name only support
|
18 |
parser.add_argument("-only_srt", help="set script output to only .srt file", action='store_true')
|
19 |
parser.add_argument("-v", help="auto encode script with video", action='store_true')
|
20 |
args = parser.parse_args()
|
@@ -36,7 +38,18 @@ RESULT_PATH = args.output_dir
|
|
36 |
if not os.path.exists(RESULT_PATH):
|
37 |
os.mkdir(RESULT_PATH)
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
model_name = args.model_name
|
41 |
|
42 |
# get source audio
|
@@ -83,6 +96,10 @@ elif args.video_file is not None:
|
|
83 |
if not os.path.exists(f'{RESULT_PATH}/{VIDEO_NAME}'):
|
84 |
os.mkdir(f'{RESULT_PATH}/{VIDEO_NAME}')
|
85 |
|
|
|
|
|
|
|
|
|
86 |
# Instead of using the script_en variable directly, we'll use script_input
|
87 |
srt_file_en = args.srt_file
|
88 |
|
@@ -164,7 +181,7 @@ def script_split(script_in, chunk_size = 1000):
|
|
164 |
script_arr, range_arr = script_split(script_input)
|
165 |
|
166 |
def get_response(model_name):
|
167 |
-
if model_name == "gpt-3.5-turbo":
|
168 |
# print(s + "\n")
|
169 |
response = openai.ChatCompletion.create(
|
170 |
model=model_name,
|
@@ -207,8 +224,9 @@ for s, range in tqdm(zip(script_arr, range_arr)):
|
|
207 |
except Exception as e:
|
208 |
print("An error has occurred during translation:",e)
|
209 |
print("Retrying...")
|
|
|
210 |
flag = True
|
211 |
-
srt.set_translation(translate, range)
|
212 |
|
213 |
srt.check_len_and_split()
|
214 |
srt.write_srt_file_translate(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt")
|
|
|
6 |
from SRT import SRT_script
|
7 |
import stable_whisper
|
8 |
|
9 |
+
import time
|
10 |
+
|
11 |
parser = argparse.ArgumentParser()
|
12 |
parser.add_argument("--link", help="youtube video link here", default=None, type=str, required=False)
|
13 |
parser.add_argument("--video_file", help="local video path here", default=None, type=str, required=False)
|
|
|
16 |
parser.add_argument("--download", help="download path", default='./downloads', type=str, required=False)
|
17 |
parser.add_argument("--output_dir", help="translate result path", default='./results', type=str, required=False)
|
18 |
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)
|
19 |
+
parser.add_argument("--model_name", help="model name only support gpt-4 and gpt-3.5-turbo", type=str, required=False, default="gpt-3.5-turbo")
|
20 |
parser.add_argument("-only_srt", help="set script output to only .srt file", action='store_true')
|
21 |
parser.add_argument("-v", help="auto encode script with video", action='store_true')
|
22 |
args = parser.parse_args()
|
|
|
38 |
if not os.path.exists(RESULT_PATH):
|
39 |
os.mkdir(RESULT_PATH)
|
40 |
|
41 |
+
# set video name as the input file name if not specified
|
42 |
+
if args.video_name == 'placeholder' :
|
43 |
+
# set video name to upload file name
|
44 |
+
if args.video_file is not None:
|
45 |
+
VIDEO_NAME = args.video_file.split('/')[-1].split('.')[0]
|
46 |
+
elif args.audio_file is not None:
|
47 |
+
VIDEO_NAME = args.audio_file.split('/')[-1].split('.')[0]
|
48 |
+
elif args.srt_file is not None:
|
49 |
+
VIDEO_NAME = args.srt_file.split('/')[-1].split('.')[0]
|
50 |
+
else:
|
51 |
+
VIDEO_NAME = args.video_name
|
52 |
+
|
53 |
model_name = args.model_name
|
54 |
|
55 |
# get source audio
|
|
|
96 |
if not os.path.exists(f'{RESULT_PATH}/{VIDEO_NAME}'):
|
97 |
os.mkdir(f'{RESULT_PATH}/{VIDEO_NAME}')
|
98 |
|
99 |
+
if args.audio_file is not None:
|
100 |
+
audio_file= open(args.audio_file, "rb")
|
101 |
+
audio_path = args.audio_file
|
102 |
+
|
103 |
# Instead of using the script_en variable directly, we'll use script_input
|
104 |
srt_file_en = args.srt_file
|
105 |
|
|
|
181 |
script_arr, range_arr = script_split(script_input)
|
182 |
|
183 |
def get_response(model_name):
|
184 |
+
if model_name == "gpt-3.5-turbo" or model_name == "gpt-4":
|
185 |
# print(s + "\n")
|
186 |
response = openai.ChatCompletion.create(
|
187 |
model=model_name,
|
|
|
224 |
except Exception as e:
|
225 |
print("An error has occurred during translation:",e)
|
226 |
print("Retrying...")
|
227 |
+
time.sleep(30)
|
228 |
flag = True
|
229 |
+
srt.set_translation(translate, range, model_name)
|
230 |
|
231 |
srt.check_len_and_split()
|
232 |
srt.write_srt_file_translate(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt")
|