Skip to content

Commit 9432a78

Browse files
refactor: (GenAI) Reorganized Video Samples (Group C) (GoogleCloudPlatform#12644)
* Reorganized Video Samples
1 parent c31c586 commit 9432a78

4 files changed

Lines changed: 143 additions & 0 deletions

File tree

generative_ai/test_gemini_examples.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def test_gemini_safety_config_example() -> None:
9797
assert len(text) > 0
9898

9999

100+
# Delete after approval /understand_video/single_turn_video_example.py
100101
def test_gemini_single_turn_video_example() -> None:
101102
text = gemini_single_turn_video_example.generate_text()
102103
text = text.lower()
@@ -138,6 +139,7 @@ def test_transcript_audio() -> None:
138139
assert len(text) > 0
139140

140141

142+
# Delete after approval /understand_video/audio_video_example.py
141143
def test_analyze_video_with_audio() -> None:
142144
text = gemini_video_audio.analyze_video_with_audio()
143145
assert len(text) > 0
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2024 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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def analyze_video_with_audio() -> str:
20+
# [START generativeaionvertexai_gemini_video_with_audio]
21+
22+
import vertexai
23+
from vertexai.generative_models import GenerativeModel, Part
24+
25+
# TODO(developer): Update and un-comment below line
26+
# PROJECT_ID = "your-project-id"
27+
28+
vertexai.init(project=PROJECT_ID, location="us-central1")
29+
30+
model = GenerativeModel("gemini-1.5-flash-002")
31+
32+
prompt = """
33+
Provide a description of the video.
34+
The description should also contain anything important which people say in the video.
35+
"""
36+
37+
video_file = Part.from_uri(
38+
uri="gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
39+
mime_type="video/mp4",
40+
)
41+
42+
contents = [video_file, prompt]
43+
44+
response = model.generate_content(contents)
45+
print(response.text)
46+
# Example response:
47+
# Here is a description of the video.
48+
# ... Then, the scene changes to a woman named Saeko Shimada..
49+
# She says, "Tokyo has many faces. The city at night is totally different
50+
# from what you see during the day."
51+
# ...
52+
53+
# [END generativeaionvertexai_gemini_video_with_audio]
54+
return response.text
55+
56+
57+
if __name__ == "__main__":
58+
analyze_video_with_audio()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2024 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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def generate_text() -> str:
20+
# [START generativeaionvertexai_gemini_single_turn_video]
21+
import vertexai
22+
23+
from vertexai.generative_models import GenerativeModel, Part
24+
25+
# TODO(developer): Update and un-comment below line
26+
# PROJECT_ID = "your-project-id"
27+
28+
vertexai.init(project=PROJECT_ID, location="us-central1")
29+
30+
vision_model = GenerativeModel("gemini-1.5-flash-002")
31+
32+
# Generate text
33+
response = vision_model.generate_content(
34+
[
35+
Part.from_uri(
36+
"gs://cloud-samples-data/video/animals.mp4", mime_type="video/mp4"
37+
),
38+
"What is in the video?",
39+
]
40+
)
41+
print(response.text)
42+
# Example response:
43+
# Here's a summary of the video's content.
44+
# The video shows a series of animals at the Los Angeles Zoo interacting
45+
# with waterproof cameras attached to various devices.
46+
# ...
47+
48+
# [END generativeaionvertexai_gemini_single_turn_video]
49+
return response.text
50+
51+
52+
if __name__ == "__main__":
53+
generate_text()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2024 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+
import audio_video_example
16+
import single_turn_video_example
17+
18+
19+
def test_analyze_video_with_audio() -> None:
20+
text = audio_video_example.analyze_video_with_audio()
21+
assert len(text) > 0
22+
23+
24+
def test_gemini_single_turn_video_example() -> None:
25+
text = single_turn_video_example.generate_text()
26+
text = text.lower()
27+
assert len(text) > 0
28+
assert any(
29+
[_ in text for _ in ("zoo", "tiger", "leaf", "water", "animals", "photos")]
30+
)

0 commit comments

Comments
 (0)