Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
naxingyu committed Oct 22, 2019
1 parent c0913dc commit 698dca2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
26 changes: 11 additions & 15 deletions kaldigstserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))


Expand Down
2 changes: 1 addition & 1 deletion kaldigstserver/master_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('\'', '\"'))

This comment has been minimized.

Copy link
@dimastro

dimastro Dec 1, 2020

replace('\'', '\"')
Is there a particular reason why this is here? It seems to be causing parsing errors
ex.

{"word": "doesn"t", "length": 0.62,.......
ERROR:websocket:error.     : Expecting ',' delimiter

thanks


def open(self):
self.id = str(uuid.uuid4())
Expand Down
9 changes: 4 additions & 5 deletions kaldigstserver/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 698dca2

Please sign in to comment.