66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
import requests
|
|
import re
|
|
import os
|
|
import json
|
|
import logging
|
|
|
|
# 로거 인스턴스 가져오기
|
|
logger = logging.getLogger('default_logger')
|
|
|
|
class GeminiClient:
|
|
def __init__(self, api_key, api_secret):
|
|
self.api_key = api_key
|
|
self.api_secret = api_secret
|
|
self.base_url = "https://api.gemini.com/v1" # Gemini API base URL
|
|
|
|
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):
|
|
# 예시: 실제 Gemini API를 사용하여 이미지 분석 요청을 보내는 부분은
|
|
# Gemini의 이미지 분석 API 문서를 참조하여 적절한 엔드포인트와 파라미터를 사용해야 합니다.
|
|
|
|
# API 호출을 위한 헤더 설정
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"X-GEMINI-APIKEY": self.api_key,
|
|
# 추가적으로 필요한 인증 헤더가 있을 경우 여기에 추가
|
|
}
|
|
|
|
# API 요청 본문 (body) 구성
|
|
data = {
|
|
# 이미지 분석을 위한 API 요청 본문을 구성합니다.
|
|
# 이는 API 문서에 따라 달라질 수 있습니다.
|
|
"image_url": image_url
|
|
"이미지의 상품을 설명하는 홍보문구를 만들어줘"
|
|
}
|
|
|
|
# Gemini 이미지 분석 API 엔드포인트로 POST 요청 보내기
|
|
response = requests.post(f"{self.base_url}/image_analysis", headers=headers, data=json.dumps(data))
|
|
response.raise_for_status()
|
|
|
|
# API 응답에서 분석 결과 추출
|
|
gemini_answer = response.json()
|
|
|
|
# 예시: 응답에서 홍보문구 추출하기
|
|
# 실제 응답 구조에 따라 접근 방식이 달라질 수 있습니다.
|
|
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
|
|
|
|
# # 사용 예시
|
|
# api_key = 'YOUR_API_KEY'
|
|
# api_secret = 'YOUR_API_SECRET'
|
|
# client = GeminiClient(api_key, api_secret)
|
|
# image_url = 'YOUR_IMAGE_URL'
|
|
# client.gemini_img(image_url)
|