From 698dca29a2b6e31bf77719b80b8696cec5bb1414 Mon Sep 17 00:00:00 2001 From: xina Date: Tue, 22 Oct 2019 08:19:46 +0000 Subject: [PATCH] update --- kaldigstserver/client.py | 26 +++++++++++--------------- kaldigstserver/master_server.py | 2 +- kaldigstserver/worker.py | 9 ++++----- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/kaldigstserver/client.py b/kaldigstserver/client.py index 6fd2fbc..712bcc7 100644 --- a/kaldigstserver/client.py +++ b/kaldigstserver/client.py @@ -44,20 +44,20 @@ def send_data(self, data): self.send(data, binary=True) def opened(self): - #print "Socket opened!" + #print("Socket opened!") def send_data_to_ws(): if self.send_adaptation_state_filename is not None: - print >> sys.stderr, "Sending adaptation state from %s" % self.send_adaptation_state_filename + print("Sending adaptation state from " + self.send_adaptation_state_filename) try: adaptation_state_props = json.load(open(self.send_adaptation_state_filename, "r")) self.send(json.dumps(dict(adaptation_state=adaptation_state_props))) except: e = sys.exc_info()[0] - print >> sys.stderr, "Failed to send adaptation state: ", e + print("Failed to send adaptation state: " + e) with self.audiofile as audiostream: for block in iter(lambda: audiostream.read(int(self.byterate/4)), ""): self.send_data(block) - print >> sys.stderr, "Audio sent, now sending EOS" + print("Audio sent, now sending EOS") self.send("EOS") t = threading.Thread(target=send_data_to_ws) @@ -66,37 +66,33 @@ def send_data_to_ws(): def received_message(self, m): response = json.loads(str(m)) - #print >> sys.stderr, "RESPONSE:", response - #print >> sys.stderr, "JSON was:", m if response['status'] == 0: if 'result' in response: - trans = response['result']['hypotheses'][0]['transcript'].encode('utf-8') + trans = response['result']['hypotheses'][0]['transcript'] if response['result']['final']: - #print >> sys.stderr, trans, self.final_hyps.append(trans) - print >> sys.stderr, '\r%s' % trans.replace("\n", "\\n") + print(trans.replace("\n", "\\n"), end="\r") else: print_trans = trans.replace("\n", "\\n") if len(print_trans) > 80: print_trans = "... %s" % print_trans[-76:] - print >> sys.stderr, '\r%s' % print_trans, + print(print_trans, end="\r") if 'adaptation_state' in response: if self.save_adaptation_state_filename: - print >> sys.stderr, "Saving adaptation state to %s" % self.save_adaptation_state_filename + print("Saving adaptation state to " + self.save_adaptation_state_filename) with open(self.save_adaptation_state_filename, "w") as f: f.write(json.dumps(response['adaptation_state'])) else: - print >> sys.stderr, "Received error from server (status %d)" % response['status'] + print("Received error from server (status %d)" % response['status']) if 'message' in response: - print >> sys.stderr, "Error message:", response['message'] + print("Error message:" + response['message']) def get_full_hyp(self, timeout=60): return self.final_hyp_queue.get(timeout) def closed(self, code, reason=None): - #print "Websocket closed() called" - #print >> sys.stderr + #print("Websocket closed() called") self.final_hyp_queue.put(" ".join(self.final_hyps)) diff --git a/kaldigstserver/master_server.py b/kaldigstserver/master_server.py index bb33349..ae26fdb 100644 --- a/kaldigstserver/master_server.py +++ b/kaldigstserver/master_server.py @@ -271,7 +271,7 @@ def send_event(self, event): if len(event_str) > 100: event_str = event_str[:97] + "..." logging.info("%s: Sending event %s to client" % (self.id, event_str)) - self.write_message(json.dumps(event)) + self.write_message(json.dumps(event).replace('False', 'false').replace('\'', '\"')) def open(self): self.id = str(uuid.uuid4()) diff --git a/kaldigstserver/worker.py b/kaldigstserver/worker.py index f5e3271..00e82a2 100644 --- a/kaldigstserver/worker.py +++ b/kaldigstserver/worker.py @@ -127,7 +127,7 @@ def received_message(self, m): if 'adaptation_state' in props: as_props = props['adaptation_state'] if as_props.get('type', "") == "string+gzip+base64": - adaptation_state = zlib.decompress(base64.b64decode(as_props.get('value', ''))) + adaptation_state = zlib.decompress(base64.b64decode(as_props.get('value', ''))).decode("utf-8") logger.info("%s: Setting adaptation state to user-provided value" % (self.request_id)) self.decoder_pipeline.set_adaptation_state(adaptation_state) else: @@ -190,8 +190,7 @@ def _on_result(self, result, final): return self.last_partial_result = result logger.info("%s: Postprocessing (final=%s) result.." % (self.request_id, final)) - #processed_transcripts = yield self.post_process([result], blocking=False) - processed_transcripts = result + processed_transcripts = yield self.post_process([result], blocking=False) if processed_transcripts: logger.info("%s: Postprocessing done. [result: %s]" % (self.request_id, processed_transcripts[0])) event = dict(status=common.STATUS_SUCCESS, @@ -299,7 +298,7 @@ def send_adaptation_state(self): adaptation_state = self.decoder_pipeline.get_adaptation_state() event = dict(status=common.STATUS_SUCCESS, adaptation_state=dict(id=self.request_id, - value=base64.b64encode(zlib.compress(adaptation_state.encode())), + value=base64.b64encode(zlib.compress(adaptation_state.encode())).decode("utf-8"), type="string+gzip+base64", time=time.strftime("%Y-%m-%dT%H:%M:%S"))) try: @@ -319,7 +318,7 @@ def post_process(self, texts, blocking=False): else: timeout=0.0 try: - with (yield self.post_processor_lock.acquire(timeout)): + with (yield self.post_processor_lock.acquire()): result = [] for text in texts: self.post_processor.stdin.write("%s\n" % text)