File size: 3,207 Bytes
d0f2803
 
 
 
 
 
3dbe507
d0f2803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3dbe507
735788e
d0f2803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import __init_lib_path
import gradio as gr
from src.task import Task
import logging
from yaml import Loader, Dumper, load, dump
import os
import sys
from pathlib import Path
from datetime import datetime
import shutil
from uuid import uuid4

launch_config = "./configs/local_launch.yaml"
task_config = './configs/task_config.yaml'

def init(output_type, src_lang, tgt_lang, domain):
    launch_cfg = load(open(launch_config), Loader=Loader)
    task_cfg = load(open(task_config), Loader=Loader)

    # overwrite config file
    task_cfg["source_lang"] = src_lang
    task_cfg["target_lang"] = tgt_lang
    task_cfg["field"] = domain

    if "Video File" in output_type:
        task_cfg["output_type"]["video"] = True
    else:
        task_cfg["output_type"]["video"] = False
    
    if "Bilingual" in output_type:
        task_cfg["output_type"]["bilingual"] = True
    else:
        task_cfg["output_type"]["bilingual"] = False
    
    if ".ass output" in output_type:
        task_cfg["output_type"]["subtitle"] = "ass"
    else:
        task_cfg["output_type"]["subtitle"] = "srt"

    # initialize dir
    local_dir = Path(launch_cfg['local_dump'])
    if not local_dir.exists():
        local_dir.mkdir(parents=False, exist_ok=False)

    # get task id
    task_id = str(uuid4())

    # create locak dir for the task
    task_dir = local_dir.joinpath(f"task_{task_id}")
    task_dir.mkdir(parents=False, exist_ok=False)
    task_dir.joinpath("results").mkdir(parents=False, exist_ok=False)

    # logging setting
    logfmt = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s"
    logging.basicConfig(level=logging.INFO, format=logfmt, handlers=[
        logging.FileHandler(
            "{}/{}_{}.log".format(task_dir, f"task_{task_id}", datetime.now().strftime("%m%d%Y_%H%M%S")),
            'w', encoding='utf-8'),
        logging.StreamHandler(sys.stdout)])
    return task_id, task_dir, task_cfg

def process_input(video_file, youtube_link, src_lang, tgt_lang, domain, output_type):
    task_id, task_dir, task_cfg = init(output_type, src_lang, tgt_lang, domain)
    if youtube_link:
        task = Task.fromYoutubeLink(youtube_link, task_id, task_dir, task_cfg)
        task.run()
        return task.result
    elif video_file is not None:
        task = Task.fromVideoFile(video_file, task_id, task_dir, task_cfg)
        task.run()
        return task.result
    else:
        return None

demo = gr.Interface(fn=process_input,
    inputs=[
        gr.components.Video(label="Upload a video"),
        gr.components.Textbox(label="Or enter a YouTube URL"), 
        gr.components.Dropdown(choices=["EN", "ZH"], label="Select Source Language"),
        gr.components.Dropdown(choices=["ZH", "EN"], label="Select Target Language"),
        gr.components.Dropdown(choices=["General", "SC2"], label="Select Domain"),
        gr.CheckboxGroup(["Video File", "Bilingual", ".ass output"], label="Output Settings", info="What do you want?"),
    ],
    outputs=[
        gr.components.Video(label="Processed Video")
    ],
    title="ViDove: video translation toolkit demo",
    description="Upload a video or enter a YouTube URL."
    )

if __name__ == "__main__":
    demo.launch()