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.

grpc_client.py 2.4 kB

2 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. """
  13. import os
  14. import grpc
  15. import base64
  16. import config as configs
  17. from logger import Logger
  18. from proto import inference_pb2_grpc, inference_pb2
  19. log = Logger().logger
  20. parser = configs.get_parser()
  21. args = parser.parse_args()
  22. _HOST = '127.0.0.1'
  23. _PORT = '8500'
  24. MAX_MESSAGE_LENGTH = 1024 * 1024 * 1024 # 可根据具体需求设置,此处设为1G
  25. def run():
  26. PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
  27. if args.enable_tls: # 使用tls加密通信
  28. with open(PROJECT_ROOT + '\\tls_crt\\server.crt', 'rb') as f:
  29. trusted_certs = f.read()
  30. credentials = grpc.ssl_channel_credentials(root_certificates=trusted_certs)
  31. channel = grpc.secure_channel(_HOST + ':' + _PORT, credentials, options=[
  32. ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
  33. ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH), ], ) # 创建连接
  34. else:
  35. channel = grpc.insecure_channel(_HOST + ':' + _PORT, options=[
  36. ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
  37. ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH), ], ) # 创建连接
  38. client = inference_pb2_grpc.InferenceServiceStub(channel=channel) # 创建客户端
  39. data_request = inference_pb2.DataRequest()
  40. data1 = data_request.data_list.add()
  41. data1.data_file = str(base64.b64encode(open("/usr/local/input/dog.jpg", "rb").read()), encoding='utf-8')
  42. data1.data_name = "dog.jpg"
  43. data2 = data_request.data_list.add()
  44. data2.data_file = str(base64.b64encode(open("/usr/local/input/6.jpg", "rb").read()), encoding='utf-8')
  45. data2.data_name = "6.jpg"
  46. response = client.inference(data_request)
  47. log.info(response.json_result.encode('utf-8').decode('unicode_escape'))
  48. if __name__ == '__main__':
  49. run()

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能