Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/subsai/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def transcribe(media_file: str, model: Union[AbstractModel, str], model_config:

:return: SSAFile: list of subtitles
"""
if type(model) == str:
if isinstance(model, str):
stt_model = SubsAI.create_model(model, model_config)
else:
stt_model = model
Expand Down Expand Up @@ -142,7 +142,7 @@ def available_translation_languages(model: Union[str, TranslationModel]) -> list
:param model: the name of the model
:return: list of available languages
"""
if type(model) == str:
if isinstance(model, str):
langs = Tools.create_translation_model(model).available_languages()
else:
langs = model.available_languages()
Expand Down Expand Up @@ -180,7 +180,7 @@ def translate(subs: SSAFile,

:return: returns an `SSAFile` subtitles translated to the target language
"""
if type(model) == str:
if isinstance(model, str):
translation_model = Tools.create_translation_model(model_name=model, model_family=model_family)
else:
translation_model = model
Expand Down Expand Up @@ -240,6 +240,11 @@ def auto_sync(subs: SSAFile,
os.unlink(srtin_file.name)
srtout_file.close()
os.unlink(srtout_file.name)
if os.path.exists(srtin):
os.unlink(srtin)
if os.path.exists(srtout):
os.unlink(srtout)

@staticmethod
def merge_subs_with_video(subs: Dict[str, SSAFile],
media_file: str,
Expand Down
2 changes: 1 addition & 1 deletion src/subsai/models/faster_whisper_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def __init__(self, model_config):
logging.basicConfig()
logging.getLogger("faster_whisper").setLevel(logging.DEBUG)

def transcribe(self, media_file) -> str:
def transcribe(self, media_file) -> SSAFile:
segments, info = self.model.transcribe(media_file, **self.transcribe_configs)
subs = SSAFile()
total_duration = round(info.duration, 2) # Same precision as the Whisper timestamps.
Expand Down
2 changes: 1 addition & 1 deletion src/subsai/models/whisperX_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __init__(self, model_config):
download_root=self.download_root,
language=self.language)

def transcribe(self, media_file) -> str:
def transcribe(self, media_file) -> SSAFile:
audio = whisperx.load_audio(media_file)
result = self.model.transcribe(audio, batch_size=self.batch_size)
model_a, metadata = whisperx.load_align_model(language_code=result["language"], device=self.device)
Expand Down
58 changes: 31 additions & 27 deletions src/subsai/models/whisper_api_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,36 +173,40 @@ def _transcribe_chunk(self, chunk_data):
os.remove(chunk_path)
raise e

def transcribe(self, media_file: str) -> str:
def transcribe(self, media_file: str) -> SSAFile:

audio_file_path = convert_video_to_audio_ffmpeg(media_file)

chunks = self.chunk_audio(audio_file_path)

print(f"Processing {len(chunks)} audio chunks with {self.n_jobs} parallel jobs")

# Prepare chunk data for parallel processing
chunk_data = [(i, chunk, offset) for i, (chunk, offset) in enumerate(chunks)]

# Use parallel processing if n_jobs > 1, otherwise process sequentially
if self.n_jobs > 1:
# Use threading backend since API calls are I/O-bound
parallel_results = Parallel(n_jobs=self.n_jobs, backend="threading")(
delayed(self._transcribe_chunk)(data) for data in chunk_data
)
else:
# Sequential processing for n_jobs=1
parallel_results = [self._transcribe_chunk(data) for data in chunk_data]
try:
chunks = self.chunk_audio(audio_file_path)

# Sort results by chunk index to maintain order
parallel_results.sort(key=lambda x: x[0])
print(f"Processing {len(chunks)} audio chunks with {self.n_jobs} parallel jobs")

# Process results and apply time offsets
results = ""
for i, result_text, offset in parallel_results:
# Shift subtitles by offset
result = SSAFile.from_string(result_text)
result.shift(ms=offset)
results += result.to_string("srt")
# Prepare chunk data for parallel processing
chunk_data = [(i, chunk, offset) for i, (chunk, offset) in enumerate(chunks)]

return SSAFile.from_string(results)
# Use parallel processing if n_jobs > 1, otherwise process sequentially
if self.n_jobs > 1:
# Use threading backend since API calls are I/O-bound
parallel_results = Parallel(n_jobs=self.n_jobs, backend="threading")(
delayed(self._transcribe_chunk)(data) for data in chunk_data
)
else:
# Sequential processing for n_jobs=1
parallel_results = [self._transcribe_chunk(data) for data in chunk_data]

# Sort results by chunk index to maintain order
parallel_results.sort(key=lambda x: x[0])

# Process results and apply time offsets
results = ""
for i, result_text, offset in parallel_results:
# Shift subtitles by offset
result = SSAFile.from_string(result_text)
result.shift(ms=offset)
results += result.to_string("srt")

return SSAFile.from_string(results)
finally:
if os.path.exists(audio_file_path):
os.unlink(audio_file_path)
3 changes: 2 additions & 1 deletion src/subsai/models/whisper_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from typing import Tuple
import pysubs2
from pysubs2 import SSAFile
from subsai.models.abstract_model import AbstractModel
import whisper
from subsai.utils import _load_config, get_available_devices
Expand Down Expand Up @@ -203,7 +204,7 @@ def __init__(self, model_config):
download_root=self.download_root,
in_memory=self.in_memory)

def transcribe(self, media_file) -> str:
def transcribe(self, media_file) -> SSAFile:
audio = whisper.load_audio(media_file)
result = self.model.transcribe(audio,
verbose=self.verbose,
Expand Down
2 changes: 1 addition & 1 deletion src/subsai/models/whisper_timestamped_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def __init__(self, model_config={}):
download_root=self.download_root,
in_memory=self.in_memory)

def transcribe(self, media_file) -> str:
def transcribe(self, media_file) -> SSAFile:
audio = whisper_timestamped.load_audio(media_file)
results = whisper_timestamped.transcribe(self.model, audio,
verbose=self.verbose,
Expand Down
2 changes: 1 addition & 1 deletion src/subsai/models/whispercpp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def __init__(self, model_config):

self.model = Model(model=self.model_type, **self.params)

def transcribe(self, media_file) -> str:
def transcribe(self, media_file) -> SSAFile:
segments = self.model.transcribe(media=media_file, **self.params)
subs = SSAFile()
for seg in segments:
Expand Down
Loading