edit : api key 파일로 관리
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -147,4 +147,5 @@ log
|
|||||||
*temp
|
*temp
|
||||||
datas/*
|
datas/*
|
||||||
|
|
||||||
*result
|
*result
|
||||||
|
*.env
|
||||||
@@ -15,6 +15,8 @@ class Config:
|
|||||||
self.sftp_port = 22
|
self.sftp_port = 22
|
||||||
self.sftp_id = "fermat"
|
self.sftp_id = "fermat"
|
||||||
self.sftp_pw = "1234"
|
self.sftp_pw = "1234"
|
||||||
|
|
||||||
|
self.api_path = "./gemini.env"
|
||||||
|
|
||||||
def set_dev(self):
|
def set_dev(self):
|
||||||
"""
|
"""
|
||||||
@@ -28,6 +30,8 @@ class Config:
|
|||||||
self.sftp_pw = "fermat3514"
|
self.sftp_pw = "fermat3514"
|
||||||
self.db_pw = "1234"
|
self.db_pw = "1234"
|
||||||
|
|
||||||
|
self.api_path = "./gemini.env"
|
||||||
|
|
||||||
if not os.path.exists(self.remote_folder):
|
if not os.path.exists(self.remote_folder):
|
||||||
os.makedirs(self.remote_folder)
|
os.makedirs(self.remote_folder)
|
||||||
|
|
||||||
@@ -37,6 +41,8 @@ class Config:
|
|||||||
|
|
||||||
self.db_pw = "Fermat3514!"
|
self.db_pw = "Fermat3514!"
|
||||||
|
|
||||||
|
self.api_path = "../gemini.env"
|
||||||
|
|
||||||
if not os.path.exists(self.local_folder):
|
if not os.path.exists(self.local_folder):
|
||||||
os.makedirs(self.local_folder)
|
os.makedirs(self.local_folder)
|
||||||
|
|
||||||
|
|||||||
6
const.py
6
const.py
@@ -1,3 +1,7 @@
|
|||||||
|
from utils.api_key_manager import ApiKeyManager
|
||||||
|
|
||||||
TEMP_FOLDER = "./temp"
|
TEMP_FOLDER = "./temp"
|
||||||
|
|
||||||
ILLEGAL_FILE_NAME = ['<', '>', ':', '"', '/', '\ ', '|', '?', '*']
|
ILLEGAL_FILE_NAME = ['<', '>', ':', '"', '/', '\ ', '|', '?', '*']
|
||||||
|
|
||||||
|
API_KEY_MANAGER = ApiKeyManager()
|
||||||
@@ -7,14 +7,18 @@ from PIL import Image
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from main_rest.app.utils.date_utils import D
|
from main_rest.app.utils.date_utils import D
|
||||||
from const import TEMP_FOLDER
|
from const import TEMP_FOLDER, API_KEY_MANAGER
|
||||||
|
|
||||||
|
|
||||||
def gemini_image(prompt, folder=None):
|
def gemini_image(prompt, folder=None):
|
||||||
from custom_logger.main_log import main_logger as LOG
|
from custom_logger.main_log import main_logger as LOG
|
||||||
|
|
||||||
image_path = ''
|
image_path = ''
|
||||||
client = genai.Client(api_key="AIzaSyCSw4pcPDYdAnjzBB7J9ZKXtRJJvunjWtA") # a2tec key
|
api_key = API_KEY_MANAGER.get_api_key()
|
||||||
|
|
||||||
|
if api_key is None:
|
||||||
|
raise Exception("API 키 세팅 필요! - 서버를 다시 구동하거나, API키 파일을 확인")
|
||||||
|
|
||||||
|
client = genai.Client(api_key=API_KEY_MANAGER.get_api_key()) # a2tec key
|
||||||
|
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
response = client.models.generate_content(
|
response = client.models.generate_content(
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ from custom_logger.main_log import main_logger as LOG
|
|||||||
|
|
||||||
API_KEY_HEADER = APIKeyHeader(name='Authorization', auto_error=False)
|
API_KEY_HEADER = APIKeyHeader(name='Authorization', auto_error=False)
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# When service starts.
|
# When service starts.
|
||||||
@@ -42,6 +41,10 @@ async def lifespan(app: FastAPI):
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import const
|
import const
|
||||||
|
from const import API_KEY_MANAGER
|
||||||
|
|
||||||
|
API_KEY_MANAGER.set_api_key()
|
||||||
|
|
||||||
if os.path.exists(const.TEMP_FOLDER):
|
if os.path.exists(const.TEMP_FOLDER):
|
||||||
for _file in os.scandir(const.TEMP_FOLDER):
|
for _file in os.scandir(const.TEMP_FOLDER):
|
||||||
os.remove(_file)
|
os.remove(_file)
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ pycryptodomex
|
|||||||
pycryptodome
|
pycryptodome
|
||||||
email-validator
|
email-validator
|
||||||
requests
|
requests
|
||||||
|
python-dotenv
|
||||||
|
|
||||||
#imagen
|
#imagen
|
||||||
google-cloud-aiplatform
|
google-cloud-aiplatform
|
||||||
|
|||||||
33
utils/api_key_manager.py
Normal file
33
utils/api_key_manager.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
from config import rest_config
|
||||||
|
from custom_logger.main_log import main_logger as LOG
|
||||||
|
|
||||||
|
|
||||||
|
class ApiKeyManager:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.api_key = None
|
||||||
|
|
||||||
|
def set_api_key(self, env_path=rest_config.api_path):
|
||||||
|
|
||||||
|
if os.path.exists(env_path):
|
||||||
|
|
||||||
|
load_dotenv(dotenv_path=env_path)
|
||||||
|
key = os.getenv("GEMINI_API_KEY")
|
||||||
|
|
||||||
|
if key is None:
|
||||||
|
LOG.error(f"api key 파일에 GEMINI_API_KEY라는 변수가 없습니다")
|
||||||
|
elif key == "":
|
||||||
|
LOG.error(f"api key 파일에 내부 변수 GEMINI_API_KEY 값이 빈값입니다 값을 설정해주세요")
|
||||||
|
else:
|
||||||
|
self.api_key = key
|
||||||
|
|
||||||
|
else:
|
||||||
|
LOG.error(f"api key 파일이 없습니다 : {os.path.abspath(env_path)}")
|
||||||
|
|
||||||
|
def get_api_key(self):
|
||||||
|
if self.api_key is not None:
|
||||||
|
return self.api_key
|
||||||
|
|
||||||
Reference in New Issue
Block a user