Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 01d1fb5

Browse filesBrowse files
msampathkumargcf-owl-bot[bot]rsamborski
authored
feat(generative_ai): add gemini pro basic and config examples (GoogleCloudPlatform#11034)
* fix(generative_ai): add gemini pro basic and config examples * fix(generative_ai): add gemini pro basic and config examples * fix(generative_ai): update region tags * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore(generative_ai): code cleanup * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore(generative_ai): code formatting * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore(generative_ai): code formatting * code formatting * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * code cleanup * fix: update file path * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: update model input args * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update generative_ai/gemini_pro_config_example.py Co-authored-by: Remigiusz Samborski <rsamborski@users.noreply.github.com> * Update generative_ai/gemini_pro_config_example.py Co-authored-by: Remigiusz Samborski <rsamborski@users.noreply.github.com> * chore(generative_ai): update region tags and fix prompt grammar --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Remigiusz Samborski <rsamborski@users.noreply.github.com>
1 parent cf58e25 commit 01d1fb5
Copy full SHA for 01d1fb5

File tree

Expand file treeCollapse file tree

5 files changed

+113
-11
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+113
-11
lines changed

‎generative_ai/gemini_guide_example.py

Copy file name to clipboardExpand all lines: generative_ai/gemini_guide_example.py
-4Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,4 @@ def generate_text(project_id: str, location: str) -> str:
4141
return response.text
4242

4343

44-
# TODO(developer): Update PROJECT_ID value and un-comment below lines
45-
# project_id = "PROJECT_ID"
46-
# location = "us-central1"
47-
# generate_text(project_id, location)
4844
# [END aiplatform_gemini_get_started]
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2023 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+
# [START aiplatform_gemini_pro_example]
16+
import vertexai
17+
from vertexai.preview.generative_models import GenerativeModel, Part
18+
19+
20+
def generate_text(project_id: str, location: str) -> None:
21+
# Initialize Vertex AI
22+
vertexai.init(project=project_id, location=location)
23+
24+
# Load the model
25+
model = GenerativeModel(model_name="gemini-pro-vision")
26+
27+
# Load example image
28+
image_url = "gs://generativeai-downloads/images/scones.jpg"
29+
image_content = Part.from_uri(image_url, "image/jpeg")
30+
31+
# Query the model
32+
response = model.generate_content([image_content, "what is this image?"])
33+
print(response)
34+
35+
return response.text
36+
37+
38+
# [END aiplatform_gemini_pro_example]
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2023 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+
# [START aiplatform_gemini_pro_config_example]
16+
import base64
17+
18+
import vertexai
19+
from vertexai.preview.generative_models import GenerativeModel, Part
20+
21+
22+
def generate_text(project_id: str, location: str) -> None:
23+
# Initialize Vertex AI
24+
vertexai.init(project=project_id, location=location)
25+
26+
# Load the model
27+
model = GenerativeModel("gemini-pro-vision")
28+
29+
# Load example image from local storage
30+
encoded_image = base64.b64encode(open("scones.jpg", "rb").read()).decode("utf-8")
31+
image_content = Part.from_data(
32+
data=base64.b64decode(encoded_image), mime_type="image/jpeg"
33+
)
34+
35+
# Generation Config
36+
config = {"max_output_tokens": 2048, "temperature": 0.4, "top_p": 1, "top_k": 32}
37+
38+
# Generate text
39+
response = model.generate_content(
40+
[image_content, "what is this image?"], generation_config=config
41+
)
42+
print(response.text)
43+
return response.text
44+
45+
46+
# [END aiplatform_gemini_pro_config_example]

‎generative_ai/gemini_safety_config_example.py

Copy file name to clipboardExpand all lines: generative_ai/gemini_safety_config_example.py
-7Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,3 @@ def generate_text(project_id: str, location: str, image: str) -> str:
5050

5151

5252
# [END aiplatform_gemini_safety_settings]
53-
54-
55-
# if __name__ == '__main__':
56-
# import base64
57-
# base64_image_data = base64.b64encode(open('scones.jpg', 'rb').read()).decode("utf-8")
58-
# image = generative_models.Part.from_data(data=base64.b64decode(base64_image_data), mime_type="image/png")
59-
# generate_text(image)

‎generative_ai/test_gemini_examples.py

Copy file name to clipboardExpand all lines: generative_ai/test_gemini_examples.py
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import gemini_count_token_example
2121
import gemini_guide_example
2222
import gemini_multi_image_example
23+
import gemini_pro_basic_example
24+
import gemini_pro_config_example
2325
import gemini_safety_config_example
2426
import gemini_single_turn_video_example
2527

@@ -37,6 +39,33 @@ def test_gemini_guide_example() -> None:
3739
assert "scones" in text
3840

3941

42+
def test_gemini_pro_basic_example() -> None:
43+
text = gemini_pro_basic_example.generate_text(PROJECT_ID, LOCATION)
44+
text = text.lower()
45+
assert len(text) > 0
46+
assert "recipe" in text or "ingredients" in text or "table" in text
47+
48+
49+
def test_gemini_pro_config_example() -> None:
50+
import urllib.request
51+
52+
# download the image
53+
fname = "scones.jpg"
54+
url = "https://storage.googleapis.com/generativeai-downloads/images/scones.jpg"
55+
urllib.request.urlretrieve(url, fname)
56+
57+
if os.path.isfile(fname):
58+
text = gemini_pro_config_example.generate_text(PROJECT_ID, LOCATION)
59+
text = text.lower()
60+
assert len(text) > 0
61+
assert "recipe" in text or "table" in text
62+
63+
# clean-up
64+
os.remove(fname)
65+
else:
66+
raise Exception("File(scones.jpg) not found!")
67+
68+
4069
def test_gemini_multi_image_example() -> None:
4170
text = gemini_multi_image_example.generate_text_multimodal(PROJECT_ID, LOCATION)
4271
text = text.lower()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.