Skip to content

Commit

Permalink
Merge pull request #195 from jhj0517/feature/local-input
Browse files Browse the repository at this point in the history
Add local input folder path
  • Loading branch information
jhj0517 authored Jul 5, 2024
2 parents 81956d0 + f6adc1d commit 2ad601b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ def launch(self):
gr.Markdown(MARKDOWN, elem_id="md_project")
with gr.Tabs():
with gr.TabItem("File"): # tab1
with gr.Row():
with gr.Column():
input_file = gr.Files(type="filepath", label="Upload File here")
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="")
with gr.Row():
dd_model = gr.Dropdown(choices=self.whisper_inf.available_models, value="large-v2",
label="Model")
Expand Down Expand Up @@ -128,7 +133,7 @@ def launch(self):
files_subtitles = gr.Files(label="Downloadable output file", scale=3, interactive=False)
btn_openfolder = gr.Button('📂', scale=1)

params = [input_file, dd_file_format, cb_timestamp]
params = [input_file, tb_input_folder, dd_file_format, cb_timestamp]
whisper_params = WhisperParameters(model_size=dd_model,
lang=dd_lang,
is_translate=cb_translate,
Expand Down
39 changes: 39 additions & 0 deletions modules/utils/files_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import fnmatch

from gradio.utils import NamedString


def get_media_files(folder_path, include_sub_directory=False):
video_extensions = ['*.mp4', '*.mkv', '*.flv', '*.avi', '*.mov', '*.wmv']
audio_extensions = ['*.mp3', '*.wav', '*.aac', '*.flac', '*.ogg', '*.m4a']
media_extensions = video_extensions + audio_extensions

media_files = []

if include_sub_directory:
for root, _, files in os.walk(folder_path):
for extension in media_extensions:
media_files.extend(
os.path.join(root, file) for file in fnmatch.filter(files, extension)
if os.path.exists(os.path.join(root, file))
)
else:
for extension in media_extensions:
media_files.extend(
os.path.join(folder_path, file) for file in fnmatch.filter(os.listdir(folder_path), extension)
if os.path.isfile(os.path.join(folder_path, file)) and os.path.exists(os.path.join(folder_path, file))
)

return media_files


def format_gradio_files(files: list):
if not files:
return files

gradio_files = []
for file in files:
gradio_files.append(NamedString(file))
return gradio_files

9 changes: 9 additions & 0 deletions modules/whisper/whisper_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from modules.utils.subtitle_manager import get_srt, get_vtt, get_txt, write_file, safe_filename
from modules.utils.youtube_manager import get_ytdata, get_ytaudio
from modules.utils.files_manager import get_media_files, format_gradio_files
from modules.whisper.whisper_parameter import *
from modules.diarize.diarizer import Diarizer
from modules.vad.silero_vad import SileroVAD
Expand Down Expand Up @@ -123,6 +124,7 @@ def run(self,

def transcribe_file(self,
files: list,
input_folder_path: str,
file_format: str,
add_timestamp: bool,
progress=gr.Progress(),
Expand All @@ -135,6 +137,9 @@ def transcribe_file(self,
----------
files: list
List of files to transcribe from gr.Files()
input_folder_path: str
Input folder path to transcribe from gr.Textbox(). If this is provided, `files` will be ignored and
this will be used instead.
file_format: str
Subtitle File format to write from gr.Dropdown(). Supported format: [SRT, WebVTT, txt]
add_timestamp: bool
Expand All @@ -152,6 +157,10 @@ def transcribe_file(self,
Output file path to return to gr.Files()
"""
try:
if input_folder_path:
files = get_media_files(input_folder_path)
files = format_gradio_files(files)

files_info = {}
for file in files:
transcribed_segments, time_for_task = self.run(
Expand Down

0 comments on commit 2ad601b

Please sign in to comment.