Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🚀 Add GitHub workflows for package testing and CI, integrate logger support in whisper.py 🤖 #15

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: kadirnar
79 changes: 79 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Python package CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.8, 3.9, 3.10, 3.11]
torch-version: [2.1.0, 2.1.1]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Restore Ubuntu cache
uses: actions/cache@v2
if: matrix.operating-system == 'ubuntu-latest'
with:
path: ~/.cache/pip
key: ${{ matrix.operating-system }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.operating-system }}-${{ matrix.python-version }}-

- name: Restore MacOS cache
uses: actions/cache@v2
if: matrix.operating-system == 'macos-latest'
with:
path: ~/Library/Caches/pip
key: ${{ matrix.operating-system }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.operating-system }}-${{ matrix.python-version }}-

- name: Restore Windows cache
uses: actions/cache@v2
if: matrix.operating-system == 'windows-latest'
with:
path: ~\AppData\Local\pip\Cache
key: ${{ matrix.operating-system }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.operating-system }}-${{ matrix.python-version }}-

- name: Update pip
run: python -m pip install --upgrade pip

- name: Check styling with pre-commit
run: |
pre-commit install
pre-commit run --all-files

- name: Install PyTorch on Linux and Windows
if: >
matrix.operating-system == 'ubuntu-latest' ||
matrix.operating-system == 'windows-latest'
run: >
pip install torch==${{ matrix.torch-version }}+cpu
-f https://download.pytorch.org/whl/torch_stable.html

- name: Install PyTorch on MacOS
if: matrix.operating-system == 'macos-latest'
run: pip install torch==${{ matrix.torch-version }}
- name: Install PyTorch on Linux and Windows

- name: Install whisperplus package from local setup.py
run: >
pip install -e .

- name: Unittest whisperplus
run: |
python -m unittest
78 changes: 78 additions & 0 deletions .github/workflows/package_testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Package Testing

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ${{ matrix.operating-system }}

strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.8, 3.9, 3.10, 3.11]
torch-version: [2.1.0, 2.1.1]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Restore Ubuntu cache
uses: actions/cache@v2
if: matrix.operating-system == 'ubuntu-latest'
with:
path: ~/.cache/pip
key: ${{ matrix.operating-system }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.operating-system }}-${{ matrix.python-version }}-

- name: Restore MacOS cache
uses: actions/cache@v2
if: matrix.operating-system == 'macos-latest'
with:
path: ~/Library/Caches/pip
key: ${{ matrix.operating-system }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.operating-system }}-${{ matrix.python-version }}-

- name: Restore Windows cache
uses: actions/cache@v2
if: matrix.operating-system == 'windows-latest'
with:
path: ~\AppData\Local\pip\Cache
key: ${{ matrix.operating-system }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.operating-system }}-${{ matrix.python-version }}-

- name: Update pip
run: python -m pip install --upgrade pip

- name: Install Numpy
run: >
pip install numpy

- name: Install PyTorch on Linux and Windows
if: >
matrix.operating-system == 'ubuntu-latest' ||
matrix.operating-system == 'windows-latest'
run: >
pip install torch==${{ matrix.torch-version }}+cpu
-f https://download.pytorch.org/whl/torch_stable.html

- name: Install PyTorch on MacOS
if: matrix.operating-system == 'macos-latest'
run: pip install torch==${{ matrix.torch-version }}

- name: Install latest whisperplus package
run: >
pip install --upgrade --force-reinstall whisperplus

- name: Unittest whisperplus
run: |
python -m unittest
34 changes: 34 additions & 0 deletions .github/workflows/publish_pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Publish Python distributions to PyPI

on:
push:
branches: [ master ]

jobs:
build-n-publish:
name: Build and publish Python distributions to PyPI
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install pypa/build
run: >-
python -m
pip install
build
- name: Build a binary wheel and a source tarball
run: >-
python -m
build
--sdist
--wheel
--outdir dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@master
with:
user: __token__
password: ${{ secrets.PYPI_WHISPERPLUS_API_KEY }}
2 changes: 1 addition & 1 deletion whisperplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from whisperplus.pipelines.whisper import SpeechToTextPipeline
from whisperplus.utils.download_utils import download_and_convert_to_mp3

__version__ = '0.0.4'
__version__ = '0.0.5'
__author__ = 'kadirnar'
__license__ = 'Apache License 2.0'
__all__ = ['']
12 changes: 11 additions & 1 deletion whisperplus/pipelines/whisper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import logging

import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


class SpeechToTextPipeline:
"""Class for converting audio to text using a pre-trained speech recognition model."""
Expand All @@ -11,6 +15,8 @@ def __init__(self, model_id: str = "openai/whisper-large-v3"):

if self.model is None:
self.load_model(model_id)
else:
logging.info("Model already loaded.")

self.set_device()

Expand All @@ -21,16 +27,20 @@ def set_device(self):
else:
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

logging.info(f"Using device: {self.device}")

def load_model(self, model_id: str = "openai/whisper-large-v3"):
"""
Loads the pre-trained speech recognition model and moves it to the specified device.

Args:
model_id (str): Identifier of the pre-trained model to be loaded.
"""
logging.info("Loading model...")
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch.float16, low_cpu_mem_usage=True, use_safetensors=True)
model.to(self.device)
logging.info("Model loaded successfully.")

self.model = model

Expand Down Expand Up @@ -60,6 +70,6 @@ def __call__(self, audio_path: str, model_id: str = "openai/whisper-large-v3", l
model_kwargs={"use_flash_attention_2": True},
generate_kwargs={"language": language},
)

logging.info("Transcribing audio...")
result = pipe(audio_path)["text"]
return result