Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transcribe function can take a callback that runs after every step in the transcription process #2469

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions whisper/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import traceback
import warnings
from typing import TYPE_CHECKING, List, Optional, Tuple, Union
from typing import TYPE_CHECKING, List, Optional, Tuple, Union, Callable

import numpy as np
import torch
Expand Down Expand Up @@ -52,6 +52,7 @@ def transcribe(
append_punctuations: str = "\"'.。,,!!??::”)]}、",
clip_timestamps: Union[str, List[float]] = "0",
hallucination_silence_threshold: Optional[float] = None,
callback: Optional[Callable[[int, int, float], None]] = None,
**decode_options,
):
"""
Expand Down Expand Up @@ -119,6 +120,10 @@ def transcribe(
When word_timestamps is True, skip silent periods longer than this threshold (in seconds)
when a possible hallucination is detected

callback: Optional[Callable[int, int, float]] = None,
After each step in the transcription process, call the callback function with
the arguments current posistion, total frames, estimated time to finish in seconds

Returns
-------
A dictionary containing the resulting text ("text") and segment-level details ("segments"), and
Expand Down Expand Up @@ -504,8 +509,17 @@ def next_words_segment(segments: List[dict]) -> Optional[dict]:
# do not feed the prompt tokens if a high temperature was used
prompt_reset_since = len(all_tokens)

total_position = min(content_frames, seek)
increase = total_position - previous_seek

if callback is not None:
rate = pbar.format_dict["rate"]
remaining = (pbar.total - pbar.n) / rate if rate and pbar.total else 0

callback(total_position, content_frames, remaining)

# update progress bar
pbar.update(min(content_frames, seek) - previous_seek)
pbar.update(increase)

return dict(
text=tokenizer.decode(all_tokens[len(initial_prompt_tokens) :]),
Expand Down