forked from verbio-technologies/python-verbio-speech-center
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Initialize GUI and monitor CSRClient activity through it.
- Loading branch information
rmarrugat
committed
Jan 2, 2025
1 parent
e49a3a4
commit 7d0188c
Showing
2 changed files
with
49 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |