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

How do I use a parameter like initial_prompt in Python's Whisper? #127

Open
xiangliangX opened this issue Apr 28, 2024 · 1 comment
Open
Labels
documentation Improvements or additions to documentation

Comments

@xiangliangX
Copy link

How do I use a parameter like initial_prompt in Python's Whisper?

@ZachNagengast
Copy link
Contributor

ZachNagengast commented Apr 28, 2024

Best way to do this is to use the promptTokens parameter in DecodingOptions. This will add whatever tokens you pass into the <|startofprev|> section of the prompt that is passed into the decoder and can help with spelling and punctuation style, but keep in mind this is not like a LLM prompt, it should purely be used as an example of the style and spelling of output you're looking for. Check out these test cases for how you might be able to implement it in your code:

func testPromptTokens() async throws {
let whisperKit = try await WhisperKit(modelFolder: tinyModelPath(), verbose: true, logLevel: .debug)
let promptText = " prompt to encourage output without any punctuation and without capitalizing americans as if it was already normalized"
let tokenizer = try XCTUnwrap(whisperKit.tokenizer)
let promptTokens = tokenizer.encode(text: promptText).filter { $0 < tokenizer.specialTokens.specialTokenBegin }
let options = DecodingOptions(skipSpecialTokens: true, promptTokens: promptTokens)
let result = try await XCTUnwrapAsync(
await transcribe(with: .tiny, options: options),
"Failed to transcribe"
)
XCTAssertEqual(result.segments.first?.text, " and so my fellow americans ask not what your country can do for you ask what you can do for your country.")
}
func testPrefixTokens() async throws {
let whisperKit = try await WhisperKit(modelFolder: tinyModelPath(), verbose: true, logLevel: .debug)
// Prefix to encourage output without any punctuation and without capitalizing americans as if it was already normalized
let prefixText = " and so my fellow americans"
let tokenizer = try XCTUnwrap(whisperKit.tokenizer)
let prefixTokens = tokenizer.encode(text: prefixText).filter { $0 < tokenizer.specialTokens.specialTokenBegin }
let options = DecodingOptions(skipSpecialTokens: true, prefixTokens: prefixTokens)
let result = try await XCTUnwrapAsync(
await transcribe(with: .tiny, options: options),
"Failed to transcribe"
)
XCTAssertEqual(result.segments.first?.text, " and so my fellow americans ask not what your country can do for you ask what you can do for your country.")
}

@ZachNagengast ZachNagengast added the documentation Improvements or additions to documentation label May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants