Try setuptools

This commit is contained in:
Charlie Marsh
2024-04-27 13:12:02 -04:00
commit 9d4a09dcd4
7 changed files with 142 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
whisper.cpp-*
build/
dist/
whisper_cpp.egg-info/
bin/

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Charles Marsh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

1
README.md Normal file
View File

@@ -0,0 +1 @@
# whisper-cpp

12
pyproject.toml Normal file
View File

@@ -0,0 +1,12 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "whisper-cpp"
version = "0.0.1"
description = "A Python package for the whisper-cpp CLI."
authors = [{ name = "Charlie Marsh", email = "charlie.r.marsh@gmail.com" }]
readme = "README.md"
requires-python = ">=3.7"
license = { file = "LICENSE" }

64
setup.py Normal file
View File

@@ -0,0 +1,64 @@
import shutil
import urllib.request
import zipfile
import os
from setuptools import setup, find_packages
import subprocess
WHISPER_CPP_VERSION = '1.5.5'
WHISPER_CPP_ZIP_URL = f"https://github.com/ggerganov/whisper.cpp/archive/refs/tags/v{WHISPER_CPP_VERSION}.zip"
WHISPER_DIR = f'whisper.cpp-{WHISPER_CPP_VERSION}'
WHISPER_BINARY = f'bin/whisper-cpp'
if not os.path.exists(WHISPER_BINARY):
def download_and_unzip(url, extract_to='.'):
"""
Downloads a ZIP file from a URL and unzips it into the given directory.
Args:
url (str): The URL of the zip file to download.
extract_to (str): The directory to extract the files into.
"""
# Download the file from `url` and save it locally under `file_name`
file_name = url.split('/')[-1]
urllib.request.urlretrieve(url, file_name)
# Unzip the file
with zipfile.ZipFile(file_name, 'r') as zip_ref:
zip_ref.extractall(extract_to)
# Clean up the downloaded zip file
os.remove(file_name)
# Remove the existing whisper-cpp directory, if it exists
shutil.rmtree(WHISPER_DIR, ignore_errors=True)
shutil.rmtree('bin', ignore_errors=True)
# Download the latest whisper-cpp release from GitHub.
download_and_unzip(WHISPER_CPP_ZIP_URL, extract_to='.')
# Run make to build the whisper-cpp library.
subprocess.check_call(['make'], cwd=WHISPER_DIR)
# Rename the built binary.
os.makedirs('bin', exist_ok=True)
# Copy the built binary to the `bin` directory.
shutil.copy2(f'{WHISPER_DIR}/main', WHISPER_BINARY)
# Include the `whisper-cpp` binary in the package.
setup(
name='whisper-cpp',
version='0.0.1',
packages=find_packages(),
package_data={"whisper-cpp": [os.path.join("bin", "*")]},
include_package_data=True,
install_requires=[],
)

0
whisper_cpp/__init__.py Normal file
View File

39
whisper_cpp/__main__.py Normal file
View File

@@ -0,0 +1,39 @@
import os
import sys
import sysconfig
def find_whisper_bin() -> str:
"""Return the whisper binary path."""
whisper_exe = "whisper" + sysconfig.get_config_var("EXE")
path = os.path.join(sysconfig.get_path("scripts"), whisper_exe)
if os.path.isfile(path):
return path
if sys.version_info >= (3, 10):
user_scheme = sysconfig.get_preferred_scheme("user")
elif os.name == "nt":
user_scheme = "nt_user"
elif sys.platform == "darwin" and sys._framework:
user_scheme = "osx_framework_user"
else:
user_scheme = "posix_user"
path = os.path.join(sysconfig.get_path("scripts", scheme=user_scheme), whisper_exe)
if os.path.isfile(path):
return path
raise FileNotFoundError(path)
if __name__ == "__main__":
whisper = os.fsdecode(find_whisper_bin())
if sys.platform == "win32":
import subprocess
completed_process = subprocess.run([whisper, *sys.argv[1:]])
sys.exit(completed_process.returncode)
else:
os.execvp(whisper, [whisper, *sys.argv[1:]])