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.cc 3.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <grpcpp/grpcpp.h>
  17. #include <iostream>
  18. #include "serving/ms_service.grpc.pb.h"
  19. using grpc::Channel;
  20. using grpc::ClientContext;
  21. using grpc::Status;
  22. using ms_serving::MSService;
  23. using ms_serving::PredictReply;
  24. using ms_serving::PredictRequest;
  25. using ms_serving::Tensor;
  26. using ms_serving::TensorShape;
  27. class MSClient {
  28. public:
  29. explicit MSClient(std::shared_ptr<Channel> channel) : stub_(MSService::NewStub(channel)) {}
  30. std::string Predict(const std::string &user) {
  31. // Data we are sending to the server.
  32. PredictRequest request;
  33. Tensor data;
  34. TensorShape shape;
  35. shape.add_dims(1);
  36. shape.add_dims(1);
  37. shape.add_dims(2);
  38. shape.add_dims(2);
  39. *data.mutable_tensor_shape() = shape;
  40. data.set_tensor_type(ms_serving::MS_FLOAT32);
  41. vector<float> input_data{1.1, 2.1, 3.1, 4.1};
  42. data.set_data(input_data.data(), input_data.size());
  43. *request.add_data() = data;
  44. *request.add_data() = data;
  45. // Container for the data we expect from the server.
  46. PredictReply reply;
  47. // Context for the client. It could be used to convey extra information to
  48. // the server and/or tweak certain RPC behaviors.
  49. ClientContext context;
  50. // The actual RPC.
  51. Status status = stub_->Predict(&context, request, &reply);
  52. // Act upon its status.
  53. if (status.ok()) {
  54. return "RPC OK";
  55. } else {
  56. std::cout << status.error_code() << ": " << status.error_message() << std::endl;
  57. return "RPC failed";
  58. }
  59. }
  60. private:
  61. std::unique_ptr<MSService::Stub> stub_;
  62. };
  63. int main(int argc, char **argv) {
  64. // Instantiate the client. It requires a channel, out of which the actual RPCs
  65. // are created. This channel models a connection to an endpoint specified by
  66. // the argument "--target=" which is the only expected argument.
  67. // We indicate that the channel isn't authenticated (use of
  68. // InsecureChannelCredentials()).
  69. std::string target_str;
  70. std::string arg_str("--target");
  71. if (argc > 1) {
  72. std::string arg_val = argv[1];
  73. size_t start_pos = arg_val.find(arg_str);
  74. if (start_pos != std::string::npos) {
  75. start_pos += arg_str.size();
  76. if (arg_val[start_pos] == '=') {
  77. target_str = arg_val.substr(start_pos + 1);
  78. } else {
  79. std::cout << "The only correct argument syntax is --target=" << std::endl;
  80. return 0;
  81. }
  82. } else {
  83. std::cout << "The only acceptable argument is --target=" << std::endl;
  84. return 0;
  85. }
  86. } else {
  87. target_str = "localhost:85010";
  88. }
  89. MSClient client(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
  90. string request;
  91. string reply = client.Predict(request);
  92. std::cout << "client received: " << reply << std::endl;
  93. return 0;
  94. }