Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions speech_recognition/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import tempfile
import zipfile
from pathlib import Path
from urllib.request import urlopen

from tqdm import tqdm
Expand Down Expand Up @@ -51,10 +52,11 @@ def main(argv=None) -> None:
"--url",
default="https://alphacephei.com/vosk/models/vosk-model-small-en-us-0.15.zip",
)
vosk_parser.add_argument("--dir", default="model")

def _download_vosk(args):
download_vosk_model(args.url, args.dir)
download_vosk_model(
args.url, str(Path(__file__).parent / "models" / "vosk")
)

vosk_parser.set_defaults(func=_download_vosk)

Expand Down
2 changes: 2 additions & 0 deletions speech_recognition/models/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
14 changes: 10 additions & 4 deletions speech_recognition/recognizers/vosk.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import json
import os
from pathlib import Path
from typing import TYPE_CHECKING, Literal, TypedDict, Union, cast, overload

from speech_recognition.exceptions import SetupError

if TYPE_CHECKING:
from speech_recognition.audio import AudioData

Expand Down Expand Up @@ -38,11 +40,15 @@ def recognize(

from vosk import KaldiRecognizer, Model

if not os.path.exists("model"):
return "Please download the model from https://alphacephei.com/vosk/models and unpack as 'model' in the current folder."
vosk_model_path = Path(__file__).parent.parent / "models" / "vosk"
if not vosk_model_path.exists():
raise SetupError(
"Vosk model not found. "
"Please download the model using `sprc download vosk` command."
)

SAMPLE_RATE = 16_000
rec = KaldiRecognizer(Model("model"), SAMPLE_RATE)
rec = KaldiRecognizer(Model(str(vosk_model_path)), SAMPLE_RATE)

rec.AcceptWaveform(
audio_data.get_raw_data(convert_rate=SAMPLE_RATE, convert_width=2)
Expand Down
Loading