google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.49k stars 293 forks source link

module 'google.generativeai' has no attribute 'ImageGenerationModel' for Image Generating #597

Open mimonirbd opened 1 day ago

mimonirbd commented 1 day ago

Description of the bug:

I am using this script for gemini api to generate image:

import os
import google.generativeai as genai

genai.configure(api_key=os.environ['GEMINI_API_KEY'])

model  = genai.ImageGenerationModel("imagen-3.0-generate-001")

result = model.generate_images(
    prompt="Fuzzy bunnies in my kitchen",
    number_of_images=2,
    safety_filter_level="block_only_high",
    person_generation="allow_adult",
    aspect_ratio="3:4",
    negative_prompt="Outside",
)

for image in result.images:
  print(image)

# The output should look similar to this:
# <vertexai.preview.vision_models.GeneratedImage object at 0x78f3396ef370>
# <vertexai.preview.vision_models.GeneratedImage object at 0x78f3396ef700>
# <vertexai.preview.vision_models.GeneratedImage object at 0x78f33953c2b0>
# <vertexai.preview.vision_models.GeneratedImage object at 0x78f33953c280>

for image in result.images:
  # Open and display the image using your local operating system.
  image._pil_image.show()

Actual vs expected behavior:

I am using python 3.10 but when I run the code then showig this error:

PS C:\Users\M i Monir\Documents\python project\gemini content writer> & "C:/Users/M i Monir/AppData/Local/Programs/Python/Python310/python.exe" "c:/Users/M i Monir/Documents/python project/gemini content writer/gemini image generator.py"
Traceback (most recent call last):
  File "c:\Users\M i Monir\Documents\python project\gemini content writer\gemini image generator.py", line 7, in <module>
    model  = genai.ImageGenerationModel("imagen-3.0-generate-001")
AttributeError: module 'google.generativeai' has no attribute 'ImageGenerationModel'
PS C:\Users\M i Monir\Documents\python project\gemini content writer> 

How to fix it

Any other information you'd like to share?

No response

Hamza-nabil commented 21 hours ago

The code is from Generate images using Imagen 3 , but I think it not accurate as the current api SDK does not support image generation.

I found similar example in this notebook that use ImageGenerationModel from vertexai.preview.vision_models . it does make sense as the output is vertexai.preview.vision_models.GeneratedImage object.

mimonirbd commented 15 hours ago

That means currently it isn't possible to generate image by gemini api without vertext ai api?

Gunand3043 commented 14 hours ago

@mimonirbd , The model is still in early access and not generally available yet.

Preview: Imagen 3 in the Gemini API is available as an early access release in private preview. It's not yet generally available.

mimonirbd commented 13 hours ago

@Gunand3043 Got it, Thanks.

MarianoMolina commented 3 hours ago

Its kinda silly that the docs are nonsense. The code example throws an exception, and the API Docs make the claim that img models are usable, yet it seems they are not.

I tried:

from google.generativeai.vision_models import ImageGenerationModel

Got this error too: File "/usr/local/lib/python3.10/site-packages/google/generativeai/vision_models/_vision_models.py", line 695, in class GenerateVideoOperation(operations.BaseOperation): AttributeError: module 'google.generativeai.operations' has no attribute 'BaseOperation'. Did you mean: 'get_operation'?

The docs have been like this for weeks btw, so not sure what was the plan with this.