8000 docs(genai): Add Veo Generation Sample (#13186) · katiemn/python-docs-samples@aa24c20 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa24c20

Browse files
authored
docs(genai): Add Veo Generation Sample (GoogleCloudPlatform#13186)
1 parent 2135347 commit aa24c20

File tree

5 files changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# http://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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
google-api-core==2.24.0
2+
google-cloud-storage==2.19.0
3+
pytest==8.2.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==1.3.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
# Using Google Cloud Vertex AI to test the code samples.
17+
#
18+
19+
from datetime import datetime as dt
20+
21+
import os
22+
23+
from google.cloud import storage
24+
25+
import pytest
26+
27+
import videogen_with_txt
28+
29+
30+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
31+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
32+
# The project name is included in the CICD pipeline
33+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
34+
35+
GCS_OUTPUT_BUCKET = "python-docs-samples-tests"
36+
37+
38+
@pytest.fixture(scope="session")
39+
def output_gcs_uri() -> str:
40+
prefix = f"text_output/{dt.now()}"
41+
42+
yield f"gs://{GCS_OUTPUT_BUCKET}/{prefix}"
43+
44+
storage_client = storage.Client()
45+
bucket = storage_client.get_bucket(GCS_OUTPUT_BUCKET)
46+
blobs = bucket.list_blobs(prefix=prefix)
47+
for blob in blobs:
48+
blob.delete()
49+
50+
51+
def test_videogen_with_txt(output_gcs_uri: str) -> None:
52+
response = videogen_with_txt.generate_videos(output_gcs_uri=output_gcs_uri)
53+
assert response
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_videos(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_videogen_with_txt]
18+
import time
19+
from google import genai
20+
from google.genai.types import GenerateVideosConfig
21+
22+
client = genai.Client()
23+
24+
# TODO(developer): Update and un-comment below line
25+
# output_gcs_uri = "gs://your-bucket/your-prefix"
26+
27+
operation = client.models.generate_videos(
28+
model="veo-2.0-generate-001",
29+
prompt="a cat reading a book",
30+
config=GenerateVideosConfig(
31+
aspect_ratio="16:9",
32+
output_gcs_uri=output_gcs_uri,
33+
),
34+
)
35+
36+
while not operation.done:
37+
time.sleep(15)
38+
operation = client.operations.get(operation)
39+
print(operation)
40+
41+
if operation.response:
42+
print(operation.result.generated_videos[0].video.uri)
43+
44+
# Example response:
45+
# gs://your-bucket/your-prefix
46+
# [END googlegenaisdk_videogen_with_txt]
47+
return operation.result.generated_videos[0].video.uri
48+
49+
50+
if __name__ == "__main__":
51+
generate_videos(output_gcs_uri="gs://your-bucket/your-prefix")