Spaces:
Running
Running
File size: 10,973 Bytes
63ab978 9cf2e86 f7d7f08 eeb8996 3fde2e0 63ab978 579e8cb 63ab978 9cf2e86 0f16dda eeb8996 9cf2e86 736206b 9cf2e86 63ab978 9cf2e86 63ab978 9cf2e86 3fde2e0 9cf2e86 957d7a3 9cf2e86 2ddb400 ccf78ae 2ddb400 9cf2e86 ccf78ae 9cf2e86 ccf78ae 9cf2e86 ccf78ae 9cf2e86 ccf78ae 9cf2e86 ccf78ae 9cf2e86 35245db 9cf2e86 35245db 3fde2e0 18639e5 9cf2e86 eeb8996 d7f2438 18639e5 d7f2438 0f16dda 9cf2e86 3fde2e0 9cf2e86 |
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
import gradio as gr
import os
import argparse
from modules.whisper_Inference import WhisperInference
from modules.faster_whisper_inference import FasterWhisperInference
from modules.nllb_inference import NLLBInference
from ui.htmls import *
from modules.youtube_manager import get_ytmetas
class App:
def __init__(self, args):
self.args = args
self.app = gr.Blocks(css=CSS, theme=self.args.theme)
self.whisper_inf = WhisperInference() if self.args.disable_faster_whisper else FasterWhisperInference()
if isinstance(self.whisper_inf, FasterWhisperInference):
print("Use Faster Whisper implementation")
else:
print("Use Open AI Whisper implementation")
self.nllb_inf = NLLBInference()
@staticmethod
def open_folder(folder_path: str):
if os.path.exists(folder_path):
os.system(f"start {folder_path}")
else:
print(f"The folder {folder_path} does not exist.")
@staticmethod
def on_change_models(model_size: str):
translatable_model = ["large", "large-v1", "large-v2"]
if model_size not in translatable_model:
return gr.Checkbox.update(visible=False, value=False, interactive=False)
else:
return gr.Checkbox.update(visible=True, value=False, label="Translate to English?", interactive=True)
def launch(self):
with self.app:
with gr.Row():
with gr.Column():
gr.Markdown(MARKDOWN, elem_id="md_project")
with gr.Tabs():
with gr.TabItem("File"): # tab1
with gr.Row():
input_file = gr.Files(type="file", label="Upload File here")
with gr.Row():
dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2",
label="Model")
dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs,
value="Automatic Detection", label="Language")
dd_subformat = gr.Dropdown(["SRT", "WebVTT"], value="SRT", label="Subtitle Format")
with gr.Row():
cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
with gr.Row():
cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename", interactive=True)
with gr.Row():
btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
with gr.Row():
tb_indicator = gr.Textbox(label="Output", scale=8)
btn_openfolder = gr.Button('π', scale=2)
btn_run.click(fn=self.whisper_inf.transcribe_file,
inputs=[input_file, dd_model, dd_lang, dd_subformat, cb_translate, cb_timestamp],
outputs=[tb_indicator])
btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
with gr.TabItem("Youtube"): # tab2
with gr.Row():
tb_youtubelink = gr.Textbox(label="Youtube Link")
with gr.Row(equal_height=True):
with gr.Column():
img_thumbnail = gr.Image(label="Youtube Thumbnail")
with gr.Column():
tb_title = gr.Label(label="Youtube Title")
tb_description = gr.Textbox(label="Youtube Description", max_lines=15)
with gr.Row():
dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2",
label="Model")
dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs,
value="Automatic Detection", label="Language")
dd_subformat = gr.Dropdown(choices=["SRT", "WebVTT"], value="SRT", label="Subtitle Format")
with gr.Row():
cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
with gr.Row():
cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename",
interactive=True)
with gr.Row():
btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
with gr.Row():
tb_indicator = gr.Textbox(label="Output", scale=8)
btn_openfolder = gr.Button('π', scale=2)
btn_run.click(fn=self.whisper_inf.transcribe_youtube,
inputs=[tb_youtubelink, dd_model, dd_lang, dd_subformat, cb_translate, cb_timestamp],
outputs=[tb_indicator])
tb_youtubelink.change(get_ytmetas, inputs=[tb_youtubelink],
outputs=[img_thumbnail, tb_title, tb_description])
btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
with gr.TabItem("Mic"): # tab3
with gr.Row():
mic_input = gr.Microphone(label="Record with Mic", type="filepath", interactive=True)
with gr.Row():
dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2",
label="Model")
dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs,
value="Automatic Detection", label="Language")
dd_subformat = gr.Dropdown(["SRT", "WebVTT"], value="SRT", label="Subtitle Format")
with gr.Row():
cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
with gr.Row():
btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
with gr.Row():
tb_indicator = gr.Textbox(label="Output", scale=8)
btn_openfolder = gr.Button('π', scale=2)
btn_run.click(fn=self.whisper_inf.transcribe_mic,
inputs=[mic_input, dd_model, dd_lang, dd_subformat, cb_translate],
outputs=[tb_indicator])
btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
with gr.TabItem("T2T Translation"): # tab 4
with gr.Row():
file_subs = gr.Files(type="file", label="Upload Subtitle Files to translate here",
file_types=['.vtt', '.srt'])
with gr.TabItem("NLLB"): # sub tab1
with gr.Row():
dd_nllb_model = gr.Dropdown(label="Model", value=self.nllb_inf.default_model_size,
choices=self.nllb_inf.available_models)
dd_nllb_sourcelang = gr.Dropdown(label="Source Language",
choices=self.nllb_inf.available_source_langs)
dd_nllb_targetlang = gr.Dropdown(label="Target Language",
choices=self.nllb_inf.available_target_langs)
with gr.Row():
cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename",
interactive=True)
with gr.Row():
btn_run = gr.Button("TRANSLATE SUBTITLE FILE", variant="primary")
with gr.Row():
tb_indicator = gr.Textbox(label="Output", scale=8)
btn_openfolder = gr.Button('π', scale=2)
with gr.Column():
md_vram_table = gr.HTML(NLLB_VRAM_TABLE, elem_id="md_nllb_vram_table")
btn_run.click(fn=self.nllb_inf.translate_file,
inputs=[file_subs, dd_nllb_model, dd_nllb_sourcelang, dd_nllb_targetlang, cb_timestamp],
outputs=[tb_indicator])
btn_openfolder.click(fn=lambda: self.open_folder(os.path.join("outputs", "translations")),
inputs=None,
outputs=None)
# Launch the app with optional gradio settings
launch_args = {}
if self.args.share:
launch_args['share'] = self.args.share
if self.args.server_name:
launch_args['server_name'] = self.args.server_name
if self.args.server_port:
launch_args['server_port'] = self.args.server_port
if self.args.username and self.args.password:
launch_args['auth'] = (self.args.username, self.args.password)
self.app.queue(api_open=False).launch(**launch_args)
# Create the parser for command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--disable_faster_whisper', type=bool, default=False, nargs='?', const=True, help='Disable the faster_whisper implementation. faster_whipser is implemented by https://github.com/guillaumekln/faster-whisper')
parser.add_argument('--share', type=bool, default=False, nargs='?', const=True, help='Gradio share value')
parser.add_argument('--server_name', type=str, default=None, help='Gradio server host')
parser.add_argument('--server_port', type=int, default=None, help='Gradio server port')
parser.add_argument('--username', type=str, default=None, help='Gradio authentication username')
parser.add_argument('--password', type=str, default=None, help='Gradio authentication password')
parser.add_argument('--theme', type=str, default=None, help='Gradio Blocks theme')
_args = parser.parse_args()
if __name__ == "__main__":
app = App(args=_args)
app.launch()
|