diff --git a/generative_ai/anthropic_claude_3_streaming.py b/generative_ai/anthropic_claude_3_streaming.py deleted file mode 100644 index 5dd2db69e798..000000000000 --- a/generative_ai/anthropic_claude_3_streaming.py +++ /dev/null @@ -1,53 +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 - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_text_streaming() -> str: - # [START generativeaionvertexai_claude_3_streaming] - # TODO(developer): Vertex AI SDK - uncomment below & run - # pip3 install --upgrade --user google-cloud-aiplatform - # gcloud auth application-default login - # pip3 install -U 'anthropic[vertex]' - # TODO(developer): Update and un-comment below line - # PROJECT_ID = "PROJECT_ID" - - from anthropic import AnthropicVertex - - client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5") - result = [] - - with client.messages.stream( - model="claude-3-5-sonnet@20240620", - max_tokens=1024, - messages=[ - { - "role": "user", - "content": "Send me a recipe for banana bread.", - } - ], - ) as stream: - for text in stream.text_stream: - print(text, end="", flush=True) - result.append(text) - - # [END generativeaionvertexai_claude_3_streaming] - return "".join(result) - - -if __name__ == "__main__": - generate_text_streaming() diff --git a/generative_ai/anthropic_claude_3_streaming_test.py b/generative_ai/anthropic_claude_3_streaming_test.py deleted file mode 100644 index ed9bd54a878d..000000000000 --- a/generative_ai/anthropic_claude_3_streaming_test.py +++ /dev/null @@ -1,24 +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 backoff -from google.api_core.exceptions import ResourceExhausted - -import anthropic_claude_3_streaming - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_generate_text_streaming() -> None: - responses = anthropic_claude_3_streaming.generate_text_streaming() - assert "bread" in responses diff --git a/generative_ai/anthropic_claude_3_tool_use.py b/generative_ai/anthropic_claude_3_tool_use.py deleted file mode 100644 index 13a163a5ab3b..000000000000 --- a/generative_ai/anthropic_claude_3_tool_use.py +++ /dev/null @@ -1,62 +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. - - -def tool_use(project_id: str) -> object: - # [START generativeaionvertexai_claude_3_tool_use] - # TODO(developer): Vertex AI SDK - uncomment below & run - # pip3 install --upgrade --user google-cloud-aiplatform - # gcloud auth application-default login - # pip3 install -U 'anthropic[vertex]' - from anthropic import AnthropicVertex - - # TODO(developer): Update your project ID and region - client = AnthropicVertex(project_id=project_id, region="us-east5") - message = client.messages.create( - model="claude-3-5-sonnet@20240620", - max_tokens=1024, - tools=[ - { - "name": "text_search_places_api", - "description": "returns information about a set of places based on a string", - "input_schema": { - "type": "object", - "properties": { - "textQuery": { - "type": "string", - "description": "The text string on which to search", - }, - "priceLevels": { - "type": "array", - "description": "Price levels to query places, value can be one of [PRICE_LEVEL_INEXPENSIVE, PRICE_LEVEL_MODERATE, PRICE_LEVEL_EXPENSIVE, PRICE_LEVEL_VERY_EXPENSIVE]", - }, - "openNow": { - "type": "boolean", - "description": "whether those places are open for business.", - }, - }, - "required": ["textQuery"], - }, - } - ], - messages=[ - { - "role": "user", - "content": "What are some affordable and good Italian restaurants open now in San Francisco??", - } - ], - ) - print(message.model_dump_json(indent=2)) - # [END generativeaionvertexai_claude_3_tool_use] - return message diff --git a/generative_ai/anthropic_claude_3_tool_use_test.py b/generative_ai/anthropic_claude_3_tool_use_test.py deleted file mode 100644 index a8a75f8455c7..000000000000 --- a/generative_ai/anthropic_claude_3_tool_use_test.py +++ /dev/null @@ -1,31 +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 anthropic_claude_3_tool_use - - -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_tool_use() -> None: - response = anthropic_claude_3_tool_use.tool_use(_PROJECT_ID) - json_response = response.model_dump_json(indent=2) - assert "restaurant" in json_response - assert "tool_use" in json_response - assert "text_search_places_api" in json_response diff --git a/generative_ai/anthropic_claude_3_unary.py b/generative_ai/anthropic_claude_3_unary.py deleted file mode 100644 index 0b3f4b57d4b9..000000000000 --- a/generative_ai/anthropic_claude_3_unary.py +++ /dev/null @@ -1,47 +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 - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_text() -> object: - # [START generativeaionvertexai_claude_3_unary] - # TODO(developer): Vertex AI SDK - uncomment below & run - # pip3 install --upgrade --user google-cloud-aiplatform - # gcloud auth application-default login - # pip3 install -U 'anthropic[vertex]' - # TODO(developer): Update and un-comment below line - # PROJECT_ID = "PROJECT_ID" - - from anthropic import AnthropicVertex - - client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5") - message = client.messages.create( - model="claude-3-5-sonnet@20240620", - max_tokens=1024, - messages=[ - { - "role": "user", - "content": "Send me a recipe for banana bread.", - } - ], - ) - print(message.model_dump_json(indent=2)) - # [END generativeaionvertexai_claude_3_unary] - return message - - -if __name__ == "__main__": - generate_text() diff --git a/generative_ai/anthropic_claude_3_unary_test.py b/generative_ai/anthropic_claude_3_unary_test.py deleted file mode 100644 index 0a0330ad3f62..000000000000 --- a/generative_ai/anthropic_claude_3_unary_test.py +++ /dev/null @@ -1,25 +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 backoff -from google.api_core.exceptions import ResourceExhausted - -import anthropic_claude_3_unary - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_generate_text() -> None: - responses = anthropic_claude_3_unary.generate_text() - assert "bread" in responses.model_dump_json(indent=2) diff --git a/generative_ai/distillation.py b/generative_ai/distillation.py deleted file mode 100644 index ca3b82f7117d..000000000000 --- a/generative_ai/distillation.py +++ /dev/null @@ -1,69 +0,0 @@ -# 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 generativeaionvertexai_sdk_distillation] -from __future__ import annotations - -import os - -from typing import Optional - -import vertexai -from vertexai.preview.language_models import TextGenerationModel, TuningEvaluationSpec - - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def distill_model( - dataset: str, - source_model: str, - evaluation_dataset: Optional[str] = None, -) -> None: - """Distill a new model using a teacher model and a dataset. - Args: - dataset (str): GCS URI of the JSONL file containing the training data. - E.g., "gs://[BUCKET]/[FILENAME].jsonl". - source_model (str): Name of the teacher model to distill from. - E.g., "text-unicorn@001". - evaluation_dataset (Optional[str]): GCS URI of the JSONL file containing the evaluation data. - """ - # TODO developer - override these parameters as needed: - vertexai.init(project=PROJECT_ID, location="us-central1") - - # Create a tuning evaluation specification with the evaluation dataset - eval_spec = TuningEvaluationSpec(evaluation_data=evaluation_dataset) - - # Load the student model from a pre-trained model - student_model = TextGenerationModel.from_pretrained("text-bison@002") - - # Start the distillation job using the teacher model and dataset - distillation_job = student_model.distill_from( - teacher_model=source_model, - dataset=dataset, - # Optional: - train_steps=300, # Number of training steps to use when tuning the model. - evaluation_spec=eval_spec, - ) - - return distillation_job - - -# [END generativeaionvertexai_sdk_distillation] -if __name__ == "__main__": - distill_model( - dataset="your-dataset-uri", - source_model="your-source-model", - evaluation_dataset="your-evaluation-dataset-uri", - ) diff --git a/generative_ai/distillation_test.py b/generative_ai/distillation_test.py deleted file mode 100644 index 421bf7922382..000000000000 --- a/generative_ai/distillation_test.py +++ /dev/null @@ -1,106 +0,0 @@ -# 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 os -import uuid - -from google.cloud import aiplatform -from google.cloud import storage -from google.cloud.aiplatform.compat.types import pipeline_state -import pytest -from vertexai.preview.language_models import TextGenerationModel - -import distillation - -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_LOCATION = "us-central1" -_BUCKET = os.environ["CLOUD_STORAGE_BUCKET"] - - -def get_model_display_name(tuned_model: TextGenerationModel) -> str: - language_model_tuning_job = tuned_model._job - pipeline_job = language_model_tuning_job._job - return dict(pipeline_job._gca_resource.runtime_config.parameter_values)[ - "model_display_name" - ] - - -def upload_to_gcs(bucket: str, name: str, data: str) -> None: - client = storage.Client() - bucket = client.get_bucket(bucket) - blob = bucket.blob(name) - blob.upload_from_string(data) - - -def download_from_gcs(bucket: str, name: str) -> str: - client = storage.Client() - bucket = client.get_bucket(bucket) - blob = bucket.blob(name) - data = blob.download_as_bytes() - return "\n".join(data.decode().splitlines()[:10]) - - -def delete_from_gcs(bucket: str, name: str) -> None: - client = storage.Client() - bucket = client.get_bucket(bucket) - blob = bucket.blob(name) - blob.delete() - - -@pytest.fixture(scope="function") -def training_data_filename() -> str: - temp_filename = f"{uuid.uuid4()}.jsonl" - data = download_from_gcs( - "cloud-samples-data", "ai-platform/generative_ai/headline_classification.jsonl" - ) - upload_to_gcs(_BUCKET, temp_filename, data) - try: - yield f"gs://{_BUCKET}/{temp_filename}" - finally: - delete_from_gcs(_BUCKET, temp_filename) - - -def teardown_model( - tuned_model: TextGenerationModel, training_data_filename: str -) -> None: - for tuned_model_name in tuned_model.list_tuned_model_names(): - model_registry = aiplatform.models.ModelRegistry(model=tuned_model_name) - if ( - training_data_filename - in model_registry.get_version_info("1").model_display_name - ): - display_name = model_registry.get_version_info("1").model_display_name - for endpoint in aiplatform.Endpoint.list(): - for _ in endpoint.list_models(): - if endpoint.display_name == display_name: - endpoint.undeploy_all() - endpoint.delete() - aiplatform.Model(model_registry.model_resource_name).delete() - - -@pytest.mark.skip("Blocked on b/277959219") -def test_distill_model(training_data_filename: str) -> None: - """Takes approx. 60 minutes.""" - student_model = distillation.distill_model( - dataset=training_data_filename, - teacher_model="text-unicorn@001", - evaluation_dataset=training_data_filename, - ) - try: - assert ( - student_model._job.state - == pipeline_state.PipelineState.PIPELINE_STATE_SUCCEEDED - ) - finally: - teardown_model(student_model, training_data_filename) diff --git a/generative_ai/evaluate_model.py b/generative_ai/evaluate_model.py deleted file mode 100644 index e7ee64ca3141..000000000000 --- a/generative_ai/evaluate_model.py +++ /dev/null @@ -1,60 +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. - -# [START generativeaionvertexai_evaluate_model] -import os - -from google.auth import default - -import vertexai -from vertexai.preview.language_models import ( - EvaluationTextClassificationSpec, - TextGenerationModel, -) - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def evaluate_model() -> object: - """Evaluate the performance of a generative AI model.""" - - # Set credentials for the pipeline components used in the evaluation task - credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) - - # TODO (developer): update project_id - vertexai.init(project=PROJECT_ID, location="us-central1", credentials=credentials) - - # Create a reference to a generative AI model - model = TextGenerationModel.from_pretrained("text-bison@002") - - # Define the evaluation specification for a text classification task - task_spec = EvaluationTextClassificationSpec( - ground_truth_data=[ - "gs://cloud-samples-data/ai-platform/generative_ai/llm_classification_bp_input_prompts_with_ground_truth.jsonl" - ], - class_names=["nature", "news", "sports", "health", "startups"], - target_column_name="ground_truth", - ) - - # Evaluate the model - eval_metrics = model.evaluate(task_spec=task_spec) - print(eval_metrics) - return eval_metrics - - -# [END generativeaionvertexai_evaluate_model] - - -if __name__ == "__main__": - evaluate_model() diff --git a/generative_ai/evaluate_model_test.py b/generative_ai/evaluate_model_test.py deleted file mode 100644 index 7017020f0975..000000000000 --- a/generative_ai/evaluate_model_test.py +++ /dev/null @@ -1,32 +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 backoff - -from google.api_core.exceptions import ResourceExhausted - -import pytest - -import evaluate_model - - -@pytest.mark.skip( - reason="Model is giving 404 Not found error." - "Need to investigate. Created an issue tracker is at " - "python-docs-samples/issues/11264" -) -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_evaluate_model() -> None: - eval_metrics = evaluate_model.evaluate_model() - assert hasattr(eval_metrics, "auRoc") diff --git a/generative_ai/gemini_grounding_example.py b/generative_ai/gemini_grounding_example.py deleted file mode 100644 index 9ccf7cf57d36..000000000000 --- a/generative_ai/gemini_grounding_example.py +++ /dev/null @@ -1,99 +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. -# TODO: Delete this file after approval /grounding/web_example.py & /grounding/vais_example.py -import os - -from vertexai.generative_models import GenerationResponse - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_text_with_grounding_web() -> GenerationResponse: - # [START generativeaionvertexai_gemini_grounding_with_web] - import vertexai - - from vertexai.generative_models import ( - GenerationConfig, - GenerativeModel, - Tool, - grounding, - ) - - # TODO (developer): update project_id - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = GenerativeModel("gemini-1.5-flash-002") - - # Use Google Search for grounding - tool = Tool.from_google_search_retrieval(grounding.GoogleSearchRetrieval()) - - prompt = "When is the next total solar eclipse in US?" - response = model.generate_content( - prompt, - tools=[tool], - generation_config=GenerationConfig( - temperature=0.0, - ), - ) - - print(response.text) - - # [END generativeaionvertexai_gemini_grounding_with_web] - return response - - -def generate_text_with_grounding_vertex_ai_search( - data_store_id: str, -) -> GenerationResponse: - # [START generativeaionvertexai_gemini_grounding_with_vais] - import vertexai - - from vertexai.preview.generative_models import ( - GenerationConfig, - GenerativeModel, - Tool, - grounding, - ) - - # TODO (developer): update project_id - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = GenerativeModel("gemini-1.5-flash-002") - - # TODO(developer): Update project id, location, and data store id for your Vertex AI Search data store. - # data_store_id = "DATA_STORE_ID" - - tool = Tool.from_retrieval( - grounding.Retrieval( - grounding.VertexAISearch( - datastore=data_store_id, - project=PROJECT_ID, - location="global", - ) - ) - ) - - prompt = "How do I make an appointment to renew my driver's license?" - response = model.generate_content( - prompt, - tools=[tool], - generation_config=GenerationConfig( - temperature=0.0, - ), - ) - - print(response.text) - - # [END generativeaionvertexai_gemini_grounding_with_vais] - return response diff --git a/generative_ai/gemini_rapid_evaluation.py b/generative_ai/gemini_rapid_evaluation.py deleted file mode 100644 index 20934164f6ec..000000000000 --- a/generative_ai/gemini_rapid_evaluation.py +++ /dev/null @@ -1,85 +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.preview.evaluation import EvalResult - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def create_evaluation_task() -> EvalResult: - # [START generativeaionvertexai_create_evaluation_task] - import pandas as pd - - import vertexai - from vertexai.preview.evaluation import EvalTask, MetricPromptTemplateExamples - - # TODO(developer): Update project_id and location - vertexai.init(project=PROJECT_ID, location="us-central1") - - eval_dataset = pd.DataFrame( - { - "instruction": [ - "Summarize the text in one sentence.", - "Summarize the text such that a five-year-old can understand.", - ], - "context": [ - """As part of a comprehensive initiative to tackle urban congestion and foster - sustainable urban living, a major city has revealed ambitious plans for an - extensive overhaul of its public transportation system. The project aims not - only to improve the efficiency and reliability of public transit but also to - reduce the city\'s carbon footprint and promote eco-friendly commuting options. - City officials anticipate that this strategic investment will enhance - accessibility for residents and visitors alike, ushering in a new era of - efficient, environmentally conscious urban transportation.""", - """A team of archaeologists has unearthed ancient artifacts shedding light on a - previously unknown civilization. The findings challenge existing historical - narratives and provide valuable insights into human history.""", - ], - "response": [ - "A major city is revamping its public transportation system to fight congestion, reduce emissions, and make getting around greener and easier.", - "Some people who dig for old things found some very special tools and objects that tell us about people who lived a long, long time ago! What they found is like a new puzzle piece that helps us understand how people used to live.", - ], - } - ) - - eval_task = EvalTask( - dataset=eval_dataset, - metrics=[ - MetricPromptTemplateExamples.Pointwise.SUMMARIZATION_QUALITY, - MetricPromptTemplateExamples.Pointwise.GROUNDEDNESS, - MetricPromptTemplateExamples.Pointwise.VERBOSITY, - MetricPromptTemplateExamples.Pointwise.INSTRUCTION_FOLLOWING, - ], - ) - - prompt_template = ( - "Instruction: {instruction}. Article: {context}. Summary: {response}" - ) - result = eval_task.evaluate(prompt_template=prompt_template) - - print("Summary Metrics:\n") - - for key, value in result.summary_metrics.items(): - print(f"{key}: \t{value}") - - print("\n\nMetrics Table:\n") - print(result.metrics_table) - - # [END generativeaionvertexai_create_evaluation_task] - return result - - -if __name__ == "__main__": - create_evaluation_task() diff --git a/generative_ai/gemini_rapid_evaluation_test.py b/generative_ai/gemini_rapid_evaluation_test.py deleted file mode 100644 index b37775515c5d..000000000000 --- a/generative_ai/gemini_rapid_evaluation_test.py +++ /dev/null @@ -1,20 +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 gemini_rapid_evaluation - - -def test_create_evaluation_task() -> None: - response = gemini_rapid_evaluation.create_evaluation_task() - assert response diff --git a/generative_ai/gemini_tuning.py b/generative_ai/gemini_tuning.py deleted file mode 100644 index 6f3ae60e1186..000000000000 --- a/generative_ai/gemini_tuning.py +++ /dev/null @@ -1,139 +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 typing import List - -from vertexai.tuning import sft - - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -LOCATION = "us-central1" - - -def gemini_tuning_basic() -> sft.SupervisedTuningJob: - # [START generativeaionvertexai_tuning_basic] - - import time - - import vertexai - from vertexai.tuning import sft - - # TODO(developer): Update project_id and location - vertexai.init(project=PROJECT_ID, location="us-central1") - - sft_tuning_job = sft.train( - source_model="gemini-1.5-pro-002", - train_dataset="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl", - ) - - # Polling for job completion - while not sft_tuning_job.has_ended: - time.sleep(60) - sft_tuning_job.refresh() - - print(sft_tuning_job.tuned_model_name) - print(sft_tuning_job.tuned_model_endpoint_name) - print(sft_tuning_job.experiment) - # [END generativeaionvertexai_tuning_basic] - - return sft_tuning_job - - -def gemini_tuning_advanced() -> sft.SupervisedTuningJob: - # [START generativeaionvertexai_tuning_advanced] - - import time - - import vertexai - from vertexai.tuning import sft - - # TODO(developer): Update project_id and location - vertexai.init(project=PROJECT_ID, location="us-central1") - - sft_tuning_job = sft.train( - source_model="gemini-1.5-pro-002", - train_dataset="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl", - # The following parameters are optional - validation_dataset="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_validation_data.jsonl", - epochs=4, - adapter_size=4, - learning_rate_multiplier=1.0, - tuned_model_display_name="tuned_gemini_1_5_pro", - ) - - # Polling for job completion - while not sft_tuning_job.has_ended: - time.sleep(60) - sft_tuning_job.refresh() - - print(sft_tuning_job.tuned_model_name) - print(sft_tuning_job.tuned_model_endpoint_name) - print(sft_tuning_job.experiment) - # [END generativeaionvertexai_tuning_advanced] - - return sft_tuning_job - - -def get_tuning_job() -> sft.SupervisedTuningJob: - # [START generativeaionvertexai_get_tuning_job] - import vertexai - from vertexai.tuning import sft - - # TODO(developer): Update project_id and location - vertexai.init(project=PROJECT_ID, location=LOCATION) - - tuning_job_id = "4982013113894174720" - response = sft.SupervisedTuningJob( - f"projects/{PROJECT_ID}/locations/{LOCATION}/tuningJobs/{tuning_job_id}" - ) - - print(response) - # [END generativeaionvertexai_get_tuning_job] - - return response - - -def list_tuning_jobs() -> List[sft.SupervisedTuningJob]: - # [START generativeaionvertexai_list_tuning_jobs] - import vertexai - from vertexai.tuning import sft - - # TODO(developer): Update project_id and location - vertexai.init(project=PROJECT_ID, location="us-central1") - - responses = sft.SupervisedTuningJob.list() - - for response in responses: - print(response) - # [END generativeaionvertexai_list_tuning_jobs] - - return responses - - -def cancel_tuning_job() -> None: - # [START generativeaionvertexai_cancel_tuning_job] - import vertexai - from vertexai.tuning import sft - - # TODO(developer): Update project, location - vertexai.init(project=PROJECT_ID, location=LOCATION) - - tuning_job_id = "4982013113894174720" - job = sft.SupervisedTuningJob( - f"projects/{PROJECT_ID}/locations/{LOCATION}/tuningJobs/{tuning_job_id}" - ) - job.cancel() - # [END generativeaionvertexai_cancel_tuning_job] diff --git a/generative_ai/gemini_tuning_test.py b/generative_ai/gemini_tuning_test.py deleted file mode 100644 index 97a09e5290d7..000000000000 --- a/generative_ai/gemini_tuning_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 pytest - -import gemini_tuning - - -@pytest.mark.skip(reason="Skip due to tuning taking a long time.") -def test_gemini_tuning() -> None: - response = gemini_tuning.gemini_tuning_basic() - assert response - - response = gemini_tuning.gemini_tuning_advanced() - assert response - - -def test_get_tuning_job() -> None: - response = gemini_tuning.get_tuning_job() - assert response - - -def test_list_tuning_jobs() -> None: - response = gemini_tuning.list_tuning_jobs() - assert response - - -@pytest.mark.skip(reason="Skip due to tuning taking a long time.") -def test_cancel_tuning_job() -> None: - gemini_tuning.cancel_tuning_job() diff --git a/generative_ai/grounding.py b/generative_ai/grounding.py deleted file mode 100644 index d7fa43da4ed6..000000000000 --- a/generative_ai/grounding.py +++ /dev/null @@ -1,69 +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. -# TODO: Delete this file after approval /grounding/palm_example.py -import os - -from typing import Optional - -from vertexai.language_models import TextGenerationResponse - - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def grounding( - data_store_location: Optional[str], - data_store_id: Optional[str], -) -> TextGenerationResponse: - """Grounding example with a Large Language Model""" - # [START generativeaionvertexai_sdk_grounding] - import vertexai - - from vertexai.language_models import GroundingSource, TextGenerationModel - - # TODO(developer): Update project_id and location - vertexai.init(project=PROJECT_ID, location="us-central1") - - # TODO developer - override these parameters as needed: - parameters = { - "temperature": 0.7, # Temperature controls the degree of randomness in token selection. - "max_output_tokens": 256, # Token limit determines the maximum amount of text output. - "top_p": 0.8, # Tokens are selected from most probable to least until the sum of their probabilities equals the top_p value. - "top_k": 40, # A top_k of 1 means the selected token is the most probable among all tokens. - } - - model = TextGenerationModel.from_pretrained("text-bison@002") - - # TODO(developer): Update values for data_store_location, data_store_id - # data_store_id = "" - # data_store_location = "" - if data_store_id and data_store_location: - # Use Vertex AI Search data store - grounding_source = GroundingSource.VertexAISearch( - data_store_id=data_store_id, location=data_store_location - ) - else: - # Use Google Search for grounding (Private Preview) - grounding_source = GroundingSource.WebSearch() - - response = model.predict( - "What are the price, available colors, and storage size options of a Pixel Tablet?", - grounding_source=grounding_source, - **parameters, - ) - print(f"Response from Model: {response.text}") - print(f"Grounding Metadata: {response.grounding_metadata}") - # [END generativeaionvertexai_sdk_grounding] - - return response diff --git a/generative_ai/grounding_test.py b/generative_ai/grounding_test.py deleted file mode 100644 index aaa75ec690cd..000000000000 --- a/generative_ai/grounding_test.py +++ /dev/null @@ -1,30 +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 backoff -from google.api_core.exceptions import ResourceExhausted - -import grounding - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_grounding() -> None: - data_store_id = "test-search-engine_1689960780551" - response = grounding.grounding( - data_store_location="global", - data_store_id=data_store_id, - ) - assert response - assert response.text - assert response.grounding_metadata diff --git a/generative_ai/imagen/edit_image_inpainting_insert_mask.py b/generative_ai/imagen/edit_image_inpainting_insert_mask.py deleted file mode 100644 index d36fd6fd5fe0..000000000000 --- a/generative_ai/imagen/edit_image_inpainting_insert_mask.py +++ /dev/null @@ -1,64 +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. -""" - -from vertexai.preview import vision_models - - -def edit_image_inpainting_insert_mask( - project_id: str, - 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 = "PROJECT_ID" - # input_file = "my-input.png" - # mask_file = "my-mask.png" - # output_file = "my-output.png" - # prompt = "" # 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") - - # [END generativeaionvertexai_imagen_edit_image_inpainting_insert_mask] - - return images diff --git a/generative_ai/imagen/edit_image_inpainting_insert_mask_mode.py b/generative_ai/imagen/edit_image_inpainting_insert_mask_mode.py deleted file mode 100644 index 7922a929c4bf..000000000000 --- a/generative_ai/imagen/edit_image_inpainting_insert_mask_mode.py +++ /dev/null @@ -1,64 +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. -""" - -from vertexai.preview import vision_models - - -def edit_image_inpainting_insert_mask_mode( - project_id: str, - 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 = "PROJECT_ID" - # input_file = "my-input.png" - # mask_mode = "background" # 'background', 'foreground', or 'semantic' - # output_file = "my-output.png" - # prompt = "" # 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") - - # [END generativeaionvertexai_imagen_edit_image_inpainting_insert_mask_mode] - - return images diff --git a/generative_ai/imagen/edit_image_inpainting_insert_mask_mode_test_.py b/generative_ai/imagen/edit_image_inpainting_insert_mask_mode_test_.py deleted file mode 100644 index d8652c6297e8..000000000000 --- a/generative_ai/imagen/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 - -import edit_image_inpainting_insert_mask_mode - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE = os.path.join(_RESOURCES, "woman.png") -_MASK_MODE = "background" -_OUTPUT_FILE = os.path.join(_RESOURCES, "woman_at_beach.png") -_PROMPT = "beach" - - -@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( - _PROJECT_ID, - _INPUT_FILE, - _MASK_MODE, - _OUTPUT_FILE, - _PROMPT, - ) - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/edit_image_inpainting_insert_mask_test_.py b/generative_ai/imagen/edit_image_inpainting_insert_mask_test_.py deleted file mode 100644 index 841af909dbb5..000000000000 --- a/generative_ai/imagen/edit_image_inpainting_insert_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 - -import edit_image_inpainting_insert_mask - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_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" - - -@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( - _PROJECT_ID, - _INPUT_FILE, - _MASK_FILE, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/edit_image_inpainting_remove_mask.py b/generative_ai/imagen/edit_image_inpainting_remove_mask.py deleted file mode 100644 index b6fdf4e4d311..000000000000 --- a/generative_ai/imagen/edit_image_inpainting_remove_mask.py +++ /dev/null @@ -1,65 +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. -""" - -from vertexai.preview import vision_models - - -def edit_image_inpainting_remove_mask( - project_id: str, - 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 = "PROJECT_ID" - # input_file = "my-input.png" - # mask_file = "my-mask.png" - # output_file = "my-output.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") - - # [END generativeaionvertexai_imagen_edit_image_inpainting_remove_mask] - - return images diff --git a/generative_ai/imagen/edit_image_inpainting_remove_mask_mode.py b/generative_ai/imagen/edit_image_inpainting_remove_mask_mode.py deleted file mode 100644 index d7e3e40c7d99..000000000000 --- a/generative_ai/imagen/edit_image_inpainting_remove_mask_mode.py +++ /dev/null @@ -1,64 +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. -""" - -from vertexai.preview import vision_models - - -def edit_image_inpainting_remove_mask_mode( - project_id: str, - 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 = "PROJECT_ID" - # input_file = "my-input.png" - # mask_mode = "foreground" # 'background', 'foreground', or 'semantic' - # output_file = "my-output.png" - # prompt = "" # 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") - - # [END generativeaionvertexai_imagen_edit_image_inpainting_remove_mask_mode] - - return images diff --git a/generative_ai/imagen/edit_image_inpainting_remove_mask_mode_test_.py b/generative_ai/imagen/edit_image_inpainting_remove_mask_mode_test_.py deleted file mode 100644 index c31582a14c01..000000000000 --- a/generative_ai/imagen/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 - -import edit_image_inpainting_remove_mask_mode - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE = os.path.join(_RESOURCES, "woman.png") -_MASK_MODE = "foreground" -_OUTPUT_FILE = os.path.join(_RESOURCES, "sports_car.png") -_PROMPT = "sports car" - - -@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( - _PROJECT_ID, - _INPUT_FILE, - _MASK_MODE, - _OUTPUT_FILE, - _PROMPT, - ) - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/edit_image_inpainting_remove_mask_test_.py b/generative_ai/imagen/edit_image_inpainting_remove_mask_test_.py deleted file mode 100644 index d1589074352b..000000000000 --- a/generative_ai/imagen/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 - -import edit_image_inpainting_remove_mask - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_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" - - -@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( - _PROJECT_ID, - _INPUT_FILE, - _MASK_FILE, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/edit_image_mask.py b/generative_ai/imagen/edit_image_mask.py deleted file mode 100644 index 9a33ca1a47a3..000000000000 --- a/generative_ai/imagen/edit_image_mask.py +++ /dev/null @@ -1,68 +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. The - edit is applied to the masked area of the image and is saved to a new file. -""" - -from vertexai.preview import vision_models - - -def edit_image_mask( - project_id: str, - input_file: str, - mask_file: str, - output_file: str, - prompt: str, -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_edit_image_mask] - - import vertexai - from vertexai.preview.vision_models import Image, ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # project_id = "PROJECT_ID" - # input_file = "my-input.png" - # mask_file = "my-mask.png" - # output_file = "my-output.png" - # prompt = "" # The text prompt describing what you want to see. - - vertexai.init(project=project_id, location="us-central1") - - model = ImageGenerationModel.from_pretrained("imagegeneration@002") - 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, - # Optional parameters - seed=1, - # Controls the strength of the prompt. - # -- 0-9 (low strength), 10-20 (medium strength), 21+ (high strength) - guidance_scale=21, - number_of_images=1, - ) - - 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") - - # [END generativeaionvertexai_imagen_edit_image_mask] - - return images diff --git a/generative_ai/imagen/edit_image_mask_free.py b/generative_ai/imagen/edit_image_mask_free.py deleted file mode 100644 index 8193c4dcf8d7..000000000000 --- a/generative_ai/imagen/edit_image_mask_free.py +++ /dev/null @@ -1,61 +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 without using a mask. The - edit is applied to the entire image and is saved to a new file. -""" - -from vertexai.preview import vision_models - - -def edit_image_mask_free( - project_id: str, input_file: str, output_file: str, prompt: str -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_edit_image_mask_free] - - import vertexai - from vertexai.preview.vision_models import Image, ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # project_id = "PROJECT_ID" - # input_file = "my-input.png" - # output_file = "my-output.png" - # prompt = "" # The text prompt describing what you want to see. - - vertexai.init(project=project_id, location="us-central1") - - model = ImageGenerationModel.from_pretrained("imagegeneration@002") - base_img = Image.load_from_file(location=input_file) - - images = model.edit_image( - base_image=base_img, - prompt=prompt, - # Optional parameters - seed=1, - # Controls the strength of the prompt. - # -- 0-9 (low strength), 10-20 (medium strength), 21+ (high strength) - guidance_scale=21, - number_of_images=1, - ) - - 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") - - # [END generativeaionvertexai_imagen_edit_image_mask_free] - - return images diff --git a/generative_ai/imagen/edit_image_mask_free_test_.py b/generative_ai/imagen/edit_image_mask_free_test_.py deleted file mode 100644 index c5b71161c698..000000000000 --- a/generative_ai/imagen/edit_image_mask_free_test_.py +++ /dev/null @@ -1,40 +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 - -import edit_image_mask_free - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE = os.path.join(_RESOURCES, "cat.png") -_OUTPUT_FILE = os.path.join(_RESOURCES, "dog.png") -_PROMPT = "a dog" - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_edit_image_mask_free() -> None: - response = edit_image_mask_free.edit_image_mask_free( - _PROJECT_ID, - _INPUT_FILE, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/edit_image_mask_test_.py b/generative_ai/imagen/edit_image_mask_test_.py deleted file mode 100644 index 86e79e9c4675..000000000000 --- a/generative_ai/imagen/edit_image_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 - -import edit_image_mask - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE = os.path.join(_RESOURCES, "dog_newspaper.png") -_MASK_FILE = os.path.join(_RESOURCES, "dog_newspaper_mask.png") -_OUTPUT_FILE = os.path.join(_RESOURCES, "dog_book.png") -_PROMPT = "a big book" - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_edit_image_mask() -> None: - response = edit_image_mask.edit_image_mask( - _PROJECT_ID, - _INPUT_FILE, - _MASK_FILE, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/edit_image_outpainting_mask.py b/generative_ai/imagen/edit_image_outpainting_mask.py deleted file mode 100644 index c12dd33f3eff..000000000000 --- a/generative_ai/imagen/edit_image_outpainting_mask.py +++ /dev/null @@ -1,64 +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. - Outpainting lets you expand the content of a base image to fit a larger or - differently sized mask canvas. -""" - -from vertexai.preview import vision_models - - -def edit_image_outpainting_mask( - project_id: str, - input_file: str, - mask_file: str, - output_file: str, - prompt: str, -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_edit_image_outpainting_mask] - - import vertexai - from vertexai.preview.vision_models import Image, ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # project_id = "PROJECT_ID" - # input_file = "my-input.png" - # mask_file = "my-mask.png" - # output_file = "my-output.png" - # prompt = "" # The optional 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="outpainting", - ) - - 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") - - # [END generativeaionvertexai_imagen_edit_image_outpainting_mask] - - return images diff --git a/generative_ai/imagen/edit_image_outpainting_mask_test_.py b/generative_ai/imagen/edit_image_outpainting_mask_test_.py deleted file mode 100644 index 52014bc34b79..000000000000 --- a/generative_ai/imagen/edit_image_outpainting_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 - -import edit_image_outpainting_mask - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE = os.path.join(_RESOURCES, "roller_skaters.png") -_MASK_FILE = os.path.join(_RESOURCES, "roller_skaters_mask.png") -_OUTPUT_FILE = os.path.join(_RESOURCES, "roller_skaters_downtown.png") -_PROMPT = "city with skyscrapers" - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_edit_image_outpainting_mask() -> None: - response = edit_image_outpainting_mask.edit_image_outpainting_mask( - _PROJECT_ID, - _INPUT_FILE, - _MASK_FILE, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/edit_image_product_image.py b/generative_ai/imagen/edit_image_product_image.py deleted file mode 100644 index 904a02742a2d..000000000000 --- a/generative_ai/imagen/edit_image_product_image.py +++ /dev/null @@ -1,59 +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 a product image. You can - modify the background content but preserve the product's appearance. -""" - -from vertexai.preview import vision_models - - -def edit_image_product_image( - project_id: str, - input_file: str, - output_file: str, - prompt: str, -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_edit_image_product_image] - - import vertexai - from vertexai.preview.vision_models import Image, ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # project_id = "PROJECT_ID" - # input_file = "my-input.png" - # output_file = "my-output.png" - # prompt = "" # The text prompt describing what you want to see in the background. - - 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, - prompt=prompt, - edit_mode="product-image", - ) - - 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") - - # [END generativeaionvertexai_imagen_edit_image_product_image] - - return images diff --git a/generative_ai/imagen/edit_image_product_image_test_.py b/generative_ai/imagen/edit_image_product_image_test_.py deleted file mode 100644 index 1a3a49177d5b..000000000000 --- a/generative_ai/imagen/edit_image_product_image_test_.py +++ /dev/null @@ -1,40 +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 - -import edit_image_product_image - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE = os.path.join(_RESOURCES, "pillow.png") -_OUTPUT_FILE = os.path.join(_RESOURCES, "pillow_on_beach.png") -_PROMPT = "beach" - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_edit_image_product_image() -> None: - response = edit_image_product_image.edit_image_product_image( - _PROJECT_ID, - _INPUT_FILE, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/generate_image.py b/generative_ai/imagen/generate_image.py deleted file mode 100644 index ba7cb094d47c..000000000000 --- a/generative_ai/imagen/generate_image.py +++ /dev/null @@ -1,61 +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 generating an image using only - descriptive text as an input. -""" - -from vertexai.preview import vision_models - - -def generate_image( - project_id: str, output_file: str, prompt: str -) -> vision_models.ImageGenerationResponse: - # [START generativeaionvertexai_imagen_generate_image] - - import vertexai - from vertexai.preview.vision_models import ImageGenerationModel - - # TODO(developer): Update and un-comment below lines - # project_id = "PROJECT_ID" - # output_file = "my-output.png" - # prompt = "" # The text prompt describing what you want to see. - - vertexai.init(project=project_id, location="us-central1") - - model = ImageGenerationModel.from_pretrained("imagen-3.0-generate-001") - - images = model.generate_images( - prompt=prompt, - # Optional parameters - number_of_images=1, - language="en", - # You can't use a seed value and watermark at the same time. - # add_watermark=False, - # seed=100, - aspect_ratio="1:1", - safety_filter_level="block_some", - person_generation="allow_adult", - ) - - images[0].save(location=output_file, include_generation_parameters=False) - - # Optional. View the generated image in a notebook. - # images[0].show() - - print(f"Created output image using {len(images[0]._image_bytes)} bytes") - - # [END generativeaionvertexai_imagen_generate_image] - - return images diff --git a/generative_ai/imagen/generate_image_test_.py b/generative_ai/imagen/generate_image_test_.py deleted file mode 100644 index 7dd7a8385db1..000000000000 --- a/generative_ai/imagen/generate_image_test_.py +++ /dev/null @@ -1,38 +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 - -import generate_image - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_OUTPUT_FILE = os.path.join(_RESOURCES, "dog_newspaper.png") -_PROMPT = "a dog reading a newspaper" - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_generate_image() -> None: - response = generate_image.generate_image( - _PROJECT_ID, - _OUTPUT_FILE, - _PROMPT, - ) - - assert len(response[0]._image_bytes) > 1000 diff --git a/generative_ai/imagen/get_short_form_image_captions.py b/generative_ai/imagen/get_short_form_image_captions.py deleted file mode 100644 index 9112ececf2a0..000000000000 --- a/generative_ai/imagen/get_short_form_image_captions.py +++ /dev/null @@ -1,45 +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 getting short-form image captions. -""" - - -def get_short_form_image_captions(project_id: str, input_file: str) -> list: - # [START generativeaionvertexai_imagen_get_short_form_image_captions] - - import vertexai - from vertexai.preview.vision_models import Image, ImageTextModel - - # TODO(developer): Update and un-comment below lines - # project_id = "PROJECT_ID" - # input_file = "my-input.png" - - vertexai.init(project=project_id, location="us-central1") - - model = ImageTextModel.from_pretrained("imagetext@001") - source_img = Image.load_from_file(location=input_file) - - captions = model.get_captions( - image=source_img, - # Optional parameters - language="en", - number_of_results=1, - ) - - print(captions) - - # [END generativeaionvertexai_imagen_get_short_form_image_captions] - - return captions diff --git a/generative_ai/imagen/get_short_form_image_captions_test_.py b/generative_ai/imagen/get_short_form_image_captions_test_.py deleted file mode 100644 index e1f8b1fa2a59..000000000000 --- a/generative_ai/imagen/get_short_form_image_captions_test_.py +++ /dev/null @@ -1,36 +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 - -import get_short_form_image_captions - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE = os.path.join(_RESOURCES, "cat.png") - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_get_short_form_image_captions() -> None: - response = get_short_form_image_captions.get_short_form_image_captions( - _PROJECT_ID, - _INPUT_FILE, - ) - - assert len(response) > 0 and "cat" in response[0] diff --git a/generative_ai/imagen/get_short_form_image_responses.py b/generative_ai/imagen/get_short_form_image_responses.py deleted file mode 100644 index cd2f011b4d69..000000000000 --- a/generative_ai/imagen/get_short_form_image_responses.py +++ /dev/null @@ -1,49 +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 getting short-form responses to a - question about an image. -""" - - -def get_short_form_image_responses( - project_id: str, input_file: str, question: str -) -> list: - # [START generativeaionvertexai_imagen_get_short_form_image_responses] - - import vertexai - from vertexai.preview.vision_models import Image, ImageTextModel - - # TODO(developer): Update and un-comment below lines - # project_id = "PROJECT_ID" - # input_file = "my-input.png" - # question = "" # The question about the contents of the image. - - vertexai.init(project=project_id, location="us-central1") - - model = ImageTextModel.from_pretrained("imagetext@001") - source_img = Image.load_from_file(location=input_file) - - answers = model.ask_question( - image=source_img, - question=question, - # Optional parameters - number_of_results=1, - ) - - print(answers) - - # [END generativeaionvertexai_imagen_get_short_form_image_responses] - - return answers diff --git a/generative_ai/imagen/get_short_form_image_responses_test_.py b/generative_ai/imagen/get_short_form_image_responses_test_.py deleted file mode 100644 index 8b83272d28a1..000000000000 --- a/generative_ai/imagen/get_short_form_image_responses_test_.py +++ /dev/null @@ -1,38 +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 - -import get_short_form_image_responses - -from google.api_core.exceptions import ResourceExhausted - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE = os.path.join(_RESOURCES, "cat.png") -_QUESTION = "What breed of cat is this a picture of?" - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_get_short_form_image_responses() -> None: - response = get_short_form_image_responses.get_short_form_image_responses( - _PROJECT_ID, - _INPUT_FILE, - _QUESTION, - ) - - assert len(response) > 0 and "tabby" in response[0] diff --git a/generative_ai/imagen/test_resources/cat.png b/generative_ai/imagen/test_resources/cat.png deleted file mode 100644 index 67f2b55a6f45..000000000000 Binary files a/generative_ai/imagen/test_resources/cat.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/dog.png b/generative_ai/imagen/test_resources/dog.png deleted file mode 100644 index a5040ca8f75f..000000000000 Binary files a/generative_ai/imagen/test_resources/dog.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/dog_book.png b/generative_ai/imagen/test_resources/dog_book.png deleted file mode 100644 index a71a7bbf8584..000000000000 Binary files a/generative_ai/imagen/test_resources/dog_book.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/dog_newspaper.png b/generative_ai/imagen/test_resources/dog_newspaper.png deleted file mode 100644 index cd47e3d77071..000000000000 Binary files a/generative_ai/imagen/test_resources/dog_newspaper.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/dog_newspaper_mask.png b/generative_ai/imagen/test_resources/dog_newspaper_mask.png deleted file mode 100644 index d0cceae3a062..000000000000 Binary files a/generative_ai/imagen/test_resources/dog_newspaper_mask.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/pillow.png b/generative_ai/imagen/test_resources/pillow.png deleted file mode 100644 index 8803f49defe3..000000000000 Binary files a/generative_ai/imagen/test_resources/pillow.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/pillow_on_beach.png b/generative_ai/imagen/test_resources/pillow_on_beach.png deleted file mode 100644 index 630d987dae9e..000000000000 Binary files a/generative_ai/imagen/test_resources/pillow_on_beach.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/roller_skaters.png b/generative_ai/imagen/test_resources/roller_skaters.png deleted file mode 100644 index e63adbfdcec4..000000000000 Binary files a/generative_ai/imagen/test_resources/roller_skaters.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/roller_skaters_downtown.png b/generative_ai/imagen/test_resources/roller_skaters_downtown.png deleted file mode 100644 index c6c2556c9a12..000000000000 Binary files a/generative_ai/imagen/test_resources/roller_skaters_downtown.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/roller_skaters_mask.png b/generative_ai/imagen/test_resources/roller_skaters_mask.png deleted file mode 100644 index 333da898979c..000000000000 Binary files a/generative_ai/imagen/test_resources/roller_skaters_mask.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/sports_car.png b/generative_ai/imagen/test_resources/sports_car.png deleted file mode 100644 index f4786c9a2caf..000000000000 Binary files a/generative_ai/imagen/test_resources/sports_car.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/volleyball_game.png b/generative_ai/imagen/test_resources/volleyball_game.png deleted file mode 100644 index 2a335ef4fba4..000000000000 Binary files a/generative_ai/imagen/test_resources/volleyball_game.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/volleyball_game_inpainting_remove_mask.png b/generative_ai/imagen/test_resources/volleyball_game_inpainting_remove_mask.png deleted file mode 100644 index 784c1f5a4233..000000000000 Binary files a/generative_ai/imagen/test_resources/volleyball_game_inpainting_remove_mask.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/volleyball_game_single_blue_player.png b/generative_ai/imagen/test_resources/volleyball_game_single_blue_player.png deleted file mode 100644 index 89c3a720e339..000000000000 Binary files a/generative_ai/imagen/test_resources/volleyball_game_single_blue_player.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/woman.png b/generative_ai/imagen/test_resources/woman.png deleted file mode 100644 index f23292436819..000000000000 Binary files a/generative_ai/imagen/test_resources/woman.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/woman_at_beach.png b/generative_ai/imagen/test_resources/woman_at_beach.png deleted file mode 100644 index 9f616dd8d3bc..000000000000 Binary files a/generative_ai/imagen/test_resources/woman_at_beach.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/woman_inpainting_insert_mask.png b/generative_ai/imagen/test_resources/woman_inpainting_insert_mask.png deleted file mode 100644 index d5399635b0b8..000000000000 Binary files a/generative_ai/imagen/test_resources/woman_inpainting_insert_mask.png and /dev/null differ diff --git a/generative_ai/imagen/test_resources/woman_with_hat.png b/generative_ai/imagen/test_resources/woman_with_hat.png deleted file mode 100644 index 5f6af7e5de5b..000000000000 Binary files a/generative_ai/imagen/test_resources/woman_with_hat.png and /dev/null differ diff --git a/generative_ai/imagen/verify_image_watermark.py b/generative_ai/imagen/verify_image_watermark.py deleted file mode 100644 index b6969f65852f..000000000000 --- a/generative_ai/imagen/verify_image_watermark.py +++ /dev/null @@ -1,56 +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 verifying if an image contains a - digital watermark. By default, a non-visible, digital watermark (called a - SynthID) is added to images generated by a model version that supports - watermark generation. -""" - -from vertexai.preview import vision_models - - -def verify_image_watermark( - project_id: str, input_file: str -) -> vision_models.WatermarkVerificationResponse: - # [START generativeaionvertexai_imagen_verify_image_watermark] - - import vertexai - from vertexai.preview.vision_models import ( - Image, - WatermarkVerificationModel, - ) - - # TODO(developer): Update and un-comment below lines - # project_id = "PROJECT_ID" - # input_file = "my-input.png" - - vertexai.init(project=project_id, location="us-central1") - - verification_model = WatermarkVerificationModel.from_pretrained( - "imageverification@001" - ) - image = Image.load_from_file(location=input_file) - - watermark_verification_response = verification_model.verify_image(image) - - print( - f"Watermark verification result: {watermark_verification_response.watermark_verification_result}" - ) - # ACCEPT: The image contains a digital watermark. - # REJECT: The image does not contain a digital watermark. - - # [END generativeaionvertexai_imagen_verify_image_watermark] - - return watermark_verification_response diff --git a/generative_ai/imagen/verify_image_watermark_test_.py b/generative_ai/imagen/verify_image_watermark_test_.py deleted file mode 100644 index f7b9cabfec41..000000000000 --- a/generative_ai/imagen/verify_image_watermark_test_.py +++ /dev/null @@ -1,50 +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 verify_image_watermark - - -_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") -_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -_INPUT_FILE_WATERMARK = os.path.join(_RESOURCES, "dog_newspaper.png") -_INPUT_FILE_NO_WATERMARK = os.path.join(_RESOURCES, "dog_book.png") - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) -def test_verify_image_watermark() -> None: - response = verify_image_watermark.verify_image_watermark( - _PROJECT_ID, - _INPUT_FILE_WATERMARK, - ) - - assert ( - len(response.watermark_verification_result) > 0 - and "ACCEPT" in response.watermark_verification_result - ) - - response = verify_image_watermark.verify_image_watermark( - _PROJECT_ID, - _INPUT_FILE_NO_WATERMARK, - ) - - assert ( - len(response.watermark_verification_result) > 0 - and "REJECT" in response.watermark_verification_result - ) diff --git a/generative_ai/list_tuned_models.py b/generative_ai/list_tuned_models.py deleted file mode 100644 index 279e2f39058e..000000000000 --- a/generative_ai/list_tuned_models.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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 os - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def list_tuned_models() -> None: - """List tuned models.""" - # [START generativeaionvertexai_sdk_list_tuned_models] - import vertexai - - from vertexai.language_models import TextGenerationModel - - # TODO(developer): Update project_id - # PROJECT_ID = "your-project-id" - vertexai.init(project=PROJECT_ID, location="us-central1") - model = TextGenerationModel.from_pretrained("text-bison@002") - tuned_model_names = model.list_tuned_model_names() - print(tuned_model_names) - # [END generativeaionvertexai_sdk_list_tuned_models] - - return tuned_model_names - - -if __name__ == "__main__": - list_tuned_models() diff --git a/generative_ai/list_tuned_models_test.py b/generative_ai/list_tuned_models_test.py deleted file mode 100644 index e1886341d225..000000000000 --- a/generative_ai/list_tuned_models_test.py +++ /dev/null @@ -1,33 +0,0 @@ -# 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 -from google.cloud import aiplatform - -import list_tuned_models - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_list_tuned_models() -> None: - tuned_model_names = list_tuned_models.list_tuned_models() - filtered_models_counter = 0 - for tuned_model_name in tuned_model_names: - model_registry = aiplatform.models.ModelRegistry(model=tuned_model_name) - if ( - "Vertex LLM Test Fixture " - "(list_tuned_models_test.py::test_list_tuned_models)" - ) in model_registry.get_version_info("1").model_display_name: - filtered_models_counter += 1 - assert filtered_models_counter == 0 diff --git a/generative_ai/test_gemini_examples.py b/generative_ai/test_gemini_examples.py index 39e73b5e9831..8472e32f2ec0 100644 --- a/generative_ai/test_gemini_examples.py +++ b/generative_ai/test_gemini_examples.py @@ -21,7 +21,6 @@ import gemini_audio import gemini_chat_example import gemini_count_token_example -import gemini_grounding_example import gemini_guide_example import gemini_multi_image_example import gemini_pdf_example @@ -127,23 +126,6 @@ def test_gemini_chat_example() -> None: assert any([_ in text for _ in ("hi", "hello", "greeting")]) -# TODO: Delete this file after approval /grounding/web_example.py -@pytest.mark.skip( - "Unable to test Google Search grounding due to allowlist restrictions." -) -def test_gemini_grounding_web_example() -> None: - response = gemini_grounding_example.generate_text_with_grounding_web() - assert response - - -# TODO: Delete this file after approval /grounding/vais_example.py -def test_gemini_grounding_vais_example() -> None: - response = gemini_grounding_example.generate_text_with_grounding_vertex_ai_search( - "grounding-test-datastore" - ) - assert response - - # Delete this test after approval /understand_audio/understand_audio_test.py def test_summarize_audio() -> None: text = gemini_audio.summarize_audio() diff --git a/generative_ai/tune_code_generation_model.py b/generative_ai/tune_code_generation_model.py deleted file mode 100644 index 2deea62bd11f..000000000000 --- a/generative_ai/tune_code_generation_model.py +++ /dev/null @@ -1,50 +0,0 @@ -# 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 generativeaionvertexai_sdk_tune_code_generation_model] -from __future__ import annotations - -import os - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def tune_code_generation_model() -> None: - # [START generativeaionvertexai_tune_code_generation_model] - import vertexai - from vertexai.language_models import CodeGenerationModel - - # Initialize Vertex AI - # TODO(developer): update project_id & location - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = CodeGenerationModel.from_pretrained("code-bison@002") - - # TODO(developer): Update the training data path - tuning_job = model.tune_model( - training_data="gs://cloud-samples-data/ai-platform/generative_ai/headline_classification.jsonl", - tuning_job_location="europe-west4", - tuned_model_location="us-central1", - ) - - print(tuning_job._status) - # [END generativeaionvertexai_tune_code_generation_model] - - return model - - -# [END generativeaionvertexai_sdk_tune_code_generation_model] - -if __name__ == "__main__": - tune_code_generation_model() diff --git a/generative_ai/tune_code_generation_model_test.py b/generative_ai/tune_code_generation_model_test.py deleted file mode 100644 index cbd0421e5c9d..000000000000 --- a/generative_ai/tune_code_generation_model_test.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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. - -from google.cloud import aiplatform -import pytest -from vertexai.language_models import TextGenerationModel - -import tune_code_generation_model - - -def teardown_model(tuned_model: TextGenerationModel) -> None: - for tuned_model_name in tuned_model.list_tuned_model_names(): - model_registry = aiplatform.models.ModelRegistry(model=tuned_model_name) - - display_name = model_registry.get_version_info("1").model_display_name - for endpoint in aiplatform.Endpoint.list(): - for _ in endpoint.list_models(): - if endpoint.display_name == display_name: - endpoint.undeploy_all() - endpoint.delete() - aiplatform.Model(model_registry.model_resource_name).delete() - - -@pytest.mark.skip("Blocked on b/277959219") -def test_tuning_code_generation_model() -> None: - """Takes approx. 20 minutes.""" - tuned_model = tune_code_generation_model.tune_code_generation_model() - try: - assert tuned_model - finally: - teardown_model(tuned_model) diff --git a/generative_ai/tuning.py b/generative_ai/tuning.py deleted file mode 100644 index d23766891b4d..000000000000 --- a/generative_ai/tuning.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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 generativeaionvertexai_sdk_tuning] -from __future__ import annotations - -import os - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def tuning() -> None: - # [START generativeaionvertexai_tuning] - import vertexai - from vertexai.language_models import TextGenerationModel - - # Initialize Vertex AI - # TODO(developer): update project_id & location - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = TextGenerationModel.from_pretrained("text-bison@002") - - # TODO(developer): Update the training data path - tuning_job = model.tune_model( - training_data="gs://cloud-samples-data/ai-platform/generative_ai/headline_classification.jsonl", - tuning_job_location="europe-west4", - tuned_model_location="us-central1", - ) - - print(tuning_job._status) - # [END generativeaionvertexai_tuning] - return model - - -# [END generativeaionvertexai_sdk_tuning] - -if __name__ == "__main__": - tuning() diff --git a/generative_ai/tuning_test.py b/generative_ai/tuning_test.py deleted file mode 100644 index cb659d0c3da6..000000000000 --- a/generative_ai/tuning_test.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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. - -from google.cloud import aiplatform -import pytest -from vertexai.preview.language_models import TextGenerationModel - -import tuning - - -def teardown_model(tuned_model: TextGenerationModel) -> None: - for tuned_model_name in tuned_model.list_tuned_model_names(): - model_registry = aiplatform.models.ModelRegistry(model=tuned_model_name) - - display_name = model_registry.get_version_info("1").model_display_name - for endpoint in aiplatform.Endpoint.list(): - for _ in endpoint.list_models(): - if endpoint.display_name == display_name: - endpoint.undeploy_all() - endpoint.delete() - aiplatform.Model(model_registry.model_resource_name).delete() - - -@pytest.mark.skip("Blocked on b/277959219") -def test_tuning() -> None: - """Takes approx. 20 minutes.""" - tuned_model = tuning.tuning() - try: - assert tuned_model - finally: - teardown_model(tuned_model)