asr_system_servicer.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. __all__= ['ASRSystemServicer','init']
  2. import grpc,threading,traceback
  3. import logging as Log
  4. import mllp_grpc.asr_system_pb2_grpc as asr_system_pb2_grpc
  5. import mllp_grpc.asr_system_pb2 as asr_system_pb2
  6. import mllp_grpc.asr_common_pb2 as asr_common_pb2
  7. import google.protobuf.empty_pb2 as empty_pb2
  8. def ii_(func,txt,context):
  9. Log.info('[%s] (%s) %s'%(context.peer(),func,txt))
  10. class ASRSystemServicer(asr_system_pb2_grpc.ASRSystemServicer):
  11. def __init__(self,system):
  12. self._sys= system
  13. def GetSystemInfo(self,request,context):
  14. def ii(txt): ii_('GetSystemInfo',txt,context)
  15. ii("Retrieving info from ASR system")
  16. langs=[asr_common_pb2.SystemInfo.Lang(code=x[0],
  17. text=x[1])
  18. for x in self._sys.lang]
  19. date= asr_common_pb2.SystemInfo.Date(day=self._sys.date[0],
  20. month=self._sys.date[1],
  21. year=self._sys.date[2])
  22. info= asr_common_pb2.SystemInfo(id=self._sys.name,
  23. langs=langs,
  24. tag=self._sys.tag,
  25. date=date)
  26. num_recos= self._sys.num_recos_available
  27. ret= asr_system_pb2.GetSystemInfoResponse(info=info,
  28. num_decoders=len(self._sys),
  29. num_decoders_available=num_recos,
  30. enabled=self._sys.enabled)
  31. return ret
  32. def Decode(self,request_iterator,context):
  33. def ii(txt): ii_('Decode',txt,context)
  34. def ee(code,txt):
  35. status= asr_common_pb2.DecodeResponse.Status(code=code,
  36. details=txt)
  37. return asr_common_pb2.DecodeResponse(status=status)
  38. # Check system selected
  39. # Check recogniser available
  40. reco= self._sys.get_reco()
  41. if reco is None:
  42. return ee(asr_common_pb2.DecodeResponse.Status.Code.ERR_NO_RECO_AVAILABLE,
  43. "no recogniser available for ASR system '%s'"%self._sys.name)
  44. code= asr_common_pb2.DecodeResponse.Status.Code.READY
  45. status= asr_common_pb2.DecodeResponse.Status(code=code)
  46. yield asr_common_pb2.DecodeResponse(status=status)
  47. ii('starting decoding with system: %s'%self._sys.name)
  48. # Create a thread to feed data
  49. def feed_data():
  50. try:
  51. for d in request_iterator:
  52. if d.HasField('token'):
  53. reco.feed(d.token)
  54. else:
  55. reco.feed(d.data)
  56. except Exception as e:
  57. traceback.print_exc()
  58. ii('[feed data] client closed connection, recognition stoped')
  59. reco.feed()
  60. t= threading.Thread(target=feed_data)
  61. t.start()
  62. # Process output
  63. try:
  64. for o in reco.output:
  65. if o.novar is None: # EOS or ERR
  66. if o.err==True: # Err
  67. yield ee(asr_common_pb2.DecodeResponse.Status.Code.ERR_RECO,
  68. "an unexpected error ocurred during recognition")
  69. else: pass # nothing on EOS
  70. else:
  71. val= asr_common_pb2.DecodeResponse.Status.Code.OK
  72. status= asr_common_pb2.DecodeResponse.Status(code=val)
  73. yield asr_common_pb2.DecodeResponse(status=status,
  74. hyp_novar=o.novar,
  75. hyp_var=o.var,
  76. score=o.score,
  77. nframes=o.nframes,
  78. eos=o.eos)
  79. except:
  80. traceback.print_exc()
  81. ii('[read output] client closed connection, recognition stoped')
  82. for o in reco.output:
  83. pass
  84. # Wait thread (should be stopped at this point) and return reco
  85. t.join()
  86. self._sys.append(reco)
  87. ii('decoding finished')
  88. # end Decode
  89. def SetEnabled(self,request,context):
  90. def ii(txt): ii_('SetEnabled',txt,context)
  91. ii("system %s"%('enabled' if request.value else 'disabled'))
  92. self._sys.set_enabled(request.value)
  93. ret= empty_pb2.Empty()
  94. return ret
  95. def init(server,system):
  96. asr_system_pb2_grpc.add_ASRSystemServicer_to_server(
  97. ASRSystemServicer(system),server)