Skip to content

Commit 2037b65

Browse files
VulumeCodejongwook
andauthored
Context prompt (#128)
Co-authored-by: Jong Wook Kim <[email protected]>
1 parent fc0f409 commit 2037b65

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

whisper/transcribe.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ def decode_with_fallback(segment: torch.Tensor) -> List[DecodingResult]:
137137
all_segments = []
138138
prompt_reset_since = 0
139139

140+
initial_prompt = decode_options.pop("initial_prompt", None) or []
141+
if initial_prompt:
142+
initial_prompt = tokenizer.encode(" " + initial_prompt.strip())
143+
all_tokens.extend(initial_prompt)
144+
140145
def add_segment(
141146
*, start: float, end: float, text_tokens: torch.Tensor, result: DecodingResult
142147
):
@@ -237,7 +242,7 @@ def add_segment(
237242
pbar.update(min(num_frames, seek) - previous_seek_value)
238243
previous_seek_value = seek
239244

240-
return dict(text=tokenizer.decode(all_tokens), segments=all_segments, language=language)
245+
return dict(text=tokenizer.decode(all_tokens[len(initial_prompt):]), segments=all_segments, language=language)
241246

242247

243248
def cli():
@@ -260,6 +265,7 @@ def cli():
260265
parser.add_argument("--length_penalty", type=float, default=None, help="optional token length penalty coefficient (alpha) as in https://arxiv.org/abs/1609.08144, uses simple lengt normalization by default")
261266

262267
parser.add_argument("--suppress_tokens", type=str, default="-1", help="comma-separated list of token ids to suppress during sampling; '-1' will suppress most special characters except common punctuations")
268+
parser.add_argument("--initial_prompt", type=str, default=None, help="optional text to provide as a prompt for the first window.")
263269
parser.add_argument("--condition_on_previous_text", type=str2bool, default=True, help="if True, provide the previous output of the model as a prompt for the next window; disabling may make the text inconsistent across windows, but the model becomes less prone to getting stuck in a failure loop")
264270
parser.add_argument("--fp16", type=str2bool, default=True, help="whether to perform inference in fp16; True by default")
265271

0 commit comments

Comments
 (0)