abdeladim-s / pywhispercpp

Python bindings for whisper.cpp
https://abdeladim-s.github.io/pywhispercpp/
MIT License
129 stars 15 forks source link

_pywhispercpp module could not be found #34

Open kutal10 opened 4 months ago

kutal10 commented 4 months ago

Just did a standard PyPi download in my venv as per

pip install pywhispercpp

A standard script with:

import pywhispercpp.model as m

modelPath: str = ...
filePath: str = ...
outputPath: str = ...

model = m.Model('modelPath', n_threads=6)
segments = model.transcribe(filePath, token_timestamps=True, max_len=1)

with open(outputPath, 'w') as file:
    for segment in segments:
        file.write(segment.text + '\n')

Is failing with error:

Traceback (most recent call last):
  File "...\whisper_file.py", line 1, in <module>
    import pywhispercpp.model as m
  File "...\model.py", line 13, in <module>
    import _pywhispercpp as pw
ImportError: DLL load failed while importing _pywhispercpp: The specified module could not be found.

For reference, FFMpeg is installed:

╰─ ffmpeg -version                                                                                                   ─╯
ffmpeg version 4.4-essentials_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 10.2.0 (Rev6, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
libavutil      56. 70.100 / 56. 70.100
libavcodec     58.134.100 / 58.134.100
libavformat    58. 76.100 / 58. 76.100
libavdevice    58. 13.100 / 58. 13.100
libavfilter     7.110.100 /  7.110.100
libswscale      5.  9.100 /  5.  9.100
libswresample   3.  9.100 /  3.  9.100
libpostproc    55.  9.100 / 55.  9.100
abdeladim-s commented 4 months ago

Seems like Python cannot see the DLL module for some reason, even though the wheels were built successfully for Windows. I would suggest to try to build the package from source or use WSL.

NBNGaming commented 3 months ago

I confirm this problem exists. Building from source results in the same error.

abdeladim-s commented 3 months ago

@NBNGaming, if building from source leads to the same error, then I think something's missing from your system, because the Winodws wheels have been successfully built using GitHub actions without any issue. Please make sure that you have the GCC toolchain and you can compile whisper.cpp first without any problems.

BBC-Esq commented 1 month ago

I'm getting the same exact error message regarding the .dll file...I searched the "lib" directory after pip installing and there are no .dll files within that directory. Any idea why?

I'm not an expert so forgive me, but am I supposed to build whisper.cpp first and then install pywhispercpp? ggerganov's repo? I'm unfamiliar with building but can learn, but need to know if I have to install whisper.cpp first please. Thanks!

BTW, I don't know what GCC toolchain means...

Windows 10 CUDA 12.5 Intel CPU

abdeladim-s commented 1 month ago

@BBC-Esq, I am not quite sure why this is happening on Windows! Usually, you don't need to build whisper.cpp if you pip installed the package and basically it should work out of the box. However if the pre-built wheel for your system is not working then you might need to build the project from source.

But, before going through this path, here are some suggestions:

  1. If you are using Python 3.12, try to downgrade to 3.10 or 3.11. This might solve the issue.
  2. Otherwise, use WSL instead if you are unfamiliar with building, this will work without issues I assume.
BBC-Esq commented 1 month ago

I'm using Python 3.11. Not familiar with WSL...any other ideas? Have you actually tested it on Windows?

abdeladim-s commented 1 month ago

Apart from the Github action which ran successfully, I didn't make any tests on Windows unfortunately, I only tested the project on Linux. I just double checked now the pre-built Windows wheel for Python 3.11 pywhispercpp-1.2.0-cp311-cp311-win_amd64.whl and I can see that the dll .pyd file is there. Could you please double check on your end ?

BBC-Esq commented 1 month ago

Actually, it wasn't that hard to test and it gave me this error:

Traceback (most recent call last):
  File "D:\Scripts\benchmark_whisper\bench_whisper_cpp.py", line 1, in <module>
    from pywhispercpp.model import Model
  File "D:\Scripts\benchmark_whisper\Lib\site-packages\pywhispercpp\model.py", line 13, in <module>
    import _pywhispercpp as pw
ImportError: DLL load failed while importing _pywhispercpp: The specified module could not be found.

I did pip install and then the link to the wheel you gave me.

Here is the directory structure that another script of mine culled...Everything from the "pywhispercpp" directory downwards:

pywhispercpp/
    constants.py
    model.py
    utils.py
    _logger.py
    __init__.py
    examples/
        assistant.py
        livestream.py
        main.py
        recording.py
        __init__.py
        __pycache__/
            assistant.cpython-311.pyc
            livestream.cpython-311.pyc
            main.cpython-311.pyc
            recording.cpython-311.pyc
            __init__.cpython-311.pyc
    __pycache__/
        constants.cpython-311.pyc
        model.cpython-311.pyc
        utils.cpython-311.pyc
        _logger.cpython-311.pyc
        __init__.cpython-311.pyc

I noticed that the .pyd file is one level up within the "site-packages" folder though.

BBC-Esq commented 1 month ago

Correction in case you didn't see my edit to the above message...The .pyd file is a directory higher in "site-packages".

abdeladim-s commented 1 month ago

@BBC-Esq, so it's there at least. Not sure why Windows cannot find it!! Can you in that case put the pyd file inside the pywhispercpp directory ? Or Maybe if didn't work try to put it also in your current working directory D:\Scripts\benchmark_whisper\ ?

BBC-Esq commented 1 month ago

Sure, I'll put it in both at the same time.

BBC-Esq commented 1 month ago

Same exact error as before. Might it be that you're importing "_pywhispercpp" with an underscore at the beginning instead of simply "pywhispercpp"?

BBC-Esq commented 1 month ago

The script I'm using is very simple. I even modified it to add appending the system path but am still getting the same error...

import sys
import os
from pywhispercpp.model import Model

# Add the directory containing the .pyd file to the sys.path
sys.path.append(os.path.dirname(os.path.abspath(r"D:\Scripts\benchmark_whisper\Lib\site-packages\_pywhispercpp.cp311-win_amd64.pyd")))

model = Model('base.en', n_threads=6)
segments = model.transcribe(r"D:\Scripts\benchmark_whisper\test_audio_flac_converted.wav", speed_up=True)
for segment in segments:
    print(segment.text)
abdeladim-s commented 1 month ago

No, the _pywhispercpp is the extension module :)

Can you try with os.add_dll_directory instead of sys.path.append

BBC-Esq commented 1 month ago

Hmm...same as before...

image

abdeladim-s commented 1 month ago

Hmm...same as before...

You should use os.add_dll... before the import!

BBC-Esq commented 1 month ago

Same error as before and here's the modified script:

import os

dll_path = r"D:\Scripts\benchmark_whisper\_pywhispercpp.cp311-win_amd64.pyd"
dll_directory = os.path.dirname(dll_path)

os.add_dll_directory(dll_directory)

from pywhispercpp.model import Model

model = Model('base.en', n_threads=6)
segments = model.transcribe(r"D:\Scripts\benchmark_whisper\test_audio_flac_converted.wav", speed_up=True)

for segment in segments:
    print(segment.text)

I also tried this script:

import os
import ctypes

dll_path = r"D:\Scripts\benchmark_whisper\_pywhispercpp.cp311-win_amd64.pyd"
dll_directory = os.path.dirname(dll_path)

with os.add_dll_directory(dll_directory):
    ctypes.CDLL(dll_path)

    from pywhispercpp.model import Model

    model = Model('base.en', n_threads=6)
    segments = model.transcribe(r"D:\Scripts\benchmark_whisper\test_audio_flac_converted.wav", speed_up=True)

for segment in segments:
    print(segment.text)

It gave me a slightly different error:

Traceback (most recent call last):
  File "D:\Scripts\benchmark_whisper\bench_whisper_cpp.py", line 8, in <module>
    ctypes.CDLL(dll_path)
  File "C:\Users\Airflow\AppData\Local\Programs\Python\Python311\Lib\ctypes\__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: Could not find module 'D:\Scripts\benchmark_whisper\_pywhispercpp.cp311-win_amd64.pyd' (or one of its dependencies). Try using the full path with constructor syntax.

NOTE: It says "or one of its dependencies"

abdeladim-s commented 1 month ago

Windows is weird to be honest! if it complains about the dependencies then maybe you need Windows c++ redistributables or something! A lot of things can go wrong! Not quite sure what's the real issue. That's why I suggested to use WSL! In that case try to build whisper.cpp first and see if it works, you can find the instructions on their reop.

BBC-Esq commented 1 month ago

I asked jeeves and he told me to try "dependency walker" from Microsoft but it didn't work...https://www.dependencywalker.com/

Then I tried https://github.com/lucasg/Dependencies/releases/tag/v1.11.1

The "file" "open" dialog only lets you select .dll files so you'll have to drag and drop the .pyd file...Anyways, this is what it gave me:

image

It seems that "whisper.dll" is missing? So basically, I need to install openai's whisper?

abdeladim-s commented 1 month ago

Great idea @BBC-Esq, Here is the whisper.dll file from ggerganov/whisper.cpp repo. Maybe include it in the same directroy as well, hopefully this will solve the issue

BBC-Esq commented 1 month ago

Fucking A...it worked. I put it in the "site-packages" folder, the "benchmark_whisper" folder, and the "pywhispercpp" folder and it worked...now it's just a matter of narrowing it down to which folder hierarchy it should be in.

BBC-Esq commented 1 month ago

...correction, it didn't actually work. It resolved that error, but now I'm getting "failed to compute log mel spectrogram:"

[2024-05-29 22:58:13,706] {utils.py:38} INFO - No download directory was provided, models will be downloaded to C:\Users\Airflow\AppData\Local\pywhispercpp\pywhispercpp\models
[2024-05-29 22:58:13,707] {utils.py:46} INFO - Model base.en already exists in C:\Users\Airflow\AppData\Local\pywhispercpp\pywhispercpp\models
[2024-05-29 22:58:13,707] {model.py:221} INFO - Initializing the model ...
whisper_init_from_file_with_params_no_state: loading model from 'C:\Users\Airflow\AppData\Local\pywhispercpp\pywhispercpp\models\ggml-base.en.bin'
whisper_model_load: loading model
whisper_model_load: n_vocab       = 51864
whisper_model_load: n_audio_ctx   = 1500
whisper_model_load: n_audio_state = 512
whisper_model_load: n_audio_head  = 8
whisper_model_load: n_audio_layer = 6
whisper_model_load: n_text_ctx    = 448
whisper_model_load: n_text_state  = 512
whisper_model_load: n_text_head   = 8
whisper_model_load: n_text_layer  = 6
whisper_model_load: n_mels        = 80
whisper_model_load: ftype         = 1
whisper_model_load: qntvr         = 0
whisper_model_load: type          = 2 (base)
whisper_model_load: adding 1607 extra tokens
whisper_model_load: n_langs       = 99
whisper_model_load:      CPU buffer size =   147.46 MB
whisper_model_load: model size    =  147.37 MB
whisper_init_state: kv self size  =   16.52 MB
whisper_init_state: kv cross size =   18.43 MB
whisper_init_state: compute buffer (conv)   =   14.86 MB
whisper_init_state: compute buffer (encode) =   85.99 MB
whisper_init_state: compute buffer (cross)  =    4.78 MB
whisper_init_state: compute buffer (decode) =   96.48 MB
[2024-05-29 22:58:14,083] {model.py:130} INFO - Transcribing ...
whisper_full_with_state: failed to compute log mel spectrogram
[2024-05-29 22:58:14,083] {model.py:133} INFO - Inference time: 0.000 s
abdeladim-s commented 1 month ago

Finally! so that's what was missing! I should find a way to include it with the wheel! For the failed to compute log mel spectrogram error, just remove speed_up True or set it to False!

BBC-Esq commented 1 month ago

I/we fixed it! Mind explaining to me what whisper.dll is and why we need it? Also, I have cuda installed. How can I use gpu acceleration in my script?

import os
import ctypes

dll_directory = r"D:\Scripts\benchmark_whisper\Lib\site-packages"
ctypes.windll.kernel32.SetDllDirectoryW(dll_directory)

ctypes.CDLL(os.path.join(dll_directory, '_pywhispercpp.cp311-win_amd64.pyd'))

from pywhispercpp.model import Model

model = Model(r"D:\Scripts\benchmark_whisper\Models\ggml-large-v2-q5_0.bin", n_threads=6)
segments = model.transcribe(r"D:\Scripts\benchmark_whisper\test_audio_flac_converted.wav", speed_up=False)

for segment in segments:
    print(segment.text)
BBC-Esq commented 1 month ago

...do tell me you've implemented the gpu acceleration like cuda, etc. in your repo? I don't see anything in the documentation about a cuda let alone any other gpu acceleration parameters... ;-)

abdeladim-s commented 1 month ago

The whisper.dll is very simply the file containing the executable code of whisper.cpp. Unfortunately GPU is not supported at the moment, I didn't find time to add it!

BBC-Esq commented 1 month ago

Damn, was looking forward to benchmarking cuda whisper.cpp. Anyways, fun little adventure tonight. Have a good one man. I'll follow new releases for when you include the .dll and/or support GPU. How hard can it be, to support cuda, vulkan, openblas, etc. ;-)

abdeladim-s commented 1 month ago

It shouldn't be so hard I assume, but it'll need time, this is the problem :) But feel free to take a look at the code as well, PRs are always welcome!