How to run Vosk on GPU?

I tried the following code:

#!/usr/bin/env python3

import sys
import json

from vosk import BatchModel, BatchRecognizer, GpuInit
from timeit import default_timer as timer

TOT_SAMPLES = 0

GpuInit()

model = BatchModel("model")

with open(sys.argv[1]) as fn:
    fnames = fn.readlines()
    fds = [open(x.strip(), "rb") for x in fnames]
    uids = [fname.strip().split("/")[-1][:-4] for fname in fnames]
    recs = [BatchRecognizer(model, 16000) for x in fnames]
    results = [""] * len(fnames)

ended = set()

start_time = timer()

while True:

    # Feed in the data
    for i, fd in enumerate(fds):
        if i in ended:
            continue
        data = fd.read(8000)
        if len(data) == 0:
            recs[i].FinishStream()
            ended.add(i)
            continue
        recs[i].AcceptWaveform(data)
        TOT_SAMPLES += len(data)

    # Wait for results from CUDA
    model.Wait()

    # Retrieve and add results
    for i, fd in enumerate(fds):
        res = recs[i].Result()
        if len(res) != 0:
            results[i] = results[i] + " " + json.loads(res)["text"]

    if len(ended) == len(fds):
        break

end_time = timer()

for i, res in enumerate(results):
    print(uids[i], res.strip())

print("Processed %.3f seconds of audio in %.3f seconds (%.3f xRT)"
    % (TOT_SAMPLES / 16000.0 / 2,
    end_time - start_time,
    (TOT_SAMPLES / 16000.0 / 2 / (end_time - start_time))),
    file=sys.stderr)

source: https://github.com/alphacep/vosk-api/blob/master/python/example/test_gpu_batch.py

but got:

C:\Users\user\PycharmProjects\vosk-test\.venv\Scripts\python.exe C:\Users\user\PycharmProjects\vosk-test\main.py 
Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\vosk-test\main.py", line 16, in <module>
    model = BatchModel("vosk-model-en-us-0.42-gigaspeech")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\PycharmProjects\vosk-test\.venv\Lib\site-packages\vosk\__init__.py", line 243, in __init__
    raise Exception("Failed to create a model")
Exception: Failed to create a model

Process finished with exit code 1

How to run Vosk on GPU using python?

Upvotes: 0

Views: 442

Answers (1)

Ga&#235;l Bost
Ga&#235;l Bost

Reputation: 1

For me, simply recompiling kaldi and vosk like in the Dockerfile of Vladimir link did the trick, don’t use the gpu fork branch but the main vosk.

Upvotes: 0

Related Questions