1from io import BytesIO
2
3from kern.agent import Agent, RunOutput # noqa
4from kern.models.google import Gemini
5from PIL import Image
6
7# No system message should be provided
8agent = Agent(
9 model=Gemini(
10 id="gemini-2.0-flash-exp-image-generation",
11 response_modalities=["Text", "Image"],
12 )
13)
14
15# Print the response in the terminal
16run_response = agent.run("Make me an image of a cat in a tree.")
17
18if run_response and isinstance(run_response, RunOutput) and run_response.images:
19 for image_response in run_response.images:
20 image_bytes = image_response.content
21 if image_bytes:
22 image = Image.open(BytesIO(image_bytes))
23 image.show()
24 # Save the image to a file
25 # image.save("generated_image.png")
26else:
27 print("No images found in run response")