Skip to content

Commit

Permalink
Enable faster whisper's "translate to english" feature. No need to do…
Browse files Browse the repository at this point in the history
… translation if all you need is English.
  • Loading branch information
philpw99 committed Dec 23, 2024
1 parent 82fdfd6 commit e88d2b0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class Config(QConfig):
faster_whisper_one_word = ConfigItem(
"FasterWhisper", "OneWord", True, BoolValidator()
)
# 翻译成英语
faster_whisper_translate_to_english = ConfigItem(
"FasterWhisper", "TranslateToEnglish", False, BoolValidator()
)
# 提示词
faster_whisper_prompt = ConfigItem("FasterWhisper", "Prompt", "")

Expand Down
9 changes: 9 additions & 0 deletions app/components/FasterWhisperSettingDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,14 @@ def __init__(self, parent=None):
self.other_group
)

# 翻译成英语
self.translate_to_english_card = SwitchSettingCard(
FIF.LANGUAGE,
self.tr("翻译成英语"),
self.tr("使用FastWhisper内置的翻译成英语功能"),
cfg.faster_whisper_translate_to_english,
self.other_group
)
# 提示词
self.prompt_card = LineEditSettingCard(
cfg.faster_whisper_prompt,
Expand Down Expand Up @@ -765,6 +773,7 @@ def __init__(self, parent=None):
# 添加其他设置的卡片
self.other_group.addSettingCard(self.ff_mdx_kim2_card)
self.other_group.addSettingCard(self.one_word_card)
self.other_group.addSettingCard(self.translate_to_english_card)
self.other_group.addSettingCard(self.prompt_card)

# 检查并提示下载 faster-whisper
Expand Down
6 changes: 6 additions & 0 deletions app/core/bk_asr/FasterWhisperASR.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self,
ff_mdx_kim2: bool = False,
# 文本处理参数
one_word: int = 0,
translate_to_english: bool = False,
sentence: bool = False,
max_line_width: int = 100,
max_line_count: int = 1,
Expand Down Expand Up @@ -69,6 +70,7 @@ def __init__(self,
self.max_line_count = max_line_count
self.max_comma = max_comma
self.max_comma_cent = max_comma_cent
self.translate_to_english = translate_to_english
self.prompt = prompt

self.process = None
Expand Down Expand Up @@ -121,6 +123,10 @@ def _build_command(self, audio_path: Path) -> List[str]:
if self.one_word in [0, 1, 2]:
cmd.extend(["--one_word", str(self.one_word)])

# 翻译成英语
if self.translate_to_english:
cmd.extend(["--task", "translate"])

if self.sentence:
cmd.extend([
"--sentence",
Expand Down
1 change: 1 addition & 0 deletions app/core/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ class Type(Enum):
faster_whisper_vad_method: Optional[VadMethodEnum] = VadMethodEnum.SILERO_V3
faster_whisper_ff_mdx_kim2: bool = False
faster_whisper_one_word: bool = True
faster_whisper_translate_to_english: bool = False
faster_whisper_prompt: Optional[str] = None

# LLM(优化翻译模型)
Expand Down
3 changes: 3 additions & 0 deletions app/core/thread/create_task_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def create_file_task(self, file_path):
faster_whisper_ff_mdx_kim2=cfg.faster_whisper_ff_mdx_kim2.value,
faster_whisper_one_word=cfg.faster_whisper_one_word.value,
faster_whisper_prompt=cfg.faster_whisper_prompt.value,
faster_whisper_translate_to_english=cfg.faster_whisper_translate_to_english.value,
video_info=video_info,
audio_format="mp3",
audio_save_path=str(audio_save_path),
Expand Down Expand Up @@ -243,6 +244,7 @@ def create_url_task(self, url):
faster_whisper_ff_mdx_kim2=cfg.faster_whisper_ff_mdx_kim2.value,
faster_whisper_one_word=cfg.faster_whisper_one_word.value,
faster_whisper_prompt=cfg.faster_whisper_prompt.value,
faster_whisper_translate_to_english=cfg.faster_whisper_translate_to_english.value,
use_asr_cache=cfg.use_asr_cache.value,
need_word_time_stamp=need_word_time_stamp,
original_subtitle_save_path=str(original_subtitle_save_path),
Expand Down Expand Up @@ -319,6 +321,7 @@ def create_transcription_task(self, file_path):
faster_whisper_vad_method=cfg.faster_whisper_vad_method.value,
faster_whisper_ff_mdx_kim2=cfg.faster_whisper_ff_mdx_kim2.value,
faster_whisper_one_word=cfg.faster_whisper_one_word.value,
faster_whisper_translate_to_english=cfg.faster_whisper_translate_to_english.value,
faster_whisper_prompt=cfg.faster_whisper_prompt.value,
video_info=video_info,
audio_format="mp3",
Expand Down
3 changes: 3 additions & 0 deletions app/core/thread/transcript_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def run(self):
args["max_line_width"] = int(self.task.max_word_count_english * 8)
args["max_comma_cent"] = 50
args["max_comma"] = 20

if self.task.faster_whisper_translate_to_english:
args["translate_to_english"] = True

self.asr = FasterWhisperASR(self.task.audio_save_path, **args)
elif self.task.transcribe_model == TranscribeModelEnum.BIJIAN:
Expand Down

0 comments on commit e88d2b0

Please sign in to comment.