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

FEATURE: Add initial_prompt support for whispercpp params #156

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/whispercpp/api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class Params:
prompt_num_tokens: int
language: str
def with_language(self, language: str) -> Params: ...
initial_prompt: str
def with_initial_prompt(self, initial_prompt: str) -> Params: ...
suppress_blank: bool
def with_suppress_blank(self, suppress_blank: bool) -> Params: ...
suppress_none_speech_tokens: bool
Expand Down
8 changes: 8 additions & 0 deletions src/whispercpp/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ struct Params {
private:
std::shared_ptr<whisper_full_params> fp;
std::string language;
std::string initial_prompt;

CallbackAndContext<NewSegmentCallback> new_segment_callback;
CallbackAndContext<ProgressCallback> progress_callback;
Expand Down Expand Up @@ -348,6 +349,13 @@ struct Params {
return this;
}

// Set initial prompt
Params *with_initial_prompt(std::string initial_prompt) {
this->initial_prompt = initial_prompt;
fp->initial_prompt = this->initial_prompt.c_str();
return this;
}

// Set suppress_blank. See
// https://github.com/openai/whisper/blob/f82bc59f5ea234d4b97fb2860842ed38519f7e65/whisper/decoding.py#L89
// for more information.
Expand Down
9 changes: 9 additions & 0 deletions src/whispercpp/params.cc
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,15 @@ void ExportParamsApi(py::module &m) {
WITH_DEPRECATION("language");
self.with_language(language);
})
// NOTE set initial_prompt
.def("with_initial_prompt", &Params::with_initial_prompt, "initial_prompt"_a,
py::return_value_policy::reference)
.def_property(
"initial_prompt", [](Params &self) { return self.get()->initial_prompt; },
[](Params &self, const char *initial_prompt) {
WITH_DEPRECATION("initial_prompt");
self.with_initial_prompt(initial_prompt);
})
Comment on lines +610 to +615
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to set this if this doesn't exist beforehand

// NOTE setting suppress_blank
.def("with_suppress_blank", &Params::with_suppress_blank,
"suppress_blank"_a, py::return_value_policy::reference)
Expand Down
8 changes: 8 additions & 0 deletions tests/params_export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ def test_set_language():
params_with_lang = params.with_language(lang)
print(lang, params_with_lang.language)
assert params_with_lang.language == lang

def test_set_initial_prompt():
params = w.api.Params.from_enum(w.api.StrategyType.SAMPLING_GREEDY)
for prompt in ["This is a test initial prompt", ""]:
assert params.initial_prompt != ""
params_with_initial_prompt = params.with_initial_prompt(prompt)
print(prompt, params_with_initial_prompt.initial_prompt)
assert params_with_initial_prompt.initial_prompt == prompt