IRISLAB commited on
Commit
533b363
β€’
1 Parent(s): fc0ec2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +270 -276
app.py CHANGED
@@ -16,14 +16,12 @@ class App:
16
  self.args = args
17
  self.app = gr.Blocks(css=CSS, theme=self.args.theme)
18
  self.whisper_inf = self.init_whisper()
19
- print(f"Use \"{self.args.whisper_type}\" implementation")
20
- print(f"Device \"{self.whisper_inf.device}\" is detected")
21
  self.nllb_inf = NLLBInference()
22
  self.deepl_api = DeepLAPI()
 
23
 
24
  def init_whisper(self):
25
  whisper_type = self.args.whisper_type.lower().strip()
26
-
27
  if whisper_type in ["faster_whisper", "faster-whisper"]:
28
  whisper_inf = FasterWhisperInference()
29
  whisper_inf.model_dir = self.args.faster_whisper_model_dir
@@ -35,26 +33,27 @@ class App:
35
  whisper_inf.model_dir = self.args.faster_whisper_model_dir
36
  return whisper_inf
37
 
 
 
 
 
38
  @staticmethod
39
  def open_folder(folder_path: str):
40
  if os.path.exists(folder_path):
41
- os.system(f"start {folder_path}")
42
  else:
43
- print(f"The folder {folder_path} does not exist.")
44
 
45
  @staticmethod
46
  def on_change_models(model_size: str):
47
  translatable_model = ["large", "large-v1", "large-v2", "large-v3"]
48
- if model_size not in translatable_model:
49
- return gr.Checkbox(visible=False, value=False, interactive=False)
50
- else:
51
- return gr.Checkbox(visible=True, value=False, label="Translate to English?", interactive=True)
52
 
53
  def transcribe_file_wrapper(self, file, file_format, timestamp, *whisper_params):
54
  try:
55
  result, output_file = self.whisper_inf.transcribe_file(file, file_format, timestamp, *whisper_params)
56
  if not os.path.exists(output_file):
57
- raise FileNotFoundError(f"Output file {output_file} does not exist.")
58
  return result, output_file
59
  except Exception as e:
60
  return str(e), None
@@ -65,285 +64,279 @@ class App:
65
  with gr.Column():
66
  gr.Markdown(MARKDOWN, elem_id="md_project")
67
  with gr.Tabs():
68
- with gr.TabItem("File"): # tab1
69
- with gr.Row():
70
- input_file = gr.Files(type="filepath", label="Upload File here")
71
- with gr.Row():
72
- dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2",
73
- label="Model")
74
- dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs,
75
- value="Automatic Detection", label="Language")
76
- dd_file_format = gr.Dropdown(["SRT", "WebVTT", "txt"], value="SRT", label="File Format")
77
- with gr.Row():
78
- cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
79
- with gr.Row():
80
- cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename", interactive=True)
81
- with gr.Accordion("VAD Options", open=False, visible=isinstance(self.whisper_inf, FasterWhisperInference)):
82
- cb_vad_filter = gr.Checkbox(label="Enable Silero VAD Filter", value=False, interactive=True)
83
- sd_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Speech Threshold", value=0.5)
84
- nb_min_speech_duration_ms = gr.Number(label="Minimum Speech Duration (ms)", precision=0, value=250)
85
- nb_max_speech_duration_s = gr.Number(label="Maximum Speech Duration (s)", value=9999)
86
- nb_min_silence_duration_ms = gr.Number(label="Minimum Silence Duration (ms)", precision=0, value=2000)
87
- nb_window_size_sample = gr.Number(label="Window Size (samples)", precision=0, value=1024)
88
- nb_speech_pad_ms = gr.Number(label="Speech Padding (ms)", precision=0, value=400)
89
- with gr.Accordion("Advanced_Parameters", open=False):
90
- nb_beam_size = gr.Number(label="Beam Size", value=1, precision=0, interactive=True)
91
- nb_log_prob_threshold = gr.Number(label="Log Probability Threshold", value=-1.0, interactive=True)
92
- nb_no_speech_threshold = gr.Number(label="No Speech Threshold", value=0.6, interactive=True)
93
- dd_compute_type = gr.Dropdown(label="Compute Type", choices=self.whisper_inf.available_compute_types, value=self.whisper_inf.current_compute_type, interactive=True)
94
- nb_best_of = gr.Number(label="Best Of", value=5, interactive=True)
95
- nb_patience = gr.Number(label="Patience", value=1, interactive=True)
96
- cb_condition_on_previous_text = gr.Checkbox(label="Condition On Previous Text", value=True, interactive=True)
97
- tb_initial_prompt = gr.Textbox(label="Initial Prompt", value=None, interactive=True)
98
- sd_temperature = gr.Slider(label="Temperature", value=0, step=0.01, maximum=1.0, interactive=True)
99
- nb_compression_ratio_threshold = gr.Number(label="Compression Ratio Threshold", value=2.4, interactive=True)
100
- with gr.Row():
101
- btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
102
- with gr.Row():
103
- tb_indicator = gr.Textbox(label="Output", scale=5)
104
- files_subtitles = gr.Files(label="Downloadable output file", scale=3, interactive=False)
105
- btn_openfolder = gr.Button('πŸ“‚', scale=1)
106
 
107
- params = [input_file, dd_file_format, cb_timestamp]
108
- whisper_params = WhisperGradioComponents(model_size=dd_model,
109
- lang=dd_lang,
110
- is_translate=cb_translate,
111
- beam_size=nb_beam_size,
112
- log_prob_threshold=nb_log_prob_threshold,
113
- no_speech_threshold=nb_no_speech_threshold,
114
- compute_type=dd_compute_type,
115
- best_of=nb_best_of,
116
- patience=nb_patience,
117
- condition_on_previous_text=cb_condition_on_previous_text,
118
- initial_prompt=tb_initial_prompt,
119
- temperature=sd_temperature,
120
- compression_ratio_threshold=nb_compression_ratio_threshold,
121
- vad_filter=cb_vad_filter,
122
- threshold=sd_threshold,
123
- min_speech_duration_ms=nb_min_speech_duration_ms,
124
- max_speech_duration_s=nb_max_speech_duration_s,
125
- min_silence_duration_ms=nb_min_silence_duration_ms,
126
- window_size_sample=nb_window_size_sample,
127
- speech_pad_ms=nb_speech_pad_ms)
128
 
129
- btn_run.click(fn=self.transcribe_file_wrapper,
130
- inputs=params + whisper_params.to_list(),
131
- outputs=[tb_indicator, files_subtitles])
132
- btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
133
- dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
134
 
135
- with gr.TabItem("Youtube"): # tab2
136
- with gr.Row():
137
- tb_youtubelink = gr.Textbox(label="Youtube Link")
138
- with gr.Row(equal_height=True):
139
- with gr.Column():
140
- img_thumbnail = gr.Image(label="Youtube Thumbnail")
141
- with gr.Column():
142
- tb_title = gr.Label(label="Youtube Title")
143
- tb_description = gr.Textbox(label="Youtube Description", max_lines=15)
144
- with gr.Row():
145
- dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2",
146
- label="Model")
147
- dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs,
148
- value="Automatic Detection", label="Language")
149
- dd_file_format = gr.Dropdown(choices=["SRT", "WebVTT", "txt"], value="SRT", label="File Format")
150
- with gr.Row():
151
- cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
152
- with gr.Row():
153
- cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename",
154
- interactive=True)
155
- with gr.Accordion("VAD Options", open=False, visible=isinstance(self.whisper_inf, FasterWhisperInference)):
156
- cb_vad_filter = gr.Checkbox(label="Enable Silero VAD Filter", value=False, interactive=True)
157
- sd_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Speech Threshold", value=0.5)
158
- nb_min_speech_duration_ms = gr.Number(label="Minimum Speech Duration (ms)", precision=0, value=250)
159
- nb_max_speech_duration_s = gr.Number(label="Maximum Speech Duration (s)", value=9999)
160
- nb_min_silence_duration_ms = gr.Number(label="Minimum Silence Duration (ms)", precision=0, value=2000)
161
- nb_window_size_sample = gr.Number(label="Window Size (samples)", precision=0, value=1024)
162
- nb_speech_pad_ms = gr.Number(label="Speech Padding (ms)", precision=0, value=400)
163
- with gr.Accordion("Advanced_Parameters", open=False):
164
- nb_beam_size = gr.Number(label="Beam Size", value=1, precision=0, interactive=True)
165
- nb_log_prob_threshold = gr.Number(label="Log Probability Threshold", value=-1.0, interactive=True)
166
- nb_no_speech_threshold = gr.Number(label="No Speech Threshold", value=0.6, interactive=True)
167
- dd_compute_type = gr.Dropdown(label="Compute Type", choices=self.whisper_inf.available_compute_types, value=self.whisper_inf.current_compute_type, interactive=True)
168
- nb_best_of = gr.Number(label="Best Of", value=5, interactive=True)
169
- nb_patience = gr.Number(label="Patience", value=1, interactive=True)
170
- cb_condition_on_previous_text = gr.Checkbox(label="Condition On Previous Text", value=True, interactive=True)
171
- tb_initial_prompt = gr.Textbox(label="Initial Prompt", value=None, interactive=True)
172
- sd_temperature = gr.Slider(label="Temperature", value=0, step=0.01, maximum=1.0, interactive=True)
173
- nb_compression_ratio_threshold = gr.Number(label="Compression Ratio Threshold", value=2.4, interactive=True)
174
- with gr.Row():
175
- btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
176
- with gr.Row():
177
- tb_indicator = gr.Textbox(label="Output", scale=5)
178
- files_subtitles = gr.Files(label="Downloadable output file", scale=3)
179
- btn_openfolder = gr.Button('πŸ“‚', scale=1)
180
 
181
- params = [tb_youtubelink, dd_file_format, cb_timestamp]
182
- whisper_params = WhisperGradioComponents(model_size=dd_model,
183
- lang=dd_lang,
184
- is_translate=cb_translate,
185
- beam_size=nb_beam_size,
186
- log_prob_threshold=nb_log_prob_threshold,
187
- no_speech_threshold=nb_no_speech_threshold,
188
- compute_type=dd_compute_type,
189
- best_of=nb_best_of,
190
- patience=nb_patience,
191
- condition_on_previous_text=cb_condition_on_previous_text,
192
- initial_prompt=tb_initial_prompt,
193
- temperature=sd_temperature,
194
- compression_ratio_threshold=nb_compression_ratio_threshold,
195
- vad_filter=cb_vad_filter,
196
- threshold=sd_threshold,
197
- min_speech_duration_ms=nb_min_speech_duration_ms,
198
- max_speech_duration_s=nb_max_speech_duration_s,
199
- min_silence_duration_ms=nb_min_silence_duration_ms,
200
- window_size_sample=nb_window_size_sample,
201
- speech_pad_ms=nb_speech_pad_ms)
202
- btn_run.click(fn=self.whisper_inf.transcribe_youtube,
203
- inputs=params + whisper_params.to_list(),
204
- outputs=[tb_indicator, files_subtitles])
205
- tb_youtubelink.change(get_ytmetas, inputs=[tb_youtubelink],
206
- outputs=[img_thumbnail, tb_title, tb_description])
207
- btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
208
- dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
209
 
210
- with gr.TabItem("Mic"): # tab3
211
- with gr.Row():
212
- mic_input = gr.Microphone(label="Record with Mic", type="filepath", interactive=True)
213
- with gr.Row():
214
- dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2",
215
- label="Model")
216
- dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs,
217
- value="Automatic Detection", label="Language")
218
- dd_file_format = gr.Dropdown(["SRT", "WebVTT", "txt"], value="SRT", label="File Format")
219
- with gr.Row():
220
- cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
221
- with gr.Accordion("VAD Options", open=False, visible=isinstance(self.whisper_inf, FasterWhisperInference)):
222
- cb_vad_filter = gr.Checkbox(label="Enable Silero VAD Filter", value=False, interactive=True)
223
- sd_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Speech Threshold", value=0.5)
224
- nb_min_speech_duration_ms = gr.Number(label="Minimum Speech Duration (ms)", precision=0, value=250)
225
- nb_max_speech_duration_s = gr.Number(label="Maximum Speech Duration (s)", value=9999)
226
- nb_min_silence_duration_ms = gr.Number(label="Minimum Silence Duration (ms)", precision=0, value=2000)
227
- nb_window_size_sample = gr.Number(label="Window Size (samples)", precision=0, value=1024)
228
- nb_speech_pad_ms = gr.Number(label="Speech Padding (ms)", precision=0, value=400)
229
- with gr.Accordion("Advanced_Parameters", open=False):
230
- nb_beam_size = gr.Number(label="Beam Size", value=1, precision=0, interactive=True)
231
- nb_log_prob_threshold = gr.Number(label="Log Probability Threshold", value=-1.0, interactive=True)
232
- nb_no_speech_threshold = gr.Number(label="No Speech Threshold", value=0.6, interactive=True)
233
- dd_compute_type = gr.Dropdown(label="Compute Type", choices=self.whisper_inf.available_compute_types, value=self.whisper_inf.current_compute_type, interactive=True)
234
- nb_best_of = gr.Number(label="Best Of", value=5, interactive=True)
235
- nb_patience = gr.Number(label="Patience", value=1, interactive=True)
236
- cb_condition_on_previous_text = gr.Checkbox(label="Condition On Previous Text", value=True, interactive=True)
237
- tb_initial_prompt = gr.Textbox(label="Initial Prompt", value=None, interactive=True)
238
- sd_temperature = gr.Slider(label="Temperature", value=0, step=0.01, maximum=1.0, interactive=True)
239
- with gr.Row():
240
- btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
241
- with gr.Row():
242
- tb_indicator = gr.Textbox(label="Output", scale=5)
243
- files_subtitles = gr.Files(label="Downloadable output file", scale=3)
244
- btn_openfolder = gr.Button('πŸ“‚', scale=1)
245
 
246
- params = [mic_input, dd_file_format]
247
- whisper_params = WhisperGradioComponents(model_size=dd_model,
248
- lang=dd_lang,
249
- is_translate=cb_translate,
250
- beam_size=nb_beam_size,
251
- log_prob_threshold=nb_log_prob_threshold,
252
- no_speech_threshold=nb_no_speech_threshold,
253
- compute_type=dd_compute_type,
254
- best_of=nb_best_of,
255
- patience=nb_patience,
256
- condition_on_previous_text=cb_condition_on_previous_text,
257
- initial_prompt=tb_initial_prompt,
258
- temperature=sd_temperature,
259
- compression_ratio_threshold=nb_compression_ratio_threshold,
260
- vad_filter=cb_vad_filter,
261
- threshold=sd_threshold,
262
- min_speech_duration_ms=nb_min_speech_duration_ms,
263
- max_speech_duration_s=nb_max_speech_duration_s,
264
- min_silence_duration_ms=nb_min_silence_duration_ms,
265
- window_size_sample=nb_window_size_sample,
266
- speech_pad_ms=nb_speech_pad_ms)
267
- btn_run.click(fn=self.whisper_inf.transcribe_mic,
268
- inputs=params + whisper_params.to_list(),
269
- outputs=[tb_indicator, files_subtitles])
270
- btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
271
- dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
- with gr.TabItem("T2T Translation"): # tab 4
274
- with gr.Row():
275
- file_subs = gr.Files(type="filepath", label="Upload Subtitle Files to translate here",
276
- file_types=['.vtt', '.srt'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
 
278
- with gr.TabItem("DeepL API"): # sub tab1
279
- with gr.Row():
280
- tb_authkey = gr.Textbox(label="Your Auth Key (API KEY)",
281
- value="")
282
- with gr.Row():
283
- dd_deepl_sourcelang = gr.Dropdown(label="Source Language", value="Automatic Detection",
284
- choices=list(
285
- self.deepl_api.available_source_langs.keys()))
286
- dd_deepl_targetlang = gr.Dropdown(label="Target Language", value="English",
287
- choices=list(
288
- self.deepl_api.available_target_langs.keys()))
289
- with gr.Row():
290
- cb_deepl_ispro = gr.Checkbox(label="Pro User?", value=False)
291
- with gr.Row():
292
- btn_run = gr.Button("TRANSLATE SUBTITLE FILE", variant="primary")
293
- with gr.Row():
294
- tb_indicator = gr.Textbox(label="Output", scale=5)
295
- files_subtitles = gr.Files(label="Downloadable output file", scale=3)
296
- btn_openfolder = gr.Button('πŸ“‚', scale=1)
297
 
298
- btn_run.click(fn=self.deepl_api.translate_deepl,
299
- inputs=[tb_authkey, file_subs, dd_deepl_sourcelang, dd_deepl_targetlang,
300
- cb_deepl_ispro],
301
- outputs=[tb_indicator, files_subtitles])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
 
303
- btn_openfolder.click(fn=lambda: self.open_folder(os.path.join("outputs", "translations")),
304
- inputs=None,
305
- outputs=None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
307
- with gr.TabItem("NLLB"): # sub tab2
308
- with gr.Row():
309
- dd_nllb_model = gr.Dropdown(label="Model", value="facebook/nllb-200-1.3B",
310
- choices=self.nllb_inf.available_models)
311
- dd_nllb_sourcelang = gr.Dropdown(label="Source Language",
312
- choices=self.nllb_inf.available_source_langs)
313
- dd_nllb_targetlang = gr.Dropdown(label="Target Language",
314
- choices=self.nllb_inf.available_target_langs)
315
- with gr.Row():
316
- cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename", interactive=True)
317
- with gr.Row():
318
- btn_run = gr.Button("TRANSLATE SUBTITLE FILE", variant="primary")
319
- with gr.Row():
320
- tb_indicator = gr.Textbox(label="Output", scale=5)
321
- files_subtitles = gr.Files(label="Downloadable output file", scale=3)
322
- btn_openfolder = gr.Button('πŸ“‚', scale=1)
323
- with gr.Column():
324
- md_vram_table = gr.HTML(NLLB_VRAM_TABLE, elem_id="md_nllb_vram_table")
325
 
326
- btn_run.click(fn=self.nllb_inf.translate_file,
327
- inputs=[file_subs, dd_nllb_model, dd_nllb_sourcelang, dd_nllb_targetlang, cb_timestamp],
328
- outputs=[tb_indicator, files_subtitles])
329
 
330
- btn_openfolder.click(fn=lambda: self.open_folder(os.path.join("outputs", "translations")),
331
- inputs=None,
332
- outputs=None)
 
 
 
 
 
 
 
 
 
 
 
 
333
 
334
- # Launch the app with optional gradio settings
335
- launch_args = {}
336
- if self.args.share:
337
- launch_args['share'] = self.args.share
338
- if self.args.server_name:
339
- launch_args['server_name'] = self.args.server_name
340
- if self.args.server_port:
341
- launch_args['server_port'] = self.args.server_port
342
- if self.args.username and self.args.password:
343
- launch_args['auth'] = (self.args.username, self.args.password)
344
- launch_args['inbrowser'] = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
 
346
- self.app.queue(api_open=False).launch(**launch_args)
 
 
347
 
348
 
349
  # Create the parser for command-line arguments
@@ -356,10 +349,11 @@ parser.add_argument('--username', type=str, default=None, help='Gradio authentic
356
  parser.add_argument('--password', type=str, default=None, help='Gradio authentication password')
357
  parser.add_argument('--theme', type=str, default=None, help='Gradio Blocks theme')
358
  parser.add_argument('--colab', type=bool, default=False, nargs='?', const=True, help='Is colab user or not')
359
- parser.add_argument('--api_open', type=bool, default=False, nargs='?', const=True, help='enable api or not')
360
  parser.add_argument('--whisper_model_dir', type=str, default=os.path.join("models", "Whisper"), help='Directory path of the whisper model')
361
  parser.add_argument('--faster_whisper_model_dir', type=str, default=os.path.join("models", "Whisper", "faster-whisper"), help='Directory path of the faster-whisper model')
362
  _args = parser.parse_args()
363
 
364
  if __name__ == "__main__":
365
- app = App(args=_args)
 
 
16
  self.args = args
17
  self.app = gr.Blocks(css=CSS, theme=self.args.theme)
18
  self.whisper_inf = self.init_whisper()
 
 
19
  self.nllb_inf = NLLBInference()
20
  self.deepl_api = DeepLAPI()
21
+ self.log_initialization()
22
 
23
  def init_whisper(self):
24
  whisper_type = self.args.whisper_type.lower().strip()
 
25
  if whisper_type in ["faster_whisper", "faster-whisper"]:
26
  whisper_inf = FasterWhisperInference()
27
  whisper_inf.model_dir = self.args.faster_whisper_model_dir
 
33
  whisper_inf.model_dir = self.args.faster_whisper_model_dir
34
  return whisper_inf
35
 
36
+ def log_initialization(self):
37
+ print(f'Use "{self.args.whisper_type}" implementation')
38
+ print(f'Device "{self.whisper_inf.device}" is detected')
39
+
40
  @staticmethod
41
  def open_folder(folder_path: str):
42
  if os.path.exists(folder_path):
43
+ os.system(f'start {folder_path}')
44
  else:
45
+ print(f'The folder {folder_path} does not exist.')
46
 
47
  @staticmethod
48
  def on_change_models(model_size: str):
49
  translatable_model = ["large", "large-v1", "large-v2", "large-v3"]
50
+ return gr.Checkbox(visible=model_size in translatable_model, value=False, label="Translate to English?", interactive=True)
 
 
 
51
 
52
  def transcribe_file_wrapper(self, file, file_format, timestamp, *whisper_params):
53
  try:
54
  result, output_file = self.whisper_inf.transcribe_file(file, file_format, timestamp, *whisper_params)
55
  if not os.path.exists(output_file):
56
+ raise FileNotFoundError(f'Output file {output_file} does not exist.')
57
  return result, output_file
58
  except Exception as e:
59
  return str(e), None
 
64
  with gr.Column():
65
  gr.Markdown(MARKDOWN, elem_id="md_project")
66
  with gr.Tabs():
67
+ with gr.TabItem("File"):
68
+ self.build_file_tab()
69
+ with gr.TabItem("Youtube"):
70
+ self.build_youtube_tab()
71
+ with gr.TabItem("Mic"):
72
+ self.build_mic_tab()
73
+ with gr.TabItem("T2T Translation"):
74
+ self.build_t2t_translation_tab()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ # Launch the app with optional Gradio settings
77
+ launch_args = {}
78
+ if self.args.share:
79
+ launch_args['share'] = self.args.share
80
+ if self.args.server_name:
81
+ launch_args['server_name'] = self.args.server_name
82
+ if self.args.server_port:
83
+ launch_args['server_port'] = self.args.server_port
84
+ if self.args.username and self.args.password:
85
+ launch_args['auth'] = (self.args.username, self.args.password)
86
+ launch_args['inbrowser'] = True
 
 
 
 
 
 
 
 
 
 
87
 
88
+ self.app.queue(api_open=self.args.api_open).launch(**launch_args)
 
 
 
 
89
 
90
+ def build_file_tab(self):
91
+ with gr.Row():
92
+ input_file = gr.Files(type="filepath", label="Upload File here")
93
+ with gr.Row():
94
+ dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2", label="Model")
95
+ dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs, value="Automatic Detection", label="Language")
96
+ dd_file_format = gr.Dropdown(choices=["SRT", "WebVTT", "txt"], value="SRT", label="File Format")
97
+ with gr.Row():
98
+ cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
99
+ with gr.Row():
100
+ cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename", interactive=True)
101
+ with gr.Accordion("VAD Options", open=False, visible=isinstance(self.whisper_inf, FasterWhisperInference)):
102
+ cb_vad_filter = gr.Checkbox(label="Enable Silero VAD Filter", value=False, interactive=True)
103
+ sd_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Speech Threshold", value=0.5)
104
+ nb_min_speech_duration_ms = gr.Number(label="Minimum Speech Duration (ms)", precision=0, value=250)
105
+ nb_max_speech_duration_s = gr.Number(label="Maximum Speech Duration (s)", value=9999)
106
+ nb_min_silence_duration_ms = gr.Number(label="Minimum Silence Duration (ms)", precision=0, value=2000)
107
+ nb_window_size_sample = gr.Number(label="Window Size (samples)", precision=0, value=1024)
108
+ nb_speech_pad_ms = gr.Number(label="Speech Padding (ms)", precision=0, value=400)
109
+ with gr.Accordion("Advanced_Parameters", open=False):
110
+ nb_beam_size = gr.Number(label="Beam Size", value=1, precision=0, interactive=True)
111
+ nb_log_prob_threshold = gr.Number(label="Log Probability Threshold", value=-1.0, interactive=True)
112
+ nb_no_speech_threshold = gr.Number(label="No Speech Threshold", value=0.6, interactive=True)
113
+ dd_compute_type = gr.Dropdown(choices=self.whisper_inf.available_compute_types, value=self.whisper_inf.current_compute_type, label="Compute Type", interactive=True)
114
+ nb_best_of = gr.Number(label="Best Of", value=5, interactive=True)
115
+ nb_patience = gr.Number(label="Patience", value=1, interactive=True)
116
+ cb_condition_on_previous_text = gr.Checkbox(label="Condition On Previous Text", value=True, interactive=True)
117
+ tb_initial_prompt = gr.Textbox(label="Initial Prompt", value=None, interactive=True)
118
+ sd_temperature = gr.Slider(label="Temperature", value=0, step=0.01, maximum=1.0, interactive=True)
119
+ nb_compression_ratio_threshold = gr.Number(label="Compression Ratio Threshold", value=2.4, interactive=True)
120
+ with gr.Row():
121
+ btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
122
+ with gr.Row():
123
+ tb_indicator = gr.Textbox(label="Output", scale=5)
124
+ files_subtitles = gr.Files(label="Downloadable output file", scale=3, interactive=False)
125
+ btn_openfolder = gr.Button('πŸ“‚', scale=1)
 
 
 
 
 
 
 
 
 
126
 
127
+ params = [input_file, dd_file_format, cb_timestamp]
128
+ whisper_params = WhisperGradioComponents(model_size=dd_model,
129
+ lang=dd_lang,
130
+ is_translate=cb_translate,
131
+ beam_size=nb_beam_size,
132
+ log_prob_threshold=nb_log_prob_threshold,
133
+ no_speech_threshold=nb_no_speech_threshold,
134
+ compute_type=dd_compute_type,
135
+ best_of=nb_best_of,
136
+ patience=nb_patience,
137
+ condition_on_previous_text=cb_condition_on_previous_text,
138
+ initial_prompt=tb_initial_prompt,
139
+ temperature=sd_temperature,
140
+ compression_ratio_threshold=nb_compression_ratio_threshold,
141
+ vad_filter=cb_vad_filter,
142
+ threshold=sd_threshold,
143
+ min_speech_duration_ms=nb_min_speech_duration_ms,
144
+ max_speech_duration_s=nb_max_speech_duration_s,
145
+ min_silence_duration_ms=nb_min_silence_duration_ms,
146
+ window_size_sample=nb_window_size_sample,
147
+ speech_pad_ms=nb_speech_pad_ms)
 
 
 
 
 
 
 
148
 
149
+ btn_run.click(fn=self.transcribe_file_wrapper,
150
+ inputs=params + whisper_params.to_list(),
151
+ outputs=[tb_indicator, files_subtitles])
152
+ btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
153
+ dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
+ def build_youtube_tab(self):
156
+ with gr.Row():
157
+ tb_youtubelink = gr.Textbox(label="Youtube Link")
158
+ with gr.Row(equal_height=True):
159
+ with gr.Column():
160
+ img_thumbnail = gr.Image(label="Youtube Thumbnail")
161
+ with gr.Column():
162
+ tb_title = gr.Label(label="Youtube Title")
163
+ tb_description = gr.Textbox(label="Youtube Description", max_lines=15)
164
+ with gr.Row():
165
+ dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2", label="Model")
166
+ dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs, value="Automatic Detection", label="Language")
167
+ dd_file_format = gr.Dropdown(choices=["SRT", "WebVTT", "txt"], value="SRT", label="File Format")
168
+ with gr.Row():
169
+ cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
170
+ with gr.Row():
171
+ cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename", interactive=True)
172
+ with gr.Accordion("VAD Options", open=False, visible=isinstance(self.whisper_inf, FasterWhisperInference)):
173
+ cb_vad_filter = gr.Checkbox(label="Enable Silero VAD Filter", value=False, interactive=True)
174
+ sd_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Speech Threshold", value=0.5)
175
+ nb_min_speech_duration_ms = gr.Number(label="Minimum Speech Duration (ms)", precision=0, value=250)
176
+ nb_max_speech_duration_s = gr.Number(label="Maximum Speech Duration (s)", value=9999)
177
+ nb_min_silence_duration_ms = gr.Number(label="Minimum Silence Duration (ms)", precision=0, value=2000)
178
+ nb_window_size_sample = gr.Number(label="Window Size (samples)", precision=0, value=1024)
179
+ nb_speech_pad_ms = gr.Number(label="Speech Padding (ms)", precision=0, value=400)
180
+ with gr.Accordion("Advanced_Parameters", open=False):
181
+ nb_beam_size = gr.Number(label="Beam Size", value=1, precision=0, interactive=True)
182
+ nb_log_prob_threshold = gr.Number(label="Log Probability Threshold", value=-1.0, interactive=True)
183
+ nb_no_speech_threshold = gr.Number(label="No Speech Threshold", value=0.6, interactive=True)
184
+ dd_compute_type = gr.Dropdown(choices=self.whisper_inf.available_compute_types, value=self.whisper_inf.current_compute_type, label="Compute Type", interactive=True)
185
+ nb_best_of = gr.Number(label="Best Of", value=5, interactive=True)
186
+ nb_patience = gr.Number(label="Patience", value=1, interactive=True)
187
+ cb_condition_on_previous_text = gr.Checkbox(label="Condition On Previous Text", value=True, interactive=True)
188
+ tb_initial_prompt = gr.Textbox(label="Initial Prompt", value=None, interactive=True)
189
+ sd_temperature = gr.Slider(label="Temperature", value=0, step=0.01, maximum=1.0, interactive=True)
190
+ nb_compression_ratio_threshold = gr.Number(label="Compression Ratio Threshold", value=2.4, interactive=True)
191
+ with gr.Row():
192
+ btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
193
+ with gr.Row():
194
+ tb_indicator = gr.Textbox(label="Output", scale=5)
195
+ files_subtitles = gr.Files(label="Downloadable output file", scale=3)
196
+ btn_openfolder = gr.Button('πŸ“‚', scale=1)
197
 
198
+ params = [tb_youtubelink, dd_file_format, cb_timestamp]
199
+ whisper_params = WhisperGradioComponents(model_size=dd_model,
200
+ lang=dd_lang,
201
+ is_translate=cb_translate,
202
+ beam_size=nb_beam_size,
203
+ log_prob_threshold=nb_log_prob_threshold,
204
+ no_speech_threshold=nb_no_speech_threshold,
205
+ compute_type=dd_compute_type,
206
+ best_of=nb_best_of,
207
+ patience=nb_patience,
208
+ condition_on_previous_text=cb_condition_on_previous_text,
209
+ initial_prompt=tb_initial_prompt,
210
+ temperature=sd_temperature,
211
+ compression_ratio_threshold=nb_compression_ratio_threshold,
212
+ vad_filter=cb_vad_filter,
213
+ threshold=sd_threshold,
214
+ min_speech_duration_ms=nb_min_speech_duration_ms,
215
+ max_speech_duration_s=nb_max_speech_duration_s,
216
+ min_silence_duration_ms=nb_min_silence_duration_ms,
217
+ window_size_sample=nb_window_size_sample,
218
+ speech_pad_ms=nb_speech_pad_ms)
219
 
220
+ btn_run.click(fn=self.whisper_inf.transcribe_youtube,
221
+ inputs=params + whisper_params.to_list(),
222
+ outputs=[tb_indicator, files_subtitles])
223
+ tb_youtubelink.change(get_ytmetas, inputs=[tb_youtubelink], outputs=[img_thumbnail, tb_title, tb_description])
224
+ btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
225
+ dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
+ def build_mic_tab(self):
228
+ with gr.Row():
229
+ mic_input = gr.Microphone(label="Record with Mic", type="filepath", interactive=True)
230
+ with gr.Row():
231
+ dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2", label="Model")
232
+ dd_lang = gr.Dropdown(choices=["Automatic Detection"] + self.whisper_inf.available_langs, value="Automatic Detection", label="Language")
233
+ dd_file_format = gr.Dropdown(choices=["SRT", "WebVTT", "txt"], value="SRT", label="File Format")
234
+ with gr.Row():
235
+ cb_translate = gr.Checkbox(value=False, label="Translate to English?", interactive=True)
236
+ with gr.Accordion("VAD Options", open=False, visible=isinstance(self.whisper_inf, FasterWhisperInference)):
237
+ cb_vad_filter = gr.Checkbox(label="Enable Silero VAD Filter", value=False, interactive=True)
238
+ sd_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Speech Threshold", value=0.5)
239
+ nb_min_speech_duration_ms = gr.Number(label="Minimum Speech Duration (ms)", precision=0, value=250)
240
+ nb_max_speech_duration_s = gr.Number(label="Maximum Speech Duration (s)", value=9999)
241
+ nb_min_silence_duration_ms = gr.Number(label="Minimum Silence Duration (ms)", precision=0, value=2000)
242
+ nb_window_size_sample = gr.Number(label="Window Size (samples)", precision=0, value=1024)
243
+ nb_speech_pad_ms = gr.Number(label="Speech Padding (ms)", precision=0, value=400)
244
+ with gr.Accordion("Advanced_Parameters", open=False):
245
+ nb_beam_size = gr.Number(label="Beam Size", value=1, precision=0, interactive=True)
246
+ nb_log_prob_threshold = gr.Number(label="Log Probability Threshold", value=-1.0, interactive=True)
247
+ nb_no_speech_threshold = gr.Number(label="No Speech Threshold", value=0.6, interactive=True)
248
+ dd_compute_type = gr.Dropdown(choices=self.whisper_inf.available_compute_types, value=self.whisper_inf.current_compute_type, label="Compute Type", interactive=True)
249
+ nb_best_of = gr.Number(label="Best Of", value=5, interactive=True)
250
+ nb_patience = gr.Number(label="Patience", value=1, interactive=True)
251
+ cb_condition_on_previous_text = gr.Checkbox(label="Condition On Previous Text", value=True, interactive=True)
252
+ tb_initial_prompt = gr.Textbox(label="Initial Prompt", value=None, interactive=True)
253
+ sd_temperature = gr.Slider(label="Temperature", value=0, step=0.01, maximum=1.0, interactive=True)
254
+ with gr.Row():
255
+ btn_run = gr.Button("GENERATE SUBTITLE FILE", variant="primary")
256
+ with gr.Row():
257
+ tb_indicator = gr.Textbox(label="Output", scale=5)
258
+ files_subtitles = gr.Files(label="Downloadable output file", scale=3)
259
+ btn_openfolder = gr.Button('πŸ“‚', scale=1)
260
 
261
+ params = [mic_input, dd_file_format]
262
+ whisper_params = WhisperGradioComponents(model_size=dd_model,
263
+ lang=dd_lang,
264
+ is_translate=cb_translate,
265
+ beam_size=nb_beam_size,
266
+ log_prob_threshold=nb_log_prob_threshold,
267
+ no_speech_threshold=nb_no_speech_threshold,
268
+ compute_type=dd_compute_type,
269
+ best_of=nb_best_of,
270
+ patience=nb_patience,
271
+ condition_on_previous_text=cb_condition_on_previous_text,
272
+ initial_prompt=tb_initial_prompt,
273
+ temperature=sd_temperature,
274
+ compression_ratio_threshold=nb_compression_ratio_threshold,
275
+ vad_filter=cb_vad_filter,
276
+ threshold=sd_threshold,
277
+ min_speech_duration_ms=nb_min_speech_duration_ms,
278
+ max_speech_duration_s=nb_max_speech_duration_s,
279
+ min_silence_duration_ms=nb_min_silence_duration_ms,
280
+ window_size_sample=nb_window_size_sample,
281
+ speech_pad_ms=nb_speech_pad_ms)
282
 
283
+ btn_run.click(fn=self.whisper_inf.transcribe_mic,
284
+ inputs=params + whisper_params.to_list(),
285
+ outputs=[tb_indicator, files_subtitles])
286
+ btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
287
+ dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
 
 
 
 
 
 
 
 
 
 
 
 
 
288
 
289
+ def build_t2t_translation_tab(self):
290
+ with gr.Row():
291
+ file_subs = gr.Files(type="filepath", label="Upload Subtitle Files to translate here", file_types=['.vtt', '.srt'])
292
 
293
+ with gr.TabItem("DeepL API"):
294
+ with gr.Row():
295
+ tb_authkey = gr.Textbox(label="Your Auth Key (API KEY)")
296
+ with gr.Row():
297
+ dd_deepl_sourcelang = gr.Dropdown(label="Source Language", value="Automatic Detection", choices=list(self.deepl_api.available_source_langs.keys()))
298
+ dd_deepl_targetlang = gr.Dropdown(label="Target Language", value="English", choices=list(self.deepl_api.available_target_langs.keys()))
299
+ with gr.Row():
300
+ cb_deepl_ispro = gr.Checkbox(label="Pro User?", value=False)
301
+ with gr.Row():
302
+ btn_run = gr.Button("TRANSLATE SUBTITLE FILE", variant="primary")
303
+ with gr.Row():
304
+ tb_indicator = gr.Textbox(label
305
+ ="Output", scale=5)
306
+ files_subtitles = gr.Files(label="Downloadable output file", scale=3)
307
+ btn_openfolder = gr.Button('πŸ“‚', scale=1)
308
 
309
+ btn_run.click(fn=self.deepl_api.translate_deepl,
310
+ inputs=[tb_authkey, file_subs, dd_deepl_sourcelang, dd_deepl_targetlang, cb_deepl_ispro],
311
+ outputs=[tb_indicator, files_subtitles])
312
+
313
+ btn_openfolder.click(fn=lambda: self.open_folder(os.path.join("outputs", "translations")),
314
+ inputs=None,
315
+ outputs=None)
316
+
317
+ with gr.TabItem("NLLB"):
318
+ with gr.Row():
319
+ dd_nllb_model = gr.Dropdown(label="Model", value="facebook/nllb-200-1.3B", choices=self.nllb_inf.available_models)
320
+ dd_nllb_sourcelang = gr.Dropdown(label="Source Language", choices=self.nllb_inf.available_source_langs)
321
+ dd_nllb_targetlang = gr.Dropdown(label="Target Language", choices=self.nllb_inf.available_target_langs)
322
+ with gr.Row():
323
+ cb_timestamp = gr.Checkbox(value=True, label="Add a timestamp to the end of the filename", interactive=True)
324
+ with gr.Row():
325
+ btn_run = gr.Button("TRANSLATE SUBTITLE FILE", variant="primary")
326
+ with gr.Row():
327
+ tb_indicator = gr.Textbox(label="Output", scale=5)
328
+ files_subtitles = gr.Files(label="Downloadable output file", scale=3)
329
+ btn_openfolder = gr.Button('πŸ“‚', scale=1)
330
+ with gr.Column():
331
+ md_vram_table = gr.HTML(NLLB_VRAM_TABLE, elem_id="md_nllb_vram_table")
332
+
333
+ btn_run.click(fn=self.nllb_inf.translate_file,
334
+ inputs=[file_subs, dd_nllb_model, dd_nllb_sourcelang, dd_nllb_targetlang, cb_timestamp],
335
+ outputs=[tb_indicator, files_subtitles])
336
 
337
+ btn_openfolder.click(fn=lambda: self.open_folder(os.path.join("outputs", "translations")),
338
+ inputs=None,
339
+ outputs=None)
340
 
341
 
342
  # Create the parser for command-line arguments
 
349
  parser.add_argument('--password', type=str, default=None, help='Gradio authentication password')
350
  parser.add_argument('--theme', type=str, default=None, help='Gradio Blocks theme')
351
  parser.add_argument('--colab', type=bool, default=False, nargs='?', const=True, help='Is colab user or not')
352
+ parser.add_argument('--api_open', type=bool, default=False, nargs='?', const=True, help='enable API or not')
353
  parser.add_argument('--whisper_model_dir', type=str, default=os.path.join("models", "Whisper"), help='Directory path of the whisper model')
354
  parser.add_argument('--faster_whisper_model_dir', type=str, default=os.path.join("models", "Whisper", "faster-whisper"), help='Directory path of the faster-whisper model')
355
  _args = parser.parse_args()
356
 
357
  if __name__ == "__main__":
358
+ app = App(args=_args)
359
+ app.launch()