Skip to content

Commit

Permalink
feature: Initialize GUI and monitor CSRClient activity through it.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmarrugat committed Jan 2, 2025
1 parent e49a3a4 commit 7d0188c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
22 changes: 21 additions & 1 deletion cli-client/helpers/csr_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
import pause
import logging
import datetime
from typing import Optional
sys.path.insert(1, '../proto/generated')

import threading
from threading import Timer
from concurrent.futures import ThreadPoolExecutor
import recognition_streaming_request_pb2

from helpers.csr_gui import CsrGUI
from helpers.common import split_audio
from helpers.audio_importer import AudioImporter
from helpers.common import VerbioGrammar, RecognizerOptions
from helpers.compiled_grammar_processing import get_compiled_grammar


class CSRClient:
def __init__(self, executor: ThreadPoolExecutor, stub, options: RecognizerOptions, audio_resource: AudioImporter, token: str):
def __init__(self, executor: ThreadPoolExecutor, stub, options: RecognizerOptions, audio_resource: AudioImporter, token: str, gui: Optional[CsrGUI]):
self._executor = executor
self._stub = stub
self._resources = audio_resource
Expand All @@ -34,6 +36,7 @@ def __init__(self, executor: ThreadPoolExecutor, stub, options: RecognizerOption
self._diarization = options.diarization
self._hide_partial_results = options.hide_partial_results
self._label = options.label
self._gui = gui
self._messages = None

def _close_stream_by_inactivity(self):
Expand All @@ -45,6 +48,19 @@ def _start_inactivity_timer(self, inactivity_timeout: float):
self._inactivity_timer.start()

def _print_result(self, response):
if self._gui:
self._print_result_in_gui(response)
else:
self._print_result_in_logs(response)

def _print_result_in_gui(self, response):
transcript = response.result.alternatives[0].transcript
if response.result.is_final:
self._gui.add_final_transcript(transcript)
elif not self._hide_partial_results:
self._gui.add_partial_transcript(transcript)

def _print_result_in_logs(self, response):
if response.result.is_final:
transcript = "New incoming FINAL response:\n" \
f'\t"transcript": "{response.result.alternatives[0].transcript}",\n' \
Expand Down Expand Up @@ -96,13 +112,17 @@ def wait_for_response(self) -> bool:
return True

def __message_iterator(self):
if self._gui:
self._gui.start_progress_bar_task(total_audio_samples=self._resources.n_samples)
for message_type, message in self._messages:
logging.info("Sending streaming message " + message_type)
get_up_time = datetime.datetime.now()
if message_type == "audio":
sent_audio_samples = len(message.audio) // self._resources.sample_width
sent_audio_duration = sent_audio_samples / self._resources.sample_rate
get_up_time += datetime.timedelta(seconds=sent_audio_duration)
if self._gui:
self._gui.advance_progress_bar(advance=sent_audio_samples)
yield message
pause.until(get_up_time)
logging.info("All audio messages sent")
Expand Down
33 changes: 28 additions & 5 deletions cli-client/recognizer_stream.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,64 @@
#!/usr/bin/env python3
import sys
import logging
from typing import Optional
from rich.logging import RichHandler
sys.path.insert(1, '../proto/generated')

import grpc
import recognition_pb2_grpc
from concurrent.futures import ThreadPoolExecutor

from helpers.csr_gui import CsrGUI
from helpers.csr_client import CSRClient
from helpers.audio_importer import AudioImporter
from helpers.grpc_connection import GrpcConnection
from helpers.common import retrieve_token, parse_csr_commandline, RecognizerOptions


def process_recognition(executor: ThreadPoolExecutor, channel: grpc.Channel, options: RecognizerOptions, access_token: str):
def process_recognition(executor: ThreadPoolExecutor, channel: grpc.Channel, options: RecognizerOptions, access_token: str, gui: Optional[CsrGUI]):
audio_resource = AudioImporter(options.audio_file, options.convert_audio)
stub = recognition_pb2_grpc.RecognizerStub(channel)
client = CSRClient(executor, stub, options, audio_resource, access_token)
client = CSRClient(executor, stub, options, audio_resource, access_token, gui)
client.send_audio()
client.wait_for_response()
logging.info("Recognition finished")


def init_gui() -> CsrGUI:
gui = CsrGUI()
logging_handler = RichHandler(
console=gui._logging_console,
show_time=False,
show_level=False,
show_path=False
)
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s][%(levelname)s]:%(message)s',
handlers=[logging_handler],
force=True
)
gui.start()
return gui


def run(options: RecognizerOptions):
logging.info("Connecting to %s", command_line_options.host)
gui = init_gui() if options.gui else None
access_token = retrieve_token(command_line_options)
grpc_connection = GrpcConnection(options.secure_channel, options.client_id, options.client_secret, access_token)

with grpc_connection.open(options.host) as grpc_channel:
executor = ThreadPoolExecutor()
future = executor.submit(process_recognition, executor, grpc_channel, options, access_token)
future = executor.submit(process_recognition, executor, grpc_channel, options, access_token, gui)
future.result()

if gui:
gui.stop()


if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='[%(asctime)s][%(levelname)s]:%(message)s')
logging.info("Running speechcenter streaming...")
command_line_options = parse_csr_commandline()
command_line_options.check()
run(command_line_options)

0 comments on commit 7d0188c

Please sign in to comment.