Build an AI Image Recognition app using Gemini on Vertex AI
목적
- Vertex AI SDK를 사용하여 Google의 AI 서비스에 연결하는 방법 학습
- 미리 훈련된 생성 AI 모델을 사용하는 방법 학습
- AI가 처리해야 할 입력 (Image + Text)을 제공하는 방법에 대한 이해
- AI 모델에서 생성된 텍스트 응을 처리하고 해석하는 방법 학습
- AI 애플리케이션 구축의 기본 사항 이해
Working with Vertex AI Python SDK
환경 변수 설정
export GOOGLE_CLOUD_PROJECT="project-id"
export GOOGLE_CLOUD_LOCATION="REGION"
export GOOGLE_GENAI_USE_VERTEXAI=True
Code
미리 학습된 `gemini-2.0-flash-001` 모델을 불러와 이미지의 URL과 이미지에 대한 질문이 포함된 프롬프트를 입력으로 한다.
from google import genai
from google.genai.types import HttpOptions, Part
client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
model="gemini-2.0-flash-001", # Load pre-trained model using generate_content
contents=[
"What is shown in this image?", # Text Question
Part.from_uri( # image url
file_uri="https://storage.googleapis.com/cloud-samples-data/generative-ai/image/scones.jpg",
mime_type="image/jpeg",
),
],
)
print(response.text)
Result
What is shown in this image?
The image shows a spread of food and flowers on a slightly textured surface. There are five blueberry scones or biscuits arranged around a small bowl of blueberries, a spoon with "LET'S JAM" inscribed on it, and two mugs of coffee, one black and one with cream. Loose blueberries are scattered throughout. To the right of the scones, there are several pink peonies with their stems and leaves. The food and flowers are placed on a piece of parchment paper with purple stains on it.
What is shown in this image? in Korean.
이 이미지에는 블루베리 스콘, 블루베리 한 그릇, 커피 두 잔, 모란 꽃이 있습니다.
답변을 한국어로 하라고 `in Korean`만 붙였을 뿐인데 이미지에 대한 상세한 묘사가 생략되었다.
What is shown in this image?
The image shows two golden-colored cats being held by a person. The person is wearing a gray sweater. The cats appear to be looking at something with wide eyes.
What is shown in this image? in Korean.
이 사진에는 두 마리의 금빛 고양이가 사람 품에 안겨 있는 모습이 담겨 있습니다.
고양이가 옷을 입고 있어서 사람이라고 인식하는 것 같다. 마찬가지로 한국어 답변은 영어 답변보다 묘사가 생략되었다.
어떤 동물이고, 어떤 품종이야?
개이고 품종은 웰시 코기입니다.
한국어보다는 영어로 프롬프트를 작성하고 영어로 답변을 받는 것이 더 상세하게 설명하는 것을 확인할 수 있었다.