natrys / whisper.el

Speech-to-Text interface for Emacs using OpenAI's whisper model and whisper.cpp as inference engine.
140 stars 10 forks source link

Document using an already install whisper.cpp #16

Open edmundmiller opened 10 months ago

edmundmiller commented 10 months ago

I'd love to be able to use the whisper that's already installed, for example the one that's in nixpkgs. https://github.com/NixOS/nixpkgs/blob/nixos-23.05/pkgs/tools/audio/openai-whisper-cpp/default.nix#L51

The installation works great though!

edmundmiller commented 10 months ago

Ah never mind! Support is already there I just couldn't find it in the README? https://github.com/natrys/whisper.el/blob/2d541ac9dc5ac8683a40e5401b3ad2d8f5078525/whisper.el#L111-L126

natrys commented 10 months ago

Actually it is documented in the README (although I will take this as an unwitting proof of the fact that the README had grown too bloated due to too many variables).

In any case, would whisper.el even work with the nix package? iirc nix store paths are read only, whereas whisper.el basically presumes that downloaded models are in the relative models/ directory. I see that they have patched the download script to put models in current directory instead. Other issue is they renamed the binary from main to whisper-cpp.

You would basically have to override whisper-command function to accommodate these two things if you want to use nixpkgs version with whisper.el.

edmundmiller commented 10 months ago

To clarify, I read the part about using a different tool, but not an already installed whisper.

It has a fix where it installs to the local directory. This would need to install to somewhere that whisper.el could find it, so it's probably more pain than it's worth 🤷🏻

natrys commented 10 months ago

but not an already installed whisper.

That part is also handled by the same variable, by setting it to 'manual. But it's really for people who are using things like Window, where Emacs can't compile whisper for various reasons, but they manually can. It still assumes they compile it in whisper.cpp subdir in whisper-install-directory, and assumes Emacs can run shell commands to download models using upstream script. Neither of these apply here, however. The fact that nix changes binary name and patches download script basically makes it as alien as a different tool to whisper.el so it's the other part of the doc about different tool that becomes relevant here.

I could maybe add couple more variables like whispercpp binary name, or custom model directory just for the nix package, but that imo feels like more pain than worth, though might become necessary in future if whisper.cpp is packaged by more distros and people want to use that. In any case if you want to just use the nixpkgs version, it's not that much extra effort today (though you would lose the automatic download and quantization feature of whisper.el).

Assuming you are at ~/models/ and used the nix script to download model like:

$ whisper-cpp-download-ggml-model base

The whisper.el configuration would be:

(setq whisper-install-whispercpp nil)

(defun whisper--nix-command (input-file)
  `("whisper-cpp"
    "--model" ,(expand-file-name (concat "~/models/" "ggml-" whisper-model ".bin"))
    ,@(when whisper-use-threads (list "--threads" (number-to-string whisper-use-threads)))
    ,@(when whisper-translate '("--translate"))
    ,@(when whisper-show-progress-in-mode-line '("--print-progress"))
    "--language" ,whisper-language
    "--no-timestamps"
    "--file" ,input-file))

(advice-add 'whisper-command :override #'whisper--nix-command)

I pushed a commit earlier to recognise "whisper-cpp" from nix as indeed whisper.cpp from name so that you can still see transcription progress in modeline which is whisper.cpp specific feature. But that's probably all I want to do for now, aside from adding the recipe here in wiki and mention it in README somewhere.