Skip to content

Commit

Permalink
command: make use of the --file option
Browse files Browse the repository at this point in the history
Append the recognized commands to the output file (option `--file`).
One command per line in the format "probability<TAB>command".

With this you are able to do things like the following:

  # Create a named pipe for later usage
  mkfifo ./myfifo
  # Let awk read from the named pipe and if the probability is > 0.9 execute the command in bash
  awk -F'\t' '{if ($1 > 0.9) { printf("%s &\n", $2) | "bash" }}' ./myfifo
  # Run ./command and let it write the recognized commands to the named pipe
  ./command --commands <(echo -e "emacs\nfirefox\nfoot\n") -m ./ggml-base.bin --file ./myfifo
  • Loading branch information
KaiHa committed Dec 22, 2023
1 parent d2ee117 commit d3ba144
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/command/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ int process_command_list(struct whisper_context * ctx, audio_async &audio, const

std::vector<float> pcmf32_cur;
std::vector<float> pcmf32_prompt;
FILE *out_stream = NULL;
if (!params.fname_out.empty()) {
out_stream = fopen(params.fname_out.c_str(), "a");
if (out_stream == NULL) {
fprintf(stderr, "%s: error: opening of \"%s\" failed!\n", __func__, params.fname_out.c_str());
perror(NULL);
return 5;
}
}

// main loop
while (is_running) {
Expand Down Expand Up @@ -451,13 +460,21 @@ int process_command_list(struct whisper_context * ctx, audio_async &audio, const
"\033[1m", allowed_commands[index].c_str(), "\033[0m", prob,
(int) std::chrono::duration_cast<std::chrono::milliseconds>(t_end - t_start).count());
fprintf(stdout, "\n");

if (out_stream != NULL) {
fprintf(out_stream, "%f\t%s\n", prob, allowed_commands[index].c_str());
fflush(out_stream);
}
}
}

audio.clear();
}
}

if (out_stream != NULL) {
fclose(out_stream);
}
return 0;
}

Expand Down Expand Up @@ -559,6 +576,15 @@ int process_general_transcription(struct whisper_context * ctx, audio_async & au

std::vector<float> pcmf32_cur;
std::vector<float> pcmf32_prompt;
FILE *out_stream = NULL;
if (!params.fname_out.empty()) {
out_stream = fopen(params.fname_out.c_str(), "a");
if (out_stream == NULL) {
fprintf(stderr, "%s: error: opening of \"%s\" failed!\n", __func__, params.fname_out.c_str());
perror(NULL);
return 1;
}
}

std::string k_prompt = "Ok Whisper, start listening for commands.";
if (!params.prompt.empty()) {
Expand Down Expand Up @@ -665,6 +691,10 @@ int process_general_transcription(struct whisper_context * ctx, audio_async & au
const std::string command = ::trim(txt.substr(best_len));

fprintf(stdout, "%s: Command '%s%s%s', (t = %d ms)\n", __func__, "\033[1m", command.c_str(), "\033[0m", (int) t_ms);
if (out_stream != NULL) {
fprintf(out_stream, "%f\t%s\n", p/100.0, command.c_str());
fflush(out_stream);
}
}

fprintf(stdout, "\n");
Expand All @@ -674,6 +704,10 @@ int process_general_transcription(struct whisper_context * ctx, audio_async & au
}
}
}
if (out_stream != NULL) {
fclose(out_stream);
}


return 0;
}
Expand Down

0 comments on commit d3ba144

Please sign in to comment.