Skip to content

Commit

Permalink
Merge pull request #125 from teto/teto/whisper-dynamic-lang
Browse files Browse the repository at this point in the history
make it possible tu use :GpWhisper <LANGUAGE>
  • Loading branch information
teto authored Jul 17, 2024
2 parents 0878f2f + 6a7a78f commit 49ded50
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
indent_style = tab
indent_size = 1
tab_width = 4
end_of_line = lf
insert_final_newline = true
charset = utf-8

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,15 @@ Provides custom context per repository:

## Speech commands

#### `:GpWhisper` <!-- {doc=:GpWhisper} -->
#### `:GpWhisper` {lang?} <!-- {doc=:GpWhisper} -->

Transcription replaces the current line, visual selection or range in the current buffer. Use your mouth to ask a question in a chat buffer instead of writing it by hand, dictate some comments for the code, notes or even your next novel..

For the rest of the whisper commands, the transcription is used as an editable prompt for the equivalent non whisper command - `GpWhisperRewrite` dictates instructions for `GpRewrite` etc.

You can override the default language by setting {lang} with the 2 letter
shortname of your language (e.g. "en" for English, "fr" for French etc).

#### `:GpWhisperRewrite` <!-- {doc=:GpWhisperRewrite} -->

Similar to `:GpRewrite`, but the prompt instruction dialog uses transcribed spoken instructions.
Expand Down
15 changes: 11 additions & 4 deletions lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ M.prepare_commands = function()
end

M.cmd["Whisper" .. command] = function(params)
M.Whisper(function(text)
M.Whisper(M.config.whisper_language, function(text)
vim.schedule(function()
cmd(params, text)
end)
Expand Down Expand Up @@ -3200,7 +3200,7 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh
end

---@param callback function # callback function(text)
M.Whisper = function(callback)
M.Whisper = function(language, callback)
-- make sure sox is installed
if vim.fn.executable("sox") == 0 then
M.error("sox is not installed")
Expand Down Expand Up @@ -3356,7 +3356,7 @@ M.Whisper = function(callback)
.. M.config.openai_api_key
.. '" -H "Content-Type: multipart/form-data" '
.. '-F model="whisper-1" -F language="'
.. M.config.whisper_language
.. language
.. '" -F file="@final.mp3" '
.. '-F response_format="json"'

Expand Down Expand Up @@ -3459,7 +3459,14 @@ M.cmd.Whisper = function(params)
end_line = params.line2
end

M.Whisper(function(text)
local args = vim.split(params.args, " ")

local language = config.whisper_language
if args[1] ~= "" then
language = args[1]
end

M.Whisper(language, function(text)
if not vim.api.nvim_buf_is_valid(buf) then
return
end
Expand Down

0 comments on commit 49ded50

Please sign in to comment.