Skip to content

Commit

Permalink
Put FileTranscriber in QRunnable (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
chidiwilliams authored Dec 4, 2022
1 parent fe2292c commit 209c0af
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ omit =
directory = coverage/html

[report]
fail_under = 78
fail_under = 77
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: macos-latest
Expand All @@ -26,7 +27,7 @@ jobs:
submodules: recursive
- uses: actions/setup-python@v4
with:
python-version: '3.9.13'
python-version: '3.10.7'

- name: Install Poetry Action
uses: snok/[email protected]
Expand Down Expand Up @@ -64,6 +65,7 @@ jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: macos-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ build/
.env
htmlcov/
libwhisper.*
whisper_cpp
whisper.dll
whisper_cpp.py
1 change: 0 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
disable=
C0114, # missing-module-docstring
C0116, # missing-function-docstring
C0115, # missing-class-docstring
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"files.associations": {
"Buzz.spec": "python"
".coveragerc": "ini",
"Buzz.spec": "python",
"iosfwd": "cpp"
},
"files.exclude": {
"**/.git": true,
Expand Down
1 change: 1 addition & 0 deletions Buzz.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ datas += copy_metadata('tokenizers')
datas += collect_data_files('whisper')
datas += [('whisper.dll' if platform.system() ==
'Windows' else 'libwhisper.*', '.')]
datas += [('whisper_cpp', '.')]
datas += [('assets/buzz.ico', 'assets')]
datas += [('assets/buzz-icon-1024.png', 'assets')]
datas += [(shutil.which('ffmpeg'), '.')]
Expand Down
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ endif

clean:
rm -f $(LIBWHISPER)
rm -f whisper_cpp
rm -f buzz/whisper_cpp.py
rm -rf dist/* || true

Expand Down Expand Up @@ -73,11 +74,15 @@ else
endif
endif

$(LIBWHISPER):
$(LIBWHISPER) whisper_cpp:
cmake -S whisper.cpp -B whisper.cpp/build/ $(CMAKE_FLAGS)
cmake --build whisper.cpp/build --verbose
cp whisper.cpp/build/$(LIBWHISPER) . || true
ls -lA whisper.cpp/build
ls -lA whisper.cpp/build/bin
cp whisper.cpp/build/bin/Debug/$(LIBWHISPER) . || true
cp whisper.cpp/build/bin/Debug/main whisper_cpp || true
cp whisper.cpp/build/$(LIBWHISPER) . || true
cp whisper.cpp/build/bin/main whisper_cpp || true

buzz/whisper_cpp.py: $(LIBWHISPER)
ctypesgen ./whisper.cpp/whisper.h -l$(LIBWHISPER) -o buzz/whisper_cpp.py
Expand Down
87 changes: 51 additions & 36 deletions buzz/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import platform
import sys
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple, Union

import humanize
import sounddevice
Expand All @@ -22,7 +22,8 @@

from .__version__ import VERSION
from .model_loader import ModelLoader
from .transcriber import FileTranscriber, OutputFormat, RecordingTranscriber
from .transcriber import (FileTranscriber, OutputFormat, RecordingTranscriber,
WhisperCppFileTranscriber)
from .whispr import LOADED_WHISPER_DLL, Task

APP_NAME = 'Buzz'
Expand Down Expand Up @@ -267,27 +268,22 @@ def update_progress(self, current_size: int):


class FileTranscriberObject(QObject):
download_model_progress = pyqtSignal(tuple)
event_received = pyqtSignal(object)
transcriber: FileTranscriber

def __init__(
self, model_path: str, use_whisper_cpp: bool, language: Optional[str],
self, model_path: str, language: Optional[str],
task: Task, file_path: str, output_file_path: str,
output_format: OutputFormat, word_level_timings: bool,
parent: Optional['QObject'], *args) -> None:
super().__init__(parent, *args)
self.transcriber = FileTranscriber(
model_path=model_path, use_whisper_cpp=use_whisper_cpp,
on_download_model_chunk=self.on_download_model_progress,
model_path=model_path,
language=language, task=task, file_path=file_path,
output_file_path=output_file_path, output_format=output_format,
event_callback=self.on_file_transcriber_event,
word_level_timings=word_level_timings)

def on_download_model_progress(self, current: int, total: int):
self.download_model_progress.emit((current, total))

def on_file_transcriber_event(self, event: FileTranscriber.Event):
self.event_received.emit(event)

Expand Down Expand Up @@ -386,7 +382,8 @@ class FileTranscriberWidget(QWidget):
enabled_word_level_timings = False
model_download_progress_dialog: Optional[DownloadModelProgressDialog] = None
transcriber_progress_dialog: Optional[TranscriberProgressDialog] = None
file_transcriber: Optional[FileTranscriberObject] = None
file_transcriber: Optional[Union[FileTranscriberObject,
WhisperCppFileTranscriber]] = None
model_loader: Optional[ModelLoader] = None

def __init__(self, file_path: str, parent: Optional[QWidget]) -> None:
Expand Down Expand Up @@ -482,17 +479,28 @@ def start_file_transcription(model_path: str):
if self.model_download_progress_dialog is not None:
self.model_download_progress_dialog = None

self.file_transcriber = FileTranscriberObject(
model_path=model_path, use_whisper_cpp=use_whisper_cpp,
file_path=self.file_path,
language=self.selected_language, task=self.selected_task,
output_file_path=output_file, output_format=self.selected_output_format,
word_level_timings=self.enabled_word_level_timings,
parent=self)
self.file_transcriber.event_received.connect(
self.on_transcriber_event)

self.file_transcriber.start()
if use_whisper_cpp:
self.file_transcriber = WhisperCppFileTranscriber(
model_path=model_path, file_path=self.file_path,
language=self.selected_language, task=self.selected_task,
output_file_path=output_file, output_format=self.selected_output_format,
word_level_timings=self.enabled_word_level_timings,
)
self.file_transcriber.signals.progress.connect(
self.on_transcriber_progress)
self.file_transcriber.signals.completed.connect(
self.on_transcriber_complete)
self.pool.start(self.file_transcriber)
else:
self.file_transcriber = FileTranscriberObject(
model_path=model_path, file_path=self.file_path,
language=self.selected_language, task=self.selected_task,
output_file_path=output_file, output_format=self.selected_output_format,
word_level_timings=self.enabled_word_level_timings,
parent=self)
self.file_transcriber.event_received.connect(
self.on_transcriber_event)
self.file_transcriber.start()

self.model_loader = ModelLoader(
name=model_name, use_whisper_cpp=use_whisper_cpp)
Expand Down Expand Up @@ -522,22 +530,28 @@ def on_download_model_error(self, error: str):

def on_transcriber_event(self, event: FileTranscriber.Event):
if isinstance(event, FileTranscriber.ProgressEvent):
current_size = event.current_value
total_size = event.max_value

# Create a dialog
if self.transcriber_progress_dialog is None:
self.transcriber_progress_dialog = TranscriberProgressDialog(
file_path=self.file_path, total_size=total_size, parent=self)
self.transcriber_progress_dialog.canceled.connect(
self.on_cancel_transcriber_progress_dialog)

# Update the progress of the dialog unless it has
# been canceled before this progress update arrived
if self.transcriber_progress_dialog is not None:
self.transcriber_progress_dialog.update_progress(current_size)
self.on_transcriber_progress(
(event.current_value, event.max_value))
elif isinstance(event, FileTranscriber.CompletedTranscriptionEvent):
self.reset_transcription()
self.on_transcriber_complete()

def on_transcriber_progress(self, progress: Tuple[int, int]):
(current_size, total_size) = progress

# Create a dialog
if self.transcriber_progress_dialog is None:
self.transcriber_progress_dialog = TranscriberProgressDialog(
file_path=self.file_path, total_size=total_size, parent=self)
self.transcriber_progress_dialog.canceled.connect(
self.on_cancel_transcriber_progress_dialog)

# Update the progress of the dialog unless it has
# been canceled before this progress update arrived
if self.transcriber_progress_dialog is not None:
self.transcriber_progress_dialog.update_progress(current_size)

def on_transcriber_complete(self):
self.reset_transcription()

def on_cancel_transcriber_progress_dialog(self):
if self.file_transcriber is not None:
Expand All @@ -547,6 +561,7 @@ def on_cancel_transcriber_progress_dialog(self):
def reset_transcription(self):
self.run_button.setDisabled(False)
if self.transcriber_progress_dialog is not None:
self.transcriber_progress_dialog.close()
self.transcriber_progress_dialog = None

def on_cancel_model_progress_dialog(self):
Expand Down
Loading

0 comments on commit 209c0af

Please sign in to comment.