Skip to content

Commit 0e0e640

Browse files
feat(genai): add python samples for text generation (2nd batch) (#13116)
* feat(genai): add python samples for text generation (2nd batch) * remove auxilliary file * move some samples into 1st batch * feat: add 2 more textgen samples to the batch * update the sdk * test with local images * linter/ordering * update the usage of for the new SDK
1 parent 16feb56 commit 0e0e640

9 files changed

+348
-9
lines changed
52 KB
Loading
384 KB
Loading

genai/text_generation/test_text_generation.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818
import textgen_chat_with_txt_stream
1919
import textgen_config_with_txt
2020
import textgen_sys_instr_with_txt
21+
import textgen_transcript_with_gcs_audio
22+
import textgen_with_gcs_audio
23+
import textgen_with_multi_img
24+
import textgen_with_multi_local_img
25+
import textgen_with_mute_video
2126
import textgen_with_txt
2227
import textgen_with_txt_img
2328
import textgen_with_txt_stream
29+
import textgen_with_video
2430

2531

2632
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
@@ -29,18 +35,13 @@
2935
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
3036

3137

32-
def test_textgen_with_txt() -> None:
33-
response = textgen_with_txt.generate_content()
34-
assert response
35-
36-
37-
def test_textgen_with_txt_img() -> None:
38-
response = textgen_with_txt_img.generate_content()
38+
def test_textgen_with_txt_stream() -> None:
39+
response = textgen_with_txt_stream.generate_content()
3940
assert response
4041

4142

42-
def test_textgen_with_txt_stream() -> None:
43-
response = textgen_with_txt_stream.generate_content()
43+
def test_textgen_with_txt() -> None:
44+
response = textgen_with_txt.generate_content()
4445
assert response
4546

4647

@@ -62,3 +63,41 @@ def test_textgen_config_with_txt() -> None:
6263
def test_textgen_sys_instr_with_txt() -> None:
6364
response = textgen_sys_instr_with_txt.generate_content()
6465
assert response
66+
67+
68+
def test_textgen_with_txt_img() -> None:
69+
response = textgen_with_txt_img.generate_content()
70+
assert response
71+
72+
73+
def test_textgen_with_multi_img() -> None:
74+
response = textgen_with_multi_img.generate_content()
75+
assert response
76+
77+
78+
def test_textgen_with_multi_local_img() -> None:
79+
response = textgen_with_multi_local_img.generate_content(
80+
"./test_data/latte.jpg",
81+
"./test_data/scones.jpg",
82+
)
83+
assert response
84+
85+
86+
def test_textgen_with_mute_video() -> None:
87+
response = textgen_with_mute_video.generate_content()
88+
assert response
89+
90+
91+
def test_textgen_with_gcs_audio() -> None:
92+
response = textgen_with_gcs_audio.generate_content()
93+
assert response
94+
95+
96+
def test_textgen_transcript_with_gcs_audio() -> None:
97+
response = textgen_transcript_with_gcs_audio.generate_content()
98+
assert response
99+
100+
101+
def test_textgen_with_video() -> None:
102+
response = textgen_with_video.generate_content()
103+
assert response
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_transcript_with_gcs_audio]
18+
from google import genai
19+
from google.genai.types import Part
20+
21+
client = genai.Client()
22+
23+
prompt = """
24+
Transcribe the interview, in the format of timecode, speaker, caption.
25+
Use speaker A, speaker B, etc. to identify speakers.
26+
"""
27+
response = client.models.generate_content(
28+
model="gemini-2.0-flash-001",
29+
contents=[
30+
prompt,
31+
Part.from_uri(
32+
file_uri="gs://cloud-samples-data/generative-ai/audio/pixel.mp3",
33+
mime_type="audio/mpeg"
34+
)
35+
]
36+
)
37+
print(response.text)
38+
# Example response:
39+
# [00:00:00] **Speaker A:** your devices are getting better over time. And so ...
40+
# [00:00:14] **Speaker B:** Welcome to the Made by Google podcast where we meet ...
41+
# [00:00:20] **Speaker B:** Here's your host, Rasheed Finch.
42+
# [00:00:23] **Speaker C:** Today we're talking to Aisha Sharif and DeCarlos Love. ...
43+
# ...
44+
# [END googlegenaisdk_textgen_transcript_with_gcs_audio]
45+
return response.text
46+
47+
48+
if __name__ == "__main__":
49+
generate_content()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_with_gcs_audio]
18+
from google import genai
19+
from google.genai.types import Part
20+
21+
client = genai.Client()
22+
23+
prompt = """
24+
Provide the summary of the audio file.
25+
Summarize the main points of the audio concisely.
26+
Create a chapter breakdown with timestamps for key sections or topics discussed.
27+
"""
28+
response = client.models.generate_content(
29+
model="gemini-2.0-flash-001",
30+
contents=[
31+
prompt,
32+
Part.from_uri(
33+
file_uri="gs://cloud-samples-data/generative-ai/audio/pixel.mp3",
34+
mime_type="audio/mpeg"
35+
)
36+
]
37+
)
38+
print(response.text)
39+
# Example response:
40+
# This episode of the Made by Google podcast features product managers ...
41+
#
42+
# **Chapter Breakdown:**
43+
#
44+
# * **[0:00-1:14] Introduction:** Host Rasheed Finch introduces Aisha and DeCarlos and ...
45+
# * **[1:15-2:44] Transformative Features:** Aisha and DeCarlos discuss their ...
46+
# ...
47+
# [END googlegenaisdk_textgen_with_gcs_audio]
48+
return response.text
49+
50+
51+
if __name__ == "__main__":
52+
generate_content()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_with_multi_img]
18+
from google import genai
19+
from google.genai.types import Part
20+
21+
client = genai.Client()
22+
response = client.models.generate_content(
23+
model="gemini-2.0-flash-001",
24+
contents=[
25+
"Generate a list of all the objects contained in both images.",
26+
Part.from_uri(
27+
file_uri="gs://cloud-samples-data/generative-ai/image/scones.jpg",
28+
mime_type="image/jpeg"
29+
),
30+
Part.from_uri(
31+
file_uri="gs://cloud-samples-data/generative-ai/image/latte.jpg",
32+
mime_type="image/jpeg"
33+
)
34+
]
35+
)
36+
print(response.text)
37+
# Example response:
38+
# Okay, here's the list of objects present in both images:
39+
# ...
40+
# [END googlegenaisdk_textgen_with_multi_img]
41+
return response.text
42+
43+
44+
if __name__ == "__main__":
45+
generate_content()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
16+
def generate_content(image_path_1: str, image_path_2: str) -> str:
17+
# [START googlegenaisdk_textgen_with_multi_local_img]
18+
from google import genai
19+
from google.genai.types import Part
20+
21+
client = genai.Client()
22+
23+
# TODO(Developer): Update the below file paths to your images
24+
# image_path_1 = "path/to/your/image1.jpg"
25+
# image_path_2 = "path/to/your/image2.jpg"
26+
with open(image_path_1, "rb") as f:
27+
image_1_bytes = f.read()
28+
with open(image_path_2, "rb") as f:
29+
image_2_bytes = f.read()
30+
31+
response = client.models.generate_content(
32+
model="gemini-2.0-flash-001",
33+
contents=[
34+
"Write an advertising jingle based on the items in both images.",
35+
Part.from_bytes(
36+
data=image_1_bytes,
37+
mime_type="image/jpeg"
38+
),
39+
Part.from_bytes(
40+
data=image_2_bytes,
41+
mime_type="image/jpeg"
42+
)
43+
]
44+
)
45+
print(response.text)
46+
# Example response:
47+
# Okay, here's a jingle combining the elements of both sets of images, focusing on ...
48+
# ...
49+
# [END googlegenaisdk_textgen_with_multi_local_img]
50+
return response.text
51+
52+
53+
if __name__ == "__main__":
54+
generate_content(
55+
"./test_data/latte.jpg",
56+
"./test_data/scones.jpg",
57+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_with_mute_video]
18+
from google import genai
19+
from google.genai.types import Part
20+
21+
client = genai.Client()
22+
23+
response = client.models.generate_content(
24+
model="gemini-2.0-flash-001",
25+
contents=[
26+
"What is in the video?",
27+
Part.from_uri(
28+
file_uri="gs://cloud-samples-data/generative-ai/video/ad_copy_from_video.mp4",
29+
mime_type="video/mp4"
30+
)
31+
]
32+
)
33+
print(response.text)
34+
# Example response:
35+
# The video shows several people surfing in an ocean with a coastline in the background. The camera ...
36+
# [END googlegenaisdk_textgen_with_mute_video]
37+
return response.text
38+
39+
40+
if __name__ == "__main__":
41+
generate_content()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_textgen_with_video]
18+
from google import genai
19+
from google.genai.types import Part
20+
21+
client = genai.Client()
22+
23+
prompt = """
24+
Analyze the provided video file, including its audio.
25+
Summarize the main points of the video concisely.
26+
Create a chapter breakdown with timestamps for key sections or topics discussed.
27+
"""
28+
response = client.models.generate_content(
29+
model="gemini-2.0-flash-001",
30+
contents=[
31+
prompt,
32+
Part.from_uri(
33+
file_uri="gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
34+
mime_type="video/mp4"
35+
)
36+
]
37+
)
38+
39+
print(response.text)
40+
# Example response:
41+
# Here's a breakdown of the video:
42+
#
43+
# **Summary:**
44+
#
45+
# Saeka Shimada, a photographer in Tokyo, uses the Google Pixel 8 Pro's "Video Boost" feature to ...
46+
#
47+
# **Chapter Breakdown with Timestamps:**
48+
#
49+
# * **[00:00-00:12] Introduction & Tokyo at Night:** Saeka Shimada introduces herself ...
50+
# ...
51+
# [END googlegenaisdk_textgen_with_video]
52+
return response.text
53+
54+
55+
if __name__ == "__main__":
56+
generate_content()

0 commit comments

Comments
 (0)