-
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.
* Update README.md * Add queueing * Move default properties to properties.py * Reorganize * Make worker cnt configurable * Update README.md
- Loading branch information
1 parent
3a7826e
commit 75fbf09
Showing
8 changed files
with
156 additions
and
84 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 |
---|---|---|
|
@@ -16,4 +16,5 @@ vosk: | |
lang: 'de' | ||
whisper: | ||
model: 'tiny' | ||
max_workers: 12 | ||
max_threads: 12 | ||
cnt_workers: 3 |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Implements gRPC Client facade""" | ||
|
||
import logging | ||
import grpc | ||
import subtitles_pb2_grpc, subtitles_pb2 | ||
from grpc._channel import _InactiveRpcError | ||
|
||
|
||
def receive(receiver: str, req: subtitles_pb2.ReceiveRequest): | ||
with grpc.insecure_channel(receiver) as channel: | ||
stub = subtitles_pb2_grpc.SubtitleReceiverStub(channel) | ||
try: | ||
stub.Receive(req) | ||
except _InactiveRpcError as grpc_err: | ||
logging.error(grpc_err.details()) | ||
except Exception as err: | ||
logging.error(err) |
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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import queue | ||
|
||
from tasks import StopTask | ||
|
||
|
||
class TaskQueue(queue.Queue): | ||
def __init__(self, task_worker_cnt: int = 1): | ||
super().__init__() | ||
self.task_worker_cnt = task_worker_cnt | ||
|
||
def stop(self): | ||
for _ in range(self.task_worker_cnt): | ||
self.put(StopTask()) |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class GenerationTask: | ||
source: str | ||
language: str | ||
stream_id: str | ||
|
||
|
||
class StopTask: | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import logging | ||
from concurrent.futures import ThreadPoolExecutor | ||
import subtitles_pb2 | ||
from tasks import GenerationTask, StopTask | ||
from taskqueue import TaskQueue | ||
from transcriber import Transcriber | ||
from client import receive | ||
|
||
|
||
class Worker: | ||
"""Thread for subtitle generation""" | ||
|
||
def __init__(self, | ||
transcriber: Transcriber, | ||
receiver: str, | ||
executor: ThreadPoolExecutor, | ||
taskqueue: TaskQueue): | ||
"""Start the generator threads.""" | ||
executor.submit(run, transcriber, receiver, taskqueue) | ||
|
||
|
||
def run(transcriber: Transcriber, receiver: str, taskqueue: TaskQueue) -> None: | ||
while True: | ||
logging.info('worker: waiting for task...') | ||
task = taskqueue.get() | ||
if isinstance(task, GenerationTask): | ||
logging.info('worker: starting to generate subtitles...') | ||
logging.debug(f'worker: task: {task}') | ||
generate(transcriber, receiver, task) | ||
elif isinstance(task, StopTask): | ||
break | ||
|
||
|
||
def generate(transcriber: Transcriber, receiver: str, task: GenerationTask) -> None: | ||
subtitles, language = transcriber.generate(task.source, task.language) | ||
|
||
logging.info(f'worker: sending receive message to receiver @ {receiver}') | ||
receive(receiver, | ||
req=subtitles_pb2.ReceiveRequest( | ||
stream_id=task.stream_id, | ||
subtitles=subtitles, | ||
language=language)) |