Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 1 addition & 46 deletions google/generativeai/types/image_types/_image_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import Any, Dict, Optional, Union

from google.generativeai import protos
from google.generativeai import client

# pylint: disable=g-import-not-at-top
if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -38,7 +37,7 @@
ImageType = Union["Image", "PIL.Image.Image", "IPython.display.Image"]
# pylint: enable=g-import-not-at-top

__all__ = ["Image", "GeneratedImage", "check_watermark", "CheckWatermarkResult", "ImageType"]
__all__ = ["Image", "GeneratedImage", "ImageType"]


def _pil_to_blob(image: PIL.Image.Image) -> protos.Blob:
Expand Down Expand Up @@ -121,48 +120,6 @@ def __bool__(self):
raise ValueError(f"Unrecognized result: {decision}")


def check_watermark(
img: Union[pathlib.Path, ImageType], model_id: str = "models/image-verification-001"
) -> "CheckWatermarkResult":
"""Checks if an image has a Google-AI watermark.

Args:
img: can be a `pathlib.Path` or a `PIL.Image.Image`, `IPython.display.Image`, or `google.generativeai.Image`.
model_id: Which version of the image-verification model to send the image to.

Returns:

"""
if isinstance(img, Image):
pass
elif isinstance(img, pathlib.Path):
img = Image.load_from_file(img)
elif IPython.display is not None and isinstance(img, IPython.display.Image):
img = Image(image_bytes=img.data)
elif PIL.Image is not None and isinstance(img, PIL.Image.Image):
blob = _pil_to_blob(img)
img = Image(image_bytes=blob.data)
elif isinstance(img, protos.Blob):
img = Image(image_bytes=img.data)
else:
raise TypeError(
f"Not implemented: Could not convert a {type(img)} into `Image`\n {img=}"
)

prediction_client = client.get_default_prediction_client()
if not model_id.startswith("models/"):
model_id = f"models/{model_id}"

instance = {"image": {"bytesBase64Encoded": base64.b64encode(img._loaded_bytes).decode()}}
parameters = {"watermarkVerification": True}

response = prediction_client.predict(
model=model_id, instances=[instance], parameters=parameters
)

return CheckWatermarkResult(response.predictions)


class Image:
"""Image."""

Expand Down Expand Up @@ -257,8 +214,6 @@ def _as_base64_string(self) -> str:
def _repr_png_(self):
return self._pil_image._repr_png_() # type:ignore

check_watermark = check_watermark


_EXIF_USER_COMMENT_TAG_IDX = 0x9286
_IMAGE_GENERATION_PARAMETERS_EXIF_KEY = (
Expand Down
3 changes: 1 addition & 2 deletions google/generativeai/vision_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
#
"""Classes for working with vision models."""

from google.generativeai.types.image_types import check_watermark, Image, GeneratedImage
from google.generativeai.types.image_types import Image, GeneratedImage

from google.generativeai.vision_models._vision_models import (
ImageGenerationModel,
ImageGenerationResponse,
)

__all__ = [
"check_watermark",
"Image",
"GeneratedImage",
"ImageGenerationModel",
Expand Down
Loading