404 models/imagen-3.0-generate-001

问题描述 投票:0回答:1

我尝试使用 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)和枕头.

python google-gemini image-generation
1个回答
0
投票

这是我使用的示例。

有两件事需要检查。

  1. 您的 api 密钥是在 AI studio 中创建的
  2. 您的 API 密钥实际上正在加载,请在其上打印。
  3. 在撰写本文时,Imagen3 还不是客户端库主分支的一部分,您需要直接从镜像分支安装它。

这就是你需要的分支。

# 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')
© www.soinside.com 2019 - 2024. All rights reserved.