我尝试使用 Google 的 Generative AI API 生成图像,但遇到“NotFound”错误。这是我的代码:
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",
)
当我运行此代码时,出现以下错误:
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.
我遵循了他们官方网站上给出的代码:https://ai.google.dev/gemini-api/docs/imagen
我还按照指南中的建议安装了 Gemini API 的 Python SDK 测试版(pip install -U git+https://github.com/google-gemini/generative-ai-python@imagen)和枕头.
这是我使用的示例。
有两件事需要检查。
这就是你需要的分支。
# 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("emoji.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')