Skip to content

Commit 7f73658

Browse files
GuinersGuinersmsampathkumargericdong
authored
feat(genai): Sample/textgen part2 (#4161)
* adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * Update genai/text-generation/textgen-transcript-with-gcs-audio.js Co-authored-by: Sampath Kumar <sam1990kumar@gmail.com> * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * fixing functions names --------- Co-authored-by: Guiners <rkoza@softserveinc.com> Co-authored-by: Sampath Kumar <sam1990kumar@gmail.com> Co-authored-by: Sampath Kumar <sampathm@google.com> Co-authored-by: Eric Dong <itseric@google.com>
1 parent 51d8cee commit 7f73658

13 files changed

+491
-19
lines changed
1.75 MB
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2025 Google LLC
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+
// https://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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-transcript-with-gcs-audio.js');
22+
const {delay} = require('./util');
23+
24+
describe('textgen-transcript-with-gcs-audio', async () => {
25+
it('should generate text content from gsc audio with transcript', async function () {
26+
this.timeout(180000);
27+
this.retries(4);
28+
await delay(this.test);
29+
const output = await sample.generateText(projectId);
30+
assert(output.length > 0);
31+
});
32+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 Google LLC
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+
// https://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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-with-gcs-audio');
22+
23+
describe('textgen-with-gcs-audio', async () => {
24+
it('should generate text content from gsc audio', async function () {
25+
this.timeout(300000);
26+
const output = await sample.generateText(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 Google LLC
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+
// https://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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-with-local-video.js');
22+
23+
describe('textgen-with-local-video', async () => {
24+
it('should generate text content from local video', async function () {
25+
this.timeout(30000);
26+
const output = await sample.generateText(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 Google LLC
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+
// https://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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-with-pdf.js');
22+
23+
describe('textgen-with-pdf', async () => {
24+
it('should generate text content from pdf', async function () {
25+
this.timeout(30000);
26+
const output = await sample.generateText(projectId);
27+
assert(output.length > 0);
28+
});
29+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 Google LLC
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+
// https://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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../text-generation/textgen-with-youtube-video');
22+
23+
describe('textgen-with-youtube-video', async () => {
24+
it('should generate text content from yt video', async function () {
25+
this.timeout(300000);
26+
const output = await sample.generateText(projectId);
27+
assert(output.length > 0);
28+
});
29+
});

genai/text-generation/textgen-code-with-pdf.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,26 @@ async function generateText(
5353

5454
console.log(response.text);
5555

56+
// Example response:
57+
// Here's the Python code converted to adhere to the Google Python Style Guide, along with explanations for the changes:
58+
//
59+
// ```python
60+
// """Calculates the Fibonacci sequence up to n numbers.
61+
//
62+
// This module provides a function to generate a Fibonacci sequence,
63+
// demonstrating adherence to the Google Python Style Guide.
64+
// """
65+
//
66+
// def fibonacci(n: int) -> list[int]:
67+
// """Calculates the Fibonacci sequence up to n numbers.
68+
//
69+
// This function generates the first 'n' terms of the Fibonacci sequence,
70+
// starting with 0, 1, 1, 2...
71+
// ...
72+
5673
return response.text;
5774
}
58-
// Example response:
59-
// Here's the Python code converted to adhere to the Google Python Style Guide, along with explanations for the changes:
60-
//
61-
// ```python
62-
// """Calculates the Fibonacci sequence up to n numbers.
63-
//
64-
// This module provides a function to generate a Fibonacci sequence,
65-
// demonstrating adherence to the Google Python Style Guide.
66-
// """
67-
//
68-
// def fibonacci(n: int) -> list[int]:
69-
// """Calculates the Fibonacci sequence up to n numbers.
70-
//
71-
// This function generates the first 'n' terms of the Fibonacci sequence,
72-
// starting with 0, 1, 1, 2...
73-
// ...
75+
7476
// [END googlegenaisdk_textgen_code_with_pdf]
7577

7678
module.exports = {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2025 Google LLC
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+
// https://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+
'use strict';
16+
17+
// [START googlegenaisdk_textgen_transcript_with_gcs_audio]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateText(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const prompt = `Transcribe the interview, in the format of timecode, speaker, caption.
34+
Use speaker A, speaker B, etc. to identify speakers.`;
35+
36+
const response = await client.models.generateContent({
37+
model: 'gemini-2.5-flash',
38+
contents: [
39+
{text: prompt},
40+
{
41+
fileData: {
42+
fileUri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3',
43+
mimeType: 'audio/mpeg',
44+
},
45+
},
46+
],
47+
// Required to enable timestamp understanding for audio-only files
48+
config: {
49+
audioTimestamp: true,
50+
},
51+
});
52+
53+
console.log(response.text);
54+
55+
// Example response:
56+
// [00:00:00] **Speaker A:** your devices are getting better over time. And so ...
57+
// [00:00:14] **Speaker B:** Welcome to the Made by Google podcast where we meet ...
58+
// [00:00:20] **Speaker B:** Here's your host, Rasheed Finch.
59+
// [00:00:23] **Speaker C:** Today we're talking to Aisha Sharif and DeCarlos Love. ...
60+
// ...
61+
62+
return response.text;
63+
}
64+
65+
// [END googlegenaisdk_textgen_transcript_with_gcs_audio]
66+
67+
module.exports = {
68+
generateText,
69+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2025 Google LLC
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+
// https://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+
'use strict';
16+
17+
// [START googlegenaisdk_textgen_with_gcs_audio]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateText(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const prompt =
34+
'Provide a concise summary of the main points in the audio file.';
35+
36+
const response = await client.models.generateContent({
37+
model: 'gemini-2.5-flash',
38+
contents: [
39+
{
40+
fileData: {
41+
fileUri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3',
42+
mimeType: 'audio/mpeg',
43+
},
44+
},
45+
{text: prompt},
46+
],
47+
});
48+
49+
console.log(response.text);
50+
51+
// Example response:
52+
// Here's a summary of the main points from the audio file:
53+
// The Made by Google podcast discusses the Pixel feature drops with product managers Aisha Sheriff and De Carlos Love. The key idea is that devices should improve over time, with a connected experience across phones, watches, earbuds, and tablets.
54+
55+
return response.text;
56+
}
57+
// [END googlegenaisdk_textgen_with_gcs_audio]
58+
59+
module.exports = {
60+
generateText,
61+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2025 Google LLC
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+
// https://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+
'use strict';
16+
17+
// [START googlegenaisdk_textgen_with_local_video]
18+
const {GoogleGenAI} = require('@google/genai');
19+
const fs = require('fs');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
23+
24+
async function generateText(
25+
projectId = GOOGLE_CLOUD_PROJECT,
26+
location = GOOGLE_CLOUD_LOCATION
27+
) {
28+
const client = new GoogleGenAI({
29+
vertexai: true,
30+
project: projectId,
31+
location: location,
32+
});
33+
34+
const videoContent = fs.readFileSync('test-data/describe_video_content.mp4');
35+
36+
const response = await client.models.generateContent({
37+
model: 'gemini-2.5-flash',
38+
contents: [
39+
{text: 'hello-world'},
40+
{
41+
inlineData: {
42+
data: videoContent.toString('base64'),
43+
mimeType: 'video/mp4',
44+
},
45+
},
46+
{text: 'Write a short and engaging blog post based on this video.'},
47+
],
48+
});
49+
50+
console.log(response.text);
51+
52+
// Example response:
53+
// Okay, here's a short and engaging blog post based on the climbing video:
54+
// **Title: Conquering the Wall: A Glimpse into the World of Indoor Climbing**
55+
// ...
56+
57+
return response.text;
58+
}
59+
60+
// [END googlegenaisdk_textgen_with_local_video]
61+
62+
module.exports = {
63+
generateText,
64+
};

0 commit comments

Comments
 (0)