test_smt_server.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import logging as Log
  2. import grpc,argparse,time,sys,threading
  3. import mllp_grpc.sim_mt_pb2_grpc as sim_mt_pb2_grpc
  4. import mllp_grpc.sim_mt_pb2 as sim_mt_pb2
  5. import mllp_grpc.sim_mt_common_pb2 as sim_mt_common_pb2
  6. import google.protobuf.empty_pb2 as empty_pb2
  7. def get_data(args,sys_id):
  8. Log.info("Loading '%s'"%args.txt)
  9. Log.info('Sending data')
  10. yield sim_mt_pb2.TranslateRequest(system_id=sys_id)
  11. with open(args.txt) as f:
  12. for l in f:
  13. l= l.strip().split()
  14. if l==[]: continue
  15. for w in l:
  16. data= sim_mt_common_pb2.TextPackage(words_novar=[w],
  17. words_var=[],
  18. eos=False)
  19. o= sim_mt_pb2.TranslateRequest(text=data)
  20. yield o
  21. data= sim_mt_common_pb2.TextPackage(words_novar=[],
  22. words_var=[],
  23. eos=True)
  24. o= sim_mt_pb2.TranslateRequest(text=data)
  25. yield o
  26. Log.info('Transmission completed')
  27. def parse_cmdline():
  28. p= argparse.ArgumentParser(description='Simultaneous MT GRPC client')
  29. p.add_argument('host',type=str,help='server host')
  30. p.add_argument('port',type=int,help='server port')
  31. p.add_argument('txt',type=str,help='input text file')
  32. args= p.parse_args()
  33. return args
  34. def run(args):
  35. addr= '%s:%d'%(args.host,args.port)
  36. with grpc.insecure_channel(addr) as channel:
  37. stub= sim_mt_pb2_grpc.SimMTStub(channel)
  38. for info in stub.GetSystemsInfo(empty_pb2.Empty()):
  39. print(info)
  40. print_host= True
  41. for o in stub.Translate(get_data(args,0)):
  42. if print_host:
  43. print_host= False
  44. print(o.host_info)
  45. if o.status.code!=sim_mt_common_pb2.TranslateResponse.Status.Code.OK:
  46. print(o)
  47. else:
  48. for w in o.words_novar:
  49. sys.stdout.write(w+' ')
  50. if o.eos: sys.stdout.write('\n')
  51. sys.stdout.flush()
  52. if __name__=='__main__':
  53. Log.basicConfig(level=Log.INFO)
  54. args= parse_cmdline()
  55. run(args)