You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ms_client.py 2.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. import sys
  16. import grpc
  17. import numpy as np
  18. import ms_service_pb2
  19. import ms_service_pb2_grpc
  20. def run():
  21. if len(sys.argv) > 2:
  22. sys.exit("input error")
  23. channel_str = ""
  24. if len(sys.argv) == 2:
  25. split_args = sys.argv[1].split('=')
  26. if len(split_args) > 1:
  27. channel_str = split_args[1]
  28. else:
  29. channel_str = 'localhost:5500'
  30. else:
  31. channel_str = 'localhost:5500'
  32. channel = grpc.insecure_channel(channel_str)
  33. stub = ms_service_pb2_grpc.MSServiceStub(channel)
  34. request = ms_service_pb2.PredictRequest()
  35. x = request.data.add()
  36. x.tensor_shape.dims.extend([2, 2])
  37. x.tensor_type = ms_service_pb2.MS_FLOAT32
  38. x.data = (np.ones([2, 2]).astype(np.float32)).tobytes()
  39. y = request.data.add()
  40. y.tensor_shape.dims.extend([2, 2])
  41. y.tensor_type = ms_service_pb2.MS_FLOAT32
  42. y.data = (np.ones([2, 2]).astype(np.float32)).tobytes()
  43. try:
  44. result = stub.Predict(request)
  45. result_np = np.frombuffer(result.result[0].data, dtype=np.float32).reshape(result.result[0].tensor_shape.dims)
  46. print("ms client received: ")
  47. print(result_np)
  48. except grpc.RpcError as e:
  49. print(e.details())
  50. status_code = e.code()
  51. print(status_code.name)
  52. print(status_code.value)
  53. if __name__ == '__main__':
  54. run()