Skip to content

Experimental support to Mozilla Deepspeech STT #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ register a project with access to the "Cloud Speech API." See Google's
speech-to-text demo site for more information:
https://cloud.google.com/speech-to-text/

### Deepspeech support (experimental)
vim-speech can utilize the Mozilla [deepspeech](https://github.com/mozilla/DeepSpeech)

You need to install deepspeech:
```
pip install deepspeech
```

And then download/train deepspeech model (and optionally the language model scorer). Then
let this plugin know about their location e.g.
```
export DEEPSPEECH_MODEL=<path to my deepspeech model e.g.deepspeech-0.9.3-models.pbmm>
export DEEPSPEECH_SCORER=<path to my deepspeech scorer e.g. deepspeech-0.9.3-models.scorer>
```

It may be helpful to finetune the pre-trained model with your own voice samples.
More info in [documentation](https://deepspeech.readthedocs.io/)

## Usage

Once you have figured out how to get everything installed, you can use the
Expand Down
4 changes: 2 additions & 2 deletions autoload/vim_speech.vim
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function! s:StartJobIfNeeded(buffer) abort
return
endif

if empty($GOOGLE_APPLICATION_CREDENTIALS)
throw 'GOOGLE_APPLICATION_CREDENTIALS is not set'
if empty($GOOGLE_APPLICATION_CREDENTIALS) && empty($DEEPSPEECH_MODEL)
throw 'Neither GOOGLE_APPLICATION_CREDENTIALS nor DEEPSPEECH_MODEL is set'
endif

let l:command = ale#Escape(g:vim_speech_dir . '/venv/bin/python')
Expand Down
23 changes: 20 additions & 3 deletions plugin/speech_to_text_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ def stop_recording(self):

return output_file.getvalue()

def transcribe_file_with_deepspeech(content):
from deepspeech import Model
import numpy as np

if not content:
return ''

ds = Model(os.environ.get('DEEPSPEECH_MODEL'))
scorer = os.environ.get('DEEPSPEECH_SCORER')
if scorer:
ds.enableExternalScorer(scorer)
numpy_content = np.frombuffer(content, dtype=np.int16)
transcribe = ds.stt(numpy_content)
return transcribe


def transcribe_file(content):
from google.cloud import speech
Expand Down Expand Up @@ -127,11 +142,13 @@ def stdin_has_data():


def main():

# Stop early if the environment variable isn't set.
if not os.environ.get('GOOGLE_APPLICATION_CREDENTIALS'):
if not os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') and not os.environ.get('DEEPSPEECH_MODEL'):
sys.exit(
'You must set GOOGLE_APPLICATION_CREDENTIALS'
' to your JSON credentials filename.'
'to your JSON credentials filename or DEEPSPEECH_MODEL'
'to trained deepspeech model.'
)

client = RecordingClient()
Expand All @@ -156,7 +173,7 @@ def main():
elif message == 'stop':
print_and_flush('record end')
audio_content = client.stop_recording()
print_and_flush('speech', transcribe_file(audio_content))
print_and_flush('speech',transcribe_file(audio_content) if os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') else transcribe_file_with_deepspeech(audio_content))
elif message == 'quit':
break

Expand Down