Skip to content

Commit 40b0236

Browse files
Fix transcription viewer opening before transcription completes (#293)
1 parent d91a279 commit 40b0236

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

buzz/gui.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
QProgressDialog, QPushButton, QVBoxLayout, QHBoxLayout, QMenu,
2525
QWidget, QGroupBox, QToolBar, QTableWidget, QMenuBar, QFormLayout, QTableWidgetItem,
2626
QHeaderView, QAbstractItemView, QListWidget, QListWidgetItem, QToolButton, QSizePolicy)
27-
from requests import get
2827
from whisper import tokenizer
2928

3029
from buzz.cache import TasksCache
@@ -615,7 +614,8 @@ def on_transcriber_finished(self):
615614
def on_transcriber_error(self, error: str):
616615
self.reset_record_button()
617616
self.set_recording_status_stopped()
618-
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.')
617+
QMessageBox.critical(self, '',
618+
f'An error occurred while starting a new recording: {error}. Please check your audio devices or check the application logs for more information.')
619619

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

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

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

863864
def load_icon(self, file_path: str):
864865
is_dark_theme = self.palette().window().color().black() > 127
865-
return self.load_icon_with_color(file_path, self.ICON_DARK_THEME_BACKGROUND if is_dark_theme else self.ICON_LIGHT_THEME_BACKGROUND)
866+
return self.load_icon_with_color(file_path,
867+
self.ICON_DARK_THEME_BACKGROUND if is_dark_theme else self.ICON_LIGHT_THEME_BACKGROUND)
866868

867869
@staticmethod
868870
def load_icon_with_color(file_path: str, color: str):
@@ -878,8 +880,8 @@ def on_record_action_triggered(self):
878880
recording_transcriber_window = RecordingTranscriberWidget(self)
879881
recording_transcriber_window.exec()
880882

881-
def set_open_transcript_action_disabled(self, disabled: bool):
882-
self.open_transcript_action.setDisabled(disabled)
883+
def set_open_transcript_action_enabled(self, enabled: bool):
884+
self.open_transcript_action.setEnabled(enabled)
883885

884886
def set_clear_history_action_enabled(self, enabled: bool):
885887
self.clear_history_action.setEnabled(enabled)
@@ -987,8 +989,16 @@ def on_open_transcript_action_triggered(self):
987989
self.open_transcription_viewer(task_id)
988990

989991
def on_table_selection_changed(self):
992+
enable_open_transcript_action = self.should_enable_open_transcript_action()
993+
self.toolbar.set_open_transcript_action_enabled(enable_open_transcript_action)
994+
995+
def should_enable_open_transcript_action(self):
990996
selected_rows = self.table_widget.selectionModel().selectedRows()
991-
self.toolbar.set_open_transcript_action_disabled(len(selected_rows) == 0)
997+
if len(selected_rows) == 1:
998+
task_id = TranscriptionTasksTableWidget.find_task_id(selected_rows[0])
999+
if self.tasks[task_id].status == FileTranscriptionTask.Status.COMPLETED:
1000+
return True
1001+
return False
9921002

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

1033+
enable_open_transcript_action = self.should_enable_open_transcript_action()
1034+
self.toolbar.set_open_transcript_action_enabled(enable_open_transcript_action)
1035+
10231036
def closeEvent(self, event: QtGui.QCloseEvent) -> None:
10241037
self.transcriber_worker.stop()
10251038
self.transcriber_thread.quit()
@@ -1040,7 +1053,8 @@ class HuggingFaceSearchLineEdit(LineEdit):
10401053
model_selected = pyqtSignal(str)
10411054
popup: QListWidget
10421055

1043-
def __init__(self, network_access_manager: Optional[QNetworkAccessManager] = None, parent: Optional[QWidget] = None):
1056+
def __init__(self, network_access_manager: Optional[QNetworkAccessManager] = None,
1057+
parent: Optional[QWidget] = None):
10441058
super().__init__('', parent)
10451059

10461060
self.setMinimumWidth(150)

tests/gui_test.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55

66
import pytest
77
import sounddevice
8-
from PyQt6.QtCore import QSize, Qt, QByteArray, QObject
8+
from PyQt6.QtCore import QSize, Qt
99
from PyQt6.QtGui import QValidator, QKeyEvent
1010
from PyQt6.QtWidgets import QPushButton, QToolBar, QTableWidget, QApplication, QMessageBox
1111
from pytestqt.qtbot import QtBot
1212

1313
from buzz.__version__ import VERSION
14-
15-
from .mock_qt import MockNetworkAccessManager, MockNetworkReply
1614
from buzz.cache import TasksCache
1715
from buzz.gui import (AboutDialog, AdvancedSettingsDialog, AudioDevicesComboBox, DownloadModelProgressDialog,
1816
FileTranscriberWidget, LanguagesComboBox, MainWindow,
@@ -24,6 +22,7 @@
2422
from buzz.transcriber import (FileTranscriptionOptions, FileTranscriptionTask,
2523
Segment, TranscriptionOptions)
2624
from tests.mock_sounddevice import MockInputStream
25+
from .mock_qt import MockNetworkAccessManager, MockNetworkReply
2726

2827

2928
class TestLanguagesComboBox:
@@ -161,24 +160,34 @@ def test_should_run_transcription_task(self, qtbot: QtBot, tasks_cache):
161160
window = MainWindow(tasks_cache=tasks_cache)
162161
qtbot.add_widget(window)
163162

164-
toolbar: QToolBar = window.findChild(QToolBar)
165-
new_transcription_action = [action for action in toolbar.actions() if action.text() == 'New Transcription'][0]
166-
167163
with patch('PyQt6.QtWidgets.QFileDialog.getOpenFileNames') as open_file_names_mock:
168164
open_file_names_mock.return_value = ([get_test_asset('whisper-french.mp3')], '')
165+
new_transcription_action = self.get_toolbar_action(window, 'New Transcription')
169166
new_transcription_action.trigger()
170167

171168
file_transcriber_widget: FileTranscriberWidget = window.findChild(FileTranscriberWidget)
172169
run_button: QPushButton = file_transcriber_widget.findChild(QPushButton)
173170
run_button.click()
174171

175-
def check_task_completed():
176-
table_widget: QTableWidget = window.findChild(QTableWidget)
177-
assert table_widget.rowCount() == 1
178-
assert table_widget.item(0, 1).text() == 'whisper-french.mp3'
179-
assert table_widget.item(0, 2).text() == 'Completed'
172+
open_transcript_action = self.get_toolbar_action(window, 'Open Transcript')
173+
assert open_transcript_action.isEnabled() is False
180174

181-
qtbot.wait_until(check_task_completed, timeout=60 * 1000)
175+
def assert_task_completed():
176+
_table_widget: QTableWidget = window.findChild(QTableWidget)
177+
assert _table_widget.rowCount() == 1
178+
assert _table_widget.item(0, 1).text() == 'whisper-french.mp3'
179+
assert _table_widget.item(0, 2).text() == 'Completed'
180+
181+
qtbot.wait_until(assert_task_completed, timeout=60 * 1000)
182+
183+
table_widget: QTableWidget = window.findChild(QTableWidget)
184+
table_widget.setCurrentIndex(table_widget.indexFromItem(table_widget.item(0, 1)))
185+
assert open_transcript_action.isEnabled()
186+
187+
@staticmethod
188+
def get_toolbar_action(window: MainWindow, text: str):
189+
toolbar: QToolBar = window.findChild(QToolBar)
190+
return [action for action in toolbar.actions() if action.text() == text][0]
182191

183192

184193
class TestFileTranscriberWidget:

0 commit comments

Comments
 (0)