Skip to content

Commit 3c389f3

Browse files
authored
feat: update Imagen 2 samples for usability (GoogleCloudPlatform#11540)
* feat: update Imagen 2 samples for usability * Trigger build * Trigger build
1 parent ec0a9a2 commit 3c389f3

File tree

2 files changed

+19
-59
lines changed

2 files changed

+19
-59
lines changed

generative_ai/imagen/generate_image.py

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,87 +14,49 @@
1414

1515
"""Google Cloud Vertex AI sample for generating an image using only
1616
descriptive text as an input.
17-
Example usage:
18-
python generate_image.py --project_id <project-id> --location <location> \
19-
--output_file <filepath> --prompt <text>
2017
"""
2118

22-
# [START generativeaionvertexai_imagen_generate_image]
19+
from vertexai.preview import vision_models
2320

24-
import argparse
2521

26-
import vertexai
27-
from vertexai.preview.vision_models import ImageGenerationModel
22+
def generate_image(
23+
project_id: str, output_file: str, prompt: str
24+
) -> vision_models.ImageGenerationResponse:
2825

26+
# [START generativeaionvertexai_imagen_generate_image]
2927

30-
def generate_image(
31-
project_id: str, location: str, output_file: str, prompt: str
32-
) -> vertexai.preview.vision_models.ImageGenerationResponse:
33-
"""Generate an image using a text prompt.
34-
Args:
35-
project_id: Google Cloud project ID, used to initialize Vertex AI.
36-
location: Google Cloud region, used to initialize Vertex AI.
37-
output_file: Local path to the output image file.
38-
prompt: The text prompt describing what you want to see."""
28+
import vertexai
29+
from vertexai.preview.vision_models import ImageGenerationModel
30+
31+
# TODO(developer): Update and un-comment below lines
32+
# project_id = "PROJECT_ID"
33+
# output_file = "my-output.png"
34+
# prompt = "" # The text prompt describing what you want to see.
3935

40-
vertexai.init(project=project_id, location=location)
36+
vertexai.init(project=project_id, location="us-central1")
4137

4238
model = ImageGenerationModel.from_pretrained("imagegeneration@006")
4339

4440
images = model.generate_images(
4541
prompt=prompt,
4642
# Optional parameters
4743
number_of_images=1,
48-
language="en", # prompt language
49-
# By default, a SynthID watermark is added to images, but you can
50-
# disable it. You can't use a seed value and watermark at the same time.
44+
language="en",
45+
# You can't use a seed value and watermark at the same time.
5146
# add_watermark=False,
5247
# seed=100,
53-
aspect_ratio="1:1", # "9:16" "16:9" "4:3" "3:4"
54-
# Adds a filter level to Safety filtering: "block_most" (most strict blocking),
55-
# "block_some" (default), "block_few", or "block_fewest" (available to
56-
# allowlisted users only).
48+
aspect_ratio="1:1",
5749
safety_filter_level="block_some",
58-
# Allows generation of people by the model: "dont_allow" (block
59-
# all people), "allow_adult" (default; allow adults but not children),
60-
# "allow_all" (available to allowlisted users only; allow adults and children)
6150
person_generation="allow_adult",
6251
)
6352

64-
images[0].save(location=output_file, include_generation_parameters=True)
53+
images[0].save(location=output_file, include_generation_parameters=False)
6554

6655
# Optional. View the generated image in a notebook.
6756
# images[0].show()
6857

6958
print(f"Created output image using {len(images[0]._image_bytes)} bytes")
7059

71-
return images
72-
73-
74-
# [END generativeaionvertexai_imagen_generate_image]
60+
# [END generativeaionvertexai_imagen_generate_image]
7561

76-
if __name__ == "__main__":
77-
parser = argparse.ArgumentParser()
78-
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
79-
parser.add_argument(
80-
"--location",
81-
help="The location in which to initialize Vertex AI.",
82-
default="us-central1",
83-
)
84-
parser.add_argument(
85-
"--output_file",
86-
help="The local path to the output file (e.g., 'my-output.png').",
87-
required=True,
88-
)
89-
parser.add_argument(
90-
"--prompt",
91-
help="The text prompt describing what you want to see (e.g., 'a dog reading a newspaper').",
92-
required=True,
93-
)
94-
args = parser.parse_args()
95-
generate_image(
96-
args.project_id,
97-
args.location,
98-
args.output_file,
99-
args.prompt,
100-
)
62+
return images

generative_ai/imagen/generate_image_test.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources")
2525
_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
26-
_LOCATION = "us-central1"
2726
_OUTPUT_FILE = os.path.join(_RESOURCES, "dog_newspaper.png")
2827
_PROMPT = "a dog reading a newspaper"
2928

@@ -32,7 +31,6 @@
3231
def test_generate_image() -> None:
3332
response = generate_image.generate_image(
3433
_PROJECT_ID,
35-
_LOCATION,
3634
_OUTPUT_FILE,
3735
_PROMPT,
3836
)

0 commit comments

Comments
 (0)