forked from jhj0517/Whisper-WebUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
410 lines (362 loc) · 26.8 KB
/
app.py
File metadata and controls
410 lines (362 loc) · 26.8 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import os
import argparse
import gradio as gr
from gradio_i18n import Translate, gettext as _
import yaml
from modules.utils.paths import (FASTER_WHISPER_MODELS_DIR, DIARIZATION_MODELS_DIR, OUTPUT_DIR, WHISPER_MODELS_DIR,
INSANELY_FAST_WHISPER_MODELS_DIR, NLLB_MODELS_DIR, DEFAULT_PARAMETERS_CONFIG_PATH,
UVR_MODELS_DIR, I18N_YAML_PATH)
from modules.utils.files_manager import load_yaml, MEDIA_EXTENSION
from modules.whisper.whisper_factory import WhisperFactory
from modules.translation.nllb_inference import NLLBInference
from modules.ui.htmls import *
from modules.utils.cli_manager import str2bool
from modules.utils.youtube_manager import get_ytmetas
from modules.translation.deepl_api import DeepLAPI
from modules.whisper.data_classes import *
from modules.utils.logger import get_logger
logger = get_logger()
class App:
def __init__(self, args):
self.args = args
# Check every 1 hour (3600) for cached files and delete them if older than 1 day (86400)
self.app = gr.Blocks(css=CSS, theme=self.args.theme, delete_cache=(3600, 86400))
self.whisper_inf = WhisperFactory.create_whisper_inference(
whisper_type=self.args.whisper_type,
whisper_model_dir=self.args.whisper_model_dir,
faster_whisper_model_dir=self.args.faster_whisper_model_dir,
insanely_fast_whisper_model_dir=self.args.insanely_fast_whisper_model_dir,
uvr_model_dir=self.args.uvr_model_dir,
output_dir=self.args.output_dir,
)
self.nllb_inf = NLLBInference(
model_dir=self.args.nllb_model_dir,
output_dir=os.path.join(self.args.output_dir, "translations")
)
self.deepl_api = DeepLAPI(
output_dir=os.path.join(self.args.output_dir, "translations")
)
self.i18n = load_yaml(I18N_YAML_PATH)
self.default_params = load_yaml(DEFAULT_PARAMETERS_CONFIG_PATH)
logger.info(f"Use \"{self.args.whisper_type}\" implementation\n"
f"Device \"{self.whisper_inf.device}\" is detected")
def create_pipeline_inputs(self):
whisper_params = self.default_params["whisper"]
vad_params = self.default_params["vad"]
diarization_params = self.default_params["diarization"]
uvr_params = self.default_params["bgm_separation"]
with gr.Row():
dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value=whisper_params["model_size"],
label=_("Model"), allow_custom_value=True)
dd_lang = gr.Dropdown(choices=self.whisper_inf.available_langs + [AUTOMATIC_DETECTION],
value=AUTOMATIC_DETECTION if whisper_params["lang"] == AUTOMATIC_DETECTION.unwrap()
else whisper_params["lang"], label=_("Language"))
dd_file_format = gr.Dropdown(choices=["SRT", "WebVTT", "txt", "LRC"], value=whisper_params["file_format"], label=_("File Format"))
with gr.Row():
cb_translate = gr.Checkbox(value=whisper_params["is_translate"], label=_("Translate to English?"),
interactive=True)
with gr.Row():
cb_timestamp = gr.Checkbox(value=whisper_params["add_timestamp"],
label=_("Add a timestamp to the end of the filename"),
interactive=True)
with gr.Accordion(_("Advanced Parameters"), open=False):
whisper_inputs = WhisperParams.to_gradio_inputs(defaults=whisper_params, only_advanced=True,
whisper_type=self.args.whisper_type,
available_compute_types=self.whisper_inf.available_compute_types,
compute_type=self.whisper_inf.current_compute_type)
with gr.Accordion(_("Background Music Remover Filter"), open=False):
uvr_inputs = BGMSeparationParams.to_gradio_input(defaults=uvr_params,
available_models=self.whisper_inf.music_separator.available_models,
available_devices=self.whisper_inf.music_separator.available_devices,
device=self.whisper_inf.music_separator.device)
with gr.Accordion(_("Voice Detection Filter"), open=False):
vad_inputs = VadParams.to_gradio_inputs(defaults=vad_params)
with gr.Accordion(_("Diarization"), open=False):
diarization_inputs = DiarizationParams.to_gradio_inputs(defaults=diarization_params,
available_devices=self.whisper_inf.diarizer.available_device,
device=self.whisper_inf.diarizer.device)
with gr.Accordion(_("VAD (aadnk)"), open=False):
aadnk_vad_enable = gr.Checkbox(label="Silero VAD 필터 활성화", value=True)
gr.Markdown("서브 모델에 의해 목소리라고 판단된 부분만 받아쓰기를 진행합니다.")
aadnk_vad_input = gr.Dropdown(choices=["silero-vad", "silero-vad-skip-gaps", "silero-vad-expand-into-gaps"], value="silero-vad", label="VAD Mode")
aadnk_vad_merge_window = gr.Number(value=5.0, label="VAD - Merge Window (s)", info="인접한 음성 구간 사이의 간격이 이 시간보다 짧으면 하나로 병합합니다. (추천: 0.5~1.0초)")
aadnk_vad_max_merge_size = gr.Number(value=30.0, label="VAD - Max Merge Size (s)", info="병합된 음성 구간의 길이가 이 시간을 초과하지 않도록 제한합니다.")
aadnk_vad_padding = gr.Number(value=1.0, label="VAD - Padding (s)", info="감지된 음성 구간의 앞뒤로 추가할 여유 시간(초)입니다.")
aadnk_vad_prompt_window = gr.Number(value=3.0, label="VAD - Prompt Window (s)", info="이전 음성의 텍스트를 다음 구간의 프롬프트로 넘겨줄 기준 시간입니다.")
gr.Markdown("### 할루시네이션 교정 옵션\n")
hal_enable = gr.Checkbox(label="할루시네이션 교정 알고리즘 활성화", value=False)
with gr.Row():
hal_cr_threshold = gr.Number(value=2.4, label="Compression Ratio 임계값", info="이 수치 이상이면 환각으로 간주 (추천: 2.4~2.7)")
hal_strategy = gr.Radio(choices=["Temp+", "Time+", "Temp+Time+"], value="Temp+", label="교정 전략")
gr.Markdown("**[Temp+] 온도 조절 설정**")
with gr.Row():
hal_temp_start = gr.Number(value=0.0, label="시작 Temperature", info="환각 감지 시 최초로 재시도할 온도입니다.")
hal_temp_step = gr.Number(value=0.07, label="Temperature 증가폭", info="재시도 시마다 온도를 얼마나 올릴지 결정합니다. (최대 1.0 제한)")
hal_temp_retry = gr.Number(value=6, precision=0, label="최대 재시도 횟수", info="온도를 올려가며 재시도할 최대 횟수입니다.")
gr.Markdown("**[Time+] 시간 절삭 설정**")
with gr.Row():
hal_time_start = gr.Number(value=1, label="시작 절삭 시간(s)", info="온도 조절 실패 시 오디오 앞부분을 처음 잘라낼 시간입니다.")
hal_time_step = gr.Number(value=0.5, label="절삭 시간 증가폭(s)", info="재시도 시마다 추가로 잘라낼 시간입니다.")
hal_time_retry = gr.Number(value=20, precision=0, label="최대 재시도 횟수", info="오디오의 절삭을 재시도할 최대 횟수입니다.")
aadnk_vad_inputs = [
aadnk_vad_enable, aadnk_vad_input, aadnk_vad_merge_window,
aadnk_vad_max_merge_size, aadnk_vad_padding, aadnk_vad_prompt_window,
hal_enable, hal_cr_threshold, hal_strategy,
hal_temp_start, hal_temp_step, hal_temp_retry,
hal_time_start, hal_time_step, hal_time_retry
]
pipeline_inputs = [dd_model, dd_lang, cb_translate] + whisper_inputs + vad_inputs + diarization_inputs + uvr_inputs + aadnk_vad_inputs
return (
pipeline_inputs,
dd_file_format,
cb_timestamp
)
def launch(self):
translation_params = self.default_params["translation"]
deepl_params = translation_params["deepl"]
nllb_params = translation_params["nllb"]
uvr_params = self.default_params["bgm_separation"]
with self.app:
lang = gr.Radio(choices=list(self.i18n.keys()),
label=_("Language"), interactive=True,
visible=False, # Set it by development purpose.
)
with Translate(self.i18n): # Add `lang = lang` here to test dynamic change of the languages.
with gr.Row():
with gr.Column():
gr.Markdown(MARKDOWN, elem_id="md_project")
with gr.Tabs():
with gr.TabItem(_("File")): # tab1
with gr.Column():
input_file = gr.Files(type="filepath", label=_("Upload File here"), file_types=MEDIA_EXTENSION)
tb_input_folder = gr.Textbox(label="Input Folder Path (Optional)",
info="Optional: Specify the folder path where the input files are located, if you prefer to use local files instead of uploading them."
" Leave this field empty if you do not wish to use a local path.",
visible=self.args.colab,
value="")
cb_include_subdirectory = gr.Checkbox(label="Include Subdirectory Files",
info="When using Input Folder Path above, whether to include all files in the subdirectory or not.",
visible=self.args.colab,
value=False)
cb_save_same_dir = gr.Checkbox(label="Save outputs at same directory",
info="When using Input Folder Path above, whether to save output in the same directory as inputs or not, in addition to the original"
" output directory.",
visible=self.args.colab,
value=True)
pipeline_params, dd_file_format, cb_timestamp = self.create_pipeline_inputs()
with gr.Row():
btn_run = gr.Button(_("GENERATE SUBTITLE FILE"), variant="primary")
with gr.Row():
tb_indicator = gr.Textbox(label=_("Output"), scale=5)
files_subtitles = gr.Files(label=_("Downloadable output file"), scale=3, interactive=False)
btn_openfolder = gr.Button('📂', scale=1)
params = [input_file, tb_input_folder, cb_include_subdirectory, cb_save_same_dir,
dd_file_format, cb_timestamp]
params = params + pipeline_params
btn_run.click(fn=self.whisper_inf.transcribe_file,
inputs=params,
outputs=[tb_indicator, files_subtitles])
btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
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)
pipeline_params, dd_file_format, cb_timestamp = self.create_pipeline_inputs()
with gr.Row():
btn_run = gr.Button(_("GENERATE SUBTITLE FILE"), variant="primary")
with gr.Row():
tb_indicator = gr.Textbox(label=_("Output"), scale=5)
files_subtitles = gr.Files(label=_("Downloadable output file"), scale=3)
btn_openfolder = gr.Button('📂', scale=1)
params = [tb_youtubelink, dd_file_format, cb_timestamp]
btn_run.click(fn=self.whisper_inf.transcribe_youtube,
inputs=params + pipeline_params,
outputs=[tb_indicator, files_subtitles])
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)
with gr.TabItem(_("Mic")): # tab3
with gr.Row():
mic_input = gr.Microphone(label=_("Record with Mic"), type="filepath", interactive=True,
show_download_button=True)
pipeline_params, dd_file_format, cb_timestamp = self.create_pipeline_inputs()
with gr.Row():
btn_run = gr.Button(_("GENERATE SUBTITLE FILE"), variant="primary")
with gr.Row():
tb_indicator = gr.Textbox(label=_("Output"), scale=5)
files_subtitles = gr.Files(label=_("Downloadable output file"), scale=3)
btn_openfolder = gr.Button('📂', scale=1)
params = [mic_input, dd_file_format, cb_timestamp]
btn_run.click(fn=self.whisper_inf.transcribe_mic,
inputs=params + pipeline_params,
outputs=[tb_indicator, files_subtitles])
btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
with gr.TabItem(_("T2T Translation")): # tab 4
with gr.Row():
file_subs = gr.Files(type="filepath", label=_("Upload Subtitle Files to translate here"))
with gr.TabItem(_("DeepL API")): # sub tab1
with gr.Row():
tb_api_key = gr.Textbox(label=_("Your Auth Key (API KEY)"),
value=deepl_params["api_key"])
with gr.Row():
dd_source_lang = gr.Dropdown(label=_("Source Language"),
value=AUTOMATIC_DETECTION if deepl_params["source_lang"] == AUTOMATIC_DETECTION.unwrap()
else deepl_params["source_lang"],
choices=list(self.deepl_api.available_source_langs.keys()))
dd_target_lang = gr.Dropdown(label=_("Target Language"),
value=deepl_params["target_lang"],
choices=list(self.deepl_api.available_target_langs.keys()))
with gr.Row():
cb_is_pro = gr.Checkbox(label=_("Pro User?"), value=deepl_params["is_pro"])
with gr.Row():
cb_timestamp = gr.Checkbox(value=translation_params["add_timestamp"],
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=5)
files_subtitles = gr.Files(label=_("Downloadable output file"), scale=3)
btn_openfolder = gr.Button('📂', scale=1)
btn_run.click(fn=self.deepl_api.translate_deepl,
inputs=[tb_api_key, file_subs, dd_source_lang, dd_target_lang,
cb_is_pro, cb_timestamp],
outputs=[tb_indicator, files_subtitles])
btn_openfolder.click(
fn=lambda: self.open_folder(os.path.join(self.args.output_dir, "translations")),
inputs=None,
outputs=None)
with gr.TabItem(_("NLLB")): # sub tab2
with gr.Row():
dd_model_size = gr.Dropdown(label=_("Model"), value=nllb_params["model_size"],
choices=self.nllb_inf.available_models)
dd_source_lang = gr.Dropdown(label=_("Source Language"),
value=nllb_params["source_lang"],
choices=self.nllb_inf.available_source_langs)
dd_target_lang = gr.Dropdown(label=_("Target Language"),
value=nllb_params["target_lang"],
choices=self.nllb_inf.available_target_langs)
with gr.Row():
nb_max_length = gr.Number(label="Max Length Per Line", value=nllb_params["max_length"],
precision=0)
with gr.Row():
cb_timestamp = gr.Checkbox(value=translation_params["add_timestamp"],
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=5)
files_subtitles = gr.Files(label=_("Downloadable output file"), scale=3)
btn_openfolder = gr.Button('📂', scale=1)
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_model_size, dd_source_lang, dd_target_lang,
nb_max_length, cb_timestamp],
outputs=[tb_indicator, files_subtitles])
btn_openfolder.click(
fn=lambda: self.open_folder(os.path.join(self.args.output_dir, "translations")),
inputs=None,
outputs=None)
with gr.TabItem(_("BGM Separation")):
files_audio = gr.Files(type="filepath", label=_("Upload Audio Files to separate background music"))
dd_uvr_device = gr.Dropdown(label=_("Device"), value=self.whisper_inf.music_separator.device,
choices=self.whisper_inf.music_separator.available_devices)
dd_uvr_model_size = gr.Dropdown(label=_("Model"), value=uvr_params["uvr_model_size"],
choices=self.whisper_inf.music_separator.available_models)
nb_uvr_segment_size = gr.Number(label="Segment Size", value=uvr_params["segment_size"],
precision=0)
cb_uvr_save_file = gr.Checkbox(label=_("Save separated files to output"),
value=True, visible=False)
btn_run = gr.Button(_("SEPARATE BACKGROUND MUSIC"), variant="primary")
with gr.Column():
with gr.Row():
ad_instrumental = gr.Audio(label=_("Instrumental"), scale=8)
btn_open_instrumental_folder = gr.Button('📂', scale=1)
with gr.Row():
ad_vocals = gr.Audio(label=_("Vocals"), scale=8)
btn_open_vocals_folder = gr.Button('📂', scale=1)
btn_run.click(fn=self.whisper_inf.music_separator.separate_files,
inputs=[files_audio, dd_uvr_model_size, dd_uvr_device, nb_uvr_segment_size,
cb_uvr_save_file],
outputs=[ad_instrumental, ad_vocals])
btn_open_instrumental_folder.click(inputs=None,
outputs=None,
fn=lambda: self.open_folder(os.path.join(
self.args.output_dir, "UVR", "instrumental"
)))
btn_open_vocals_folder.click(inputs=None,
outputs=None,
fn=lambda: self.open_folder(os.path.join(
self.args.output_dir, "UVR", "vocals"
)))
# Launch the app with optional gradio settings
args = self.args
self.app.queue(
api_open=args.api_open
).launch(
share=args.share,
server_name=args.server_name,
server_port=args.server_port,
auth=(args.username, args.password) if args.username and args.password else None,
root_path=args.root_path,
inbrowser=args.inbrowser,
ssl_verify=args.ssl_verify,
ssl_keyfile=args.ssl_keyfile,
ssl_keyfile_password=args.ssl_keyfile_password,
ssl_certfile=args.ssl_certfile,
allowed_paths=eval(args.allowed_paths) if args.allowed_paths else None
)
@staticmethod
def open_folder(folder_path: str):
if os.path.exists(folder_path):
os.system(f"start {folder_path}")
else:
os.makedirs(folder_path, exist_ok=True)
logger.info(f"The directory path {folder_path} has newly created.")
parser = argparse.ArgumentParser()
parser.add_argument('--whisper_type', type=str, default=WhisperImpl.FASTER_WHISPER.value,
choices=[item.value for item in WhisperImpl],
help='A type of the whisper implementation (Github repo name)')
parser.add_argument('--share', type=str2bool, 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('--root_path', type=str, default=None, help='Gradio root path')
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')
parser.add_argument('--colab', type=str2bool, default=False, nargs='?', const=True, help='Is colab user or not')
parser.add_argument('--api_open', type=str2bool, default=False, nargs='?', const=True,
help='Enable api or not in Gradio')
parser.add_argument('--allowed_paths', type=str, default=None, help='Gradio allowed paths')
parser.add_argument('--inbrowser', type=str2bool, default=True, nargs='?', const=True,
help='Whether to automatically start Gradio app or not')
parser.add_argument('--ssl_verify', type=str2bool, default=True, nargs='?', const=True,
help='Whether to verify SSL or not')
parser.add_argument('--ssl_keyfile', type=str, default=None, help='SSL Key file location')
parser.add_argument('--ssl_keyfile_password', type=str, default=None, help='SSL Key file password')
parser.add_argument('--ssl_certfile', type=str, default=None, help='SSL cert file location')
parser.add_argument('--whisper_model_dir', type=str, default=WHISPER_MODELS_DIR,
help='Directory path of the whisper model')
parser.add_argument('--faster_whisper_model_dir', type=str, default=FASTER_WHISPER_MODELS_DIR,
help='Directory path of the faster-whisper model')
parser.add_argument('--insanely_fast_whisper_model_dir', type=str,
default=INSANELY_FAST_WHISPER_MODELS_DIR,
help='Directory path of the insanely-fast-whisper model')
parser.add_argument('--diarization_model_dir', type=str, default=DIARIZATION_MODELS_DIR,
help='Directory path of the diarization model')
parser.add_argument('--nllb_model_dir', type=str, default=NLLB_MODELS_DIR,
help='Directory path of the Facebook NLLB model')
parser.add_argument('--uvr_model_dir', type=str, default=UVR_MODELS_DIR,
help='Directory path of the UVR model')
parser.add_argument('--output_dir', type=str, default=OUTPUT_DIR, help='Directory path of the outputs')
_args = parser.parse_args()
if __name__ == "__main__":
app = App(args=_args)
app.launch()