Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Imagen): Add python sdk samples for Multimodal Embeddings #10267

Merged
merged 14 commits into from
Feb 22, 2024
Merged
47 changes: 47 additions & 0 deletions generative_ai/text_embedding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2023 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.

# [START aiplatform_sdk_text_embedding]
from google.cloud.aiplatform.private_preview.vision_models import ImageGenerationModel
holtskinner marked this conversation as resolved.
Show resolved Hide resolved


def generate_image_from_text(
prompt: str = "Google company logo",
) -> object:
"""Example of how to generate an image from a text prompt.

Args:
temperature: The temperature of the text prompt.
"""

model = ImageGenerationModel.from_pretrained("imagegeneration")
images = model.generate_images(
prompt=prompt,
# Optional:
negative_prompt="bad quality",
seed=1,
width=1024,
height=512,
guidance_scale=20,
number_of_images=1,)

images[0].show()
print(f"Response from Model: {images[0]}")
# [END aiplatform_sdk_text_embedding]

return images


if __name__ == "__main__":
generate_image_from_text()
24 changes: 24 additions & 0 deletions generative_ai/text_embedding_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 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.

import backoff
from google.api_core.exceptions import ResourceExhausted

import text_embedding


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_text_embedding() -> None:
images = text_embedding.generate_image_from_text(prompt="Google company logo")
assert images[0] is not None
44 changes: 44 additions & 0 deletions generative_ai/text_image_embedding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2023 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.

# [START aiplatform_sdk_text_image_embedding]
from google.cloud.aiplatform.private_preview.vision_models import ImageGenerationModel
holtskinner marked this conversation as resolved.
Show resolved Hide resolved


def generate_image_from_text_and_image(
prompt: str = "Ancient yellowed paper scroll",
) -> object:
"""Example of how to generate an image from a text and an image.

Args:
prompt: The prompt to generate an image from.
"""

model = ImageGenerationModel.from_pretrained("imagegeneration")
base_image = model.generate_images("Google company logo", seed=1)
images = model.generate_images(
prompt=prompt,
seed=1,
base_image=base_image[0],
guidance_scale=20)

images[0].show()
print(f"Response from Model: {images[0]}")
# [END aiplatform_sdk_text_image_embedding]

return images


if __name__ == "__main__":
generate_image_from_text_and_image()
24 changes: 24 additions & 0 deletions generative_ai/text_image_embedding_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 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.

import backoff
from google.api_core.exceptions import ResourceExhausted

import text_image_embedding


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_text_embedding() -> None:
images = text_image_embedding.generate_image_from_text_and_image(prompt="Ancient yellowed paper scroll")
assert images[0] is not None