Skip to content

Commit

Permalink
docs(genai): Add Veo Generation Sample (#13186)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtskinner authored Feb 27, 2025
1 parent 2135347 commit aa24c20
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
42 changes: 42 additions & 0 deletions genai/video_generation/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be imported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": True,
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
# If you need to use a specific version of pip,
# change pip_version_override to the string representation
# of the version number, for example, "20.2.4"
"pip_version_override": None,
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
"envs": {},
}
3 changes: 3 additions & 0 deletions genai/video_generation/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
google-api-core==2.24.0
google-cloud-storage==2.19.0
pytest==8.2.0
1 change: 1 addition & 0 deletions genai/video_generation/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-genai==1.3.0
53 changes: 53 additions & 0 deletions genai/video_generation/test_video_generation_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Using Google Cloud Vertex AI to test the code samples.
#

from datetime import datetime as dt

import os

from google.cloud import storage

import pytest

import videogen_with_txt


os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
# The project name is included in the CICD pipeline
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"

GCS_OUTPUT_BUCKET = "python-docs-samples-tests"


@pytest.fixture(scope="session")
def output_gcs_uri() -> str:
prefix = f"text_output/{dt.now()}"

yield f"gs://{GCS_OUTPUT_BUCKET}/{prefix}"

storage_client = storage.Client()
bucket = storage_client.get_bucket(GCS_OUTPUT_BUCKET)
blobs = bucket.list_blobs(prefix=prefix)
for blob in blobs:
blob.delete()


def test_videogen_with_txt(output_gcs_uri: str) -> None:
response = videogen_with_txt.generate_videos(output_gcs_uri=output_gcs_uri)
assert response
51 changes: 51 additions & 0 deletions genai/video_generation/videogen_with_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def generate_videos(output_gcs_uri: str) -> str:
# [START googlegenaisdk_videogen_with_txt]
import time
from google import genai
from google.genai.types import GenerateVideosConfig

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

operation = client.models.generate_videos(
model="veo-2.0-generate-001",
prompt="a cat reading a book",
config=GenerateVideosConfig(
aspect_ratio="16:9",
output_gcs_uri=output_gcs_uri,
),
)

while not operation.done:
time.sleep(15)
operation = client.operations.get(operation)
print(operation)

if operation.response:
print(operation.result.generated_videos[0].video.uri)

# Example response:
# gs://your-bucket/your-prefix
# [END googlegenaisdk_videogen_with_txt]
return operation.result.generated_videos[0].video.uri


if __name__ == "__main__":
generate_videos(output_gcs_uri="gs://your-bucket/your-prefix")

0 comments on commit aa24c20

Please sign in to comment.