edit : imagen -> gemini로 변경, bingimg -> 사용불가 , 벡터검색api 이미지저장이아닌 데이터 전송하는 api 추가 , vactor -> vector 오타 수정
This commit is contained in:
40
custom_apps/gemini/main.py
Normal file
40
custom_apps/gemini/main.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
from google import genai
|
||||
from google.genai import types
|
||||
from PIL import Image
|
||||
from io import BytesIO
|
||||
|
||||
from main_rest.app.utils.date_utils import D
|
||||
from const import TEMP_FOLDER
|
||||
|
||||
|
||||
def gemini_image(prompt, folder=None):
|
||||
from custom_logger.main_log import main_logger as LOG
|
||||
|
||||
image_path = ''
|
||||
client = genai.Client(api_key="AIzaSyB7tu67y9gOkJkpQtvI5OAYSzUzwv9qwnE")
|
||||
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.0-flash-preview-image-generation",
|
||||
contents=prompt,
|
||||
config=types.GenerateContentConfig(
|
||||
response_modalities=['TEXT', 'IMAGE']
|
||||
)
|
||||
)
|
||||
if folder == None:
|
||||
folder = TEMP_FOLDER
|
||||
|
||||
if not os.path.exists(folder):
|
||||
os.makedirs(folder)
|
||||
|
||||
for part in response.candidates[0].content.parts:
|
||||
if part.inline_data is not None:
|
||||
image = Image.open(BytesIO((part.inline_data.data)))
|
||||
image_path = os.path.join(folder,f"gemini_{D.date_file_name()}_query.png")
|
||||
image.save(image_path)
|
||||
|
||||
LOG.info(f"image generate : {image_path}")
|
||||
time.sleep(2)
|
||||
return image_path
|
||||
Reference in New Issue
Block a user