worker_offline.py 3.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jan  3 17:10:23 2018

@author: rbaraglia
"""
import os
import argparse
import thread
import logging
import json
import subprocess
import configparser
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
15
import re
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

from ws4py.client.threadedclient import WebSocketClient

#LOADING CONFIGURATION
worker_settings = configparser.ConfigParser()
worker_settings.read('worker.cfg')
SERVER_IP = worker_settings.get('server_params', 'server_ip')
SERVER_PORT = worker_settings.get('server_params', 'server_port')
SERVER_TARGET = worker_settings.get('server_params', 'server_target')
DECODER_COMMAND = worker_settings.get('worker_params', 'decoder_command')
TEMP_FILE_PATH = worker_settings.get('worker_params', 'temp_file_location')
PREPROCESSING = True if worker_settings.get('worker_params', 'preprocessing') == 'true' else False


class WorkerWebSocket(WebSocketClient):
    def __init__(self, uri):
        WebSocketClient.__init__(self, url=uri, heartbeat_freq=10)
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
33

34 35 36 37 38 39 40 41 42 43
    def opened(self):
        pass
    def guard_timeout(self):
        pass
    def received_message(self, m):
        try:
            json_msg = json.loads(str(m))
        except:
            logging.debug("Message received: %s" % str(m))
        else: 
44
            if 'uuid' in json_msg.keys():
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
45 46
                self.client_uuid = json_msg['uuid']
                self.fileName = self.client_uuid.replace('-', '')
47
                self.file = json_msg['file'].decode('base64')
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
48

Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
49
                with open('./wavs/'+self.fileName+'.wav', 'wb') as f:
50
                    f.write(self.file)
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
51
                logging.debug("FileName received: %s" % self.fileName)
52 53 54
                # TODO: preprocessing ? (sox python)
                if PREPROCESSING:
                    pass
55
                # Offline decoder call
56
                
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
57
                logging.debug(DECODER_COMMAND + ' ' + TEMP_FILE_PATH + self.fileName+'.wav')
58
                subprocess.call("cd scripts; ./decode.sh ../systems/models "+self.fileName+".wav", shell=True)
59
                
60
                # Check result
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
61 62
                if os.path.isfile('trans/decode_'+self.fileName+'.log'):
                    with open('trans/decode_'+self.fileName+'.log', 'r') as resultFile:
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
63
                        result = resultFile.read().strip()
64
                        logging.debug("Transcription is: %s" % result)
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
65 66 67 68
                        self.send_result(result)
                else:
                    logging.error("Worker Failed to create transcription file")
                    self.send_error("File was not created by worker")
69 70 71 72
                
                # Delete temporary files
                for file in os.listdir(TEMP_FILE_PATH):
                    os.remove(TEMP_FILE_PATH+file)
73 74 75 76 77

    def post(self, m):
        logging.debug('POST received')

    def send_result(self, result=None):
Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
78 79
        msg = json.dumps({u'uuid': self.client_uuid, u'transcription':result, u'trust_ind':u"0.1235"})
        self.client_uuid = None
80
        self.send(msg)
81

Rudy BARAGLIA's avatar
Rudy BARAGLIA committed
82 83 84
    def send_error(self, message):
        msg = json.dumps({u'uuid': self.client_uuid, u'error':message})
        self.send(msg)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    def closed(self, code, reason=None): 
        pass
    
    def finish_request(self):
        pass
    
    
def main():
    parser = argparse.ArgumentParser(description='Worker for linstt-dispatch')
    parser.add_argument('-u', '--uri', default="ws://"+SERVER_IP+":"+SERVER_PORT+SERVER_TARGET, dest="uri", help="Server<-->worker websocket URI")

    args = parser.parse_args()
    #thread.start_new_thread(loop.run, ())
    if not os.path.isdir(TEMP_FILE_PATH):
        os.mkdir(TEMP_FILE_PATH)
101
    print('#'*50)
102
    logging.basicConfig(level=logging.DEBUG, format="%(levelname)8s %(asctime)s %(message)s ")
103
    logging.info('Starting up worker')
104 105 106
    ws = WorkerWebSocket(args.uri)
    try:
        ws.connect()
107
        logging.info("Worker succefully connected to server at %s:%s" % (SERVER_IP, SERVER_PORT))
108 109 110 111 112 113 114
        ws.run_forever()
    except KeyboardInterrupt:
        ws.close()
    
    
if __name__ == '__main__':
    main()