Skip to content

Commit 96bdd49

Browse files
committed
Vision AutoML updates + Translate AutoML
1 parent f4531a9 commit 96bdd49

File tree

5 files changed

+251
-13
lines changed

5 files changed

+251
-13
lines changed

translate/automl/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# AutoML Translate Sample
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=vision/beta/cloud-client/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
[Google Cloud Translate API][translate] provides feature AutoML.
7+
This API is part of the larger collection of Cloud Machine Learning APIs.
8+
9+
This sample Java application demonstrates how to access the Cloud Translate AutoML API
10+
using the [Google Cloud Client Library for Java][google-cloud-java].
11+
12+
## Set the environment variables
13+
14+
PROJECT_ID = [Id of the project]
15+
REGION_NAME = [Region name]
16+
17+
## Build the sample
18+
19+
Install [Maven](http://maven.apache.org/).
20+
21+
Build your project with:
22+
23+
```
24+
mvn clean package
25+
```
26+
27+
### Dataset API
28+
29+
#### Create a new dataset
30+
```
31+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.DatasetApi" -Dexec.args="create_dataset test_dataset"
32+
```
33+
34+
#### List datasets
35+
```
36+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.DatasetApi" -Dexec.args="list_datasets"
37+
```
38+
39+
#### Get dataset
40+
```
41+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.DatasetApi" -Dexec.args="get_dataset [dataset-id]"
42+
```
43+
44+
#### Import data
45+
```
46+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.DatasetApi" -Dexec.args="import_data gs://java-docs-samples-testing/flower_traindata.csv"
47+
```
48+
49+
### Model API
50+
51+
#### Create Model
52+
```
53+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.ModelApi" -Dexec.args="create_model test_model"
54+
```
55+
56+
#### List Models
57+
```
58+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.ModelApi" -Dexec.args="list_models"
59+
```
60+
61+
#### Get Model
62+
```
63+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.ModelApi" -Dexec.args="get_model [model-id]"
64+
```
65+
66+
#### List Model Evaluations
67+
```
68+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.ModelApi" -Dexec.args="list_model_evaluation [model-id]"
69+
```
70+
71+
#### Get Model Evaluation
72+
```
73+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.ModelApi" -Dexec.args="get_model_evaluation [model-id] [model-evaluation-id]"
74+
```
75+
76+
#### Delete Model
77+
```
78+
mvn exec:java-Dexec.mainClass="com.google.cloud.translate.samples.ModelApi" -Dexec.args="delete_model [model-id]"
79+
```
80+
### Predict API
81+
82+
```
83+
mvn exec:java -Dexec.mainClass="com.google.cloud.translate.samples.PredictApi" -Dexec.args="predict [model-id] ./resources/dandelion.jpg 0.7"
84+
```
85+
86+

translate/automl/pom.xml

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<!--
2+
Copyright 2016 Google Inc.
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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.google.cloud.translate.automl</groupId>
19+
<artifactId>translate-automl</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!--
23+
The parent pom defines common style checks and testing strategies for our samples.
24+
Removing or replacing it should not affect the execution of the samples in anyway.
25+
-->
26+
<parent>
27+
<groupId>com.google.cloud.samples</groupId>
28+
<artifactId>shared-configuration</artifactId>
29+
<version>1.0.9</version>
30+
</parent>
31+
32+
<properties>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
<maven.compiler.source>1.8</maven.compiler.source>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
</properties>
37+
38+
<dependencies>
39+
<!-- [START dependencies] -->
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>google-cloud-automl</artifactId>
43+
<version>0.55.0-beta</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>net.sourceforge.argparse4j</groupId>
47+
<artifactId>argparse4j</artifactId>
48+
<version>0.8.1</version>
49+
</dependency>
50+
<!-- [END dependencies] -->
51+
52+
<!-- Test dependencies -->
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<version>4.12</version>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.truth</groupId>
61+
<artifactId>truth</artifactId>
62+
<version>0.41</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
67+
<profiles>
68+
<profile>
69+
<id>DatasetApi</id>
70+
<activation>
71+
<property>
72+
<name>DatasetApi</name>
73+
</property>
74+
</activation>
75+
<build>
76+
<plugins>
77+
<plugin>
78+
<groupId>org.codehaus.mojo</groupId>
79+
<artifactId>exec-maven-plugin</artifactId>
80+
<version>1.6.0</version>
81+
<executions>
82+
<execution>
83+
<goals>
84+
<goal>java</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
<configuration>
89+
<mainClass>com.google.cloud.translate.automl.DatasetApi</mainClass>
90+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
91+
</configuration>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
</profile>
96+
<profile>
97+
<id>ModelApi</id>
98+
<activation>
99+
<property>
100+
<name>ModelApi</name>
101+
</property>
102+
</activation>
103+
<build>
104+
<plugins>
105+
<plugin>
106+
<groupId>org.codehaus.mojo</groupId>
107+
<artifactId>exec-maven-plugin</artifactId>
108+
<version>1.6.0</version>
109+
<executions>
110+
<execution>
111+
<goals>
112+
<goal>java</goal>
113+
</goals>
114+
</execution>
115+
</executions>
116+
<configuration>
117+
<mainClass>com.google.cloud.translate.automl.ModelApi</mainClass>
118+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
119+
</configuration>
120+
</plugin>
121+
</plugins>
122+
</build>
123+
</profile>
124+
<profile>
125+
<id>PredictionApi</id>
126+
<activation>
127+
<property>
128+
<name>PredictionApi</name>
129+
</property>
130+
</activation>
131+
<build>
132+
<plugins>
133+
<plugin>
134+
<groupId>org.codehaus.mojo</groupId>
135+
<artifactId>exec-maven-plugin</artifactId>
136+
<version>1.6.0</version>
137+
<executions>
138+
<execution>
139+
<goals>
140+
<goal>java</goal>
141+
</goals>
142+
</execution>
143+
</executions>
144+
<configuration>
145+
<mainClass>com.google.cloud.translate.automl.PredictionApi</mainClass>
146+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
147+
</configuration>
148+
</plugin>
149+
</plugins>
150+
</build>
151+
</profile>
152+
</profiles>
153+
</project>

translate/automl/resources/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tell me how this ends

vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import java.io.IOException;
3838
import java.io.PrintStream;
3939
import java.util.List;
40-
import java.util.concurrent.ExecutionException;
40+
4141
import net.sourceforge.argparse4j.ArgumentParsers;
4242
import net.sourceforge.argparse4j.inf.ArgumentParser;
4343
import net.sourceforge.argparse4j.inf.ArgumentParserException;
@@ -106,7 +106,7 @@ public static void createModel(
106106
/**
107107
* Demonstrates using the AutoML client to get operation status.
108108
*
109-
* @param operationFullId : Full name of a operation. For example, the name of your operation is
109+
* @param operationFullId the complete name of a operation. For example, the name of your operation is
110110
* projects/[projectId]/locations/us-central1/operations/[operationId].
111111
* @throws IOException on Input/Output errors.
112112
*/
@@ -124,8 +124,8 @@ public static void getOperationStatus(String operationFullId) throws IOException
124124
/**
125125
* Demonstrates using the AutoML client to list all models.
126126
*
127-
* @param projectId - Id of the project.
128-
* @param computeRegion - Region name.
127+
* @param projectId the Id of the project.
128+
* @param computeRegion the Region name.
129129
* @param filter - Filter expression.
130130
* @throws IOException on Input/Output errors.
131131
*/
@@ -137,14 +137,14 @@ public static void listModels(String projectId, String computeRegion, String fil
137137
LocationName projectLocation = LocationName.of(projectId, computeRegion);
138138

139139
// Create list models request
140-
ListModelsRequest listModlesRequest =
140+
ListModelsRequest listModelsRequest =
141141
ListModelsRequest.newBuilder()
142142
.setParent(projectLocation.toString())
143143
.setFilter(filter)
144144
.build();
145145

146146
System.out.println("List of models:");
147-
for (Model model : client.listModels(listModlesRequest).iterateAll()) {
147+
for (Model model : client.listModels(listModelsRequest).iterateAll()) {
148148
// Display the model information.
149149
System.out.println(String.format("Model name: %s", model.getName()));
150150
System.out.println(
@@ -278,7 +278,7 @@ public static void getModelEvaluation(
278278
* @param projectId the Id of the project.
279279
* @param computeRegion the Region name.
280280
* @param modelId the Id of the model.
281-
* @param filter the Filter expression.
281+
* @param filter the filter expression.
282282
* @throws IOException on Input/Output errors.
283283
*/
284284
public static void displayEvaluation(
@@ -353,7 +353,7 @@ public static void displayEvaluation(
353353
* @throws Exception on AutoML Client errors
354354
*/
355355
public static void deleteModel(String projectId, String computeRegion, String modelId)
356-
throws InterruptedException, ExecutionException, IOException {
356+
throws Exception {
357357
AutoMlClient client = AutoMlClient.create();
358358

359359
// Get the full path of the model.
@@ -367,13 +367,13 @@ public static void deleteModel(String projectId, String computeRegion, String mo
367367
// [END automl_vision_delete_model]
368368

369369
public static void main(String[] args)
370-
throws IOException, InterruptedException, ExecutionException {
370+
throws Exception {
371371
ModelApi modelApi = new ModelApi();
372372
modelApi.argsHelper(args, System.out);
373373
}
374374

375375
public static void argsHelper(String[] args, PrintStream out)
376-
throws IOException, InterruptedException, ExecutionException {
376+
throws Exception {
377377
ArgumentParser parser =
378378
ArgumentParsers.newFor("ModelApi")
379379
.build()
@@ -453,8 +453,6 @@ public static void argsHelper(String[] args, PrintStream out)
453453
}
454454
} catch (ArgumentParserException e) {
455455
parser.handleError(e);
456-
} catch (Exception e) {
457-
System.out.println("Exception in Model create caught");
458456
}
459457
}
460458
}

vision/automl/src/main/java/com/google/cloud/vision/samples/automl/PredictionApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void predict(
7575
String scoreThreshold)
7676
throws IOException {
7777

78-
// Create client for prediction service.
78+
// Instantiate client for prediction service.
7979
PredictionServiceClient predictionClient = PredictionServiceClient.create();
8080

8181
// Get the full path of the model.

0 commit comments

Comments
 (0)