From aa24c20ad41b99a6b3f1f3b83ef16bcbf29cf6be Mon Sep 17 00:00:00 2001 From: Holt Skinner <13262395+holtskinner@users.noreply.github.com> Date: Thu, 27 Feb 2025 10:30:50 -0600 Subject: [PATCH] docs(genai): Add Veo Generation Sample (#13186) --- genai/video_generation/noxfile_config.py | 42 +++++++++++++++ genai/video_generation/requirements-test.txt | 3 ++ genai/video_generation/requirements.txt | 1 + .../test_video_generation_examples.py | 53 +++++++++++++++++++ genai/video_generation/videogen_with_txt.py | 51 ++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 genai/video_generation/noxfile_config.py create mode 100644 genai/video_generation/requirements-test.txt create mode 100644 genai/video_generation/requirements.txt create mode 100644 genai/video_generation/test_video_generation_examples.py create mode 100644 genai/video_generation/videogen_with_txt.py diff --git a/genai/video_generation/noxfile_config.py b/genai/video_generation/noxfile_config.py new file mode 100644 index 000000000000..962ba40a9269 --- /dev/null +++ b/genai/video_generation/noxfile_config.py @@ -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": {}, +} diff --git a/genai/video_generation/requirements-test.txt b/genai/video_generation/requirements-test.txt new file mode 100644 index 000000000000..4ccc4347cbe7 --- /dev/null +++ b/genai/video_generation/requirements-test.txt @@ -0,0 +1,3 @@ +google-api-core==2.24.0 +google-cloud-storage==2.19.0 +pytest==8.2.0 diff --git a/genai/video_generation/requirements.txt b/genai/video_generation/requirements.txt new file mode 100644 index 000000000000..68d0f1d330b1 --- /dev/null +++ b/genai/video_generation/requirements.txt @@ -0,0 +1 @@ +google-genai==1.3.0 diff --git a/genai/video_generation/test_video_generation_examples.py b/genai/video_generation/test_video_generation_examples.py new file mode 100644 index 000000000000..626f4b494c63 --- /dev/null +++ b/genai/video_generation/test_video_generation_examples.py @@ -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 diff --git a/genai/video_generation/videogen_with_txt.py b/genai/video_generation/videogen_with_txt.py new file mode 100644 index 000000000000..8642331dc267 --- /dev/null +++ b/genai/video_generation/videogen_with_txt.py @@ -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")