Skip to content

Commit

Permalink
chore: upgrade OpenAI (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
chidiwilliams authored Jan 6, 2024
1 parent c7be2f1 commit b922d65
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 563 deletions.
77 changes: 0 additions & 77 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ jobs:
poetry run make bundle_windows
elif [ "$RUNNER_OS" == "Linux" ]; then
poetry run make bundle_linux
fi
env:
BUZZ_CODESIGN_IDENTITY: ${{ secrets.BUZZ_CODESIGN_IDENTITY }}
Expand All @@ -188,79 +184,6 @@ jobs:
dist/Buzz*-mac.dmg
dist/buzz-*.deb
benchmark:
runs-on: ${{ matrix.os }}
if: github.ref == 'refs/heads/main'
strategy:
fail-fast: false
matrix:
include:
- os: macos-latest
- os: windows-latest
- os: ubuntu-20.04
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: actions/setup-python@v4
with:
python-version: "3.10.7"

- name: Install Poetry Action
uses: snok/[email protected]
with:
virtualenvs-create: true
virtualenvs-in-project: true

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-2

- name: Load cached Whisper models
id: cached-whisper-models
uses: actions/cache@v3
with:
path: |
~/Library/Caches/Buzz
~/.cache/whisper
~/.cache/huggingface
~/AppData/Local/Buzz/Buzz/Cache
key: whisper-models-${{ runner.os }}

- uses: FedericoCarboni/setup-ffmpeg@v1
id: setup-ffmpeg
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: poetry install

- name: Install apt dependencies
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends libyaml-dev libegl1-mesa libxkbcommon-x11-0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-xinerama0 libxcb-shape0 libxcb-cursor0 libportaudio2 gettext
if: "startsWith(matrix.os, 'ubuntu-')"

- name: Test
run: |
poetry run make benchmarks
shell: bash

- name: Store benchmark results
uses: benchmark-action/github-action-benchmark@v1
with:
name: ${{ runner.os }}
tool: "pytest"
output-file-path: benchmarks.json
github-token: ${{ secrets.GITHUB_TOKEN }}
comment-on-alert: true
summary-always: true
auto-push: true

publish-pypi:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ mac_app_path := ./dist/Buzz.app
mac_zip_path := ./dist/Buzz-${version}-mac.zip
mac_dmg_path := ./dist/Buzz-${version}-mac.dmg

bundle_linux: dist/Buzz
bash scripts/bundle_linux.sh

bundle_windows: dist/Buzz
iscc //DAppVersion=${version} installer.iss

Expand Down
49 changes: 24 additions & 25 deletions buzz/transcriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from typing import Any, List, Optional, Tuple, Union, Set

import numpy as np
import openai
from openai import OpenAI

import tqdm
from PyQt6.QtCore import QObject, pyqtSignal, pyqtSlot
from dataclasses_json import dataclass_json, config, Exclude
Expand Down Expand Up @@ -388,6 +389,9 @@ def __init__(self, task: FileTranscriptionTask, parent: Optional["QObject"] = No
super().__init__(task=task, parent=parent)
self.file_path = task.file_path
self.task = task.transcription_options.task
self.openai_client = OpenAI(
api_key=self.transcription_task.transcription_options.openai_access_token
)

def transcribe(self) -> List[Segment]:
logging.debug(
Expand Down Expand Up @@ -422,10 +426,6 @@ def transcribe(self) -> List[Segment]:
total_size = os.path.getsize(mp3_file)
max_chunk_size = 25 * 1024 * 1024

openai.api_key = (
self.transcription_task.transcription_options.openai_access_token
)

self.progress.emit((0, 100))

if total_size < max_chunk_size:
Expand Down Expand Up @@ -468,27 +468,26 @@ def transcribe(self) -> List[Segment]:
return segments

def get_segments_for_file(self, file: str, offset_ms: int = 0):
with open(file, "rb") as audio_file:
kwargs = {
"model": "whisper-1",
"file": audio_file,
"response_format": "verbose_json",
"language": self.transcription_task.transcription_options.language,
}
transcript = (
openai.Audio.translate(**kwargs)
if self.transcription_task.transcription_options.task == Task.TRANSLATE
else openai.Audio.transcribe(**kwargs)
)
kwargs = {
"model": "whisper-1",
"file": file,
"response_format": "verbose_json",
"language": self.transcription_task.transcription_options.language,
}
transcript = (
self.openai_client.audio.transcriptions.create(**kwargs)
if self.transcription_task.transcription_options.task == Task.TRANSLATE
else self.openai_client.audio.translations.create(**kwargs)
)

return [
Segment(
int(segment["start"] * 1000 + offset_ms),
int(segment["end"] * 1000 + offset_ms),
segment["text"],
)
for segment in transcript["segments"]
]
return [
Segment(
int(segment["start"] * 1000 + offset_ms),
int(segment["end"] * 1000 + offset_ms),
segment["text"],
)
for segment in transcript["segments"]
]

def stop(self):
pass
Expand Down
10 changes: 4 additions & 6 deletions buzz/widgets/preferences_dialog/general_preferences_widget.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import logging
from typing import Optional

import openai
from PyQt6.QtCore import QRunnable, QObject, pyqtSignal, QThreadPool
from PyQt6.QtWidgets import QWidget, QFormLayout, QPushButton, QMessageBox
from openai.error import AuthenticationError
from openai import AuthenticationError, OpenAI

from buzz.store.keyring_store import KeyringStore
from buzz.widgets.line_edit import LineEdit
Expand Down Expand Up @@ -96,8 +94,8 @@ def __init__(self, api_key: str):

def run(self):
try:
openai.Model.list(api_key=self.api_key)
client = OpenAI(api_key=self.api_key)
client.models.list()
self.signals.success.emit()
except AuthenticationError as exc:
logging.error(exc)
self.signals.failed.emit(str(exc))
self.signals.failed.emit(exc.body["message"])
Loading

0 comments on commit b922d65

Please sign in to comment.