Skip to content

Instantly share code, notes, and snippets.

@paolo-losi
Created November 13, 2019 18:13
Show Gist options
  • Save paolo-losi/75a307a243a58059dd7566872c6d7363 to your computer and use it in GitHub Desktop.
Save paolo-losi/75a307a243a58059dd7566872c6d7363 to your computer and use it in GitHub Desktop.
import os
import logging
import subprocess
import sys
import m3
import m3.lib.ws
from m3.lib.model import media_table
from m3.natlang.blockly import register_custom_toolbox
from pgaccess import pgsql
logger = logging.getLogger('deepspeech')
pool = m3.lib.ws.Pool('deepspeech')
@pgsql.transaction
def s2t(media_id):
devnull = open(os.devnull, 'w')
wav_name = os.path.expanduser('~/audio/%s.wav' % media_id)
data = media_table.select('content').where(id=media_id).scalar()
with open(wav_name, 'wb') as fout:
fout.write(data)
out = subprocess.Popen('/home/m3/deepspeech.env/bin/deepspeech '
'--model ~/italian/output_graph.pbmm '
'--lm ~/italian/lm.binary '
'--trie ~/italian/trie '
'--alphabet ~/italian/alphabet.txt '
'--audio %s' % wav_name,
stdout=subprocess.PIPE,
stderr=devnull,
shell=True).communicate()[0]
return out
@pool.delegate
def delegated_s2t(media_id):
return s2t(media_id)
def deepspeech():
last_msg = m3.session.received_message
if last_msg is None:
logger.error('last message not present')
return
media_id = last_msg.metadata.get('audio')
if media_id is None:
logger.error('last message has no audio')
return
txt = delegated_s2t(media_id)
return dict(txt=txt, media_id=media_id)
def main():
print s2t(sys.argv[1])
def init_m3():
register_custom_toolbox('deepspeech', deepspeech)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment