Skip to content

Commit

Permalink
Fix transcription viewer opening before transcription completes (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
chidiwilliams committed Jan 3, 2023
1 parent d91a279 commit 40b0236
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
30 changes: 22 additions & 8 deletions buzz/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
QProgressDialog, QPushButton, QVBoxLayout, QHBoxLayout, QMenu,
QWidget, QGroupBox, QToolBar, QTableWidget, QMenuBar, QFormLayout, QTableWidgetItem,
QHeaderView, QAbstractItemView, QListWidget, QListWidgetItem, QToolButton, QSizePolicy)
from requests import get
from whisper import tokenizer

from buzz.cache import TasksCache
Expand Down Expand Up @@ -615,7 +614,8 @@ def on_transcriber_finished(self):
def on_transcriber_error(self, error: str):
self.reset_record_button()
self.set_recording_status_stopped()
QMessageBox.critical(self, '', f'An error occurred while starting a new recording: {error}. Please check your audio devices or check the application logs for more information.')
QMessageBox.critical(self, '',
f'An error occurred while starting a new recording: {error}. Please check your audio devices or check the application logs for more information.')

def on_cancel_model_progress_dialog(self):
if self.model_loader is not None:
Expand Down Expand Up @@ -669,7 +669,8 @@ class AboutDialog(QDialog):
GITHUB_API_LATEST_RELEASE_URL = 'https://api.github.com/repos/chidiwilliams/buzz/releases/latest'
GITHUB_LATEST_RELEASE_URL = 'https://github.com/chidiwilliams/buzz/releases/latest'

def __init__(self, network_access_manager: Optional[QNetworkAccessManager]=None, parent: Optional[QWidget] = None) -> None:
def __init__(self, network_access_manager: Optional[QNetworkAccessManager] = None,
parent: Optional[QWidget] = None) -> None:
super().__init__(parent)

self.setFixedSize(200, 250)
Expand Down Expand Up @@ -862,7 +863,8 @@ def __init__(self, parent: Optional[QWidget]):

def load_icon(self, file_path: str):
is_dark_theme = self.palette().window().color().black() > 127
return self.load_icon_with_color(file_path, self.ICON_DARK_THEME_BACKGROUND if is_dark_theme else self.ICON_LIGHT_THEME_BACKGROUND)
return self.load_icon_with_color(file_path,
self.ICON_DARK_THEME_BACKGROUND if is_dark_theme else self.ICON_LIGHT_THEME_BACKGROUND)

@staticmethod
def load_icon_with_color(file_path: str, color: str):
Expand All @@ -878,8 +880,8 @@ def on_record_action_triggered(self):
recording_transcriber_window = RecordingTranscriberWidget(self)
recording_transcriber_window.exec()

def set_open_transcript_action_disabled(self, disabled: bool):
self.open_transcript_action.setDisabled(disabled)
def set_open_transcript_action_enabled(self, enabled: bool):
self.open_transcript_action.setEnabled(enabled)

def set_clear_history_action_enabled(self, enabled: bool):
self.clear_history_action.setEnabled(enabled)
Expand Down Expand Up @@ -987,8 +989,16 @@ def on_open_transcript_action_triggered(self):
self.open_transcription_viewer(task_id)

def on_table_selection_changed(self):
enable_open_transcript_action = self.should_enable_open_transcript_action()
self.toolbar.set_open_transcript_action_enabled(enable_open_transcript_action)

def should_enable_open_transcript_action(self):
selected_rows = self.table_widget.selectionModel().selectedRows()
self.toolbar.set_open_transcript_action_disabled(len(selected_rows) == 0)
if len(selected_rows) == 1:
task_id = TranscriptionTasksTableWidget.find_task_id(selected_rows[0])
if self.tasks[task_id].status == FileTranscriptionTask.Status.COMPLETED:
return True
return False

def on_table_double_clicked(self, index: QModelIndex):
task_id = TranscriptionTasksTableWidget.find_task_id(index)
Expand Down Expand Up @@ -1020,6 +1030,9 @@ def on_tasks_changed(self):
self.toolbar.set_clear_history_action_enabled(
any([self.task_completed_or_errored(task) for task in self.tasks.values()]))

enable_open_transcript_action = self.should_enable_open_transcript_action()
self.toolbar.set_open_transcript_action_enabled(enable_open_transcript_action)

def closeEvent(self, event: QtGui.QCloseEvent) -> None:
self.transcriber_worker.stop()
self.transcriber_thread.quit()
Expand All @@ -1040,7 +1053,8 @@ class HuggingFaceSearchLineEdit(LineEdit):
model_selected = pyqtSignal(str)
popup: QListWidget

def __init__(self, network_access_manager: Optional[QNetworkAccessManager] = None, parent: Optional[QWidget] = None):
def __init__(self, network_access_manager: Optional[QNetworkAccessManager] = None,
parent: Optional[QWidget] = None):
super().__init__('', parent)

self.setMinimumWidth(150)
Expand Down
33 changes: 21 additions & 12 deletions tests/gui_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

import pytest
import sounddevice
from PyQt6.QtCore import QSize, Qt, QByteArray, QObject
from PyQt6.QtCore import QSize, Qt
from PyQt6.QtGui import QValidator, QKeyEvent
from PyQt6.QtWidgets import QPushButton, QToolBar, QTableWidget, QApplication, QMessageBox
from pytestqt.qtbot import QtBot

from buzz.__version__ import VERSION

from .mock_qt import MockNetworkAccessManager, MockNetworkReply
from buzz.cache import TasksCache
from buzz.gui import (AboutDialog, AdvancedSettingsDialog, AudioDevicesComboBox, DownloadModelProgressDialog,
FileTranscriberWidget, LanguagesComboBox, MainWindow,
Expand All @@ -24,6 +22,7 @@
from buzz.transcriber import (FileTranscriptionOptions, FileTranscriptionTask,
Segment, TranscriptionOptions)
from tests.mock_sounddevice import MockInputStream
from .mock_qt import MockNetworkAccessManager, MockNetworkReply


class TestLanguagesComboBox:
Expand Down Expand Up @@ -161,24 +160,34 @@ def test_should_run_transcription_task(self, qtbot: QtBot, tasks_cache):
window = MainWindow(tasks_cache=tasks_cache)
qtbot.add_widget(window)

toolbar: QToolBar = window.findChild(QToolBar)
new_transcription_action = [action for action in toolbar.actions() if action.text() == 'New Transcription'][0]

with patch('PyQt6.QtWidgets.QFileDialog.getOpenFileNames') as open_file_names_mock:
open_file_names_mock.return_value = ([get_test_asset('whisper-french.mp3')], '')
new_transcription_action = self.get_toolbar_action(window, 'New Transcription')
new_transcription_action.trigger()

file_transcriber_widget: FileTranscriberWidget = window.findChild(FileTranscriberWidget)
run_button: QPushButton = file_transcriber_widget.findChild(QPushButton)
run_button.click()

def check_task_completed():
table_widget: QTableWidget = window.findChild(QTableWidget)
assert table_widget.rowCount() == 1
assert table_widget.item(0, 1).text() == 'whisper-french.mp3'
assert table_widget.item(0, 2).text() == 'Completed'
open_transcript_action = self.get_toolbar_action(window, 'Open Transcript')
assert open_transcript_action.isEnabled() is False

qtbot.wait_until(check_task_completed, timeout=60 * 1000)
def assert_task_completed():
_table_widget: QTableWidget = window.findChild(QTableWidget)
assert _table_widget.rowCount() == 1
assert _table_widget.item(0, 1).text() == 'whisper-french.mp3'
assert _table_widget.item(0, 2).text() == 'Completed'

qtbot.wait_until(assert_task_completed, timeout=60 * 1000)

table_widget: QTableWidget = window.findChild(QTableWidget)
table_widget.setCurrentIndex(table_widget.indexFromItem(table_widget.item(0, 1)))
assert open_transcript_action.isEnabled()

@staticmethod
def get_toolbar_action(window: MainWindow, text: str):
toolbar: QToolBar = window.findChild(QToolBar)
return [action for action in toolbar.actions() if action.text() == text][0]


class TestFileTranscriberWidget:
Expand Down

0 comments on commit 40b0236

Please sign in to comment.