forked from alphacep/vosk-api
-
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.
- Loading branch information
Showing
8 changed files
with
217 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import json | ||
import os | ||
import sys | ||
import asyncio | ||
import pathlib | ||
import websockets | ||
import logging | ||
|
||
from vosk import BatchRecognizer, GpuInit | ||
|
||
|
||
async def recognize(websocket, path): | ||
global args | ||
global loop | ||
global pool | ||
global rec | ||
global client_cnt | ||
|
||
uid = client_cnt | ||
client_cnt += 1 | ||
|
||
logging.info('Connection %d from %s', uid, websocket.remote_address); | ||
|
||
while True: | ||
|
||
message = await websocket.recv() | ||
|
||
if message == '{"eof" : 1}': | ||
rec.FinishStream(uid) | ||
break | ||
|
||
if isinstance(message, str) and 'config' in message: | ||
continue | ||
|
||
rec.AcceptWaveform(uid, message) | ||
await asyncio.sleep(len(message) / 16000.0 / 2) | ||
res = rec.Result(uid) | ||
if len(res) == 0: | ||
await websocket.send('{ "partial" : "" }') | ||
else: | ||
await websocket.send(res) | ||
|
||
rec.Wait() | ||
res = rec.Result(uid) | ||
await websocket.send(res) | ||
|
||
def start(): | ||
|
||
global rec | ||
global args | ||
global loop | ||
global client_cnt | ||
|
||
# Enable loging if needed | ||
# | ||
# logger = logging.getLogger('websockets') | ||
# logger.setLevel(logging.INFO) | ||
# logger.addHandler(logging.StreamHandler()) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
args = type('', (), {})() | ||
|
||
args.interface = os.environ.get('VOSK_SERVER_INTERFACE', '0.0.0.0') | ||
args.port = int(os.environ.get('VOSK_SERVER_PORT', 2700)) | ||
|
||
GpuInit() | ||
|
||
rec = BatchRecognizer() | ||
|
||
client_cnt = 0 | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
start_server = websockets.serve( | ||
recognize, args.interface, args.port) | ||
|
||
logging.info("Listening on %s:%d", args.interface, args.port) | ||
loop.run_until_complete(start_server) | ||
loop.run_forever() | ||
|
||
|
||
if __name__ == '__main__': | ||
start() |
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,28 +1,42 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from vosk import Model, BatchRecognizer, GpuInit, GpuThreadInit | ||
import sys | ||
import os | ||
import wave | ||
from time import sleep | ||
|
||
from vosk import Model, BatchRecognizer, GpuInit | ||
|
||
GpuInit() | ||
GpuThreadInit() | ||
|
||
rec = BatchRecognizer() | ||
|
||
fnames = open("tedlium.list").readlines() | ||
fds = [open(x.strip(), "rb") for x in fnames] | ||
ended = set() | ||
while True: | ||
|
||
for i, fd in enumerate(fds): | ||
if i in ended: | ||
continue | ||
data = fd.read(8000) | ||
if len(data) == 0: | ||
rec.FinishStream(i) | ||
ended.add(i) | ||
else: | ||
rec.AcceptWaveform(i, data) | ||
rec.Results() | ||
continue | ||
rec.AcceptWaveform(i, data) | ||
|
||
sleep(0.3) | ||
for i, fd in enumerate(fds): | ||
res = rec.Result(i) | ||
print (i, res) | ||
|
||
if len(ended) == len(fds): | ||
break | ||
|
||
sleep(20) | ||
print ("Done") | ||
for i, fd in enumerate(fds): | ||
res = rec.Result(i) | ||
print (i, res) | ||
rec.Wait() |
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
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