diff --git a/generative_ai/image_generation/edit_image_inpainting_insert_mask.py b/generative_ai/image_generation/edit_image_inpainting_insert_mask.py deleted file mode 100644 index beb2bd6351..0000000000 --- a/generative_ai/image_generation/edit_image_inpainting_insert_mask.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright 2024 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. - -"""Google Cloud Vertex AI sample for editing an image using a mask file. - Inpainting can insert the object designated by the prompt into the masked - area. -""" -import os - -from vertexai.preview import vision_models - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def edit_image_inpainting_insert_mask( - input_file: str, - mask_file: str, - output_file: str, - prompt: str, -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_edit_image_inpainting_insert_mask] - - import vertexai - from vertexai.preview.vision_models import Image, ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # PROJECT_ID = "your-project-id" - # input_file = "input-image.png" - # mask_file = "mask-image.png" - # output_file = "output-image.png" - # prompt = "red hat" # The text prompt describing what you want to see inserted. - - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = ImageGenerationModel.from_pretrained("imagegeneration@006") - base_img = Image.load_from_file(location=input_file) - mask_img = Image.load_from_file(location=mask_file) - - images = model.edit_image( - base_image=base_img, - mask=mask_img, - prompt=prompt, - edit_mode="inpainting-insert", - ) - - images[0].save(location=output_file, include_generation_parameters=False) - - # Optional. View the edited image in a notebook. - # images[0].show() - - print(f"Created output image using {len(images[0]._image_bytes)} bytes") - # Example response: - # Created output image using 1400814 bytes - - # [END generativeaionvertexai_imagen_edit_image_inpainting_insert_mask] - - return images - - -if __name__ == "__main__": - edit_image_inpainting_insert_mask( - input_file="test_resources/woman.png", - mask_file="test_resources/woman_inpainting_insert_mask.png", - output_file="test_resources/woman_with_hat.png", - prompt="red hat", - ) diff --git a/generative_ai/image_generation/edit_image_inpainting_insert_mask_mode.py b/generative_ai/image_generation/edit_image_inpainting_insert_mask_mode.py deleted file mode 100644 index e6f388dffe..0000000000 --- a/generative_ai/image_generation/edit_image_inpainting_insert_mask_mode.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright 2024 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. - -"""Google Cloud Vertex AI sample for editing an image using a mask mode. The - mask mode is used to automatically select the background, foreground (i.e., - the primary subject of the image), or an object based on segmentation class. - Inpainting can insert the object or background designated by the prompt. -""" -import os - -from vertexai.preview import vision_models - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def edit_image_inpainting_insert_mask_mode( - input_file: str, - mask_mode: str, - output_file: str, - prompt: str, -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_edit_image_inpainting_insert_mask_mode] - - import vertexai - from vertexai.preview.vision_models import Image, ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # PROJECT_ID = "your-project-id" - # input_file = "input-image.png" - # mask_mode = "background" # 'background', 'foreground', or 'semantic' - # output_file = "output-image.png" - # prompt = "beach" # The text prompt describing what you want to see inserted. - - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = ImageGenerationModel.from_pretrained("imagegeneration@006") - base_img = Image.load_from_file(location=input_file) - - images = model.edit_image( - base_image=base_img, - mask_mode=mask_mode, - prompt=prompt, - edit_mode="inpainting-insert", - ) - - images[0].save(location=output_file, include_generation_parameters=False) - - # Optional. View the edited image in a notebook. - # images[0].show() - - print(f"Created output image using {len(images[0]._image_bytes)} bytes") - # Example response: - # Created output image using 1234567 bytes - - # [END generativeaionvertexai_imagen_edit_image_inpainting_insert_mask_mode] - - return images - - -if __name__ == "__main__": - edit_image_inpainting_insert_mask_mode( - input_file="test_resources/woman.png", - mask_mode="background", - output_file="test_resources/woman_at_beach.png", - prompt="beach", - ) diff --git a/generative_ai/image_generation/edit_image_inpainting_insert_mask_mode_test.py b/generative_ai/image_generation/edit_image_inpainting_insert_mask_mode_test.py deleted file mode 100644 index bdae7e6041..0000000000 --- a/generative_ai/image_generation/edit_image_inpainting_insert_mask_mode_test.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2024 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 os - -import backoff - -from google.api_core.exceptions import ResourceExhausted -import pytest - -import edit_image_inpainting_insert_mask_mode - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_INPUT_FILE = os.path.join(_RESOURCES, "woman.png") -_MASK_MODE = "background" -_OUTPUT_FILE = os.path.join(_RESOURCES, "woman_at_beach.png") -_PROMPT = "beach" - - -@pytest.mark.skip("imagegeneration@006 samples pending deprecation") -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_edit_image_inpainting_insert_mask_mode() -> None: - response = ( - edit_image_inpainting_insert_mask_mode.edit_image_inpainting_insert_mask_mode( - _INPUT_FILE, - _MASK_MODE, - _OUTPUT_FILE, - _PROMPT, - ) - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/image_generation/edit_image_inpainting_insert_mask_test.py b/generative_ai/image_generation/edit_image_inpainting_insert_mask_test.py deleted file mode 100644 index 5fadcfa78d..0000000000 --- a/generative_ai/image_generation/edit_image_inpainting_insert_mask_test.py +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright 2024 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 os - -import backoff - -from google.api_core.exceptions import ResourceExhausted -import pytest - -import edit_image_inpainting_insert_mask - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_INPUT_FILE = os.path.join(_RESOURCES, "woman.png") -_MASK_FILE = os.path.join(_RESOURCES, "woman_inpainting_insert_mask.png") -_OUTPUT_FILE = os.path.join(_RESOURCES, "woman_with_hat.png") -_PROMPT = "hat" - - -@pytest.mark.skip("imagegeneration@006 samples pending deprecation") -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_edit_image_inpainting_insert_mask() -> None: - response = edit_image_inpainting_insert_mask.edit_image_inpainting_insert_mask( - _INPUT_FILE, - _MASK_FILE, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/image_generation/edit_image_inpainting_remove_mask.py b/generative_ai/image_generation/edit_image_inpainting_remove_mask.py deleted file mode 100644 index 6990e24e77..0000000000 --- a/generative_ai/image_generation/edit_image_inpainting_remove_mask.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright 2024 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. - -"""Google Cloud Vertex AI sample for editing an image using a mask file. - Inpainting can remove an object from the masked area. -""" -import os - -from vertexai.preview import vision_models - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def edit_image_inpainting_remove_mask( - input_file: str, - mask_file: str, - output_file: str, - prompt: str, -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_edit_image_inpainting_remove_mask] - - import vertexai - from vertexai.preview.vision_models import Image, ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # PROJECT_ID = "your-project-id" - # input_file = "input-image.png" - # mask_file = "mask-image.png" - # output_file = "outpur-image.png" - # prompt = "" # The text prompt describing the entire image. - - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = ImageGenerationModel.from_pretrained("imagegeneration@006") - base_img = Image.load_from_file(location=input_file) - mask_img = Image.load_from_file(location=mask_file) - - images = model.edit_image( - base_image=base_img, - mask=mask_img, - prompt=prompt, - edit_mode="inpainting-remove", - # Optional parameters - # negative_prompt="", # Describes the object being removed (i.e., "person") - ) - - images[0].save(location=output_file, include_generation_parameters=False) - - # Optional. View the edited image in a notebook. - # images[0].show() - - print(f"Created output image using {len(images[0]._image_bytes)} bytes") - # Example response: - # Created output image using 12345678 bytes - - # [END generativeaionvertexai_imagen_edit_image_inpainting_remove_mask] - - return images - - -if __name__ == "__main__": - edit_image_inpainting_remove_mask( - input_file="test_resources/volleyball_game.png", - mask_file="test_resources/volleyball_game_inpainting_remove_mask.png", - output_file="test_resources/volleyball_game_single_blue_player.png", - prompt="volleyball game", - ) diff --git a/generative_ai/image_generation/edit_image_inpainting_remove_mask_mode.py b/generative_ai/image_generation/edit_image_inpainting_remove_mask_mode.py deleted file mode 100644 index 2130fdc6da..0000000000 --- a/generative_ai/image_generation/edit_image_inpainting_remove_mask_mode.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright 2024 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. - -"""Google Cloud Vertex AI sample for editing an image using a mask mode. The - mask mode is used to automatically select the background, foreground (i.e., - the primary subject of the image), or an object based on segmentation class. - Inpainting can remove the object or background designated by the prompt. -""" -import os - -from vertexai.preview import vision_models - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def edit_image_inpainting_remove_mask_mode( - input_file: str, - mask_mode: str, - output_file: str, - prompt: str, -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_edit_image_inpainting_remove_mask_mode] - - import vertexai - from vertexai.preview.vision_models import Image, ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # PROJECT_ID = "your-project-id" - # input_file = "input-image.png" - # mask_mode = "foreground" # 'background', 'foreground', or 'semantic' - # output_file = "output-image.png" - # prompt = "sports car" # The text prompt describing what you want to see in the edited image. - - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = ImageGenerationModel.from_pretrained("imagegeneration@006") - base_img = Image.load_from_file(location=input_file) - - images = model.edit_image( - base_image=base_img, - mask_mode=mask_mode, - prompt=prompt, - edit_mode="inpainting-remove", - ) - - images[0].save(location=output_file, include_generation_parameters=False) - - # Optional. View the edited image in a notebook. - # images[0].show() - - print(f"Created output image using {len(images[0]._image_bytes)} bytes") - # Example response: - # Created output image using 1279948 bytes - - # [END generativeaionvertexai_imagen_edit_image_inpainting_remove_mask_mode] - - return images - - -if __name__ == "__main__": - edit_image_inpainting_remove_mask_mode( - input_file="test_resources/woman.png", - mask_mode="foreground", - output_file="test_resources/sports_car.png", - prompt="sports car", - ) diff --git a/generative_ai/image_generation/edit_image_inpainting_remove_mask_mode_test.py b/generative_ai/image_generation/edit_image_inpainting_remove_mask_mode_test.py deleted file mode 100644 index 68dea24551..0000000000 --- a/generative_ai/image_generation/edit_image_inpainting_remove_mask_mode_test.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2024 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 os - -import backoff - -from google.api_core.exceptions import ResourceExhausted -import pytest - -import edit_image_inpainting_remove_mask_mode - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_INPUT_FILE = os.path.join(_RESOURCES, "woman.png") -_MASK_MODE = "foreground" -_OUTPUT_FILE = os.path.join(_RESOURCES, "sports_car.png") -_PROMPT = "sports car" - - -@pytest.mark.skip("imagegeneration@006 samples pending deprecation") -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_edit_image_inpainting_remove_mask_mode() -> None: - response = ( - edit_image_inpainting_remove_mask_mode.edit_image_inpainting_remove_mask_mode( - _INPUT_FILE, - _MASK_MODE, - _OUTPUT_FILE, - _PROMPT, - ) - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/image_generation/edit_image_inpainting_remove_mask_test.py b/generative_ai/image_generation/edit_image_inpainting_remove_mask_test.py deleted file mode 100644 index b11b1b1605..0000000000 --- a/generative_ai/image_generation/edit_image_inpainting_remove_mask_test.py +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2024 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 os - -import backoff - -from google.api_core.exceptions import ResourceExhausted -import pytest - -import edit_image_inpainting_remove_mask - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_INPUT_FILE = os.path.join(_RESOURCES, "volleyball_game.png") -_MASK_FILE = os.path.join(_RESOURCES, "volleyball_game_inpainting_remove_mask.png") -_OUTPUT_FILE = os.path.join(_RESOURCES, "volleyball_game_single_blue_player.png") -_PROMPT = "volleyball game" - - -@pytest.mark.skip("imagegeneration@006 samples pending deprecation") -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_edit_image_inpainting_remove_mask() -> None: - response = edit_image_inpainting_remove_mask.edit_image_inpainting_remove_mask( - _INPUT_FILE, - _MASK_FILE, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/prompts/prompt_template.py b/generative_ai/prompts/prompt_template.py deleted file mode 100644 index 7517c7bb66..0000000000 --- a/generative_ai/prompts/prompt_template.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright 2024 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 os - -from vertexai.generative_models import GenerationResponse - - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def prompt_template_example() -> list[GenerationResponse]: - """Build a parameterized prompt template to generate content with multiple variable sets""" - - # [START generativeaionvertexai_prompt_template] - import vertexai - from vertexai.preview.prompts import Prompt - - # Initialize vertexai - vertexai.init(project=PROJECT_ID, location="us-central1") - - variables = [ - {"animal": "Eagles", "activity": "eat berries"}, - {"animal": "Coyotes", "activity": "jump"}, - {"animal": "Squirrels", "activity": "fly"} - ] - - # define prompt template - prompt = Prompt( - prompt_data="Do {animal} {activity}?", - model_name="gemini-2.0-flash-001", - variables=variables, - system_instruction="You are a helpful zoologist" - # generation_config=generation_config, # Optional - # safety_settings=safety_settings, # Optional - ) - - # Generates content using the assembled prompt. - responses = [] - for variable_set in prompt.variables: - response = prompt.generate_content( - contents=prompt.assemble_contents(**variable_set) - ) - responses.append(response) - - for response in responses: - print(response.text, end="") - - # Example response - # Assembled prompt replacing: 1 instances of variable animal, 1 instances of variable activity - # Eagles are primarily carnivorous. While they might *accidentally* ingest a berry...... - # [END generativeaionvertexai_prompt_template] - return responses - - -if __name__ == "__main__": - prompt_template_example() diff --git a/generative_ai/prompts/test_prompt_template.py b/generative_ai/prompts/test_prompt_template.py index 92c358e5d1..218df2e596 100644 --- a/generative_ai/prompts/test_prompt_template.py +++ b/generative_ai/prompts/test_prompt_template.py @@ -20,12 +20,6 @@ import prompt_list_prompts import prompt_list_version # import prompt_restore_version -import prompt_template - - -def test_prompt_template() -> None: - text = prompt_template.prompt_template_example() - assert len(text) > 2 def test_prompt_create() -> None: