Imagen on Vertex AI brings Google's state of the art image generative AI capabilities to application developers. With Imagen on Vertex AI, application developers can build next-generation AI products that transform their user's imagination into high quality visual assets using AI generation, in seconds.
With Imagen on Langchain , You can do the following tasks
Generate novel images using only a text prompt (text-to-image AI generation)
from langchain_core.messages import AIMessage, HumanMessage
from langchain_google_vertexai.vision_models import VertexAIImageGeneratorChat
generator = VertexAIImageGeneratorChat()
messages = [HumanMessage(content=["a cat at the beach"])]
response = generator.invoke(messages)
generated_image = response.content[0]
import base64
import io
from PIL import Image
img_base64 = generated_image["image_url"]["url"].split(",")[-1]
img = Image.open(io.BytesIO(base64.decodebytes(bytes(img_base64, "utf-8"))))
img
Image Editing
Edit an entire uploaded or generated image with a text prompt.
Edit Generated Imagefrom langchain_core.messages import AIMessage, HumanMessage
from langchain_google_vertexai.vision_models import (
VertexAIImageEditorChat,
VertexAIImageGeneratorChat,
)
generator = VertexAIImageGeneratorChat()
messages = [HumanMessage(content=["a cat at the beach"])]
response = generator.invoke(messages)
generated_image = response.content[0]
editor = VertexAIImageEditorChat()
messages = [HumanMessage(content=[generated_image, "a dog at the beach "])]
editor_response = editor.invoke(messages)
import base64
import io
from PIL import Image
edited_img_base64 = editor_response.content[0]["image_url"]["url"].split(",")[-1]
edited_img = Image.open(
io.BytesIO(base64.decodebytes(bytes(edited_img_base64, "utf-8")))
)
edited_img
Image Captioning
from langchain_google_vertexai import VertexAIImageCaptioning
model = VertexAIImageCaptioning()
NOTE : we're using generated image in Image Generation Section
img_base64 = generated_image["image_url"]["url"]
response = model.invoke(img_base64)
print(f"Generated Caption : {response}")
img = Image.open(
io.BytesIO(base64.decodebytes(bytes(img_base64.split(",")[-1], "utf-8")))
)
img
Generated Cpation : a cat sitting on the beach looking at the camera
Visual Question Answering (VQA)
from langchain_google_vertexai import VertexAIVisualQnAChat
model = VertexAIVisualQnAChat()
NOTE : we're using generated image in Image Generation Section
question = "What animal is shown in the image?"
response = model.invoke(
input=[
HumanMessage(
content=[
{"type": "image_url", "image_url": {"url": img_base64}},
question,
]
)
]
)
print(f"question : {question}\nanswer : {response.content}")
img = Image.open(
io.BytesIO(base64.decodebytes(bytes(img_base64.split(",")[-1], "utf-8")))
)
img
question : What animal is shown in the image?
answer : cat
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4