From 90609c08b8538cb67d940fd4c5b5c5665e23d62b Mon Sep 17 00:00:00 2001 From: jwkim Date: Wed, 6 Aug 2025 16:04:02 +0900 Subject: [PATCH] =?UTF-8?q?edit:=20ict=EC=84=BC=ED=84=B0=EC=9E=85=EA=B3=A0?= =?UTF-8?q?=EC=84=B8=ED=8C=85=EC=9D=84=20=EA=B8=B0=EB=B3=B8=EA=B0=92?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=84=A4=EC=A0=95,=20=20ict=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=EC=8B=9C=20sftp=EB=B9=84=ED=99=9C=EC=84=B1=ED=99=94,?= =?UTF-8?q?=20l14=5F336=20=EC=82=AC=EC=9A=A9=20=EB=AA=BB=ED=95=98=EB=8D=98?= =?UTF-8?q?=EA=B2=83=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 22 ++----- .../faiss_similarity_search.py | 4 +- custom_apps/gemini/main.py | 43 +++++++------ main_rest/app/common/config.py | 17 +++-- main_rest/app/routes/services.py | 64 ++++++++++++------- utils/custom_sftp.py | 3 +- 6 files changed, 82 insertions(+), 71 deletions(-) diff --git a/config.py b/config.py index 63503a5..d7b564a 100644 --- a/config.py +++ b/config.py @@ -1,13 +1,13 @@ class Config: def __init__(self): - self.set_fermat() + self.set_ict() self.use_imagen = True def set_fermat(self): """ 내부 서버용 """ - self.config = 'rel' + self.config = 'dev' self.remote_folder = "/home/fermat/STORAGE/01.Projects/A2TEC/K_EYEWEAR/02.ML_DATA/Image_generator_result" self.sftp_host = "192.168.200.230" self.sftp_port = 22 @@ -30,20 +30,8 @@ class Config: os.makedirs(self.remote_folder) def set_ict(self): - pass - # self.config = 'rel' - # self.remote_folder = "/home/fermat/STORAGE/01.Projects/A2TEC/K_EYEWEAR/02.ML_DATA/Image_generator_result" - # self.sftp_host = "192.168.200.230" - # self.sftp_port = 22 - # self.sftp_id = "fermat" - # self.sftp_pw = "1234" - - def toggle_imagen(self): - if self.use_imagen: - self.use_imagen = False - else: - self.use_imagen = True + self.config = 'release' + self.local_folder = "./result" # 결과물 저장 폴더 rest_config = Config() -rest_config.set_dev() -rest_config.toggle_imagen() \ No newline at end of file +# rest_config.set_dev() \ No newline at end of file diff --git a/custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/faiss_similarity_search.py b/custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/faiss_similarity_search.py index 6982e8d..059dac2 100755 --- a/custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/faiss_similarity_search.py +++ b/custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/faiss_similarity_search.py @@ -103,12 +103,12 @@ class FEMUsageInfo(Enum): token=HUGGINGFACE_TOKEN, index_type=INDEX_TYPE_COSINE)] - openaiclip_vit_b14_336_l2 = [FEOpenAIClipViT, FEM.FEMArguments( + openaiclip_vit_l14_336_l2 = [FEOpenAIClipViT, FEM.FEMArguments( trained_model=os.path.join(PRETRAINED_MODEL_PATH,ViTL14_336), token=HUGGINGFACE_TOKEN, index_type=INDEX_TYPE_L2)] - openaiclip_vit_b14_336_cos = [FEOpenAIClipViT, FEM.FEMArguments( + openaiclip_vit_l14_336_cos = [FEOpenAIClipViT, FEM.FEMArguments( trained_model=os.path.join(PRETRAINED_MODEL_PATH,ViTL14_336), token=HUGGINGFACE_TOKEN, index_type=INDEX_TYPE_COSINE)] diff --git a/custom_apps/gemini/main.py b/custom_apps/gemini/main.py index 1f26df1..cd2c7a2 100644 --- a/custom_apps/gemini/main.py +++ b/custom_apps/gemini/main.py @@ -16,25 +16,30 @@ def gemini_image(prompt, folder=None): 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'] + for i in range(3): + 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 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) + + if os.path.exists(image_path): + return image_path - 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 \ No newline at end of file diff --git a/main_rest/app/common/config.py b/main_rest/app/common/config.py index f433fd5..b013876 100644 --- a/main_rest/app/common/config.py +++ b/main_rest/app/common/config.py @@ -49,12 +49,11 @@ class Config: @dataclass -class LocalConfig(Config): +class ReleaseConfig(Config): TRUSTED_HOSTS = ['*'] ALLOW_SITE = ['*'] DEBUG: bool = True - @dataclass class ProdConfig(Config): TRUSTED_HOSTS = ['*'] @@ -74,8 +73,6 @@ class DevConfig(Config): TRUSTED_HOSTS = ['*'] ALLOW_SITE = ['*'] DEBUG: bool = True - DB_URL: str = environ.get('DB_URL', f'mysql+pymysql://{consts.DB_USER_ID}:{consts.DB_USER_PW}@{consts.DB_ADDRESS}/{consts.DB_NAME}_dev?charset={consts.DB_CHARSET}') - REST_SERVER_PORT = consts.REST_SERVER_PORT + 1 SW_TITLE = '[Dev] ' + consts.SW_TITLE @@ -103,9 +100,11 @@ def conf(): 환경 불러오기 :return: """ - config = dict(prod=ProdConfig, local=LocalConfig, test=TestConfig, dev=DevConfig, my=MyConfig) - return config[environ.get('API_ENV', 'local')]() - return config[environ.get('API_ENV', 'dev')]() - return config[environ.get('API_ENV', 'my')]() - return config[environ.get('API_ENV', 'test')]() + from config import rest_config + + config = dict(prod=ProdConfig, release=ReleaseConfig, test=TestConfig, dev=DevConfig, my=MyConfig) + return config[environ.get('API_ENV', rest_config.config)]() + # return config[environ.get('API_ENV', 'dev')]() + # return config[environ.get('API_ENV', 'my')]() + # return config[environ.get('API_ENV', 'test')]() diff --git a/main_rest/app/routes/services.py b/main_rest/app/routes/services.py index fcde088..7de101d 100644 --- a/main_rest/app/routes/services.py +++ b/main_rest/app/routes/services.py @@ -20,9 +20,9 @@ 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 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 custom_apps.utils import cookie_manager from custom_apps.gemini.main import gemini_image from main_rest.app.utils.parsing_utils import download_range @@ -144,12 +144,17 @@ async def imagen(request: Request, request_body_info: M.ImageGenerateReq): # 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")) + + if rest_config.config != 'release': + _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")) + else: + _local_forder = os.path.join(rest_config.local_folder,"image_generate","imagen") + os.makedirs(_local_forder,exist_ok=True) + shutil.copy(temp_image_path, os.path.join(_local_forder,f"imagen_{request_body_info.prompt}_{1}_{D.date_file_name()}.png")) # Clean up temporary files if 'temp_image_path' in locals(): @@ -266,16 +271,26 @@ async def vactor_vit(request: Request, request_body_info: M.VectorImageSearchVit result_image_paths = vector_response_dict.get('img_list').get('result_image_paths') result_percents = vector_response_dict.get('img_list').get('result_percents') - # 원격지 폴더 생성 - remote_directory = os.path.join(rest_config.remote_folder, f"imagen_query_{request_body_info.modelType}_{request_body_info.indexType}_{request_body_info.prompt}_{D.date_file_name()}") - sftp_client.remote_mkdir(remote_directory) - - # 원격지에 이미지 저장 - sftp_client.remote_copy_data(local_path=query_image_path, remote_path=os.path.join(remote_directory,"query.png")) - - for img_path, img_percent in zip(result_image_paths,result_percents): - sftp_client.remote_copy_data(local_path=img_path, remote_path=os.path.join(remote_directory,f"search_{img_percent}.png")) - + if rest_config.config != 'release': + # 원격지 폴더 생성 + remote_directory = os.path.join(rest_config.remote_folder, f"imagen_query_{request_body_info.modelType}_{request_body_info.indexType}_{request_body_info.prompt}_{D.date_file_name()}") + sftp_client.remote_mkdir(remote_directory) + + # 원격지에 이미지 저장 + sftp_client.remote_copy_data(local_path=query_image_path, remote_path=os.path.join(remote_directory,"query.png")) + + for img_path, img_percent in zip(result_image_paths,result_percents): + sftp_client.remote_copy_data(local_path=img_path, remote_path=os.path.join(remote_directory,f"search_{img_percent}.png")) + else: + local_directory = os.path.join(rest_config.local_folder, f"imagen_query_{request_body_info.modelType}_{request_body_info.indexType}_{request_body_info.prompt}_{D.date_file_name()}") + os.makedirs(local_directory, exist_ok=True) + + shutil.copy(query_image_path, os.path.join(local_directory,"query.png")) + + for img_path, img_percent in zip(result_image_paths,result_percents): + shutil.copy(img_path, os.path.join(local_directory,f"search_{img_percent}.png")) + + # Clean up temporary files if 'query_image_path' in locals(): if os.path.exists(query_image_path): @@ -414,10 +429,13 @@ async def vactor_vit_report(request: Request, request_body_info: M.VectorImageSe if json.loads(vactor_response.text)["error"] != None: raise Exception(f"vector error: {json.loads(vactor_response.text)['error']}") - # remote 폴더에 이미지 저장 - sftp_client.remote_copy_data(local_path=report_image_path, - remote_path=os.path.join(rest_config.remote_folder, f"imagen_report_vit_{request_body_info.prompt}_{D.date_file_name()}.png")) - + if rest_config.config != 'release': + # remote 폴더에 이미지 저장 + sftp_client.remote_copy_data(local_path=report_image_path, + remote_path=os.path.join(rest_config.remote_folder, f"imagen_report_vit_{request_body_info.prompt}_{D.date_file_name()}.png")) + else: + shutil.copy(report_image_path, os.path.join(rest_config.local_folder, f"imagen_report_vit_{request_body_info.prompt}_{D.date_file_name()}.png")) + # Clean up temporary files if 'query_image_path' in locals(): if os.path.exists(query_image_path): diff --git a/utils/custom_sftp.py b/utils/custom_sftp.py index 3b6f591..d32e946 100644 --- a/utils/custom_sftp.py +++ b/utils/custom_sftp.py @@ -1,4 +1,5 @@ import paramiko +from config import rest_config class CustomSFTPClient(): def __init__(self): @@ -26,5 +27,5 @@ class CustomSFTPClient(): sftp_client = None -if sftp_client is None: +if sftp_client is None and rest_config.config != 'release': sftp_client = CustomSFTPClient() \ No newline at end of file