Skip to content

Commit

Permalink
fix: use try-with-resources to close the client in doc/examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tadayosi committed Nov 28, 2024
1 parent df7fd26 commit 63fa0d2
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 141 deletions.
140 changes: 70 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ TensorFlowServingClient client = TensorFlowServingClient.builder()
To get the status of a model:

```java
TensorFlowServingClient client = TensorFlowServingClient.newInstance();

GetModelStatusRequest request = GetModelStatusRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.build();
GetModelStatusResponse response = client.getModelStatus(request);
System.out.println(response);
try (TensorFlowServingClient client = TensorFlowServingClient.newInstance()) {
GetModelStatusRequest request = GetModelStatusRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.build();
GetModelStatusResponse response = client.getModelStatus(request);
System.out.println(response);
}
```

Output:
Expand All @@ -88,16 +88,16 @@ model_version_status {
To get the metadata of a model:

```java
TensorFlowServingClient client = TensorFlowServingClient.newInstance();

GetModelMetadataRequest request = GetModelMetadataRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.addMetadataField("signature_def")) // metadata_field is mandatory
.build();
GetModelMetadataResponse response = client.getModelMetadata(request);
System.out.println(response);
try (TensorFlowServingClient client = TensorFlowServingClient.newInstance()) {
GetModelMetadataRequest request = GetModelMetadataRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.addMetadataField("signature_def")) // metadata_field is mandatory
.build();
GetModelMetadataResponse response = client.getModelMetadata(request);
System.out.println(response);
}
```

Output:
Expand All @@ -123,23 +123,23 @@ metadata {
To classify:

```java
TensorFlowServingClient client = TensorFlowServingClient.newInstance();

ClassificationRequest request = ClassificationRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123))
.setSignatureName("classify_x_to_y"))
.setInput(Input.newBuilder()
.setExampleList(ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build())))))
.build();
ClassificationResponse response = client.classify(request);
System.out.println(response);
try (TensorFlowServingClient client = TensorFlowServingClient.newInstance()) {
ClassificationRequest request = ClassificationRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123))
.setSignatureName("classify_x_to_y"))
.setInput(Input.newBuilder()
.setExampleList(ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build())))))
.build();
ClassificationResponse response = client.classify(request);
System.out.println(response);
}
```

Output:
Expand All @@ -166,23 +166,23 @@ model_spec {
To regress:

```java
TensorFlowServingClient client = TensorFlowServingClient.newInstance();

RegressionRequest request = RegressionRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123))
.setSignatureName("regress_x_to_y"))
.setInput(Input.newBuilder()
.setExampleList(ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build())))))
.build();
RegressionResponse response = client.regress(request);
System.out.println(response);
try (TensorFlowServingClient client = TensorFlowServingClient.newInstance()) {
RegressionRequest request = RegressionRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123))
.setSignatureName("regress_x_to_y"))
.setInput(Input.newBuilder()
.setExampleList(ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build())))))
.build();
RegressionResponse response = client.regress(request);
System.out.println(response);
}
```

Output:
Expand All @@ -207,23 +207,23 @@ model_spec {
To predict:

```java
TensorFlowServingClient client = TensorFlowServingClient.newInstance();

PredictRequest request = PredictRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.putInputs("x", TensorProto.newBuilder()
.setDtype(DataType.DT_FLOAT)
.setTensorShape(TensorShapeProto.newBuilder()
.addDim(Dim.newBuilder().setSize(3)))
.addFloatVal(1.0f)
.addFloatVal(2.0f)
.addFloatVal(5.0f)
.build())
.build();
PredictResponse response = client.predict(request);
System.out.println(response);
try (TensorFlowServingClient client = TensorFlowServingClient.newInstance()) {
PredictRequest request = PredictRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.putInputs("x", TensorProto.newBuilder()
.setDtype(DataType.DT_FLOAT)
.setTensorShape(TensorShapeProto.newBuilder()
.addDim(Dim.newBuilder().setSize(3)))
.addFloatVal(1.0f)
.addFloatVal(2.0f)
.addFloatVal(5.0f)
.build())
.build();
PredictResponse response = client.predict(request);
System.out.println(response);
}
```

Output:
Expand Down
35 changes: 18 additions & 17 deletions examples/classify.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@
public class classify {

public static void main(String... args) throws Exception {
var client = TensorFlowServingClient.newInstance();
var request = ClassificationRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123))
.setSignatureName("classify_x_to_y"))
.setInput(Input.newBuilder()
.setExampleList(ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build())))))
.build();
var response = client.classify(request);
System.out.println("Response>");
System.out.println(response);
try (var client = TensorFlowServingClient.newInstance()) {
var request = ClassificationRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123))
.setSignatureName("classify_x_to_y"))
.setInput(Input.newBuilder()
.setExampleList(ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build())))))
.build();
var response = client.classify(request);
System.out.println("Response>");
System.out.println(response);
}
}
}
23 changes: 12 additions & 11 deletions examples/model_metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
public class model_metadata {

public static void main(String... args) throws Exception {
var client = TensorFlowServingClient.newInstance();
var request = GetModelMetadataRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
// metadata_field is mandatory
.addMetadataField("signature_def")
.build();
var response = client.getModelMetadata(request);
System.out.println("Response>");
System.out.println(response);
try (var client = TensorFlowServingClient.newInstance()) {
var request = GetModelMetadataRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
// metadata_field is mandatory
.addMetadataField("signature_def")
.build();
var response = client.getModelMetadata(request);
System.out.println("Response>");
System.out.println(response);
}
}
}
19 changes: 10 additions & 9 deletions examples/model_status.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
public class model_status {

public static void main(String... args) throws Exception {
var client = TensorFlowServingClient.newInstance();
var request = GetModelStatusRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.build();
var response = client.getModelStatus(request);
System.out.println("Response>");
System.out.println(response);
try (var client = TensorFlowServingClient.newInstance()) {
var request = GetModelStatusRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.build();
var response = client.getModelStatus(request);
System.out.println("Response>");
System.out.println(response);
}
}
}
35 changes: 18 additions & 17 deletions examples/predict.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@
public class predict {

public static void main(String... args) throws Exception {
var client = TensorFlowServingClient.newInstance();
var request = PredictRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.putInputs("x", TensorProto.newBuilder()
.setDtype(DataType.DT_FLOAT)
.setTensorShape(TensorShapeProto.newBuilder()
.addDim(Dim.newBuilder().setSize(3)))
.addFloatVal(1.0f)
.addFloatVal(2.0f)
.addFloatVal(5.0f)
.build())
.build();
var response = client.predict(request);
System.out.println("Response>");
System.out.println(response);
try (var client = TensorFlowServingClient.newInstance()) {
var request = PredictRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123)))
.putInputs("x", TensorProto.newBuilder()
.setDtype(DataType.DT_FLOAT)
.setTensorShape(TensorShapeProto.newBuilder()
.addDim(Dim.newBuilder().setSize(3)))
.addFloatVal(1.0f)
.addFloatVal(2.0f)
.addFloatVal(5.0f)
.build())
.build();
var response = client.predict(request);
System.out.println("Response>");
System.out.println(response);
}
}
}
35 changes: 18 additions & 17 deletions examples/regress.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@
public class regress {

public static void main(String... args) throws Exception {
var client = TensorFlowServingClient.newInstance();
var request = RegressionRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123))
.setSignatureName("regress_x_to_y"))
.setInput(Input.newBuilder()
.setExampleList(ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build())))))
.build();
var response = client.regress(request);
System.out.println("Response>");
System.out.println(response);
try (var client = TensorFlowServingClient.newInstance()) {
var request = RegressionRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("half_plus_two")
.setVersion(Int64Value.of(123))
.setSignatureName("regress_x_to_y"))
.setInput(Input.newBuilder()
.setExampleList(ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build())))))
.build();
var response = client.regress(request);
System.out.println("Response>");
System.out.println(response);
}
}
}

0 comments on commit 63fa0d2

Please sign in to comment.