Skip to content

Commit

Permalink
Adding translations to transcription view
Browse files Browse the repository at this point in the history
  • Loading branch information
raivisdejus committed Jun 13, 2024
1 parent 41311d3 commit 4270942
Show file tree
Hide file tree
Showing 23 changed files with 400 additions and 75 deletions.
23 changes: 23 additions & 0 deletions buzz/assets/translate_black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion buzz/buzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
from typing import TextIO

from platformdirs import user_log_dir, user_cache_dir
from platformdirs import user_log_dir, user_cache_dir, user_data_dir

from buzz.assets import APP_BASE_DIR

Expand Down Expand Up @@ -60,6 +60,7 @@ def main():
logging.debug("app_dir: %s", APP_BASE_DIR)
logging.debug("log_dir: %s", log_dir)
logging.debug("cache_dir: %s", user_cache_dir("Buzz"))
logging.debug("data_dir: %s", user_data_dir("Buzz"))

app = Application(sys.argv)
parse_command_line(app)
Expand Down
15 changes: 15 additions & 0 deletions buzz/db/dao/transcription_segment_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ def get_segments(self, transcription_id: UUID) -> List[TranscriptionSegment]:
)
query.bindValue(":transcription_id", str(transcription_id))
return self._execute_all(query)

def update_segment_translation(self, segment_id: int, translation: str):
query = self._create_query()
query.prepare(
"""
UPDATE transcription_segment
SET translation = :translation
WHERE id = :id
"""
)

query.bindValue(":id", segment_id)
query.bindValue(":translation", translation)
if not query.exec():
raise Exception(query.lastError().text())
1 change: 1 addition & 0 deletions buzz/db/entity/transcription_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class TranscriptionSegment(Entity):
start_time: int
end_time: int
text: str
translation: str
transcription_id: str
id: int = -1
5 changes: 3 additions & 2 deletions buzz/db/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ def copy_transcriptions_from_json_to_sqlite(conn: Connection):
for segment in task.segments:
cursor.execute(
"""
INSERT INTO transcription_segment (end_time, start_time, text, transcription_id)
VALUES (?, ?, ?, ?);
INSERT INTO transcription_segment (end_time, start_time, text, translation, transcription_id)
VALUES (?, ?, ?, ?, ?);
""",
(
segment.end,
segment.start,
segment.text,
segment.translation,
transcription_id,
),
)
Expand Down
4 changes: 4 additions & 0 deletions buzz/db/service/transcription_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ def update_transcription_as_completed(self, id: UUID, segments: List[Segment]):
start_time=segment.start,
end_time=segment.end,
text=segment.text,
translation='',
transcription_id=str(id),
)
)

def get_transcription_segments(self, transcription_id: UUID):
return self.transcription_segment_dao.get_segments(transcription_id)

def update_segment_translation(self, segment_id: int, translation: str):
return self.transcription_segment_dao.update_segment_translation(segment_id, translation)
52 changes: 34 additions & 18 deletions buzz/locale/lv_LV/LC_MESSAGES/buzz.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-10 21:58+0300\n"
"PO-Revision-Date: 2024-06-10 21:59+0300\n"
"POT-Creation-Date: 2024-06-13 07:37+0300\n"
"PO-Revision-Date: 2024-06-13 07:38+0300\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: lv_LV\n"
Expand Down Expand Up @@ -140,11 +140,11 @@ msgstr "Gaida MI tulkojumu..."
msgid "Microphone:"
msgstr "Mikrofons:"

#: buzz/widgets/recording_transcriber_widget.py:382
#: buzz/widgets/recording_transcriber_widget.py:391
msgid "An error occurred while starting a new recording:"
msgstr "Sākot jaunu ierakstu notikusi kļūda:"

#: buzz/widgets/recording_transcriber_widget.py:386
#: buzz/widgets/recording_transcriber_widget.py:395
msgid ""
"Please check your audio devices or check the application logs for more "
"information."
Expand All @@ -161,8 +161,8 @@ msgid ""
"Detected missing permissions, please check that snap permissions have been "
"granted"
msgstr ""
"Ne visi nepieciešamie moduļi darbojas korekti, iespējams nav piešķirtas "
"snap atļaujas"
"Ne visi nepieciešamie moduļi darbojas korekti, iespējams nav piešķirtas snap "
"atļaujas"

#: buzz/widgets/snap_notice.py:16
msgid ""
Expand Down Expand Up @@ -205,12 +205,12 @@ msgstr ""
msgid "Select audio file"
msgstr "Izvēlieties audio failu"

#: buzz/widgets/main_window.py:278
#: buzz/widgets/main_window.py:280
#: buzz/widgets/preferences_dialog/models_preferences_widget.py:191
msgid "Error"
msgstr "Kļūda"

#: buzz/widgets/main_window.py:278
#: buzz/widgets/main_window.py:280
msgid "Unable to save OpenAI API key to keyring"
msgstr "Neizdevās saglabāt OpenAI API atslēgu atslēgu saišķī"

Expand All @@ -227,26 +227,42 @@ msgid "View"
msgstr "Skats"

#: buzz/widgets/transcription_viewer/transcription_view_mode_tool_button.py:27
#: buzz/widgets/transcription_viewer/transcription_segments_editor_widget.py:68
#: buzz/widgets/transcription_viewer/transcription_segments_editor_widget.py:95
msgid "Text"
msgstr "Teksts"

#: buzz/widgets/transcription_viewer/transcription_view_mode_tool_button.py:33
msgid "Timestamps"
msgstr "Laiks"

#: buzz/widgets/transcription_viewer/transcription_segments_editor_widget.py:66
#: buzz/widgets/transcription_viewer/transcription_segments_editor_widget.py:93
msgid "Start"
msgstr "Sākums"

#: buzz/widgets/transcription_viewer/transcription_segments_editor_widget.py:67
#: buzz/widgets/transcription_viewer/transcription_segments_editor_widget.py:94
msgid "End"
msgstr "Beigas"

#: buzz/widgets/transcription_viewer/transcription_viewer_widget.py:92
#: buzz/widgets/transcription_viewer/transcription_segments_editor_widget.py:96
msgid "Translation"
msgstr "Tulkojums"

#: buzz/widgets/transcription_viewer/transcription_viewer_widget.py:148
msgid "Export"
msgstr "Eksportēt"

#: buzz/widgets/transcription_viewer/transcription_viewer_widget.py:162
msgid "Translate"
msgstr "Tulkot"

#: buzz/widgets/transcription_viewer/transcription_viewer_widget.py:242
msgid "API Key Required"
msgstr "API atslēgas kļūda"

#: buzz/widgets/transcription_viewer/transcription_viewer_widget.py:243
msgid "Please enter OpenAI API Key in preferences"
msgstr "Lūdzu ievadiet OpenAI API atslēgu iestatījumos"

#: buzz/widgets/record_button.py:21
msgid "Stop"
msgstr "Apturēt"
Expand Down Expand Up @@ -301,20 +317,20 @@ msgstr "Papildu iestatījumi..."
msgid "Enter prompt..."
msgstr "Ievadiet vaicājumu..."

#: buzz/widgets/transcriber/transcription_options_group_box.py:79
#: buzz/widgets/transcriber/transcription_options_group_box.py:86
msgid "Model:"
msgstr "Modelis:"

#: buzz/widgets/transcriber/transcription_options_group_box.py:83
#: buzz/widgets/transcriber/transcription_options_group_box.py:90
msgid "Task:"
msgstr "Uzdevums:"

#: buzz/widgets/transcriber/transcription_options_group_box.py:84
#: buzz/widgets/transcriber/transcription_options_group_box.py:91
msgid "Language:"
msgstr "Valoda:"

#: buzz/widgets/transcriber/languages_combo_box.py:25
#: buzz/transcriber/transcriber.py:152
#: buzz/transcriber/transcriber.py:153
msgid "Detect Language"
msgstr "Noteikt valodu"

Expand Down Expand Up @@ -380,10 +396,10 @@ msgstr "OpenAI API atslēgas pārbaude"
#: buzz/widgets/preferences_dialog/general_preferences_widget.py:129
msgid ""
"Your API key is valid. Buzz will use this key to perform Whisper API "
"transcriptions."
"transcriptions and AI translations with ChatGPT."
msgstr ""
"Jūsu API atslēga ir derīga. Buzz izmantos to runas atpazīšanai ar Whisper "
"API."
"API un tulkošanai ar ChatGPT."

#: buzz/widgets/preferences_dialog/general_preferences_widget.py:156
msgid "Select Export Folder"
Expand Down
1 change: 1 addition & 0 deletions buzz/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CREATE TABLE transcription_segment (
end_time INT DEFAULT 0,
start_time INT DEFAULT 0,
text TEXT NOT NULL,
translation TEXT DEFAULT '',
transcription_id TEXT,
FOREIGN KEY (transcription_id) REFERENCES transcription(id) ON DELETE CASCADE
);
Expand Down
1 change: 1 addition & 0 deletions buzz/settings/shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __new__(cls, sequence: str, description: str):
OPEN_PREFERENCES_WINDOW = ("Ctrl+,", _("Open Preferences Window"))

VIEW_TRANSCRIPT_TEXT = ("Ctrl+E", _("View Transcript Text"))
VIEW_TRANSCRIPT_TRANSLATION = ("Ctrl+L", _("View Transcript Translation"))
VIEW_TRANSCRIPT_TIMESTAMPS = ("Ctrl+T", _("View Transcript Timestamps"))

CLEAR_HISTORY = ("Ctrl+S", _("Clear History"))
Expand Down
13 changes: 9 additions & 4 deletions buzz/transcriber/file_transcriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ def stop(self):


# TODO: Move to transcription service
def write_output(path: str, segments: List[Segment], output_format: OutputFormat):
def write_output(
path: str,
segments: List[Segment],
output_format: OutputFormat,
segment_key: str = 'text'
):
logging.debug(
"Writing transcription output, path = %s, output format = %s, number of segments = %s",
path,
Expand All @@ -114,7 +119,7 @@ def write_output(path: str, segments: List[Segment], output_format: OutputFormat
with open(path, "w", encoding="utf-8") as file:
if output_format == OutputFormat.TXT:
for i, segment in enumerate(segments):
file.write(segment.text)
file.write(getattr(segment, segment_key))
file.write("\n")

elif output_format == OutputFormat.VTT:
Expand All @@ -123,15 +128,15 @@ def write_output(path: str, segments: List[Segment], output_format: OutputFormat
file.write(
f"{to_timestamp(segment.start)} --> {to_timestamp(segment.end)}\n"
)
file.write(f"{segment.text}\n\n")
file.write(f"{getattr(segment, segment_key)}\n\n")

elif output_format == OutputFormat.SRT:
for i, segment in enumerate(segments):
file.write(f"{i + 1}\n")
file.write(
f'{to_timestamp(segment.start, ms_separator=",")} --> {to_timestamp(segment.end, ms_separator=",")}\n'
)
file.write(f"{segment.text}\n\n")
file.write(f"{getattr(segment, segment_key)}\n\n")

logging.debug("Written transcription output")

Expand Down
1 change: 1 addition & 0 deletions buzz/transcriber/transcriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Segment:
start: int # start time in ms
end: int # end time in ms
text: str
translation: str = ""


LANGUAGES = {
Expand Down
23 changes: 17 additions & 6 deletions buzz/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@
from buzz.settings.settings import Settings
from buzz.store.keyring_store import get_password, Key
from buzz.transcriber.transcriber import TranscriptionOptions
from buzz.widgets.transcriber.advanced_settings_dialog import AdvancedSettingsDialog


class Translator(QObject):
translation = pyqtSignal(str)
translation = pyqtSignal(str, int)
finished = pyqtSignal()
is_running = False

def __init__(
self,
transcription_options: TranscriptionOptions,
advanced_settings_dialog: AdvancedSettingsDialog,
parent: Optional[QObject] = None,
) -> None:
super().__init__(parent)

logging.debug(f"Translator init: {transcription_options}")

self.transcription_options = transcription_options
self.advanced_settings_dialog = advanced_settings_dialog
self.advanced_settings_dialog.transcription_options_changed.connect(
self.on_transcription_options_changed
)

self.queue = queue.Queue()

settings = Settings()
Expand All @@ -44,7 +51,7 @@ def start(self):

while self.is_running:
try:
transcript = self.queue.get(timeout=1)
transcript, transcript_id = self.queue.get(timeout=1)
except queue.Empty:
continue

Expand All @@ -64,13 +71,17 @@ def start(self):
logging.error(f"Translation error! Server response: {completion}")
next_translation = "Translation error, see logs!"

self.translation.emit(next_translation)
self.translation.emit(next_translation, transcript_id)

self.finished.emit()

def enqueue(self, transcript: str):
if self.is_running:
self.queue.put(transcript)
def on_transcription_options_changed(
self, transcription_options: TranscriptionOptions
):
self.transcription_options = transcription_options

def enqueue(self, transcript: str, transcript_id: Optional[int] = None):
self.queue.put((transcript, transcript_id))

def stop(self):
self.is_running = False
5 changes: 5 additions & 0 deletions buzz/widgets/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def __init__(self, parent: QWidget):
super().__init__(get_path("assets/file_download_black_24dp.svg"), parent)


class TranslateIcon(Icon):
def __init__(self, parent: QWidget):
super().__init__(get_path("assets/translate_black.svg"), parent)


class VisibilityIcon(Icon):
def __init__(self, parent: QWidget):
super().__init__(
Expand Down
2 changes: 2 additions & 0 deletions buzz/widgets/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def open_file_transcriber_widget(
self.on_openai_access_token_changed
)
file_transcriber_window.show()
file_transcriber_window.raise_()
file_transcriber_window.activateWindow()

@staticmethod
def on_openai_access_token_changed(access_token: str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def on_test_openai_api_key_success(self):
QMessageBox.information(
self,
_("OpenAI API Key Test"),
_("Your API key is valid. Buzz will use this key to perform Whisper API transcriptions."),
_("Your API key is valid. Buzz will use this key to perform Whisper API transcriptions and AI translations with ChatGPT."),
)

def on_test_openai_api_key_failure(self, error: str):
Expand Down
Loading

0 comments on commit 4270942

Please sign in to comment.