Skip to content

Commit

Permalink
move input files into temporary directory to avoid issues with Gradio…
Browse files Browse the repository at this point in the history
… deleting the cached files before everything is finished
  • Loading branch information
niwa2 committed Jan 26, 2025
1 parent cae0267 commit cc4d389
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def launch(self):
params = params + pipeline_params
btn_run.click(fn=self.whisper_inf.transcribe_file,
inputs=params,
outputs=[tb_indicator, files_subtitles])
outputs=[tb_indicator, files_subtitles]).then(
fn=lambda value: gr.update(value=None), inputs=input_file, outputs=input_file)
btn_openfolder.click(fn=lambda: self.open_folder("outputs"), inputs=None, outputs=None)

with gr.TabItem(_("Youtube")): # tab2
Expand Down
58 changes: 35 additions & 23 deletions modules/whisper/base_transcription_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import gc
from copy import deepcopy
import time
import shutil
import tempfile

from modules.uvr.music_separator import MusicSeparator
from modules.utils.paths import (WHISPER_MODELS_DIR, DIARIZATION_MODELS_DIR, OUTPUT_DIR, DEFAULT_PARAMETERS_CONFIG_PATH,
Expand Down Expand Up @@ -258,37 +260,47 @@ def transcribe_file(self,
if files and isinstance(files[0], gr.utils.NamedString):
files = [file.name for file in files]


progress(0, desc="getting input files")
files_info = {}
for file in files:
transcribed_segments, time_for_task = self.run(
file,
progress,
file_format,
add_timestamp,
*pipeline_params,
)

file_name, file_ext = os.path.splitext(os.path.basename(file))
if save_same_dir and input_folder_path:
output_dir = os.path.dirname(file)
with tempfile.TemporaryDirectory(dir=".", prefix="_tmp_input") as tmp_folder:
tmp_files = []
for f in files:
if "PYTEST_CURRENT_TEST" in os.environ:
# during tests we do not want to move/delete our input files
tmp_files.append(shutil.copy(f, tmp_folder))
else:
tmp_files.append(shutil.move(f, tmp_folder))
for file in tmp_files:
transcribed_segments, time_for_task = self.run(
file,
progress,
file_format,
add_timestamp,
*pipeline_params,
)

file_name, file_ext = os.path.splitext(os.path.basename(file))
if save_same_dir and input_folder_path:
output_dir = os.path.dirname(file)
subtitle, file_path = generate_file(
output_dir=output_dir,
output_file_name=file_name,
output_format=file_format,
result=transcribed_segments,
add_timestamp=add_timestamp,
**writer_options
)

subtitle, file_path = generate_file(
output_dir=output_dir,
output_dir=self.output_dir,
output_file_name=file_name,
output_format=file_format,
result=transcribed_segments,
add_timestamp=add_timestamp,
**writer_options
)

subtitle, file_path = generate_file(
output_dir=self.output_dir,
output_file_name=file_name,
output_format=file_format,
result=transcribed_segments,
add_timestamp=add_timestamp,
**writer_options
)
files_info[file_name] = {"subtitle": read_file(file_path), "time_for_task": time_for_task, "path": file_path}
files_info[file_name] = {"subtitle": read_file(file_path), "time_for_task": time_for_task, "path": file_path}

total_result = ''
total_time = 0
Expand Down

0 comments on commit cc4d389

Please sign in to comment.