Skip to content

Commit 1e40224

Browse files
authored
Merge branch 'master' into ssml-tutorial
2 parents d2c56b3 + eb80831 commit 1e40224

File tree

10 files changed

+357
-3
lines changed

10 files changed

+357
-3
lines changed

.kvgbot.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ assign_issues:
33
- dzlier-gcp
44
- gguuss
55
- kurtisvg
6-
- lesv
76
- nnegrey
87
assign_prs:
98
- dzlier-gcp

dialogflow/cloud-client/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
public class KnowledgeBaseManagementIT {
5050

5151
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
52-
private static String TEST_KNOWLEDGE_BASE_ID = "MTUwMTg2NzM1MjY0OTAwMDU1MDQ";
53-
private static String TEST_DOCUMENT_ID = "MTE4MTI3OTY2ODcwNTc5NDQ1NzY";
52+
private static String TEST_KNOWLEDGE_BASE_ID = "MTA4MzE0ODY5NTczMTQzNzU2ODA";
53+
private static String TEST_DOCUMENT_ID = "MTUwNjk0ODg1NTU4NzkzMDExMg";
5454
private static String SESSION_ID = "fake_session_for_testing";
5555
private static String LANGUAGE_CODE = "en-US";
5656
private static String KNOWLEDGE_BASE_NAME = "fake_knowledge_base_name";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
17+
// [START functions_helloworld_background]
18+
import com.google.gson.Gson;
19+
import com.google.gson.JsonObject;
20+
import java.io.IOException;
21+
import java.io.PrintWriter;
22+
import java.util.logging.Logger;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
26+
public class HelloBackground {
27+
28+
// Use GSON (https://github.com/google/gson) to parse JSON content.
29+
private Gson gsonParser = new Gson();
30+
31+
// Background Cloud Function.
32+
public void helloBackground(HttpServletRequest request, HttpServletResponse response)
33+
throws IOException {
34+
String contentType = request.getContentType();
35+
String name = "World";
36+
JsonObject body = gsonParser.fromJson(request.getReader(), JsonObject.class);
37+
if (body.has("name")) {
38+
name = body.get("name").getAsString();
39+
}
40+
PrintWriter writer = response.getWriter();
41+
writer.write(String.format("Hello %s!", name));
42+
}
43+
}
44+
45+
// [END functions_helloworld_background]

functions/snippets/src/test/java/SnippetsTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,14 @@ public void retrieveLogsTest() throws IOException {
149149

150150
assertThat(responseOut.toString(), containsString("Logs retrieved successfully."));
151151
}
152+
153+
@Test
154+
public void helloBackgroundTest() throws IOException {
155+
when(request.getContentType()).thenReturn("application/json");
156+
BufferedReader bodyReader = new BufferedReader(new StringReader("{\"name\":\"John\"}"));
157+
when(request.getReader()).thenReturn(bodyReader);
158+
159+
new HelloBackground().helloBackground(request, response);
160+
assertThat(responseOut.toString(), containsString("Hello John!"));
161+
}
152162
}

vision/spring-framework/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Cloud Vision on Spring Boot
2+
3+
This sample demonstrates leveraging Cloud Vision APIs within a Spring Boot application by using
4+
[Spring Cloud GCP libraries](https://github.com/spring-cloud/spring-cloud-gcp).
5+
6+
The Spring Cloud GCP libraries for Cloud Vision offer [Spring Auto-configuration classes](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html)
7+
and convenience libraries to allow you to more quickly start using Cloud Vision in Spring.
8+
9+
## Overview
10+
11+
This sample starts a [Spring Boot](https://spring.io/projects/spring-boot) application
12+
containing a simple web interface that allows you submit image URLs and get the image's text or
13+
classification labels through the Google Cloud Vision APIs.
14+
15+
This application is built and run with [Maven](https://maven.apache.org/), a tool for building and
16+
managing dependencies in Java projects.
17+
18+
## Build and Run
19+
20+
1. **Follow the set-up instructions in [the documentation](https://cloud.google.com/java/docs/setup).**
21+
22+
2. Enable the Google Cloud Vision APIs for your Google Cloud Platform project.
23+
[Click here](https://console.cloud.google.com/flows/enableapi?apiid=vision.googleapis.com)
24+
to visit Cloud Platform Console and enable the Google Cloud Vision APIs.
25+
26+
3. After following step 1, you should have the `gcloud` command line tool installed.
27+
Open up a terminal and run the command `gcloud auth application-default login`.
28+
This will provide the account authentication necessary to run the application.
29+
30+
4. In this directory, run the following Maven command to start the Spring Boot application.
31+
```
32+
mvn clean spring-boot:run
33+
```
34+
35+
5. After running the command, the application can be visited at http://localhost:8080/.

vision/spring-framework/pom.xml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!--
2+
Copyright 2019 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+
19+
<properties>
20+
<maven.compiler.target>1.8</maven.compiler.target>
21+
<maven.compiler.source>1.8</maven.compiler.source>
22+
<spring.version>2.1.6.RELEASE</spring.version>
23+
</properties>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<groupId>com.google.cloud.samples</groupId>
31+
<artifactId>shared-configuration</artifactId>
32+
<version>1.0.11</version>
33+
</parent>
34+
35+
<groupId>com.example.vision</groupId>
36+
<artifactId>spring-framework</artifactId>
37+
<name>Spring Framework with Cloud Vision Sample</name>
38+
39+
<!-- [START spring_vision_bom_fragment] -->
40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<groupId>org.springframework.cloud</groupId>
44+
<artifactId>spring-cloud-gcp-dependencies</artifactId>
45+
<version>1.1.2.RELEASE</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
<!-- [END spring_vision_bom_fragment] -->
52+
53+
<dependencies>
54+
<!-- [START spring_vision_dependency_fragment] -->
55+
<dependency>
56+
<groupId>org.springframework.cloud</groupId>
57+
<artifactId>spring-cloud-gcp-starter-vision</artifactId>
58+
</dependency>
59+
<!-- [END spring_vision_dependency_fragment] -->
60+
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-web</artifactId>
64+
<version>${spring.version}</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
70+
<version>${spring.version}</version>
71+
</dependency>
72+
</dependencies>
73+
74+
<build>
75+
<plugins>
76+
<plugin>
77+
<groupId>org.springframework.boot</groupId>
78+
<artifactId>spring-boot-maven-plugin</artifactId>
79+
<version>${spring.version}</version>
80+
<configuration>
81+
<mainClass>com.example.vision.Application</mainClass>
82+
</configuration>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2019 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+
17+
package com.example.vision;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
/**
23+
* Entry point to running the Spring Boot application.
24+
*/
25+
@SpringBootApplication
26+
public class Application {
27+
28+
public static void main(String[] args) {
29+
SpringApplication.run(Application.class, args);
30+
}
31+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2019 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+
17+
package com.example.vision;
18+
19+
import com.google.cloud.vision.v1.AnnotateImageResponse;
20+
import com.google.cloud.vision.v1.EntityAnnotation;
21+
import com.google.cloud.vision.v1.Feature.Type;
22+
import java.util.LinkedHashMap;
23+
import java.util.Map;
24+
import java.util.stream.Collectors;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.cloud.gcp.vision.CloudVisionTemplate;
27+
import org.springframework.core.io.ResourceLoader;
28+
import org.springframework.ui.ModelMap;
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.RestController;
31+
import org.springframework.web.servlet.ModelAndView;
32+
33+
/**
34+
* Code sample demonstrating Cloud Vision usage within the context of Spring Framework using
35+
* Spring Cloud GCP libraries. The sample is written as a Spring Boot application to demonstrate
36+
* a practical application of this usage.
37+
*/
38+
@RestController
39+
public class VisionController {
40+
41+
@Autowired
42+
private ResourceLoader resourceLoader;
43+
44+
// [START spring_vision_autowire]
45+
@Autowired
46+
private CloudVisionTemplate cloudVisionTemplate;
47+
// [END spring_vision_autowire]
48+
49+
/**
50+
* This method downloads an image from a URL and sends its contents
51+
* to the Vision API for label detection.
52+
*
53+
* @param imageUrl the URL of the image
54+
* @param map the model map to use
55+
* @return a string with the list of labels and percentage of certainty
56+
*/
57+
@GetMapping("/extractLabels")
58+
public ModelAndView extractLabels(String imageUrl, ModelMap map) {
59+
//[START spring_vision_image_labelling]
60+
AnnotateImageResponse response = this.cloudVisionTemplate.analyzeImage(
61+
this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);
62+
63+
Map<String, Float> imageLabels =
64+
response.getLabelAnnotationsList()
65+
.stream()
66+
.collect(
67+
Collectors.toMap(
68+
EntityAnnotation::getDescription,
69+
EntityAnnotation::getScore,
70+
(u, v) -> {
71+
throw new IllegalStateException(String.format("Duplicate key %s", u));
72+
},
73+
LinkedHashMap::new));
74+
//[END spring_vision_image_labelling]
75+
76+
map.addAttribute("annotations", imageLabels);
77+
map.addAttribute("imageUrl", imageUrl);
78+
79+
return new ModelAndView("result", map);
80+
}
81+
82+
@GetMapping("/extractText")
83+
public String extractText(String imageUrl) {
84+
//[START spring_vision_text_extraction]
85+
String textFromImage = this.cloudVisionTemplate.extractTextFromImage(
86+
this.resourceLoader.getResource(imageUrl));
87+
return "Text from image: " + textFromImage;
88+
//[END spring_vision_text_extraction]
89+
}
90+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Google Cloud Vision API with Spring code sample</title>
6+
</head>
7+
<body>
8+
9+
<div>
10+
<h1>Image Label Annotations</h1>
11+
<p>Returns labels classifying the content of the image:</p>
12+
<form action="/extractLabels">
13+
Web URL of image to analyze:
14+
<input type="text"
15+
name="imageUrl"
16+
value="https://www.google.com/logos/doodles/2018/childrens-day-2018-argentina-peru-5906663952351232-2x.png"/>
17+
<input type="submit"/>
18+
</form>
19+
</div>
20+
21+
<div>
22+
<h1>Text Extraction</h1>
23+
<p>Read and extract the text from the image:</p>
24+
<form action="/extractText">
25+
Web URL of image to analyze:
26+
<input type="text"
27+
name="imageUrl"
28+
value="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"/>
29+
<input type="submit" />
30+
</form>
31+
</div>
32+
33+
</body>
34+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="https://www.thymeleaf.org">
3+
<head>
4+
<title>Google Cloud Vision Results</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6+
</head>
7+
<body>
8+
<h1>Annotations Produced for the Image</h1>
9+
<table border="1">
10+
<tr>
11+
<th>Description</th>
12+
<th>Score</th>
13+
</tr>
14+
<tr th:each="entry : ${annotations}">
15+
<td>[[${entry.key}]]</td>
16+
<td>[[${entry.value}]]</td>
17+
</tr>
18+
</table>
19+
20+
<p>
21+
<img th:src="${imageUrl}"/>
22+
</p>
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)