Skip to content

Commit 0269120

Browse files
committed
adding lessons
1 parent e0ea93e commit 0269120

22 files changed

+6346
-0
lines changed

lesson-2/Andrew_power_tools.png

710 KB
Loading

lesson-2/L2_colab_prompting_and_parameters.ipynb

+2,033
Large diffs are not rendered by default.

lesson-2/panda.png

975 KB
Loading

lesson-2/utils.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
### needed for "authenticate" function
2+
import os
3+
from dotenv import load_dotenv
4+
import json
5+
import base64
6+
from google.auth.transport.requests import Request
7+
from google.oauth2.service_account import Credentials
8+
##########
9+
10+
import typing
11+
import IPython.display
12+
from PIL import Image as PIL_Image
13+
from PIL import ImageOps as PIL_ImageOps
14+
15+
from vertexai.generative_models import (
16+
GenerationConfig,
17+
Image,
18+
Part,
19+
)
20+
21+
def gemini(prompt, model):
22+
responses = model.generate_content(prompt,
23+
stream=True)
24+
25+
response_text = ""
26+
for response in responses:
27+
response_text += response.text
28+
29+
return response_text
30+
31+
def display_images(
32+
images: typing.Iterable[Image],
33+
max_width: int = 600,
34+
max_height: int = 350,
35+
) -> None:
36+
for image in images:
37+
pil_image = typing.cast(PIL_Image.Image, image._pil_image)
38+
if pil_image.mode != "RGB":
39+
# RGB is supported by all Jupyter environments (e.g. RGBA is not yet)
40+
pil_image = pil_image.convert("RGB")
41+
image_width, image_height = pil_image.size
42+
if max_width < image_width or max_height < image_height:
43+
# Resize to display a smaller notebook image
44+
pil_image = PIL_ImageOps.contain(pil_image, (max_width, max_height))
45+
IPython.display.display(pil_image)
46+
47+
def print_multimodal_prompt(contents: list):
48+
"""
49+
Given contents that would be sent to Gemini,
50+
output the full multimodal prompt for ease of readability.
51+
"""
52+
for content in contents:
53+
if isinstance(content, Image):
54+
display_images([content])
55+
elif isinstance(content, Part):
56+
url = get_url_from_gcs(content.file_data.file_uri)
57+
IPython.display.display(load_image_from_url(url))
58+
else:
59+
print(content)
60+
61+
def gemini_vision(contents_image, model):
62+
63+
responses = model.generate_content(
64+
contents_image,
65+
stream=True)
66+
67+
response_text = ""
68+
for response in responses:
69+
response_text += response.text
70+
return response_text
71+
72+
def gemini_vision_parameters(contents_image, model, config):
73+
74+
responses = model.generate_content(
75+
contents=contents_image,
76+
generation_config=config,
77+
stream=True
78+
)
79+
80+
response_text = ""
81+
for response in responses:
82+
response_text += response.text
83+
84+
return response_text
85+
86+
def authenticate():
87+
#Load .env
88+
load_dotenv()
89+
90+
#Decode key and store in .JSON
91+
SERVICE_ACCOUNT_KEY_STRING_B64 = os.getenv('SERVICE_ACCOUNT_KEY')
92+
SERVICE_ACCOUNT_KEY_BYTES_B64 = SERVICE_ACCOUNT_KEY_STRING_B64.encode("ascii")
93+
SERVICE_ACCOUNT_KEY_STRING_BYTES = base64.b64decode(SERVICE_ACCOUNT_KEY_BYTES_B64)
94+
SERVICE_ACCOUNT_KEY_STRING = SERVICE_ACCOUNT_KEY_STRING_BYTES.decode("ascii")
95+
96+
SERVICE_ACCOUNT_KEY = json.loads(SERVICE_ACCOUNT_KEY_STRING)
97+
98+
99+
# Create credentials based on key from service account
100+
# Make sure your account has the roles listed in the Google Cloud Setup section
101+
credentials = Credentials.from_service_account_info(
102+
SERVICE_ACCOUNT_KEY,
103+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
104+
105+
if credentials.expired:
106+
credentials.refresh(Request())
107+
108+
#Set project ID according to environment variable
109+
PROJECT_ID = os.getenv('PROJECT_ID')
110+
111+
return credentials, PROJECT_ID
112+

0 commit comments

Comments
 (0)