49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
from gemini import Gemini
|
|
import re, os
|
|
import requests
|
|
import logging
|
|
|
|
# 로거 인스턴스 가져오기
|
|
logger = logging.getLogger('default_logger')
|
|
|
|
class GeminiClient:
|
|
|
|
def __init__(self, api_key):
|
|
self.api_key = api_key
|
|
self.gemini = Gemini(api_key=api_key)
|
|
|
|
def extract_numbered_lines(self, text):
|
|
# 정규 표현식 패턴: 숫자 + 점 + 공백으로 시작하고, 줄바꿈 문자가 나올 때까지의 내용을 찾음
|
|
pattern = r'\d\.\s.+?(?=\n|$)'
|
|
matches = re.findall(pattern, text)
|
|
|
|
return "\n".join(matches)
|
|
|
|
def gemini_img(self, image_url):
|
|
# 이미지 URL
|
|
# image_url = 'https://file.percenty.co.kr/public/652bed8e865b1f32ea62bf1f/products/6575b191d39b6b71ccac77ce/a5ffe56b-3349-4dca-bc36-fa569a68c337.jpg'
|
|
|
|
# requests를 사용하여 이미지 다운로드
|
|
response = requests.get(image_url)
|
|
response.raise_for_status() # HTTP 요청 에러 체크
|
|
|
|
# 이미지 데이터를 Bard에 전달
|
|
image_data = response.content
|
|
gemini_answer = self.gemini.generate('이미지의 상품을 한국의 쇼핑몰에서 판매할때 적절한 홍보문구를 3줄로 만들어줘.', image_data)
|
|
|
|
contents = self.extract_numbered_lines(gemini_answer['predictions'])
|
|
|
|
logger.debug("이미지 분석 결과입니다")
|
|
logger.debug(contents)
|
|
|
|
result = self.extract_numbered_lines(contents)
|
|
logger.debug("분석 결과 파싱 데이터입니다")
|
|
logger.debug(result)
|
|
|
|
return result
|
|
|
|
# 예시 코드
|
|
client = GeminiClient('YOUR_API_KEY')
|
|
image_url = 'https://file.percenty.co.kr/public/652bed8e865b1f32ea62bf1f/products/6575b191d39b6b71ccac77ce/a5ffe56b-3349-4dca-bc36-fa569a68c337.jpg'
|
|
client.gemini_img(image_url)
|