值得称赞的
Anish Nangia
谷歌指出我看错了代码。我问题中的OAuth代码不起作用。以下是我应该使用的代码:
https://cloud.google.com/vertex-ai/docs/generative-ai/image/visual-question-answering#-python
注意,当我在当地的Conda Jupyter笔记本电脑上进行实验时(
https://github.com/CsabaConsulting/NextGenAI/blob/main/ImagenTest.ipynb
)我仍然需要处理ADC(应用程序默认凭据),请参阅
https://cloud.google.com/docs/authentication#auth-decision-tree
和
https://cloud.google.com/docs/authentication/application-default-credentials
然后你会得到一个
Your application is authenticating by using local Application Default Credentials. The aiplatform.googleapis.com API requires a quota project, which is not set by default. To learn how to set your quota project...
,所以有一些有趣的环,但这些环是可以解决的。
当部署在云功能中时,您希望建立一个正确的服务帐户。示例代码:
https://github.com/CsabaConsulting/NextGenAI/tree/main/imagen_test
requirements.txt:
functions-framework==3.*
google-cloud-aiplatform==1.35.*
main.py:
import base64
import functions_framework
import vertexai
from flask import jsonify
from vertexai.vision_models import ImageQnAModel, ImageTextModel, Image
PROJECT_ID = "gdg-demos"
LOCATION = "us-central1"
@functions_framework.http
def imagen_test(request):
"""HTTP Cloud Function.
Args:
request (flask.Request): The request object.
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
"""
request_json = request.get_json(silent=True)
request_args = request.args
if request_json and 'image' in request_json:
image_b64 = request_json['image']
elif request_args and 'image' in request_args:
image_b64 = request_args['image']
else:
image_b64 = None
if not image_b64:
return jsonify(dict(data=[]))
vertexai.init(project=PROJECT_ID, location=LOCATION)
model = ImageQnAModel.from_pretrained("imagetext@001")
image_binary = base64.b64decode(image_b64)
image = Image(image_binary)
answers = model.ask_question(
image=image,
question="Describe what is on the photo in great detail, be very verbose",
number_of_results=3,
)
return jsonify(dict(data=answers))