Skip to content

Commit 238a9f0

Browse files
authored
Merge branch 'main' into fix/update-markdown-preview-esm
2 parents bb8cc9d + 67909a4 commit 238a9f0

File tree

7 files changed

+299
-6
lines changed

7 files changed

+299
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
'use strict';
18+
19+
async function main(
20+
project,
21+
location = 'us-central1',
22+
baseImagePath,
23+
textPrompt
24+
) {
25+
// [START aiplatform_sdk_text_image_embedding]
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.\
28+
* (Not necessary if passing values as arguments)
29+
*/
30+
// const project = 'YOUR_PROJECT_ID';
31+
// const location = 'YOUR_PROJECT_LOCATION';
32+
// const bastImagePath = "YOUR_BASED_IMAGE_PATH"
33+
// const textPrompt = 'YOUR_TEXT_PROMPT';
34+
const aiplatform = require('@google-cloud/aiplatform');
35+
36+
// Imports the Google Cloud Prediction service client
37+
const {PredictionServiceClient} = aiplatform.v1;
38+
39+
// Import the helper module for converting arbitrary protobuf.Value objects.
40+
const {helpers} = aiplatform;
41+
42+
// Specifies the location of the api endpoint
43+
const clientOptions = {
44+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
45+
};
46+
const publisher = 'google';
47+
const model = 'multimodalembedding@001';
48+
49+
// Instantiates a client
50+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
51+
52+
async function predictImageFromImageAndText() {
53+
// Configure the parent resource
54+
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
55+
56+
const fs = require('fs');
57+
const imageFile = fs.readFileSync(baseImagePath);
58+
59+
// Convert the image data to a Buffer and base64 encode it.
60+
const encodedImage = Buffer.from(imageFile).toString('base64');
61+
62+
const prompt = {
63+
text: textPrompt,
64+
image: {
65+
bytesBase64Encoded: encodedImage,
66+
},
67+
};
68+
const instanceValue = helpers.toValue(prompt);
69+
const instances = [instanceValue];
70+
71+
const parameter = {
72+
sampleCount: 1,
73+
};
74+
const parameters = helpers.toValue(parameter);
75+
76+
const request = {
77+
endpoint,
78+
instances,
79+
parameters,
80+
};
81+
82+
// Predict request
83+
const [response] = await predictionServiceClient.predict(request);
84+
console.log('Get image embedding response');
85+
const predictions = response.predictions;
86+
console.log('\tPredictions :');
87+
for (const prediction of predictions) {
88+
console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
89+
}
90+
}
91+
92+
await predictImageFromImageAndText();
93+
// [END aiplatform_sdk_text_image_embedding]
94+
}
95+
96+
exports.predictImageFromImageAndText = main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
'use strict';
18+
19+
async function main(project, location = 'us-central1', textPrompt) {
20+
// [START aiplatform_sdk_text_embedding]
21+
/**
22+
* TODO(developer): Uncomment these variables before running the sample.\
23+
* (Not necessary if passing values as arguments)
24+
*/
25+
// const project = 'YOUR_PROJECT_ID';
26+
// const location = 'YOUR_PROJECT_LOCATION';
27+
// const textPrompt = 'YOUR_TEXT_PROMPT';
28+
const aiplatform = require('@google-cloud/aiplatform');
29+
30+
// Imports the Google Cloud Prediction service client
31+
const {PredictionServiceClient} = aiplatform.v1;
32+
33+
// Import the helper module for converting arbitrary protobuf.Value objects.
34+
const {helpers} = aiplatform;
35+
36+
// Specifies the location of the api endpoint
37+
const clientOptions = {
38+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
39+
};
40+
const publisher = 'google';
41+
const model = 'multimodalembedding@001';
42+
43+
// Instantiates a client
44+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
45+
46+
async function predictImageFromText() {
47+
// Configure the parent resource
48+
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
49+
50+
const prompt = {
51+
text: textPrompt,
52+
};
53+
const instanceValue = helpers.toValue(prompt);
54+
const instances = [instanceValue];
55+
56+
const parameter = {
57+
sampleCount: 1,
58+
};
59+
const parameters = helpers.toValue(parameter);
60+
61+
const request = {
62+
endpoint,
63+
instances,
64+
parameters,
65+
};
66+
67+
// Predict request
68+
const [response] = await predictionServiceClient.predict(request);
69+
console.log('Get image embedding response');
70+
const predictions = response.predictions;
71+
console.log('\tPredictions :');
72+
for (const prediction of predictions) {
73+
console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
74+
}
75+
}
76+
77+
await predictImageFromText();
78+
// [END aiplatform_sdk_text_embedding]
79+
}
80+
81+
exports.predictImageFromText = main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
'use strict';
18+
19+
const {assert} = require('chai');
20+
const {describe, it} = require('mocha');
21+
const sinon = require('sinon');
22+
23+
const {
24+
predictImageFromImageAndText,
25+
} = require('../predict-image-from-image-and-text');
26+
27+
const project = process.env.CAIP_PROJECT_ID;
28+
const LOCATION = 'us-central1';
29+
const baseImagePath = 'resources/daisy.jpg';
30+
const textPrompt = 'an impressionist painting';
31+
32+
describe('AI platform generates image from image and text', async () => {
33+
const stubConsole = function () {
34+
sinon.stub(console, 'error');
35+
sinon.stub(console, 'log');
36+
};
37+
38+
const restoreConsole = function () {
39+
console.log.restore();
40+
console.error.restore();
41+
};
42+
43+
beforeEach(stubConsole);
44+
afterEach(restoreConsole);
45+
46+
it('should make predictions using a large language model', async () => {
47+
await predictImageFromImageAndText(
48+
project,
49+
LOCATION,
50+
baseImagePath,
51+
textPrompt
52+
);
53+
assert.include(console.log.firstCall.args, 'Get image embedding response');
54+
});
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
'use strict';
18+
19+
const {assert} = require('chai');
20+
const {describe, it} = require('mocha');
21+
const sinon = require('sinon');
22+
23+
const {predictImageFromText} = require('../predict-image-from-text');
24+
25+
const project = process.env.CAIP_PROJECT_ID;
26+
const LOCATION = 'us-central1';
27+
const textPrompt =
28+
'small red boat on water in the morning watercolor illustration muted colors';
29+
30+
describe('AI platform generates image from text', async () => {
31+
const stubConsole = function () {
32+
sinon.stub(console, 'error');
33+
sinon.stub(console, 'log');
34+
};
35+
36+
const restoreConsole = function () {
37+
console.log.restore();
38+
console.error.restore();
39+
};
40+
41+
beforeEach(stubConsole);
42+
afterEach(restoreConsole);
43+
44+
it('should make predictions using a large language model', async () => {
45+
await predictImageFromText(project, LOCATION, textPrompt);
46+
assert.include(console.log.firstCall.args, 'Get image embedding response');
47+
});
48+
});

recaptcha_enterprise/demosite/app/controllers/controller.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ const onHomepageLoad = async (req, res) => {
7474
const assessmentResponse = await createAssessment(
7575
context.project_id,
7676
context.site_key,
77-
req.body.token
77+
req.body.token,
78+
recaptchaAction
7879
);
7980

8081
// Check if the token is valid, score is above threshold score and the action equals expected.
@@ -117,7 +118,8 @@ const onSignup = async (req, res) => {
117118
const assessmentResponse = await createAssessment(
118119
context.project_id,
119120
context.site_key,
120-
req.body.token
121+
req.body.token,
122+
recaptchaAction
121123
);
122124

123125
// Check if the token is valid, score is above threshold score and the action equals expected.
@@ -162,7 +164,8 @@ const onLogin = async (req, res) => {
162164
const assessmentResponse = await createAssessment(
163165
context.project_id,
164166
context.site_key,
165-
req.body.token
167+
req.body.token,
168+
recaptchaAction
166169
);
167170

168171
// Check if the token is valid, score is above threshold score and the action equals expected.
@@ -207,7 +210,8 @@ const onStoreCheckout = async (req, res) => {
207210
const assessmentResponse = await createAssessment(
208211
context.project_id,
209212
context.site_key,
210-
req.body.token
213+
req.body.token,
214+
recaptchaAction
211215
);
212216

213217
// Check if the token is valid, score is above threshold score and the action equals expected.
@@ -251,7 +255,8 @@ const onCommentSubmit = async (req, res) => {
251255
const assessmentResponse = await createAssessment(
252256
context.project_id,
253257
context.site_key,
254-
req.body.token
258+
req.body.token,
259+
recaptchaAction
255260
);
256261

257262
// Check if the token is valid, score is above threshold score and the action equals expected.

recaptcha_enterprise/demosite/app/recaptcha/createAssessment.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ const {RecaptchaEnterpriseServiceClient} =
2020
* @param projectId: GCloud Project ID
2121
* @param recaptchaSiteKey: Site key obtained by registering a domain/app to use recaptcha services.
2222
* @param token: The token obtained from the client on passing the recaptchaSiteKey.
23+
* @param expectedAction: The expected action for this type of event.
2324
* @returns Assessment
2425
*/
25-
async function createAssessment(projectId, recaptchaSiteKey, token) {
26+
async function createAssessment(
27+
projectId,
28+
recaptchaSiteKey,
29+
token,
30+
expectedAction
31+
) {
2632
// <!-- ATTENTION: reCAPTCHA Example (Server Part 2/2) Starts -->
2733
const client = new RecaptchaEnterpriseServiceClient();
2834

@@ -34,6 +40,7 @@ async function createAssessment(projectId, recaptchaSiteKey, token) {
3440
event: {
3541
siteKey: recaptchaSiteKey,
3642
token: token,
43+
expectedAction: expectedAction,
3744
},
3845
},
3946
});

run/helloworld/package.docs.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": "1.0.0",
55
"private": true,
66
"main": "index.js",
7+
"type": "module",
78
"scripts": {
89
"start": "node index.js"
910
},

0 commit comments

Comments
 (0)