Spaces:
Sleeping
Sleeping
JiaenLiu
commited on
Commit
·
04ef04e
1
Parent(s):
00ca03a
module 1 and module 4
Browse filesFormer-commit-id: 5053510b5fc3e67be394af64af26b347de3739a1
- src/task.py +72 -3
src/task.py
CHANGED
@@ -10,8 +10,10 @@ import logging
|
|
10 |
import subprocess
|
11 |
from src.srt_util.srt import SrtScript
|
12 |
from src.srt_util.srt2ass import srt2ass
|
|
|
13 |
|
14 |
-
|
|
|
15 |
|
16 |
"""
|
17 |
Youtube link
|
@@ -77,6 +79,8 @@ class Task:
|
|
77 |
self.progress = NotImplemented
|
78 |
self.SRT_Script = None
|
79 |
self.result = None
|
|
|
|
|
80 |
|
81 |
|
82 |
@staticmethod
|
@@ -100,7 +104,40 @@ class Task:
|
|
100 |
def get_srt_class(self, whisper_model='tiny', method="stable"):
|
101 |
# Instead of using the script_en variable directly, we'll use script_input
|
102 |
self.status = TaskStatus.INITIALIZING_ASR
|
103 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
time.sleep(5)
|
105 |
pass
|
106 |
|
@@ -129,11 +166,43 @@ class Task:
|
|
129 |
# Module 3: perform srt translation
|
130 |
def translation(self):
|
131 |
time.sleep(5)
|
|
|
|
|
|
|
132 |
pass
|
133 |
|
134 |
# Module 4: perform srt post process steps
|
135 |
-
def postprocess(self):
|
136 |
self.status = TaskStatus.POST_PROCESSING
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
time.sleep(5)
|
138 |
pass
|
139 |
|
|
|
10 |
import subprocess
|
11 |
from src.srt_util.srt import SrtScript
|
12 |
from src.srt_util.srt2ass import srt2ass
|
13 |
+
from time import time, strftime, gmtime, sleep
|
14 |
|
15 |
+
import torch
|
16 |
+
import stable_whisper
|
17 |
|
18 |
"""
|
19 |
Youtube link
|
|
|
79 |
self.progress = NotImplemented
|
80 |
self.SRT_Script = None
|
81 |
self.result = None
|
82 |
+
self.s_t = None
|
83 |
+
self.t_e = None
|
84 |
|
85 |
|
86 |
@staticmethod
|
|
|
104 |
def get_srt_class(self, whisper_model='tiny', method="stable"):
|
105 |
# Instead of using the script_en variable directly, we'll use script_input
|
106 |
self.status = TaskStatus.INITIALIZING_ASR
|
107 |
+
self.t_s = time()
|
108 |
+
# self.SRT_Script = SrtScript
|
109 |
+
|
110 |
+
en_srt_path = self.task_local_dir.joinpath(f"task_{self.task_id})_en.srt")
|
111 |
+
if not Path.exists(en_srt_path):
|
112 |
+
# extract script from audio
|
113 |
+
logging.info("extract script from audio")
|
114 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
115 |
+
logging.info("device: ", device)
|
116 |
+
|
117 |
+
audio_path = self.task_local_dir.joinpath(f"task_{self.task_id}.mp3")
|
118 |
+
|
119 |
+
if method == "api":
|
120 |
+
with open(audio_path, 'rb') as audio_file:
|
121 |
+
transcript = openai.Audio.transcribe(model="whisper-1", file=audio_file, response_format="srt")
|
122 |
+
elif method == "stable":
|
123 |
+
model = stable_whisper.load_model(whisper_model, device)
|
124 |
+
transcript = model.transcribe(str(self.audio_path), regroup=False,
|
125 |
+
initial_prompt="Hello, welcome to my lecture. Are you good my friend?")
|
126 |
+
(
|
127 |
+
transcript
|
128 |
+
.split_by_punctuation(['.', '。', '?'])
|
129 |
+
.merge_by_gap(.15, max_words=3)
|
130 |
+
.merge_by_punctuation([' '])
|
131 |
+
.split_by_punctuation(['.', '。', '?'])
|
132 |
+
)
|
133 |
+
transcript = transcript.to_dict()
|
134 |
+
|
135 |
+
# after get the transcript, release the gpu resource
|
136 |
+
torch.cuda.empty_cache()
|
137 |
+
|
138 |
+
self.SRT_Script = SrtScript(transcript)
|
139 |
+
# save the srt script to local
|
140 |
+
self.SRT_Script.write_srt_file_src(en_srt_path)
|
141 |
time.sleep(5)
|
142 |
pass
|
143 |
|
|
|
166 |
# Module 3: perform srt translation
|
167 |
def translation(self):
|
168 |
time.sleep(5)
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
pass
|
173 |
|
174 |
# Module 4: perform srt post process steps
|
175 |
+
def postprocess(self, encode=False, srt_only=False):
|
176 |
self.status = TaskStatus.POST_PROCESSING
|
177 |
+
|
178 |
+
logging.info("---------------------Start Post-processing SRT class---------------------")
|
179 |
+
self.SRT_Script.check_len_and_split()
|
180 |
+
self.SRT_Script.remove_trans_punctuation()
|
181 |
+
|
182 |
+
base_path = Path(self.dir_result).joinpath(self.video_name).joinpath(self.video_name)
|
183 |
+
|
184 |
+
self.SRT_Script.write_srt_file_translate(f"{base_path}_zh.srt")
|
185 |
+
self.SRT_Script.write_srt_file_bilingual(f"{base_path}_bi.srt")
|
186 |
+
|
187 |
+
logging.info("write Chinese .srt file to .ass")
|
188 |
+
assSub_zh = srt2ass(f"{base_path}_zh.srt", "default", "No", "Modest")
|
189 |
+
logging.info('ASS subtitle saved as: ' + assSub_zh)
|
190 |
+
|
191 |
+
# encode to .mp4 video file
|
192 |
+
if encode:
|
193 |
+
logging.info("encoding video file")
|
194 |
+
if srt_only:
|
195 |
+
subprocess.run(
|
196 |
+
f'ffmpeg -i {self.video_path} -vf "subtitles={base_path}_zh.srt" {base_path}.mp4')
|
197 |
+
else:
|
198 |
+
subprocess.run(
|
199 |
+
f'ffmpeg -i {self.video_path} -vf "subtitles={base_path}_zh.ass" {base_path}.mp4')
|
200 |
+
|
201 |
+
self.t_e = time()
|
202 |
+
logging.info(
|
203 |
+
"Pipeline finished, time duration:{}".format(strftime("%H:%M:%S", gmtime(self.t_e - self.t_s))))
|
204 |
+
|
205 |
+
|
206 |
time.sleep(5)
|
207 |
pass
|
208 |
|