Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 08ff013

Browse files
committedOct 8, 2024·
Added prompt for sample app
1 parent bfb1316 commit 08ff013

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed
 

‎Examples/WhisperAX/WhisperAX/Views/ContentView.swift

+41-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct ContentView: View {
3131
@State private var availableModels: [String] = []
3232
@State private var availableLanguages: [String] = []
3333
@State private var disabledModels: [String] = WhisperKit.recommendedModels().disabled
34-
34+
@AppStorage("promptText") private var promptText: String?
3535
@AppStorage("selectedAudioInput") private var selectedAudioInput: String = "No Audio Input"
3636
@AppStorage("selectedModel") private var selectedModel: String = WhisperKit.recommendedModels().default
3737
@AppStorage("selectedTab") private var selectedTab: String = "Transcribe"
@@ -765,6 +765,11 @@ struct ContentView: View {
765765

766766
var settingsForm: some View {
767767
List {
768+
769+
770+
771+
772+
768773
HStack {
769774
Text("Show Timestamps")
770775
InfoButton("Toggling this will include/exclude timestamps in both the UI and the prefill tokens.\nEither <|notimestamps|> or <|0.00|> will be forced based on this setting unless \"Prompt Prefill\" is de-selected.")
@@ -817,6 +822,14 @@ struct ContentView: View {
817822
}
818823
.padding(.horizontal)
819824
.padding(.bottom)
825+
826+
TextField("Enter prompt text", text: Binding(
827+
get: { self.promptText ?? "" },
828+
set: { self.promptText = $0.isEmpty ? nil : $0 }
829+
))
830+
.textFieldStyle(.roundedBorder)
831+
.padding(.horizontal)
832+
.padding(.bottom)
820833

821834
VStack {
822835
Text("Starting Temperature:")
@@ -1303,7 +1316,7 @@ struct ContentView: View {
13031316
let task: DecodingTask = selectedTask == "transcribe" ? .transcribe : .translate
13041317
let seekClip: [Float] = [lastConfirmedSegmentEndSeconds]
13051318

1306-
let options = DecodingOptions(
1319+
var options = DecodingOptions(
13071320
verbose: true,
13081321
task: task,
13091322
language: languageCode,
@@ -1318,6 +1331,19 @@ struct ContentView: View {
13181331
clipTimestamps: seekClip,
13191332
chunkingStrategy: chunkingStrategy
13201333
)
1334+
1335+
// Prompt
1336+
if let promptText = promptText {
1337+
guard whisperKit.tokenizer != nil else {
1338+
throw WhisperError.tokenizerUnavailable()
1339+
}
1340+
1341+
if promptText.count > 0, let tokenizer = whisperKit.tokenizer {
1342+
options.promptTokens = tokenizer.encode(text: " " + promptText.trimmingCharacters(in: .whitespaces)).filter { $0 < tokenizer.specialTokens.specialTokenBegin }
1343+
options.usePrefillPrompt = true
1344+
}
1345+
}
1346+
13211347

13221348
// Early stopping checks
13231349
let decodingCallback: ((TranscriptionProgress) -> Bool?) = { (progress: TranscriptionProgress) in
@@ -1542,7 +1568,7 @@ struct ContentView: View {
15421568
print(selectedLanguage)
15431569
print(languageCode)
15441570

1545-
let options = DecodingOptions(
1571+
var options = DecodingOptions(
15461572
verbose: true,
15471573
task: task,
15481574
language: languageCode,
@@ -1556,6 +1582,18 @@ struct ContentView: View {
15561582
wordTimestamps: true, // required for eager mode
15571583
firstTokenLogProbThreshold: -1.5 // higher threshold to prevent fallbacks from running to often
15581584
)
1585+
1586+
// Prompt
1587+
if let promptText = promptText {
1588+
guard whisperKit.tokenizer != nil else {
1589+
throw WhisperError.tokenizerUnavailable()
1590+
}
1591+
1592+
if promptText.count > 0, let tokenizer = whisperKit.tokenizer {
1593+
options.promptTokens = tokenizer.encode(text: " " + promptText.trimmingCharacters(in: .whitespaces)).filter { $0 < tokenizer.specialTokens.specialTokenBegin }
1594+
options.usePrefillPrompt = true
1595+
}
1596+
}
15591597

15601598
// Early stopping checks
15611599
let decodingCallback: ((TranscriptionProgress) -> Bool?) = { progress in

0 commit comments

Comments
 (0)
Please sign in to comment.