jhj0517 commited on
Commit
806824b
·
1 Parent(s): e667af9

Refactor to gradio functions

Browse files
Files changed (1) hide show
  1. app.py +10 -145
app.py CHANGED
@@ -66,158 +66,23 @@ class App:
66
  interactive=True)
67
 
68
  with gr.Accordion(_("Advanced Parameters"), open=False):
69
- nb_beam_size = gr.Number(label="Beam Size", value=whisper_params["beam_size"], precision=0,
70
- interactive=True,
71
- info="Beam size to use for decoding.")
72
- nb_log_prob_threshold = gr.Number(label="Log Probability Threshold",
73
- value=whisper_params["log_prob_threshold"], interactive=True,
74
- info="If the average log probability over sampled tokens is below this value, treat as failed.")
75
- nb_no_speech_threshold = gr.Number(label="No Speech Threshold", value=whisper_params["no_speech_threshold"],
76
- interactive=True,
77
- info="If the no speech probability is higher than this value AND the average log probability over sampled tokens is below 'Log Prob Threshold', consider the segment as silent.")
78
- dd_compute_type = gr.Dropdown(label="Compute Type", choices=self.whisper_inf.available_compute_types,
79
- value=self.whisper_inf.current_compute_type, interactive=True,
80
- allow_custom_value=True,
81
- info="Select the type of computation to perform.")
82
- nb_best_of = gr.Number(label="Best Of", value=whisper_params["best_of"], interactive=True,
83
- info="Number of candidates when sampling with non-zero temperature.")
84
- nb_patience = gr.Number(label="Patience", value=whisper_params["patience"], interactive=True,
85
- info="Beam search patience factor.")
86
- cb_condition_on_previous_text = gr.Checkbox(label="Condition On Previous Text",
87
- value=whisper_params["condition_on_previous_text"],
88
- interactive=True,
89
- info="Condition on previous text during decoding.")
90
- sld_prompt_reset_on_temperature = gr.Slider(label="Prompt Reset On Temperature",
91
- value=whisper_params["prompt_reset_on_temperature"],
92
- minimum=0, maximum=1, step=0.01, interactive=True,
93
- info="Resets prompt if temperature is above this value."
94
- " Arg has effect only if 'Condition On Previous Text' is True.")
95
- tb_initial_prompt = gr.Textbox(label="Initial Prompt", value=None, interactive=True,
96
- info="Initial prompt to use for decoding.")
97
- sd_temperature = gr.Slider(label="Temperature", value=whisper_params["temperature"], minimum=0.0,
98
- step=0.01, maximum=1.0, interactive=True,
99
- info="Temperature for sampling. It can be a tuple of temperatures, which will be successively used upon failures according to either `Compression Ratio Threshold` or `Log Prob Threshold`.")
100
- nb_compression_ratio_threshold = gr.Number(label="Compression Ratio Threshold",
101
- value=whisper_params["compression_ratio_threshold"],
102
- interactive=True,
103
- info="If the gzip compression ratio is above this value, treat as failed.")
104
- nb_chunk_length = gr.Number(label="Chunk Length (s)", value=lambda: whisper_params["chunk_length"],
105
- precision=0,
106
- info="The length of audio segments. If it is not None, it will overwrite the default chunk_length of the FeatureExtractor.")
107
- with gr.Group(visible=isinstance(self.whisper_inf, FasterWhisperInference)):
108
- nb_length_penalty = gr.Number(label="Length Penalty", value=whisper_params["length_penalty"],
109
- info="Exponential length penalty constant.")
110
- nb_repetition_penalty = gr.Number(label="Repetition Penalty",
111
- value=whisper_params["repetition_penalty"],
112
- info="Penalty applied to the score of previously generated tokens (set > 1 to penalize).")
113
- nb_no_repeat_ngram_size = gr.Number(label="No Repeat N-gram Size",
114
- value=whisper_params["no_repeat_ngram_size"],
115
- precision=0,
116
- info="Prevent repetitions of n-grams with this size (set 0 to disable).")
117
- tb_prefix = gr.Textbox(label="Prefix", value=lambda: whisper_params["prefix"],
118
- info="Optional text to provide as a prefix for the first window.")
119
- cb_suppress_blank = gr.Checkbox(label="Suppress Blank", value=whisper_params["suppress_blank"],
120
- info="Suppress blank outputs at the beginning of the sampling.")
121
- tb_suppress_tokens = gr.Textbox(label="Suppress Tokens", value=whisper_params["suppress_tokens"],
122
- info="List of token IDs to suppress. -1 will suppress a default set of symbols as defined in the model config.json file.")
123
- nb_max_initial_timestamp = gr.Number(label="Max Initial Timestamp",
124
- value=whisper_params["max_initial_timestamp"],
125
- info="The initial timestamp cannot be later than this.")
126
- cb_word_timestamps = gr.Checkbox(label="Word Timestamps", value=whisper_params["word_timestamps"],
127
- info="Extract word-level timestamps using the cross-attention pattern and dynamic time warping, and include the timestamps for each word in each segment.")
128
- tb_prepend_punctuations = gr.Textbox(label="Prepend Punctuations",
129
- value=whisper_params["prepend_punctuations"],
130
- info="If 'Word Timestamps' is True, merge these punctuation symbols with the next word.")
131
- tb_append_punctuations = gr.Textbox(label="Append Punctuations",
132
- value=whisper_params["append_punctuations"],
133
- info="If 'Word Timestamps' is True, merge these punctuation symbols with the previous word.")
134
- nb_max_new_tokens = gr.Number(label="Max New Tokens", value=lambda: whisper_params["max_new_tokens"],
135
- precision=0,
136
- info="Maximum number of new tokens to generate per-chunk. If not set, the maximum will be set by the default max_length.")
137
- nb_hallucination_silence_threshold = gr.Number(label="Hallucination Silence Threshold (sec)",
138
- value=lambda: whisper_params[
139
- "hallucination_silence_threshold"],
140
- info="When 'Word Timestamps' is True, skip silent periods longer than this threshold (in seconds) when a possible hallucination is detected.")
141
- tb_hotwords = gr.Textbox(label="Hotwords", value=lambda: whisper_params["hotwords"],
142
- info="Hotwords/hint phrases to provide the model with. Has no effect if prefix is not None.")
143
- nb_language_detection_threshold = gr.Number(label="Language Detection Threshold",
144
- value=lambda: whisper_params[
145
- "language_detection_threshold"],
146
- info="If the maximum probability of the language tokens is higher than this value, the language is detected.")
147
- nb_language_detection_segments = gr.Number(label="Language Detection Segments",
148
- value=lambda: whisper_params["language_detection_segments"],
149
- precision=0,
150
- info="Number of segments to consider for the language detection.")
151
- with gr.Group(visible=isinstance(self.whisper_inf, InsanelyFastWhisperInference)):
152
- nb_batch_size = gr.Number(label="Batch Size", value=whisper_params["batch_size"], precision=0)
153
 
154
  with gr.Accordion(_("Background Music Remover Filter"), open=False):
155
- cb_bgm_separation = gr.Checkbox(label=_("Enable Background Music Remover Filter"),
156
- value=uvr_params["is_separate_bgm"],
157
- interactive=True,
158
- info=_("Enabling this will remove background music"))
159
- dd_uvr_device = gr.Dropdown(label=_("Device"), value=self.whisper_inf.music_separator.device,
160
- choices=self.whisper_inf.music_separator.available_devices)
161
- dd_uvr_model_size = gr.Dropdown(label=_("Model"), value=uvr_params["model_size"],
162
- choices=self.whisper_inf.music_separator.available_models)
163
- nb_uvr_segment_size = gr.Number(label="Segment Size", value=uvr_params["segment_size"], precision=0)
164
- cb_uvr_save_file = gr.Checkbox(label=_("Save separated files to output"), value=uvr_params["save_file"])
165
- cb_uvr_enable_offload = gr.Checkbox(label=_("Offload sub model after removing background music"),
166
- value=uvr_params["enable_offload"])
167
 
168
  with gr.Accordion(_("Voice Detection Filter"), open=False):
169
- cb_vad_filter = gr.Checkbox(label=_("Enable Silero VAD Filter"), value=vad_params["vad_filter"],
170
- interactive=True,
171
- info=_("Enable this to transcribe only detected voice"))
172
- sd_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Speech Threshold",
173
- value=vad_params["threshold"],
174
- info="Lower it to be more sensitive to small sounds.")
175
- nb_min_speech_duration_ms = gr.Number(label="Minimum Speech Duration (ms)", precision=0,
176
- value=vad_params["min_speech_duration_ms"],
177
- info="Final speech chunks shorter than this time are thrown out")
178
- nb_max_speech_duration_s = gr.Number(label="Maximum Speech Duration (s)",
179
- value=vad_params["max_speech_duration_s"],
180
- info="Maximum duration of speech chunks in \"seconds\".")
181
- nb_min_silence_duration_ms = gr.Number(label="Minimum Silence Duration (ms)", precision=0,
182
- value=vad_params["min_silence_duration_ms"],
183
- info="In the end of each speech chunk wait for this time"
184
- " before separating it")
185
- nb_speech_pad_ms = gr.Number(label="Speech Padding (ms)", precision=0, value=vad_params["speech_pad_ms"],
186
- info="Final speech chunks are padded by this time each side")
187
 
188
  with gr.Accordion(_("Diarization"), open=False):
189
- cb_diarize = gr.Checkbox(label=_("Enable Diarization"), value=diarization_params["is_diarize"])
190
- tb_hf_token = gr.Text(label=_("HuggingFace Token"), value=diarization_params["hf_token"],
191
- info=_("This is only needed the first time you download the model"))
192
- dd_diarization_device = gr.Dropdown(label=_("Device"),
193
- choices=self.whisper_inf.diarizer.get_available_device(),
194
- value=self.whisper_inf.diarizer.get_device())
195
 
196
  dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
197
 
 
 
198
  return (
199
- TranscriptionPipelineGradioComponents(
200
- model_size=dd_model, lang=dd_lang, is_translate=cb_translate, beam_size=nb_beam_size,
201
- log_prob_threshold=nb_log_prob_threshold, no_speech_threshold=nb_no_speech_threshold,
202
- compute_type=dd_compute_type, best_of=nb_best_of, patience=nb_patience,
203
- condition_on_previous_text=cb_condition_on_previous_text, initial_prompt=tb_initial_prompt,
204
- temperature=sd_temperature, compression_ratio_threshold=nb_compression_ratio_threshold,
205
- vad_filter=cb_vad_filter, threshold=sd_threshold, min_speech_duration_ms=nb_min_speech_duration_ms,
206
- max_speech_duration_s=nb_max_speech_duration_s, min_silence_duration_ms=nb_min_silence_duration_ms,
207
- speech_pad_ms=nb_speech_pad_ms, chunk_length=nb_chunk_length, batch_size=nb_batch_size,
208
- is_diarize=cb_diarize, hf_token=tb_hf_token, diarization_device=dd_diarization_device,
209
- length_penalty=nb_length_penalty, repetition_penalty=nb_repetition_penalty,
210
- no_repeat_ngram_size=nb_no_repeat_ngram_size, prefix=tb_prefix, suppress_blank=cb_suppress_blank,
211
- suppress_tokens=tb_suppress_tokens, max_initial_timestamp=nb_max_initial_timestamp,
212
- word_timestamps=cb_word_timestamps, prepend_punctuations=tb_prepend_punctuations,
213
- append_punctuations=tb_append_punctuations, max_new_tokens=nb_max_new_tokens,
214
- hallucination_silence_threshold=nb_hallucination_silence_threshold, hotwords=tb_hotwords,
215
- language_detection_threshold=nb_language_detection_threshold,
216
- language_detection_segments=nb_language_detection_segments,
217
- prompt_reset_on_temperature=sld_prompt_reset_on_temperature, is_bgm_separate=cb_bgm_separation,
218
- uvr_device=dd_uvr_device, uvr_model_size=dd_uvr_model_size, uvr_segment_size=nb_uvr_segment_size,
219
- uvr_save_file=cb_uvr_save_file, uvr_enable_offload=cb_uvr_enable_offload
220
- ),
221
  dd_file_format,
222
  cb_timestamp
223
  )
@@ -254,7 +119,7 @@ class App:
254
 
255
  params = [input_file, tb_input_folder, dd_file_format, cb_timestamp]
256
  btn_run.click(fn=self.whisper_inf.transcribe_file,
257
- inputs=params + whisper_params.as_list(),
258
  outputs=[tb_indicator, files_subtitles])
259
  btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
260
 
@@ -280,7 +145,7 @@ class App:
280
  params = [tb_youtubelink, dd_file_format, cb_timestamp]
281
 
282
  btn_run.click(fn=self.whisper_inf.transcribe_youtube,
283
- inputs=params + whisper_params.as_list(),
284
  outputs=[tb_indicator, files_subtitles])
285
  tb_youtubelink.change(get_ytmetas, inputs=[tb_youtubelink],
286
  outputs=[img_thumbnail, tb_title, tb_description])
@@ -302,7 +167,7 @@ class App:
302
  params = [mic_input, dd_file_format, cb_timestamp]
303
 
304
  btn_run.click(fn=self.whisper_inf.transcribe_mic,
305
- inputs=params + whisper_params.as_list(),
306
  outputs=[tb_indicator, files_subtitles])
307
  btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
308
 
 
66
  interactive=True)
67
 
68
  with gr.Accordion(_("Advanced Parameters"), open=False):
69
+ whisper_inputs = WhisperParams.to_gradio_inputs(defaults=whisper_params, only_advanced=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  with gr.Accordion(_("Background Music Remover Filter"), open=False):
72
+ uvr_inputs = BGMSeparationParams.to_gradio_input(defaults=uvr_params)
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  with gr.Accordion(_("Voice Detection Filter"), open=False):
75
+ vad_inputs = VadParams.to_gradio_inputs(defaults=vad_params)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  with gr.Accordion(_("Diarization"), open=False):
78
+ diarization_inputs = DiarizationParams.to_gradio_inputs(defaults=diarization_params)
 
 
 
 
 
79
 
80
  dd_model.change(fn=self.on_change_models, inputs=[dd_model], outputs=[cb_translate])
81
 
82
+ inputs = [dd_model, dd_lang, cb_translate] + whisper_inputs + vad_inputs + diarization_inputs + uvr_inputs
83
+
84
  return (
85
+ inputs,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  dd_file_format,
87
  cb_timestamp
88
  )
 
119
 
120
  params = [input_file, tb_input_folder, dd_file_format, cb_timestamp]
121
  btn_run.click(fn=self.whisper_inf.transcribe_file,
122
+ inputs=params + whisper_params,
123
  outputs=[tb_indicator, files_subtitles])
124
  btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
125
 
 
145
  params = [tb_youtubelink, dd_file_format, cb_timestamp]
146
 
147
  btn_run.click(fn=self.whisper_inf.transcribe_youtube,
148
+ inputs=params + whisper_params,
149
  outputs=[tb_indicator, files_subtitles])
150
  tb_youtubelink.change(get_ytmetas, inputs=[tb_youtubelink],
151
  outputs=[img_thumbnail, tb_title, tb_description])
 
167
  params = [mic_input, dd_file_format, cb_timestamp]
168
 
169
  btn_run.click(fn=self.whisper_inf.transcribe_mic,
170
+ inputs=params + whisper_params,
171
  outputs=[tb_indicator, files_subtitles])
172
  btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)
173