Reputation: 61
I'm trying to generate images using Google's Generative AI API, but I'm encountering a "NotFound" error. Here's my code:
import os
import google.generativeai as genai
genai.configure(api_key='my_api_key_here')
imagen = genai.ImageGenerationModel("imagen-3.0-generate-001")
result = imagen.generate_images(
prompt="Fuzzy bunnies in my kitchen",
number_of_images=4,
safety_filter_level="block_only_high",
person_generation="allow_adult",
aspect_ratio="3:4",
negative_prompt="Outside",
)
When I run this code, I get the following error:
File c:\Users\salos\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\grpc_helpers.py:76, in _wrap_unary_errors.<locals>.error_remapped_callable(*args, **kwargs)
75 try:
---> 76 return callable_(*args, **kwargs)
77 except grpc.RpcError as exc:
File c:\Users\salos\AppData\Local\Programs\Python\Python310\lib\site-packages\grpc\_channel.py:1181, in _UnaryUnaryMultiCallable.__call__(self, request, timeout, metadata, credentials, wait_for_ready, compression)
1175 (
1176 state,
1177 call,
1178 ) = self._blocking(
1179 request, timeout, metadata, credentials, wait_for_ready, compression
1180 )
-> 1181 return _end_unary_response_blocking(state, call, False, None)
File c:\Users\salos\AppData\Local\Programs\Python\Python310\lib\site-packages\grpc\_channel.py:1006, in _end_unary_response_blocking(state, call, with_call, deadline)
1005 else:
-> 1006 raise _InactiveRpcError(state)
_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.NOT_FOUND
details = "models/imagen-3.0-generate-001 is not found for API version v1beta, or is not supported for predict. Call ListModels to see the list of available models and their supported methods."
debug_error_string = "UNKNOWN:Error received from peer ipv4:142.250.182.138:443 {created_time:"2024-10-19T14:38:26.5900722+00:00", grpc_status:5, grpc_message:"models/imagen-3.0-generate-001 is not found for API version v1beta, or is not supported for predict. Call ListModels to see the list of available models and their supported methods."}"
>
...
76 return callable_(*args, **kwargs)
77 except grpc.RpcError as exc:
---> 78 raise exceptions.from_grpc_error(exc) from exc
NotFound: 404 models/imagen-3.0-generate-001 is not found for API version v1beta, or is not supported for predict. Call ListModels to see the list of available models and their supported methods.
I was following the code given in their official website: https://ai.google.dev/gemini-api/docs/imagen
I have also installed the beta version of Python SDK for the Gemini API (pip install -U git+https://github.com/google-gemini/generative-ai-python@imagen) and pillow as suggested in the guide.
Upvotes: 2
Views: 1457
Reputation: 117254
This is the sample i use.
Two things to check.
This is the branch you need.
# imagen GitHub branch:
pip install -U git+https://github.com/google-gemini/generative-ai-python@imagen
from dotenv import load_dotenv
import google.generativeai as genai
import os
load_dotenv()
genai.configure(api_key=os.environ['API_KEY'])
def read_text_from_file(file_path):
with open(file_path, "r") as file:
text = file.read()
return text
# The stable version of Python SDK for the Gemini API does not contain Imagen support. Instead of installing the
# google-generativeai package from pypi you need to install it from the imagen GitHub branch: pip install -U
# git+https://github.com/google-gemini/generative-ai-python@imagen
imagen = genai.ImageGenerationModel("imagen-3.0-generate-001")
result = imagen.generate_images(
prompt=read_text_from_file("prompt.txt"),
number_of_images=4,
safety_filter_level="block_only_high",
person_generation="allow_adult",
aspect_ratio="16:9",
negative_prompt="Outside",
)
for index, image in enumerate(result.images):
# Open and display the image using your local operating system.
image._pil_image.show()
# Save the image using the PIL library's save function
image._pil_image.save(f'image_{index}.jpg')
Note: i store my prompt in a the prompt.txt file locally you can just replace it with the prompt for your image.
At the time of writing this is still in early access and you must be whitelisted to use it.
This error message
NotFound: 404 models/imagen-3.0-generate-001 is not found for API version v1beta
If you are seeing that message it means you dont have access.
As you can see at the top of the documentation page
If you got an email saying you were whitelisted try to contact who ever white listed you. If you are sure make sure that you used the white listed account to create the API key.
If you haven't been whitelisted you cant use it. I'm not sure where you can apply for white list to this feature. I can try to find out.
Upvotes: 0