Develop GenAI Apps with Gemini and Streamlit : Develop GenAI Apps with Gemini and Streamlit: 챌린지 실습

JIEUN JANG·2024년 8월 4일
0

Google Cloud Skills Boost

목록 보기
13/13

챌린지 시나리오

1. cURL을 사용하여 API로 프롬프트 테스트

Vertex AI Workbench -> UsersNotebook 접속 후에 Terminal

# Cloud Storage에서 prompt.ipynb 파일을 다운로드
gsutil cp gs://spls/gsp517/prompt.ipynb .

후에 prompt.ipynb 파일을 수정해준다.
3번 단락에서 PROJECT_ID와 LOCATION 수정
5번 단락에서 기존 프롬프트를 변경 후 저장한다.

I am a Chef.  I need to create Japanese recipes for customers who want low sodium meals. However, I do not want to include recipes that use ingredients associated with a peanuts food allergy. I have ahi tuna, fresh ginger, and edamame in my kitchen and other ingredients. The customer wine preference is red. Please provide some for meal recommendations. For each recommendation include preparation instructions, time to prepare and the recipe title at the begining of the response. Then include the wine paring for each recommendation. At the end of the recommendation provide the calories associated with the meal and the nutritional facts.


2. Streamlit 프레임워크를 작성하고 chef.py를 완성하도록 Python 코드 프롬프트를 입력합니다.

CLOUD SHELL을 열어준다.

# 기본 디렉터리에서 아래 저장소를 클론
git clone https://github.com/GoogleCloudPlatform/generative-ai.git

# gemini-streamlit-cloudrun 디렉터리로 이동
cd generative-ai/gemini/sample-apps/gemini-streamlit-cloudrun

# chef.py 파일을 다운로드
gsutil cp gs://spls/gsp517/chef.py .

Open Editor로 chef.py 파일을 열고 수정해준다.

와인 변수에 Streamlit 프레임워크 라디오 버튼 옵션을 추가. 레드, 화이트, 없음 옵션을 포함

프롬프트 변경

# 파일을 qwiklabs-gcp-00-548dd34bc216-generative-ai 버킷에 업로드
gcloud storage cp chef.py gs://qwiklabs-gcp-00-548dd34bc216-generative-ai/

3. 애플리케이션 테스트

# Python 가상 환경을 설정하고 종속 항목을 설치
python3 -m venv gemini-streamlit
source gemini-streamlit/bin/activate
pip install -r requirements.txt

# PROJECT(프로젝트 ID) 및 REGION(실습 환경에서 사용 중인 리전)의 환경 변수를 설정
PROJECT='qwiklabs-gcp-00-548dd34bc216'
REGION='us-central1'

# chef.py 애플리케이션을 실행하고 테스트
streamlit run chef.py \
--browser.serverAddress=localhost \
--server.enableCORS=false \
--server.enableXsrfProtection=false \
--server.port 8080

페이지 동작을 확인 할 수 있다.
위에서 추가한 Red, White, None 버튼도 생성되어 있다.

4. Dockerfile 수정 및 Artifact Registry로 이미지 내보내기

Terminal을 열어서 Dockerfile을 chef.py를 사용하도록 수정해준다.

환경 변수 설정

Artifact Registry 저장소 생성

다음 매개변수를 사용하여 빌드

# 환경 변수 설정
AR_REPO='chef-repo'
SERVICE_NAME='chef-streamlit-app'
# Artifact Registry 저장소 생성
gcloud artifacts repositories create "$AR_REPO" --location="$REGION" --repository-format=Docker
# 다음 매개변수를 사용하여 빌드
gcloud builds submit --tag "$REGION-docker.pkg.dev/$PROJECT/$AR_REPO/$SERVICE_NAME"

5. Cloud Run에 애플리케이션 배포 및 테스트

다음 매개변수 값을 사용하여 Docker 아티팩트로 애플리케이션을 배포

gcloud run deploy "$SERVICE_NAME" \
--port=8080 \
--image="$REGION-docker.pkg.dev/$PROJECT/$AR_REPO/$SERVICE_NAME" \
--allow-unauthenticated \
--region=$REGION \
--platform=managed  \
--project=$PROJECT \
--set-env-vars=PROJECT=$PROJECT,REGION=$REGION


배포된 서비스가 정상 작동하는지 확인하기 위해 작동해보면 해당하는 레시피가 나오는 걸 확인할 수 있다.

6. 정리

Develop GenAI Apps with Gemini and Streamlit을 통해서
Python SDK와 Gemini API를 사용한 함수 호출 적용, Cloud Run으로 Streamlit 애플리케이션 배포하는 것을 배웠다.
텍스트 생성을 위해 Gemini에 프롬프트를 입력하는 여러 가지 방법과 Cloud Shell을 사용해 Streamlit 애플리케이션을 테스트하고 반복하는 방법, Streamlit 애플리케이션을 Cloud Run에 배포된 Docker 컨테이너로 패키징 해보았다.

Gemini를 이용해서 GCP를 사용하는 법을 진행했다. CLI를 이용해서 일반 서버에 배포하고, 도커로 배포하고, GKE까지 사용해보는 경험을 할 수 있었다.

직접 관리할 수 있기 때문에 AWS와는 다르게 세부사항을 직접 눈으로 볼 수 있다는 점이 흥미로웠고, 데이터베이스에 직접 쿼리문을 입력해서 관리할 수 있기에 데이터를 다루는 곳은 GCP를 쓰는 이유를 알 수 있었다.

0개의 댓글