arrow_back

Build an AI Image Recognition app using Gemini on Vertex AI

로그인 가입
지식을 테스트하고 커뮤니티와 공유하기
done
700개 이상의 실무형 실습, 기술 배지, 과정에 액세스

Build an AI Image Recognition app using Gemini on Vertex AI

실습 15분 universal_currency_alt 무료 show_chart 입문
info 이 실습에는 학습을 지원하는 AI 도구가 통합되어 있을 수 있습니다.
지식을 테스트하고 커뮤니티와 공유하기
done
700개 이상의 실무형 실습, 기술 배지, 과정에 액세스

bb-ide-genai-001

Google Cloud self-paced labs logo

Overview

  • Labs are timed and cannot be paused. The timer starts when you click Start Lab.
  • The included cloud terminal is preconfigured with the gcloud SDK.
  • Use the terminal to execute commands and then click Check my progress to verify your work.

Objective

Generative AI on Vertex AI (also known as genAI or gen AI) gives you access to Google's large generative AI models so you can test, tune, and deploy them for use in your AI-powered applications. In this lab, you will:

  • Connect to Vertex AI (Google Cloud AI platform): Learn how to establish a connection to Google's AI services using the Vertex AI SDK.
  • Load a pre-trained generative AI model -Gemini: Discover how to use a powerful, pre-trained AI model without building one from scratch.
  • Send image + text questions to the AI model: Understand how to provide input for the AI to process.
  • Extract text-based answers from the AI: Learn to handle and interpret the text responses generated by the AI model.
  • Understand the basics of building AI applications: Gain insights into the core concepts of integrating AI into software projects.

Working with Vertex AI Python SDK

After starting the lab, you will get a split pane view consisting of the Code Editor on the left side and the lab instructions on the right side. Follow these steps to interact with the Generative AI APIs using Vertex AI Python SDK.

  1. Click File->New File to open a new file within the Code Editor.
  2. Copy and paste the provided code snippet into your file.
import vertexai from vertexai.generative_models import GenerativeModel, Part def generate_text(project_id: str, location: str) -> str: # Initialize Vertex AI vertexai.init(project=project_id, location=location) # Load the model multimodal_model = GenerativeModel("gemini-1.0-pro-vision") # Query the model response = multimodal_model.generate_content( [ # Add an example image Part.from_uri( "gs://generativeai-downloads/images/scones.jpg", mime_type="image/jpeg" ), # Add an example query "what is shown in this image?", ] ) return response.text # -------- Important: Variable declaration -------- project_id = "{{{ project_0.project_id | project-id }}}" location = "{{{ project_0.default_region | REGION }}}" # -------- Call the Function -------- response = generate_text(project_id, location) print(response)
  1. Click File->Save, choose genai.py for the Name field and click Save.
  2. Execute the Python file by clicking the triangle icon on the Code Editor or by invoking the below command inside the terminal within the Code Editor pane to view the output.
/usr/bin/python3 /home/student/genai.py Note: If you encounter a 401 error, try re-running the code.

Code Explanation

  • The code snippet is loading a pre-trained AI model called Gemini (gemini-1.0-pro-vision) on Vertex AI.
  • The code calls the generate_content method of the loaded Gemini model.
  • The input to the method is an image URI and a prompt containing a question about the image.
  • The code uses Gemini's ability to understand images and text together. It uses the text provided in the prompt to describe the contents of the image.

Try it yourself! Experiment with different image URIs and prompt questions to explore Gemini's capabilities.

Click Check my progress to verify the objective.

Generate content for the image

Congratulations!

You have completed the lab! Congratulations!!

Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

현재 이 콘텐츠를 이용할 수 없습니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

감사합니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.