|
|
|
|
@@ -9,7 +9,7 @@
|
|
|
|
|
@brief: services api
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import requests, json, traceback, os
|
|
|
|
|
import requests, json, traceback, os, shutil
|
|
|
|
|
from fastapi import APIRouter, Depends, Body
|
|
|
|
|
from starlette.requests import Request
|
|
|
|
|
from typing import Annotated, List
|
|
|
|
|
@@ -17,109 +17,111 @@ from typing import Annotated, List
|
|
|
|
|
from main_rest.app.common import consts
|
|
|
|
|
from main_rest.app import models as M
|
|
|
|
|
from main_rest.app.utils.date_utils import D
|
|
|
|
|
from main_rest.app.utils.parsing_utils import image_to_base64_string
|
|
|
|
|
from custom_logger.main_log import main_logger as LOG
|
|
|
|
|
|
|
|
|
|
from custom_apps.bingimagecreator.utils import DallEArgument,dalle3_generate_image
|
|
|
|
|
from custom_apps.bingart.bingart import BingArtGenerator
|
|
|
|
|
from custom_apps.imagen.custom_imagen import imagen_generate_image, imagen_generate_image_path, imagen_generate_temp_image_path
|
|
|
|
|
from main_rest.app.utils.parsing_utils import download_range
|
|
|
|
|
from custom_apps.utils import cookie_manager
|
|
|
|
|
from custom_apps.gemini.main import gemini_image
|
|
|
|
|
from main_rest.app.utils.parsing_utils import download_range
|
|
|
|
|
from utils.custom_sftp import sftp_client
|
|
|
|
|
from config import rest_config
|
|
|
|
|
from const import TEMP_FOLDER
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/services")
|
|
|
|
|
|
|
|
|
|
@router.post("/bing/cookie/set", summary="bing 관련 쿠키 set", response_model=M.BingCookieSetRes)
|
|
|
|
|
async def bing_cookie_set(request: Request, request_body_info: M.BingCookieSetReq):
|
|
|
|
|
"""
|
|
|
|
|
## Bing cookie set
|
|
|
|
|
> 쿠키정보 set
|
|
|
|
|
# @router.post("/bing/cookie/set", summary="bing 관련 쿠키 set", response_model=M.BingCookieSetRes)
|
|
|
|
|
# async def bing_cookie_set(request: Request, request_body_info: M.BingCookieSetReq):
|
|
|
|
|
# """
|
|
|
|
|
# ## Bing cookie set
|
|
|
|
|
# > 쿠키정보 set
|
|
|
|
|
|
|
|
|
|
## 정보
|
|
|
|
|
> cookie 값이 빈 값일경우 쿠키정보를 set 하지 않고 현재 쿠키값 return 함
|
|
|
|
|
> cookie 값이 정상 쿠키 인지는 확인안함
|
|
|
|
|
# ## 정보
|
|
|
|
|
# > cookie 값이 빈 값일경우 쿠키정보를 set 하지 않고 현재 쿠키값 return 함
|
|
|
|
|
# > cookie 값이 정상 쿠키 인지는 확인안함
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
response = M.BingCookieSetRes()
|
|
|
|
|
try:
|
|
|
|
|
if len(request_body_info.cookie) == 0:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
cookie_manager.set_cookie(request_body_info.cookie)
|
|
|
|
|
# """
|
|
|
|
|
# response = M.BingCookieSetRes()
|
|
|
|
|
# try:
|
|
|
|
|
# if len(request_body_info.cookie) == 0:
|
|
|
|
|
# pass
|
|
|
|
|
# else:
|
|
|
|
|
# cookie_manager.set_cookie(request_body_info.cookie)
|
|
|
|
|
|
|
|
|
|
return response.set_message(current_cookie=cookie_manager.get_cookie())
|
|
|
|
|
# return response.set_message(current_cookie=cookie_manager.get_cookie())
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
LOG.error(traceback.format_exc())
|
|
|
|
|
return response.set_error(e,current_cookie=cookie_manager.get_cookie())
|
|
|
|
|
# except Exception as e:
|
|
|
|
|
# LOG.error(traceback.format_exc())
|
|
|
|
|
# return response.set_error(e,current_cookie=cookie_manager.get_cookie())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/imageGenerate/bingimg", summary="이미지 생성(AI) - bing image generator (DALL-E 3)", response_model=M.ImageGenerateRes)
|
|
|
|
|
async def bing_img_generate(request: Request, request_body_info: M.ImageGenerateReq):
|
|
|
|
|
"""
|
|
|
|
|
## 이미지 생성(AI) - bing image generator (DALL-E 3)
|
|
|
|
|
> bing image generator를 이용하여 이미지 생성
|
|
|
|
|
# @router.post("/imageGenerate/bingimg", summary="이미지 생성(AI) - bing image generator (DALL-E 3)", response_model=M.ImageGenerateRes)
|
|
|
|
|
# async def bing_img_generate(request: Request, request_body_info: M.ImageGenerateReq):
|
|
|
|
|
# """
|
|
|
|
|
# ## 이미지 생성(AI) - bing image generator (DALL-E 3)
|
|
|
|
|
# > bing image generator를 이용하여 이미지 생성
|
|
|
|
|
|
|
|
|
|
### Requriements
|
|
|
|
|
# ### Requriements
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 정보
|
|
|
|
|
> 오류 발생시 오류 발생한 파일은 에러 메세지에만 남기고 저장은 안함
|
|
|
|
|
> *동작 안함.
|
|
|
|
|
# ## 정보
|
|
|
|
|
# > 오류 발생시 오류 발생한 파일은 에러 메세지에만 남기고 저장은 안함
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
response = M.ImageGenerateRes()
|
|
|
|
|
try:
|
|
|
|
|
if not download_range(request_body_info.downloadCount):
|
|
|
|
|
raise Exception(f"downloadCount is 1~4 (current value = {request_body_info.downloadCount})")
|
|
|
|
|
# """
|
|
|
|
|
# response = M.ImageGenerateRes()
|
|
|
|
|
# try:
|
|
|
|
|
# if not download_range(request_body_info.downloadCount):
|
|
|
|
|
# raise Exception(f"downloadCount is 1~4 (current value = {request_body_info.downloadCount})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
args = DallEArgument(
|
|
|
|
|
prompt=request_body_info.prompt,
|
|
|
|
|
download_count=request_body_info.downloadCount
|
|
|
|
|
)
|
|
|
|
|
# args = DallEArgument(
|
|
|
|
|
# prompt=request_body_info.prompt,
|
|
|
|
|
# download_count=request_body_info.downloadCount
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
info = dalle3_generate_image(args)
|
|
|
|
|
# info = dalle3_generate_image(args)
|
|
|
|
|
|
|
|
|
|
if info.get_error_messages():
|
|
|
|
|
error_message = f"파일생성 error: {info.get_error_messages()}"
|
|
|
|
|
LOG.error(error_message)
|
|
|
|
|
return response.set_error(error=error_message, img_len=info.get_counter())
|
|
|
|
|
# if info.get_error_messages():
|
|
|
|
|
# error_message = f"파일생성 error: {info.get_error_messages()}"
|
|
|
|
|
# LOG.error(error_message)
|
|
|
|
|
# return response.set_error(error=error_message, img_len=info.get_counter())
|
|
|
|
|
|
|
|
|
|
return response.set_message(img_len=info.get_counter())
|
|
|
|
|
# return response.set_message(img_len=info.get_counter())
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
LOG.error(traceback.format_exc())
|
|
|
|
|
return response.set_error(e)
|
|
|
|
|
# except Exception as e:
|
|
|
|
|
# LOG.error(traceback.format_exc())
|
|
|
|
|
# return response.set_error(e)
|
|
|
|
|
|
|
|
|
|
@router.post("/imageGenerate/bingart", summary="이미지 생성(AI) - bing art (DALL-E 3)", response_model=M.ImageGenerateRes)
|
|
|
|
|
async def bing_art(request: Request, request_body_info: M.ImageGenerateReq):
|
|
|
|
|
"""
|
|
|
|
|
## 이미지 생성(AI) - bing art (DALL-E 3)
|
|
|
|
|
> bing art를 이용하여 이미지 생성
|
|
|
|
|
# @router.post("/imageGenerate/bingart", summary="이미지 생성(AI) - bing art (DALL-E 3)", response_model=M.ImageGenerateRes)
|
|
|
|
|
# async def bing_art(request: Request, request_body_info: M.ImageGenerateReq):
|
|
|
|
|
# """
|
|
|
|
|
# ## 이미지 생성(AI) - bing art (DALL-E 3)
|
|
|
|
|
# > bing art를 이용하여 이미지 생성
|
|
|
|
|
|
|
|
|
|
### Requriements
|
|
|
|
|
# ### Requriements
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 정보
|
|
|
|
|
> 오류 발생시 오류 발생한 파일은 에러 메세지에만 남기고 저장은 안함
|
|
|
|
|
> *동작 안함.
|
|
|
|
|
# ## 정보
|
|
|
|
|
# > 오류 발생시 오류 발생한 파일은 에러 메세지에만 남기고 저장은 안함
|
|
|
|
|
# > *동작 안함.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
response = M.ImageGenerateRes()
|
|
|
|
|
try:
|
|
|
|
|
if not download_range(request_body_info.downloadCount):
|
|
|
|
|
raise Exception(f"downloadCount is 1~4 (current value = {request_body_info.downloadCount})")
|
|
|
|
|
# """
|
|
|
|
|
# response = M.ImageGenerateRes()
|
|
|
|
|
# try:
|
|
|
|
|
# if not download_range(request_body_info.downloadCount):
|
|
|
|
|
# raise Exception(f"downloadCount is 1~4 (current value = {request_body_info.downloadCount})")
|
|
|
|
|
|
|
|
|
|
bing_art = BingArtGenerator()
|
|
|
|
|
# bing_art = BingArtGenerator()
|
|
|
|
|
|
|
|
|
|
info = bing_art.get_images(prompt=request_body_info.prompt,image_len=request_body_info.downloadCount)
|
|
|
|
|
# info = bing_art.get_images(prompt=request_body_info.prompt,image_len=request_body_info.downloadCount)
|
|
|
|
|
|
|
|
|
|
return response.set_message(img_len=info)
|
|
|
|
|
# return response.set_message(img_len=info)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
LOG.error(traceback.format_exc())
|
|
|
|
|
return response.set_error(e)
|
|
|
|
|
# except Exception as e:
|
|
|
|
|
# LOG.error(traceback.format_exc())
|
|
|
|
|
# return response.set_error(e)
|
|
|
|
|
|
|
|
|
|
@router.post("/imageGenerate/imagen", summary="이미지 생성(AI) - imagen", response_model=M.ImageGenerateRes)
|
|
|
|
|
async def imagen(request: Request, request_body_info: M.ImageGenerateReq):
|
|
|
|
|
@@ -127,78 +129,102 @@ async def imagen(request: Request, request_body_info: M.ImageGenerateReq):
|
|
|
|
|
## 이미지 생성(AI) - imagen
|
|
|
|
|
> imagen AI를 이용하여 이미지 생성
|
|
|
|
|
|
|
|
|
|
### Requriements
|
|
|
|
|
> - googlecli 설치(https://cloud.google.com/sdk/docs/install?hl=ko#linux)
|
|
|
|
|
> - const.py 에 지정한 OUTPUT_FOLDER 하위에 imagen 폴더가 있어야함.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# imagen 사용중단 gemini로 변경
|
|
|
|
|
|
|
|
|
|
response = M.ImageGenerateRes()
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
if not download_range(request_body_info.downloadCount):
|
|
|
|
|
raise Exception(f"downloadCount is 1~4 (current value = {request_body_info.downloadCount})")
|
|
|
|
|
|
|
|
|
|
img_length = imagen_generate_image(prompt=request_body_info.prompt,
|
|
|
|
|
download_count=request_body_info.downloadCount
|
|
|
|
|
)
|
|
|
|
|
# NOTE(JWKIM) : imagen 사용 중단
|
|
|
|
|
# img_length = imagen_generate_image(prompt=request_body_info.prompt,
|
|
|
|
|
# download_count=request_body_info.downloadCount
|
|
|
|
|
# )
|
|
|
|
|
temp_image_path = gemini_image(request_body_info.prompt)
|
|
|
|
|
_remote_folder = os.path.join(rest_config.remote_folder,"imagen")
|
|
|
|
|
|
|
|
|
|
# remote save
|
|
|
|
|
sftp_client.remote_copy_data(
|
|
|
|
|
temp_image_path,
|
|
|
|
|
os.path.join(_remote_folder,f"imagen_{request_body_info.prompt}_{1}_{D.date_file_name()}.png"))
|
|
|
|
|
|
|
|
|
|
return response.set_message(img_len=img_length)
|
|
|
|
|
# Clean up temporary files
|
|
|
|
|
if 'temp_image_path' in locals():
|
|
|
|
|
if os.path.exists(temp_image_path):
|
|
|
|
|
os.remove(temp_image_path)
|
|
|
|
|
del temp_image_path
|
|
|
|
|
|
|
|
|
|
return response.set_message(img_len=1)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
LOG.error(traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
# Clean up temporary files
|
|
|
|
|
if 'query_image_path' in locals():
|
|
|
|
|
if os.path.exists(query_image_path):
|
|
|
|
|
os.remove(query_image_path)
|
|
|
|
|
del query_image_path
|
|
|
|
|
|
|
|
|
|
return response.set_error(error=e)
|
|
|
|
|
|
|
|
|
|
@router.post("/vactorImageSearch/imagenet/imageGenerate/imagen", summary="벡터 이미지 검색(imagenet) - imagen", response_model=M.ResponseBase)
|
|
|
|
|
async def vactor_imagenet(request: Request, request_body_info: M.VactorImageSearchReq):
|
|
|
|
|
"""
|
|
|
|
|
## 벡터 이미지 검색 - imagen
|
|
|
|
|
> imagen AI를 이용하여 이미지 생성 후 vactor 검색
|
|
|
|
|
# @router.post("/vactorImageSearch/imagenet/imageGenerate/imagen", summary="벡터 이미지 검색(imagenet) - imagen", response_model=M.ResponseBase)
|
|
|
|
|
# async def vactor_imagenet(request: Request, request_body_info: M.VactorImageSearchReq):
|
|
|
|
|
# """
|
|
|
|
|
# ## 벡터 이미지 검색 - imagen
|
|
|
|
|
# > imagen AI를 이용하여 이미지 생성 후 vector 검색
|
|
|
|
|
|
|
|
|
|
### Requriements
|
|
|
|
|
> - googlecli 설치(https://cloud.google.com/sdk/docs/install?hl=ko#linux)
|
|
|
|
|
> - const.py 에 지정한 OUTPUT_FOLDER 하위에 imagen 폴더가 있어야함.
|
|
|
|
|
# ### Requriements
|
|
|
|
|
# > - googlecli 설치(https://cloud.google.com/sdk/docs/install?hl=ko#linux)
|
|
|
|
|
# > - const.py 에 지정한 OUTPUT_FOLDER 하위에 imagen 폴더가 있어야함.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
response = M.ResponseBase()
|
|
|
|
|
try:
|
|
|
|
|
if request_body_info.indexType not in [M.IndexType.hnsw, M.IndexType.l2]:
|
|
|
|
|
raise Exception(f"indexType is hnsw or l2 (current value = {request_body_info.indexType})")
|
|
|
|
|
# """
|
|
|
|
|
# # NOTE(JWKIM) : GPU 메모리 이슈로 사용 중단
|
|
|
|
|
# response = M.ResponseBase()
|
|
|
|
|
# try:
|
|
|
|
|
# if request_body_info.indexType not in [M.IndexType.hnsw, M.IndexType.l2]:
|
|
|
|
|
# raise Exception(f"indexType is hnsw or l2 (current value = {request_body_info.indexType})")
|
|
|
|
|
|
|
|
|
|
img_path = imagen_generate_image_path(image_prompt=request_body_info.prompt)
|
|
|
|
|
# _temp_folder = f"{request_body_info.prompt}_{D.date_file_name()}"
|
|
|
|
|
|
|
|
|
|
vactor_request_data = {'query_image_path' : img_path,
|
|
|
|
|
'index_type' : request_body_info.indexType,
|
|
|
|
|
'search_num' : request_body_info.searchNum}
|
|
|
|
|
vactor_response = requests.post('http://localhost:51002/api/services/faiss/vactor/search/imagenet', data=json.dumps(vactor_request_data))
|
|
|
|
|
# # img_path = imagen_generate_image_path(image_prompt=request_body_info.prompt) #imagen
|
|
|
|
|
# img_path = gemini_image(request_body_info.prompt, os.path.join(TEMP_FOLDER, _temp_folder)) #gemini
|
|
|
|
|
|
|
|
|
|
if vactor_response.status_code != 200:
|
|
|
|
|
raise Exception(f"response error: {json.loads(vactor_response.text)['error']}")
|
|
|
|
|
# vector_request_data = {'query_image_path' : img_path,
|
|
|
|
|
# 'index_type' : request_body_info.indexType,
|
|
|
|
|
# 'search_num' : request_body_info.searchNum}
|
|
|
|
|
# vector_response = requests.post('http://localhost:51002/api/services/faiss/vector/search/imagenet', data=json.dumps(vector_request_data))
|
|
|
|
|
|
|
|
|
|
if json.loads(vactor_response.text)["error"] != None:
|
|
|
|
|
raise Exception(f"vactor error: {json.loads(vactor_response.text)['error']}")
|
|
|
|
|
# if vector_response.status_code != 200:
|
|
|
|
|
# raise Exception(f"response error: {json.loads(vector_response.text)['error']}")
|
|
|
|
|
|
|
|
|
|
# remote
|
|
|
|
|
_directory_path, _file = os.path.split(img_path)
|
|
|
|
|
_base_bame = os.path.basename(_directory_path)
|
|
|
|
|
# if json.loads(vector_response.text)["error"] != None:
|
|
|
|
|
# raise Exception(f"vector error: {json.loads(vector_response.text)['error']}")
|
|
|
|
|
|
|
|
|
|
# remote 폴더 생성
|
|
|
|
|
sftp_client.remote_mkdir(os.path.join(rest_config.remote_folder, _base_bame))
|
|
|
|
|
# # remote
|
|
|
|
|
# _directory_path, _file = os.path.split(img_path)
|
|
|
|
|
|
|
|
|
|
# remote 폴더에 이미지 저장
|
|
|
|
|
for i in os.listdir(_directory_path):
|
|
|
|
|
sftp_client.remote_copy_data(local_path=os.path.join(_directory_path, i), remote_path=os.path.join(rest_config.remote_folder, _base_bame, i))
|
|
|
|
|
# # remote 폴더 생성
|
|
|
|
|
# sftp_client.remote_mkdir(os.path.join(rest_config.remote_folder, _temp_folder))
|
|
|
|
|
|
|
|
|
|
return response.set_message()
|
|
|
|
|
# # remote 폴더에 이미지 저장
|
|
|
|
|
# for i in os.listdir(_directory_path):
|
|
|
|
|
# sftp_client.remote_copy_data(local_path=os.path.join(_directory_path, i),
|
|
|
|
|
# remote_path=os.path.join(rest_config.remote_folder, _temp_folder, i))
|
|
|
|
|
# shutil.rmtree(_directory_path)
|
|
|
|
|
# return response.set_message()
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
LOG.error(traceback.format_exc())
|
|
|
|
|
return response.set_error(error=e)
|
|
|
|
|
# except Exception as e:
|
|
|
|
|
# LOG.error(traceback.format_exc())
|
|
|
|
|
# return response.set_error(error=e)
|
|
|
|
|
|
|
|
|
|
@router.post("/vactorImageSearch/vit/imageGenerate/imagen", summary="벡터 이미지 검색(clip-vit) - imagen", response_model=M.ResponseBase)
|
|
|
|
|
async def vactor_vit_report(request: Request, request_body_info: M.VactorImageSearchVitReq):
|
|
|
|
|
async def vactor_vit(request: Request, request_body_info: M.VectorImageSearchVitReq):
|
|
|
|
|
"""
|
|
|
|
|
## 벡터 이미지 검색(clip-vit) - imagen
|
|
|
|
|
> imagen AI를 이용하여 이미지 생성 후 vactor 검색 그후 결과 이미지 생성
|
|
|
|
|
> imagen AI를 이용하여 이미지 생성 후 vector 검색 그후 결과 이미지 생성
|
|
|
|
|
|
|
|
|
|
### Requriements
|
|
|
|
|
> - googlecli 설치(https://cloud.google.com/sdk/docs/install?hl=ko#linux)
|
|
|
|
|
@@ -219,14 +245,15 @@ async def vactor_vit_report(request: Request, request_body_info: M.VactorImageSe
|
|
|
|
|
if request_body_info.indexType not in [M.VitIndexType.cos, M.VitIndexType.l2]:
|
|
|
|
|
raise Exception(f"indexType is invalid (current value = {request_body_info.indexType})")
|
|
|
|
|
|
|
|
|
|
query_image_path = imagen_generate_temp_image_path(image_prompt=request_body_info.prompt)
|
|
|
|
|
# query_image_path = imagen_generate_temp_image_path(image_prompt=request_body_info.prompt) #imagen
|
|
|
|
|
query_image_path = gemini_image(request_body_info.prompt) #gemini
|
|
|
|
|
|
|
|
|
|
vector_request_data = {'query_image_path' : query_image_path,
|
|
|
|
|
'index_type' : request_body_info.indexType,
|
|
|
|
|
'model_type' : request_body_info.modelType,
|
|
|
|
|
'search_num' : request_body_info.searchNum}
|
|
|
|
|
|
|
|
|
|
vector_response = requests.post('http://localhost:51002/api/services/faiss/vactor/search/vit', data=json.dumps(vector_request_data))
|
|
|
|
|
vector_response = requests.post('http://localhost:51002/api/services/faiss/vector/search/vit', data=json.dumps(vector_request_data))
|
|
|
|
|
|
|
|
|
|
vector_response_dict = json.loads(vector_response.text)
|
|
|
|
|
|
|
|
|
|
@@ -234,7 +261,7 @@ async def vactor_vit_report(request: Request, request_body_info: M.VactorImageSe
|
|
|
|
|
raise Exception(f"response error: {vector_response_dict['error']}")
|
|
|
|
|
|
|
|
|
|
if vector_response_dict["error"] != None:
|
|
|
|
|
raise Exception(f"vactor error: {vector_response_dict['error']}")
|
|
|
|
|
raise Exception(f"vector error: {vector_response_dict['error']}")
|
|
|
|
|
|
|
|
|
|
result_image_paths = vector_response_dict.get('img_list').get('result_image_paths')
|
|
|
|
|
result_percents = vector_response_dict.get('img_list').get('result_percents')
|
|
|
|
|
@@ -267,12 +294,92 @@ async def vactor_vit_report(request: Request, request_body_info: M.VactorImageSe
|
|
|
|
|
del query_image_path
|
|
|
|
|
|
|
|
|
|
return response.set_error(error=e)
|
|
|
|
|
|
|
|
|
|
@router.post("/vactorImageSearch/vit/imageGenerate/imagen/data", summary="벡터 이미지 검색(clip-vit) - imagen(data)", response_model=M.VectorImageSerachDataRes)
|
|
|
|
|
async def vactor_vit_report_data(request: Request, request_body_info: M.VectorImageSearchVitDataReq):
|
|
|
|
|
"""
|
|
|
|
|
## 벡터 이미지 검색(clip-vit) - imagen
|
|
|
|
|
> imagen AI를 이용하여 이미지 생성 후 vector 검색 그후 결과 이미지데이터 return
|
|
|
|
|
|
|
|
|
|
### Requriements
|
|
|
|
|
> - googlecli 설치(https://cloud.google.com/sdk/docs/install?hl=ko#linux)
|
|
|
|
|
|
|
|
|
|
### options
|
|
|
|
|
> - modelType -> b32,b16,l14,l14_336
|
|
|
|
|
> - indexType -> l2,cos
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
response = M.VectorImageSerachDataRes()
|
|
|
|
|
query_image_data = ''
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if not download_range(request_body_info.searchNum, max=10):
|
|
|
|
|
raise Exception(f"downloadCound is invalid (current value = {request_body_info.searchNum})")
|
|
|
|
|
|
|
|
|
|
if request_body_info.modelType not in [M.VitModelType.b32, M.VitModelType.b16, M.VitModelType.l14, M.VitModelType.l14_336]:
|
|
|
|
|
raise Exception(f"modelType is invalid (current value = {request_body_info.modelType})")
|
|
|
|
|
|
|
|
|
|
if request_body_info.indexType not in [M.VitIndexType.cos, M.VitIndexType.l2]:
|
|
|
|
|
raise Exception(f"indexType is invalid (current value = {request_body_info.indexType})")
|
|
|
|
|
|
|
|
|
|
# query_image_path = imagen_generate_temp_image_path(image_prompt=request_body_info.prompt) #imagen
|
|
|
|
|
query_image_path = gemini_image(request_body_info.prompt) #gemini
|
|
|
|
|
|
|
|
|
|
vector_request_data = {'query_image_path' : query_image_path,
|
|
|
|
|
'index_type' : request_body_info.indexType,
|
|
|
|
|
'model_type' : request_body_info.modelType,
|
|
|
|
|
'search_num' : request_body_info.searchNum}
|
|
|
|
|
|
|
|
|
|
vector_response = requests.post('http://localhost:51002/api/services/faiss/vector/search/vit', data=json.dumps(vector_request_data))
|
|
|
|
|
|
|
|
|
|
vector_response_dict = json.loads(vector_response.text)
|
|
|
|
|
|
|
|
|
|
if vector_response.status_code != 200:
|
|
|
|
|
raise Exception(f"response error: {vector_response_dict['error']}")
|
|
|
|
|
|
|
|
|
|
if vector_response_dict["error"] != None:
|
|
|
|
|
raise Exception(f"vector error: {vector_response_dict['error']}")
|
|
|
|
|
|
|
|
|
|
result_image_paths = vector_response_dict.get('img_list').get('result_image_paths')
|
|
|
|
|
result_percents = vector_response_dict.get('img_list').get('result_percents')
|
|
|
|
|
|
|
|
|
|
# 이미지 데이터 생성
|
|
|
|
|
vector_image_results = []
|
|
|
|
|
for img, percents in zip(result_image_paths, result_percents):
|
|
|
|
|
b64_data = image_to_base64_string(img)
|
|
|
|
|
float_percent = float(percents)
|
|
|
|
|
|
|
|
|
|
info = M.VectorImageResult(image=b64_data,percents=float_percent)
|
|
|
|
|
|
|
|
|
|
vector_image_results.append(info)
|
|
|
|
|
|
|
|
|
|
if request_body_info.querySend:
|
|
|
|
|
query_image_data = image_to_base64_string(query_image_path)
|
|
|
|
|
|
|
|
|
|
# Clean up temporary files
|
|
|
|
|
if 'query_image_path' in locals():
|
|
|
|
|
if os.path.exists(query_image_path):
|
|
|
|
|
os.remove(query_image_path)
|
|
|
|
|
del query_image_path
|
|
|
|
|
|
|
|
|
|
return response.set_message(vector_result=vector_image_results,query_img=query_image_data)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
LOG.error(traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
# Clean up temporary files
|
|
|
|
|
if 'query_image_path' in locals():
|
|
|
|
|
if os.path.exists(query_image_path):
|
|
|
|
|
os.remove(query_image_path)
|
|
|
|
|
del query_image_path
|
|
|
|
|
|
|
|
|
|
return response.set_error(error=e)
|
|
|
|
|
|
|
|
|
|
@router.post("/vactorImageSearch/vit/imageGenerate/imagen/report", summary="벡터 이미지 검색(clip-vit) - imagen, report 생성", response_model=M.ResponseBase)
|
|
|
|
|
async def vactor_vit_report(request: Request, request_body_info: M.VactorImageSearchVitReportReq):
|
|
|
|
|
async def vactor_vit_report(request: Request, request_body_info: M.VectorImageSearchVitReportReq):
|
|
|
|
|
"""
|
|
|
|
|
## 벡터 이미지 검색(clip-vit) - imagen, report 생성
|
|
|
|
|
> imagen AI를 이용하여 이미지 생성 후 vactor 검색 그후 종합결과 이미지 생성
|
|
|
|
|
> imagen AI를 이용하여 이미지 생성 후 vector 검색 그후 종합결과 이미지 생성
|
|
|
|
|
|
|
|
|
|
### Requriements
|
|
|
|
|
> - googlecli 설치(https://cloud.google.com/sdk/docs/install?hl=ko#linux)
|
|
|
|
|
@@ -290,7 +397,8 @@ async def vactor_vit_report(request: Request, request_body_info: M.VactorImageSe
|
|
|
|
|
if request_body_info.indexType not in [M.VitIndexType.cos, M.VitIndexType.l2]:
|
|
|
|
|
raise Exception(f"indexType is invalid (current value = {request_body_info.indexType})")
|
|
|
|
|
|
|
|
|
|
query_image_path = imagen_generate_temp_image_path(image_prompt=request_body_info.prompt)
|
|
|
|
|
# query_image_path = imagen_generate_temp_image_path(image_prompt=request_body_info.prompt) #imagen
|
|
|
|
|
query_image_path = gemini_image(request_body_info.prompt) #gemini
|
|
|
|
|
report_image_path = f"{os.path.splitext(query_image_path)[0]}_report.png"
|
|
|
|
|
|
|
|
|
|
vactor_request_data = {'query_image_path' : query_image_path,
|
|
|
|
|
@@ -298,13 +406,13 @@ async def vactor_vit_report(request: Request, request_body_info: M.VactorImageSe
|
|
|
|
|
'model_type' : request_body_info.modelType,
|
|
|
|
|
'report_path' : report_image_path}
|
|
|
|
|
|
|
|
|
|
vactor_response = requests.post('http://localhost:51002/api/services/faiss/vactor/search/vit/report', data=json.dumps(vactor_request_data))
|
|
|
|
|
vactor_response = requests.post('http://localhost:51002/api/services/faiss/vector/search/vit/report', data=json.dumps(vactor_request_data))
|
|
|
|
|
|
|
|
|
|
if vactor_response.status_code != 200:
|
|
|
|
|
raise Exception(f"response error: {json.loads(vactor_response.text)['error']}")
|
|
|
|
|
|
|
|
|
|
if json.loads(vactor_response.text)["error"] != None:
|
|
|
|
|
raise Exception(f"vactor error: {json.loads(vactor_response.text)['error']}")
|
|
|
|
|
raise Exception(f"vector error: {json.loads(vactor_response.text)['error']}")
|
|
|
|
|
|
|
|
|
|
# remote 폴더에 이미지 저장
|
|
|
|
|
sftp_client.remote_copy_data(local_path=report_image_path,
|
|
|
|
|
|