Spaces:
Running
Running
Eason Lu
commited on
Commit
·
d0f2803
1
Parent(s):
be41edb
debug& add gradio web interface
Browse filesFormer-commit-id: d0cbc42fc6d7d78764a65c7872e0a6ed58ab999d
- configs/task_config.yaml +2 -2
- entries/app.py +90 -0
- src/task.py +20 -11
configs/task_config.yaml
CHANGED
@@ -29,7 +29,7 @@ post_process:
|
|
29 |
# output type that user receive
|
30 |
output_type:
|
31 |
subtitle: srt
|
32 |
-
video:
|
33 |
-
|
34 |
|
35 |
|
|
|
29 |
# output type that user receive
|
30 |
output_type:
|
31 |
subtitle: srt
|
32 |
+
video: True
|
33 |
+
bilingual: True
|
34 |
|
35 |
|
entries/app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import __init_lib_path
|
2 |
+
import gradio as gr
|
3 |
+
from src.task import Task
|
4 |
+
import logging
|
5 |
+
from yaml import Loader, Dumper, load, dump
|
6 |
+
import os
|
7 |
+
from pathlib import Path
|
8 |
+
from datetime import datetime
|
9 |
+
import shutil
|
10 |
+
from uuid import uuid4
|
11 |
+
|
12 |
+
launch_config = "./configs/local_launch.yaml"
|
13 |
+
task_config = './configs/task_config.yaml'
|
14 |
+
|
15 |
+
def init(output_type, src_lang, tgt_lang, domain):
|
16 |
+
launch_cfg = load(open(launch_config), Loader=Loader)
|
17 |
+
task_cfg = load(open(task_config), Loader=Loader)
|
18 |
+
|
19 |
+
# overwrite config file
|
20 |
+
task_cfg["source_lang"] = src_lang
|
21 |
+
task_cfg["target_lang"] = tgt_lang
|
22 |
+
task_cfg["field"] = domain
|
23 |
+
|
24 |
+
if "Video File" in output_type:
|
25 |
+
task_cfg["output_type"]["video"] = True
|
26 |
+
else:
|
27 |
+
task_cfg["output_type"]["video"] = False
|
28 |
+
|
29 |
+
if "Bilingual" in output_type:
|
30 |
+
task_cfg["output_type"]["bilingual"] = True
|
31 |
+
else:
|
32 |
+
task_cfg["output_type"]["bilingual"] = False
|
33 |
+
|
34 |
+
if ".ass output" in output_type:
|
35 |
+
task_cfg["output_type"]["subtitle"] = "ass"
|
36 |
+
else:
|
37 |
+
task_cfg["output_type"]["subtitle"] = "srt"
|
38 |
+
|
39 |
+
# initialize dir
|
40 |
+
local_dir = Path(launch_cfg['local_dump'])
|
41 |
+
if not local_dir.exists():
|
42 |
+
local_dir.mkdir(parents=False, exist_ok=False)
|
43 |
+
|
44 |
+
# get task id
|
45 |
+
task_id = str(uuid4())
|
46 |
+
|
47 |
+
# create locak dir for the task
|
48 |
+
task_dir = local_dir.joinpath(f"task_{task_id}")
|
49 |
+
task_dir.mkdir(parents=False, exist_ok=False)
|
50 |
+
task_dir.joinpath("results").mkdir(parents=False, exist_ok=False)
|
51 |
+
|
52 |
+
# logging setting
|
53 |
+
logfmt = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s"
|
54 |
+
logging.basicConfig(level=logging.INFO, format=logfmt, handlers=[
|
55 |
+
logging.FileHandler(
|
56 |
+
"{}/{}_{}.log".format(task_dir, f"task_{task_id}", datetime.now().strftime("%m%d%Y_%H%M%S")),
|
57 |
+
'w', encoding='utf-8')])
|
58 |
+
return task_id, task_dir, task_cfg
|
59 |
+
|
60 |
+
def process_input(video_file, youtube_link, src_lang, tgt_lang, domain, output_type):
|
61 |
+
task_id, task_dir, task_cfg = init(output_type, src_lang, tgt_lang, domain)
|
62 |
+
if youtube_link:
|
63 |
+
task = Task.fromYoutubeLink(youtube_link, task_id, task_dir, task_cfg)
|
64 |
+
task.run()
|
65 |
+
return task.result
|
66 |
+
elif video_file is not None:
|
67 |
+
task = Task.fromVideoFile(video_file, task_id, task_dir, task_cfg)
|
68 |
+
task.run()
|
69 |
+
return task.result
|
70 |
+
else:
|
71 |
+
return None
|
72 |
+
|
73 |
+
demo = gr.Interface(fn=process_input,
|
74 |
+
inputs=[
|
75 |
+
gr.components.Video(label="Upload a video"),
|
76 |
+
gr.components.Textbox(label="Or enter a YouTube URL"),
|
77 |
+
gr.components.Dropdown(choices=["EN", "ZH"], label="Select Source Language"),
|
78 |
+
gr.components.Dropdown(choices=["ZH", "EN"], label="Select Target Language"),
|
79 |
+
gr.components.Dropdown(choices=["General", "SC2"], label="Select Domain"),
|
80 |
+
gr.CheckboxGroup(["Video File", "Bilingual", ".ass output"], label="Output Settings", info="What do you want?"),
|
81 |
+
],
|
82 |
+
outputs=[
|
83 |
+
gr.components.Video(label="Processed Video")
|
84 |
+
],
|
85 |
+
title="ViDove: video translation toolkit demo",
|
86 |
+
description="Upload a video or enter a YouTube URL."
|
87 |
+
)
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
demo.launch()
|
src/task.py
CHANGED
@@ -3,7 +3,7 @@ import time
|
|
3 |
|
4 |
import openai
|
5 |
from pytube import YouTube
|
6 |
-
from os import getenv
|
7 |
from pathlib import Path
|
8 |
from enum import Enum, auto
|
9 |
import logging
|
@@ -15,6 +15,7 @@ from src.translators.translation import get_translation, prompt_selector
|
|
15 |
|
16 |
import torch
|
17 |
import stable_whisper
|
|
|
18 |
|
19 |
"""
|
20 |
Youtube link
|
@@ -97,7 +98,7 @@ class Task:
|
|
97 |
logging.info(f"Translation Model: {self.translation_model}")
|
98 |
logging.info(f"subtitle_type: {self.output_type['subtitle']}")
|
99 |
logging.info(f"video_ouput: {self.output_type['video']}")
|
100 |
-
logging.info(f"
|
101 |
logging.info("Pre-process setting:")
|
102 |
for key in self.pre_setting:
|
103 |
logging.info(f"{key}: {self.pre_setting[key]}")
|
@@ -176,7 +177,7 @@ class Task:
|
|
176 |
|
177 |
if self.output_type["subtitle"] == "ass":
|
178 |
logging.info("write English .srt file to .ass")
|
179 |
-
assSub_src = srt2ass(processed_srt_path_src)
|
180 |
logging.info('ASS subtitle saved as: ' + assSub_src)
|
181 |
self.script_input = self.SRT_Script.get_source_only()
|
182 |
pass
|
@@ -207,13 +208,13 @@ class Task:
|
|
207 |
self.status = TaskStatus.OUTPUT_MODULE
|
208 |
video_out = self.output_type["video"]
|
209 |
subtitle_type = self.output_type["subtitle"]
|
210 |
-
|
211 |
|
212 |
-
results_dir =
|
213 |
|
214 |
subtitle_path = f"{results_dir}/{self.task_id}_{self.target_lang}.srt"
|
215 |
self.SRT_Script.write_srt_file_translate(subtitle_path)
|
216 |
-
if
|
217 |
subtitle_path = f"{results_dir}/{self.task_id}_{self.source_lang}_{self.target_lang}.srt"
|
218 |
self.SRT_Script.write_srt_file_bilingual(subtitle_path)
|
219 |
|
@@ -227,8 +228,12 @@ class Task:
|
|
227 |
# encode to .mp4 video file
|
228 |
if video_out and self.video_path is not None:
|
229 |
logging.info("encoding video file")
|
|
|
230 |
subprocess.run(
|
231 |
-
|
|
|
|
|
|
|
232 |
final_res = f"{results_dir}/{self.task_id}.mp4"
|
233 |
|
234 |
self.t_e = time()
|
@@ -242,7 +247,7 @@ class Task:
|
|
242 |
self.translation()
|
243 |
self.postprocess()
|
244 |
self.result = self.output_render()
|
245 |
-
print(self.result)
|
246 |
|
247 |
class YoutubeTask(Task):
|
248 |
def __init__(self, task_id, task_local_dir, task_cfg, youtube_url):
|
@@ -286,8 +291,8 @@ class AudioTask(Task):
|
|
286 |
self.video_path = None
|
287 |
|
288 |
def run(self):
|
289 |
-
logging.info(f"
|
290 |
-
logging.info(f"
|
291 |
logging.info("Data Prep Complete. Start pipeline")
|
292 |
super().run_pipeline()
|
293 |
|
@@ -295,7 +300,11 @@ class VideoTask(Task):
|
|
295 |
def __init__(self, task_id, task_local_dir, task_cfg, video_path):
|
296 |
super().__init__(task_id, task_local_dir, task_cfg)
|
297 |
# TODO: check video format {.mp4}
|
298 |
-
|
|
|
|
|
|
|
|
|
299 |
|
300 |
def run(self):
|
301 |
logging.info("using ffmpeg to extract audio")
|
|
|
3 |
|
4 |
import openai
|
5 |
from pytube import YouTube
|
6 |
+
from os import getenv, getcwd
|
7 |
from pathlib import Path
|
8 |
from enum import Enum, auto
|
9 |
import logging
|
|
|
15 |
|
16 |
import torch
|
17 |
import stable_whisper
|
18 |
+
import shutil
|
19 |
|
20 |
"""
|
21 |
Youtube link
|
|
|
98 |
logging.info(f"Translation Model: {self.translation_model}")
|
99 |
logging.info(f"subtitle_type: {self.output_type['subtitle']}")
|
100 |
logging.info(f"video_ouput: {self.output_type['video']}")
|
101 |
+
logging.info(f"bilingual_ouput: {self.output_type['bilingual']}")
|
102 |
logging.info("Pre-process setting:")
|
103 |
for key in self.pre_setting:
|
104 |
logging.info(f"{key}: {self.pre_setting[key]}")
|
|
|
177 |
|
178 |
if self.output_type["subtitle"] == "ass":
|
179 |
logging.info("write English .srt file to .ass")
|
180 |
+
assSub_src = srt2ass(processed_srt_path_src, "default", "No", "Modest")
|
181 |
logging.info('ASS subtitle saved as: ' + assSub_src)
|
182 |
self.script_input = self.SRT_Script.get_source_only()
|
183 |
pass
|
|
|
208 |
self.status = TaskStatus.OUTPUT_MODULE
|
209 |
video_out = self.output_type["video"]
|
210 |
subtitle_type = self.output_type["subtitle"]
|
211 |
+
is_bilingual = self.output_type["bilingual"]
|
212 |
|
213 |
+
results_dir =f"{self.task_local_dir}/results"
|
214 |
|
215 |
subtitle_path = f"{results_dir}/{self.task_id}_{self.target_lang}.srt"
|
216 |
self.SRT_Script.write_srt_file_translate(subtitle_path)
|
217 |
+
if is_bilingual:
|
218 |
subtitle_path = f"{results_dir}/{self.task_id}_{self.source_lang}_{self.target_lang}.srt"
|
219 |
self.SRT_Script.write_srt_file_bilingual(subtitle_path)
|
220 |
|
|
|
228 |
# encode to .mp4 video file
|
229 |
if video_out and self.video_path is not None:
|
230 |
logging.info("encoding video file")
|
231 |
+
logging.info(f'ffmpeg comand: \nffmpeg -i {self.video_path} -vf "subtitles={subtitle_path}" {results_dir}/{self.task_id}.mp4')
|
232 |
subprocess.run(
|
233 |
+
["ffmpeg",
|
234 |
+
"-i", self.video_path,
|
235 |
+
"-vf", f"subtitles={subtitle_path}",
|
236 |
+
f"{results_dir}/{self.task_id}.mp4"])
|
237 |
final_res = f"{results_dir}/{self.task_id}.mp4"
|
238 |
|
239 |
self.t_e = time()
|
|
|
247 |
self.translation()
|
248 |
self.postprocess()
|
249 |
self.result = self.output_render()
|
250 |
+
# print(self.result)
|
251 |
|
252 |
class YoutubeTask(Task):
|
253 |
def __init__(self, task_id, task_local_dir, task_cfg, youtube_url):
|
|
|
291 |
self.video_path = None
|
292 |
|
293 |
def run(self):
|
294 |
+
logging.info(f"Video File Dir: {self.video_path}")
|
295 |
+
logging.info(f"Audio File Dir: {self.audio_path}")
|
296 |
logging.info("Data Prep Complete. Start pipeline")
|
297 |
super().run_pipeline()
|
298 |
|
|
|
300 |
def __init__(self, task_id, task_local_dir, task_cfg, video_path):
|
301 |
super().__init__(task_id, task_local_dir, task_cfg)
|
302 |
# TODO: check video format {.mp4}
|
303 |
+
new_video_path = f"{task_local_dir}/task_{self.task_id}.mp4"
|
304 |
+
print(new_video_path)
|
305 |
+
logging.info(f"Copy video file to: {new_video_path}")
|
306 |
+
shutil.copyfile(video_path, new_video_path)
|
307 |
+
self.video_path = new_video_path
|
308 |
|
309 |
def run(self):
|
310 |
logging.info("using ffmpeg to extract audio")
|