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_server.py 2.1 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. from concurrent import futures
  16. import time
  17. import grpc
  18. import numpy as np
  19. import ms_service_pb2
  20. import ms_service_pb2_grpc
  21. import test_cpu_lenet
  22. from mindspore import Tensor
  23. class MSService(ms_service_pb2_grpc.MSServiceServicer):
  24. def Predict(self, request, context):
  25. request_data = request.data
  26. request_label = request.label
  27. data_from_buffer = np.frombuffer(request_data.data, dtype=np.float32)
  28. data_from_buffer = data_from_buffer.reshape(request_data.tensor_shape.dims)
  29. data = Tensor(data_from_buffer)
  30. label_from_buffer = np.frombuffer(request_label.data, dtype=np.int32)
  31. label_from_buffer = label_from_buffer.reshape(request_label.tensor_shape.dims)
  32. label = Tensor(label_from_buffer)
  33. result = test_cpu_lenet.test_lenet(data, label)
  34. result_reply = ms_service_pb2.PredictReply()
  35. result_reply.result.tensor_shape.dims.extend(result.shape())
  36. result_reply.result.data = result.asnumpy().tobytes()
  37. return result_reply
  38. def serve():
  39. server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
  40. ms_service_pb2_grpc.add_MSServiceServicer_to_server(MSService(), server)
  41. server.add_insecure_port('[::]:50051')
  42. server.start()
  43. try:
  44. while True:
  45. time.sleep(60*60*24) # one day in seconds
  46. except KeyboardInterrupt:
  47. server.stop(0)
  48. if __name__ == '__main__':
  49. serve()