-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscribeWorker.py
More file actions
56 lines (48 loc) · 2.11 KB
/
Copy pathTranscribeWorker.py
File metadata and controls
56 lines (48 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from PySide6 import QtCore
import threading
import traceback
from AzureConversationTranscriber import AzureConversationTranscriber
import logging
logger = logging.getLogger(__name__)
class TranscribeWorker(QtCore.QObject):
"""
用來做語音轉文字的工作執行緒
在前端透過QThread啟動此工作者
"""
TranscribeWorker_Transcribed_Signal = QtCore.Signal(str)
TranscribeWorker_FinishedSignal = QtCore.Signal()
def __init__(self, transcriber : AzureConversationTranscriber):
super().__init__()
self.transcriber = transcriber
self._stop_event = threading.Event()
def run(self):
logger.info(f'TranscribeWorker.run() started.')
try:
# transcribe_from_mic_with_diarization must support on_transcribed and stop_event
self.transcriber.transcribe_from_mic_with_diarization(on_transcribed=self._emit, stop_event=self._stop_event)
except Exception:
logger.exception('Exception in TranscribeWorker.run():\n' + traceback.format_exc())
finally:
try:
self.TranscribeWorker_FinishedSignal.emit()
except Exception:
logger.exception('Failed to emit finished signal from TranscribeWorker.')
def _emit(self, text):
try:
self.TranscribeWorker_Transcribed_Signal.emit(text)
except Exception:
logger.exception('Failed to emit transcribed_signal from TranscribeWorker.')
def stop(self):
try:
self._stop_event.set()
logger.info('Stop event set in TranscribeWorker.')
except Exception:
logger.exception('Failed to set stop event in TranscribeWorker.')
try:
if hasattr(self.transcriber, 'stop_transcribing_async'):
try:
self.transcriber.stop_transcribing_async()
except Exception:
logger.exception('Failed to call stop_transcribing_async in TranscribeWorker.')
except Exception:
logger.exception('Failed to check or call stop_transcribing_async in TranscribeWorker.')