test_smt_system.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import logging as Log
  2. import grpc,argparse,time,sys,threading
  3. import mllp_grpc.sim_mt_system_pb2_grpc as sim_mt_system_pb2_grpc
  4. import mllp_grpc.sim_mt_system_pb2 as sim_mt_system_pb2
  5. import mllp_grpc.sim_mt_common_pb2 as sim_mt_common_pb2
  6. import google.protobuf.empty_pb2 as empty_pb2
  7. class LazzyIterator:
  8. def __init__(self,i):
  9. self._i= i
  10. self._cv= threading.Condition()
  11. self._ready= False
  12. def __call__(self):
  13. with self._cv:
  14. if not self._ready:
  15. self._cv.wait()
  16. for v in self._i:
  17. yield v
  18. def ready(self):
  19. with self._cv:
  20. self._ready= True
  21. self._cv.notify()
  22. # end LazzyIterator
  23. def get_data(args):
  24. Log.info("Loading '%s'"%args.txt)
  25. Log.info('Sending data')
  26. with open(args.txt) as f:
  27. for l in f:
  28. l= l.strip().split()
  29. if l==[]: continue
  30. for w in l:
  31. o= sim_mt_common_pb2.TextPackage(words_novar=[w],
  32. words_var=[],
  33. eos=False)
  34. yield o
  35. o= sim_mt_common_pb2.TextPackage(words_novar=[],
  36. words_var=[],
  37. eos=True)
  38. yield o
  39. Log.info('Transmission completed')
  40. def parse_cmdline():
  41. p= argparse.ArgumentParser(description='Simultaneous MT GRPC client')
  42. p.add_argument('host',type=str,help='server host')
  43. p.add_argument('port',type=int,help='server port')
  44. p.add_argument('txt',type=str,help='txt file to translate')
  45. args= p.parse_args()
  46. return args
  47. def run(args):
  48. addr= '%s:%d'%(args.host,args.port)
  49. with grpc.insecure_channel(addr) as channel:
  50. stub= sim_mt_system_pb2_grpc.SimMTSystemStub(channel)
  51. info= stub.GetSystemInfo(empty_pb2.Empty())
  52. stub.SetEnabled(sim_mt_system_pb2.SetEnabledRequest(value=True))
  53. print(info)
  54. data= LazzyIterator(get_data(args))
  55. for o in stub.Translate(data()):
  56. if o.status.code==sim_mt_common_pb2.TranslateResponse.Status.READY:
  57. data.ready()
  58. continue
  59. if o.status.code!=sim_mt_common_pb2.TranslateResponse.Status.OK:
  60. print(o)
  61. else:
  62. for w in o.words_novar:
  63. sys.stdout.write(w+' ')
  64. if o.eos: sys.stdout.write('\n')
  65. sys.stdout.flush()
  66. print('------------------------------------------------')
  67. if __name__=='__main__':
  68. Log.basicConfig(level=Log.INFO)
  69. args= parse_cmdline()
  70. run(args)