TensorFlow Serving C++ API Documentation
servable.cc
1 /* Copyright 2023 Google Inc. All Rights Reserved.
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 
16 #include "tensorflow_serving/servables/tensorflow/servable.h"
17 
18 #include <utility>
19 
20 #include "absl/functional/any_invocable.h"
21 #include "absl/status/status.h"
22 #include "tensorflow_serving/apis/predict.pb.h"
23 
24 namespace tensorflow {
25 namespace serving {
26 
27 bool Servable::SupportsPaging() const { return false; }
28 
29 absl::Status Servable::Suspend() {
30  return absl::UnimplementedError("paging not supported");
31 }
32 
33 absl::Status Servable::Resume() {
34  return absl::UnimplementedError("paging not supported");
35 }
36 
37 EmptyServable::EmptyServable()
38  : Servable(/*name=*/"", /*version=*/0),
39  error_(absl::FailedPreconditionError("No models loaded")) {}
40 
41 SingleRequestPredictStreamedContext::SingleRequestPredictStreamedContext(
42  absl::AnyInvocable<absl::Status(const PredictRequest&)> f)
43  : f_(std::move(f)) {}
44 
45 absl::Status SingleRequestPredictStreamedContext::ProcessRequest(
46  const PredictRequest& request) {
47  if (one_request_received_) {
48  return absl::UnimplementedError(
49  "PredictStreamed already received one request. Accepting more than "
50  "one request in a stream is not supported yet");
51  }
52  one_request_received_ = true;
53  return f_(request);
54 }
55 
56 absl::Status SingleRequestPredictStreamedContext::Close() {
57  if (!one_request_received_) {
58  return absl::FailedPreconditionError(
59  "PredictStreamed requires at least one request");
60  }
61  return absl::OkStatus();
62 }
63 
64 } // namespace serving
65 } // namespace tensorflow