pywhispercpp

repository·main·Indexed 43 days ago

https://github.com/absadiki/pywhispercpp

High-performance Python bindings for whisper.cpp providing a Pythonic API for speech-to-text transcription. Supports hardware backends including CUDA, CoreML, Vulkan, OpenBLAS, and OpenVINO. Includes a Model API, a CLI (pwcpp), a PyQt5-based GUI, and an Assistant class for voice activity detection.

Tokens
1.7K
Snippets
14
Records
14
Agent score
6%

What's inside pywhispercpp

  1. Install pywhispercpp with CoreML support

    main

    To enable CoreML support (typically for macOS hardware acceleration), set the WHISPER_COREML=1 environment variable during the pip installation from source.

    WHISPER_COREML=1 pip install git+https://github.com/absadiki/pywhispercpp
  2. Install ffmpeg for non-wav file support

    main

    To transcribe audio files other than .wav, you must have ffmpeg installed on your system. Use the command corresponding to your operating system:

    # on Ubuntu or Debian
    sudo apt update && sudo apt install ffmpeg
    
    # on Arch Linux
    sudo pacman -S ffmpeg
    
    # on MacOS using Homebrew
    brew install ffmpeg
    
    # on Windows using Chocolatey
    choco install ffmpeg
    
    # on Windows using Scoop
    scoop install ffmpeg
  3. Install pywhispercpp with OpenVINO support

    main

    To use OpenVINO, follow the official whisper.cpp OpenVINO setup steps to download the toolkit. Then, initialize the environment and install with WHISPER_OPENVINO=1.

    source ~/l_openvino_toolkit_ubuntu22_2023.0.0.10926.b4452d56304_x86_64/setupvars.sh 
    WHISPER_OPENVINO=1 pip install git+https://github.com/absadiki/pywhispercpp --no-cache --force-reinstall
  4. Install pywhispercpp from source

    main

    For optimal performance, install the package directly from the GitHub repository. This allows the build process to optimize for your specific hardware and backend requirements.

    pip install git+https://github.com/absadiki/pywhispercpp
  5. Install pywhispercpp with OpenBLAS support

    main

    If OpenBLAS is installed on your system, you can use GGML_BLAS=1. It is recommended to use --no-cache and --force-reinstall to ensure a fresh build with the correct flags.

    GGML_BLAS=1 pip install git+https://github.com/absadiki/pywhispercpp --no-cache --force-reinstall -v
  6. Quick start with pywhispercpp Model API

    main

    Use the Model class to load a Whisper model and transcribe audio. Models are downloaded automatically if not found locally. You can pass any whisper.cpp parameter as a keyword argument to the Model constructor or the transcribe method.

    from pywhispercpp.model import Model
    
    # Initialize model (e.g., 'base.en')
    model = Model('base.en')
    
    # Transcribe a file
    segments = model.transcribe('file.wav')
    for segment in segments:
        print(segment.text)
  7. Install pywhispercpp via pre-built wheels

    main

    You can install basic CPU-only wheels from PyPI. If you want to run the provided examples, install the [examples] extra to include necessary dependencies.

    # Basic CPU installation
    pip install pywhispercpp
    
    # Installation with example dependencies
    pip install pywhispercpp[examples]
  8. Create a voice assistant with Assistant class

    main

    The Assistant class uses a Voice Activity Detector (VAD) to detect speech and trigger transcription. You can use it programmatically or via the pwcpp-assistant CLI.

    from pywhispercpp.examples.assistant import Assistant
    
    # Initialize assistant with a callback for commands
    my_assistant = Assistant(commands_callback=print, n_threads=8)
    my_assistant.start()
  9. Use a callback for real-time transcription segments

    main

    You can provide a new_segment_callback to the transcribe method to handle segments as they are produced (e.g., for real-time printing).

    from pywhispercpp.model import Model
    
    model = Model('base.en', print_realtime=False, print_progress=False)
    segments = model.transcribe('file.mp3', new_segment_callback=print)