Created
November 13, 2019 18:13
-
-
Save paolo-losi/e044ffe55c033a91142c6037ec664b52 to your computer and use it in GitHub Desktop.
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
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