Update app.py
Browse files
app.py
CHANGED
|
@@ -7,79 +7,109 @@ from typing import List, Tuple, Optional, Any
|
|
| 7 |
import gradio as gr
|
| 8 |
from gradio_client import Client, handle_file
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 10 |
|
| 11 |
# .env ํ์ผ ๋ก๋
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
# ๋ก๊น
์ค์ (API ์๋ํฌ์ธํธ ์ ๋ณด๋ ๋ก๊ทธ์ ๋จ๊ธฐ์ง ์์)
|
| 15 |
-
logging.
|
| 16 |
-
|
| 17 |
-
format
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
logger = logging.getLogger("image-enhancer-control-tower")
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
class GradioClientController:
|
| 26 |
"""ํ๊น
ํ์ด์ค ๊ทธ๋ผ๋์ค API ํด๋ผ์ด์ธํธ ์ปจํธ๋กค๋ฌ"""
|
| 27 |
|
| 28 |
def __init__(self):
|
| 29 |
self.client = None
|
|
|
|
| 30 |
self._initialize_client()
|
| 31 |
|
| 32 |
def _initialize_client(self):
|
| 33 |
"""ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (์๋ํฌ์ธํธ ์ ๋ณด๋ ๋ก๊ทธ์ ๋จ๊ธฐ์ง ์์)"""
|
| 34 |
try:
|
| 35 |
-
api_endpoint = os.environ.get("API_ENDPOINT")
|
| 36 |
-
if not api_endpoint:
|
| 37 |
logger.error("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 38 |
raise ValueError("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ํ์ํฉ๋๋ค.")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
logger.error(f"ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์คํจ: {e}")
|
| 45 |
self.client = None
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def update_dropdowns(self, bg_type: str) -> Tuple:
|
| 48 |
"""๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ฅธ ๋๋กญ๋ค์ด ์
๋ฐ์ดํธ"""
|
| 49 |
try:
|
| 50 |
-
if not self.
|
| 51 |
-
logger.error("
|
| 52 |
return tuple([gr.update() for _ in range(7)])
|
| 53 |
|
| 54 |
result = self.client.predict(
|
| 55 |
-
bg_type
|
| 56 |
api_name="/update_dropdowns"
|
| 57 |
)
|
| 58 |
|
| 59 |
logger.info(f"๋๋กญ๋ค์ด ์
๋ฐ์ดํธ ์๋ฃ: {bg_type}")
|
| 60 |
|
| 61 |
# ๊ฒฐ๊ณผ๋ฅผ Gradio ์
๋ฐ์ดํธ ํ์์ผ๋ก ๋ณํ
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
return tuple(updates)
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
-
logger.error(f"๋๋กญ๋ค์ด ์
๋ฐ์ดํธ ์ค๋ฅ: {e}")
|
| 83 |
return tuple([gr.update() for _ in range(7)])
|
| 84 |
|
| 85 |
def generate_prompt_only(self, password: str, bg_type: str, simple: str, studio: str,
|
|
@@ -87,30 +117,29 @@ class GradioClientController:
|
|
| 87 |
special_effects: str, request_text: str, aspect_ratio: str) -> str:
|
| 88 |
"""ํ๋กฌํํธ๋ง ์์ฑ"""
|
| 89 |
try:
|
| 90 |
-
if not self.
|
| 91 |
-
|
| 92 |
-
return "ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ค๋ฅ"
|
| 93 |
|
| 94 |
result = self.client.predict(
|
| 95 |
-
password
|
| 96 |
-
bg_type
|
| 97 |
-
simple
|
| 98 |
-
studio
|
| 99 |
-
nature
|
| 100 |
-
indoor
|
| 101 |
-
special
|
| 102 |
-
jewelry
|
| 103 |
-
special_effects
|
| 104 |
-
request_text
|
| 105 |
-
aspect_ratio
|
| 106 |
api_name="/generate_prompt_with_password_check"
|
| 107 |
)
|
| 108 |
|
| 109 |
logger.info("ํ๋กฌํํธ ์์ฑ ์๋ฃ")
|
| 110 |
-
return result
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
-
logger.error(f"ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {e}")
|
| 114 |
return f"ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {str(e)}"
|
| 115 |
|
| 116 |
def process_image(self, password: str, image, bg_type: str, simple: str, studio: str,
|
|
@@ -118,58 +147,66 @@ class GradioClientController:
|
|
| 118 |
request_text: str, quality_level: str, aspect_ratio: str,
|
| 119 |
output_format: str, enable_enhancement: bool) -> Tuple:
|
| 120 |
"""์ด๋ฏธ์ง ์ฒ๋ฆฌ (ํธ์ง ๋ฐ ํ์ง ๊ฐ์ )"""
|
|
|
|
| 121 |
try:
|
| 122 |
-
if not self.
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
| 127 |
-
temp_path =
|
| 128 |
-
|
| 129 |
-
temp_path =
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# API ํธ์ถ
|
|
|
|
| 133 |
result = self.client.predict(
|
| 134 |
-
password
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
api_name="/check_password"
|
| 150 |
)
|
| 151 |
|
| 152 |
-
|
| 153 |
-
if temp_path and os.path.exists(temp_path):
|
| 154 |
-
try:
|
| 155 |
-
os.remove(temp_path)
|
| 156 |
-
except:
|
| 157 |
-
pass
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
-
logger.error(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค๋ฅ: {e}")
|
|
|
|
|
|
|
|
|
|
| 166 |
# ์์ ํ์ผ ์ ๋ฆฌ
|
| 167 |
-
if
|
| 168 |
try:
|
| 169 |
os.remove(temp_path)
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
|
| 174 |
# ์ ์ญ ํด๋ผ์ด์ธํธ ์ปจํธ๋กค๋ฌ ์ธ์คํด์ค
|
| 175 |
controller = GradioClientController()
|
|
@@ -179,10 +216,17 @@ def create_gradio_interface():
|
|
| 179 |
try:
|
| 180 |
logger.info("Gradio ์ธํฐํ์ด์ค ์์ฑ ์์")
|
| 181 |
|
| 182 |
-
# ๋ฐฐ๊ฒฝ ์ ํ ์ ํ์ง
|
| 183 |
background_types = ["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํน์๋ฐฐ๊ฒฝ", "์ฃผ์ผ๋ฆฌ", "ํน์ํจ๊ณผ"]
|
| 184 |
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
gr.Markdown("# AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ๋๊ตฌ")
|
| 187 |
|
| 188 |
# ๋น๋ฐ๋ฒํธ ์
๋ ฅ ํ๋
|
|
@@ -256,14 +300,6 @@ def create_gradio_interface():
|
|
| 256 |
visible=False,
|
| 257 |
interactive=True
|
| 258 |
)
|
| 259 |
-
|
| 260 |
-
# ๋๋กญ๋ค์ด ๋ณ๊ฒฝ ์ด๋ฒคํธ
|
| 261 |
-
background_type.change(
|
| 262 |
-
fn=controller.update_dropdowns,
|
| 263 |
-
inputs=[background_type],
|
| 264 |
-
outputs=[simple_dropdown, studio_dropdown, nature_dropdown,
|
| 265 |
-
indoor_dropdown, special_dropdown, jewelry_dropdown, special_effects_dropdown]
|
| 266 |
-
)
|
| 267 |
|
| 268 |
# ์์ฒญ์ฌํญ ์
๋ ฅ
|
| 269 |
request_text = gr.Textbox(
|
|
@@ -277,7 +313,7 @@ def create_gradio_interface():
|
|
| 277 |
label="ํ์ง ๋ ๋ฒจ",
|
| 278 |
choices=["gpt", "flux"],
|
| 279 |
value="flux",
|
| 280 |
-
info="GPT: GPT ๋ชจ๋ธ (๊ณ ํ์ง),
|
| 281 |
)
|
| 282 |
|
| 283 |
aspect_ratio = gr.Dropdown(
|
|
@@ -300,10 +336,10 @@ def create_gradio_interface():
|
|
| 300 |
)
|
| 301 |
|
| 302 |
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ
|
| 303 |
-
generate_prompt_btn = gr.Button("ํ๋กฌํํธ๋ง ์์ฑ")
|
| 304 |
|
| 305 |
# ํธ์ง ๋ฒํผ
|
| 306 |
-
edit_btn = gr.Button("์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ")
|
| 307 |
|
| 308 |
with gr.Column():
|
| 309 |
with gr.Row():
|
|
@@ -322,6 +358,14 @@ def create_gradio_interface():
|
|
| 322 |
info = gr.Textbox(label="์ฒ๋ฆฌ ์ ๋ณด", interactive=False)
|
| 323 |
error = gr.Textbox(label="์ค๋ฅ ๋ฉ์์ง", interactive=False, visible=True)
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
| 326 |
generate_prompt_btn.click(
|
| 327 |
fn=controller.generate_prompt_only,
|
|
@@ -352,7 +396,7 @@ def create_gradio_interface():
|
|
| 352 |
]
|
| 353 |
)
|
| 354 |
|
| 355 |
-
# ์ด๊ธฐ ๋๋กญ๋ค์ด
|
| 356 |
app.load(
|
| 357 |
fn=controller.update_dropdowns,
|
| 358 |
inputs=[background_type],
|
|
@@ -364,7 +408,7 @@ def create_gradio_interface():
|
|
| 364 |
return app
|
| 365 |
|
| 366 |
except Exception as e:
|
| 367 |
-
logger.error(f"Gradio ์ธํฐํ์ด์ค ์์ฑ ์ค๋ฅ: {e}")
|
| 368 |
logger.error(traceback.format_exc())
|
| 369 |
raise
|
| 370 |
|
|
@@ -377,10 +421,27 @@ if __name__ == "__main__":
|
|
| 377 |
os.makedirs("imgs", exist_ok=True)
|
| 378 |
logger.info("์ด๋ฏธ์ง ๋๋ ํ ๋ฆฌ ์ค๋น ์๋ฃ")
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
app = create_gradio_interface()
|
| 381 |
logger.info("Gradio ์ฑ ์์")
|
| 382 |
-
app.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
|
|
|
|
|
|
| 384 |
except Exception as e:
|
| 385 |
-
logger.error(f"์ฑ ์คํ ์ค๋ฅ: {e}")
|
| 386 |
-
logger.error(traceback.format_exc())
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
from gradio_client import Client, handle_file
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
import time
|
| 11 |
|
| 12 |
# .env ํ์ผ ๋ก๋
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# ๋ก๊น
์ค์ (API ์๋ํฌ์ธํธ ์ ๋ณด๋ ๋ก๊ทธ์ ๋จ๊ธฐ์ง ์์)
|
| 16 |
+
class SafeFormatter(logging.Formatter):
|
| 17 |
+
"""API ์๋ํฌ์ธํธ ์ ๋ณด๋ฅผ ๋ก๊ทธ์์ ์ ๊ฑฐํ๋ ์์ ํ ํฌ๋งคํฐ"""
|
| 18 |
+
def format(self, record):
|
| 19 |
+
# API_ENDPOINT ๊ด๋ จ ์ ๋ณด ํํฐ๋ง
|
| 20 |
+
msg = super().format(record)
|
| 21 |
+
# ๋ฏผ๊ฐํ ์ ๋ณด๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ๋ง์คํน
|
| 22 |
+
if "API_ENDPOINT" in msg or "happydoggg" in msg or "49493h" in msg:
|
| 23 |
+
return msg.replace(os.environ.get("API_ENDPOINT", ""), "[API_ENDPOINT_HIDDEN]")
|
| 24 |
+
return msg
|
| 25 |
+
|
| 26 |
+
# ๋ก๊ทธ ํธ๋ค๋ฌ ์ค์
|
| 27 |
+
file_handler = logging.FileHandler("app.log")
|
| 28 |
+
console_handler = logging.StreamHandler(sys.stdout)
|
| 29 |
+
formatter = SafeFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 30 |
+
file_handler.setFormatter(formatter)
|
| 31 |
+
console_handler.setFormatter(formatter)
|
| 32 |
+
|
| 33 |
logger = logging.getLogger("image-enhancer-control-tower")
|
| 34 |
+
logger.setLevel(logging.INFO)
|
| 35 |
+
logger.addHandler(file_handler)
|
| 36 |
+
logger.addHandler(console_handler)
|
| 37 |
|
| 38 |
class GradioClientController:
|
| 39 |
"""ํ๊น
ํ์ด์ค ๊ทธ๋ผ๋์ค API ํด๋ผ์ด์ธํธ ์ปจํธ๋กค๋ฌ"""
|
| 40 |
|
| 41 |
def __init__(self):
|
| 42 |
self.client = None
|
| 43 |
+
self.api_endpoint = None
|
| 44 |
self._initialize_client()
|
| 45 |
|
| 46 |
def _initialize_client(self):
|
| 47 |
"""ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (์๋ํฌ์ธํธ ์ ๋ณด๋ ๋ก๊ทธ์ ๋จ๊ธฐ์ง ์์)"""
|
| 48 |
try:
|
| 49 |
+
self.api_endpoint = os.environ.get("API_ENDPOINT")
|
| 50 |
+
if not self.api_endpoint:
|
| 51 |
logger.error("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 52 |
raise ValueError("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ํ์ํฉ๋๋ค.")
|
| 53 |
|
| 54 |
+
# ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์๋ (์ฌ์๋ ๋ก์ง ํฌํจ)
|
| 55 |
+
max_retries = 3
|
| 56 |
+
for attempt in range(max_retries):
|
| 57 |
+
try:
|
| 58 |
+
self.client = Client(self.api_endpoint)
|
| 59 |
+
logger.info("API ํด๋ผ์ด์ธํธ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ด๊ธฐํ๋์์ต๋๋ค.")
|
| 60 |
+
return
|
| 61 |
+
except Exception as e:
|
| 62 |
+
if attempt < max_retries - 1:
|
| 63 |
+
logger.warning(f"ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์ฌ์๋ {attempt + 1}/{max_retries}")
|
| 64 |
+
time.sleep(2)
|
| 65 |
+
else:
|
| 66 |
+
raise e
|
| 67 |
+
|
| 68 |
except Exception as e:
|
| 69 |
+
logger.error(f"ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์คํจ: {str(e)}")
|
| 70 |
self.client = None
|
| 71 |
|
| 72 |
+
def _ensure_client(self) -> bool:
|
| 73 |
+
"""ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ํ ํ์ธ ๋ฐ ์ฌ์ฐ๊ฒฐ"""
|
| 74 |
+
if self.client is None:
|
| 75 |
+
logger.info("ํด๋ผ์ด์ธํธ ์ฌ์ฐ๊ฒฐ ์๋")
|
| 76 |
+
self._initialize_client()
|
| 77 |
+
return self.client is not None
|
| 78 |
+
|
| 79 |
def update_dropdowns(self, bg_type: str) -> Tuple:
|
| 80 |
"""๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ฅธ ๋๋กญ๋ค์ด ์
๋ฐ์ดํธ"""
|
| 81 |
try:
|
| 82 |
+
if not self._ensure_client():
|
| 83 |
+
logger.error("ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์คํจ")
|
| 84 |
return tuple([gr.update() for _ in range(7)])
|
| 85 |
|
| 86 |
result = self.client.predict(
|
| 87 |
+
bg_type,
|
| 88 |
api_name="/update_dropdowns"
|
| 89 |
)
|
| 90 |
|
| 91 |
logger.info(f"๋๋กญ๋ค์ด ์
๋ฐ์ดํธ ์๋ฃ: {bg_type}")
|
| 92 |
|
| 93 |
# ๊ฒฐ๊ณผ๋ฅผ Gradio ์
๋ฐ์ดํธ ํ์์ผ๋ก ๋ณํ
|
| 94 |
+
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
| 95 |
+
updates = []
|
| 96 |
+
visibility_map = ["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํน์๋ฐฐ๊ฒฝ", "์ฃผ์ผ๋ฆฌ", "ํน์ํจ๊ณผ"]
|
| 97 |
+
|
| 98 |
+
for i, choices in enumerate(result[:7]):
|
| 99 |
+
is_visible = (bg_type == visibility_map[i])
|
| 100 |
+
updates.append(gr.update(
|
| 101 |
+
visible=is_visible,
|
| 102 |
+
choices=choices if choices else [],
|
| 103 |
+
value=choices[0] if choices and len(choices) > 0 else None
|
| 104 |
+
))
|
| 105 |
+
|
| 106 |
+
return tuple(updates)
|
| 107 |
+
else:
|
| 108 |
+
logger.warning("API์์ ์์๊ณผ ๋ค๋ฅธ ํ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ์ต๋๋ค.")
|
| 109 |
+
return tuple([gr.update() for _ in range(7)])
|
|
|
|
|
|
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
+
logger.error(f"๋๋กญ๋ค์ด ์
๋ฐ์ดํธ ์ค๋ฅ: {str(e)}")
|
| 113 |
return tuple([gr.update() for _ in range(7)])
|
| 114 |
|
| 115 |
def generate_prompt_only(self, password: str, bg_type: str, simple: str, studio: str,
|
|
|
|
| 117 |
special_effects: str, request_text: str, aspect_ratio: str) -> str:
|
| 118 |
"""ํ๋กฌํํธ๋ง ์์ฑ"""
|
| 119 |
try:
|
| 120 |
+
if not self._ensure_client():
|
| 121 |
+
return "ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
|
|
|
| 122 |
|
| 123 |
result = self.client.predict(
|
| 124 |
+
password,
|
| 125 |
+
bg_type,
|
| 126 |
+
simple or "",
|
| 127 |
+
studio or "",
|
| 128 |
+
nature or "",
|
| 129 |
+
indoor or "",
|
| 130 |
+
special or "",
|
| 131 |
+
jewelry or "",
|
| 132 |
+
special_effects or "",
|
| 133 |
+
request_text or "",
|
| 134 |
+
aspect_ratio,
|
| 135 |
api_name="/generate_prompt_with_password_check"
|
| 136 |
)
|
| 137 |
|
| 138 |
logger.info("ํ๋กฌํํธ ์์ฑ ์๋ฃ")
|
| 139 |
+
return result if result else "ํ๋กฌํํธ ์์ฑ์ ์คํจํ์ต๋๋ค."
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
+
logger.error(f"ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {str(e)}")
|
| 143 |
return f"ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {str(e)}"
|
| 144 |
|
| 145 |
def process_image(self, password: str, image, bg_type: str, simple: str, studio: str,
|
|
|
|
| 147 |
request_text: str, quality_level: str, aspect_ratio: str,
|
| 148 |
output_format: str, enable_enhancement: bool) -> Tuple:
|
| 149 |
"""์ด๋ฏธ์ง ์ฒ๋ฆฌ (ํธ์ง ๋ฐ ํ์ง ๊ฐ์ )"""
|
| 150 |
+
temp_path = None
|
| 151 |
try:
|
| 152 |
+
if not self._ensure_client():
|
| 153 |
+
return ([], None, [], None, "", "", "ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.")
|
| 154 |
+
|
| 155 |
+
# ์ด๋ฏธ์ง ๊ฒ์ฆ
|
| 156 |
+
if image is None:
|
| 157 |
+
return ([], None, [], None, "", "", "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")
|
| 158 |
|
| 159 |
# ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
| 160 |
+
temp_path = tempfile.mktemp(suffix='.png')
|
| 161 |
+
try:
|
| 162 |
+
image.save(temp_path, format='PNG')
|
| 163 |
+
logger.info(f"์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ: {os.path.basename(temp_path)}")
|
| 164 |
+
except Exception as e:
|
| 165 |
+
logger.error(f"์ด๋ฏธ์ง ์ ์ฅ ์คํจ: {str(e)}")
|
| 166 |
+
return ([], None, [], None, "", "", "์ด๋ฏธ์ง ์ ์ฅ์ ์คํจํ์ต๋๋ค. ๋ค๋ฅธ ์ด๋ฏธ์ง๋ฅผ ์๋ํด๋ณด์ธ์.")
|
| 167 |
|
| 168 |
# API ํธ์ถ
|
| 169 |
+
logger.info("์ด๋ฏธ์ง ์ฒ๋ฆฌ API ํธ์ถ ์์")
|
| 170 |
result = self.client.predict(
|
| 171 |
+
password,
|
| 172 |
+
handle_file(temp_path),
|
| 173 |
+
bg_type,
|
| 174 |
+
simple or "",
|
| 175 |
+
studio or "",
|
| 176 |
+
nature or "",
|
| 177 |
+
indoor or "",
|
| 178 |
+
special or "",
|
| 179 |
+
jewelry or "",
|
| 180 |
+
special_effects or "",
|
| 181 |
+
request_text or "",
|
| 182 |
+
quality_level,
|
| 183 |
+
aspect_ratio,
|
| 184 |
+
output_format,
|
| 185 |
+
enable_enhancement,
|
| 186 |
api_name="/check_password"
|
| 187 |
)
|
| 188 |
|
| 189 |
+
logger.info("์ด๋ฏธ์ง ์ฒ๋ฆฌ API ํธ์ถ ์๋ฃ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
# ๊ฒฐ๊ณผ ๊ฒ์ฆ ๋ฐ ๋ฐํ
|
| 192 |
+
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
| 193 |
+
return result
|
| 194 |
+
else:
|
| 195 |
+
logger.warning("API์์ ์์๊ณผ ๋ค๋ฅธ ํ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ์ต๋๋ค.")
|
| 196 |
+
return ([], None, [], None, "", "", "API์์ ์ฌ๋ฐ๋ฅด์ง ์์ ์๋ต์ ๋ฐ์์ต๋๋ค.")
|
| 197 |
|
| 198 |
except Exception as e:
|
| 199 |
+
logger.error(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค๋ฅ: {str(e)}")
|
| 200 |
+
return ([], None, [], None, "", "", f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
| 201 |
+
|
| 202 |
+
finally:
|
| 203 |
# ์์ ํ์ผ ์ ๋ฆฌ
|
| 204 |
+
if temp_path and os.path.exists(temp_path):
|
| 205 |
try:
|
| 206 |
os.remove(temp_path)
|
| 207 |
+
logger.info("์์ ํ์ผ ์ ๋ฆฌ ์๋ฃ")
|
| 208 |
+
except Exception as e:
|
| 209 |
+
logger.warning(f"์์ ํ์ผ ์ ๋ฆฌ ์คํจ: {str(e)}")
|
| 210 |
|
| 211 |
# ์ ์ญ ํด๋ผ์ด์ธํธ ์ปจํธ๋กค๋ฌ ์ธ์คํด์ค
|
| 212 |
controller = GradioClientController()
|
|
|
|
| 216 |
try:
|
| 217 |
logger.info("Gradio ์ธํฐํ์ด์ค ์์ฑ ์์")
|
| 218 |
|
| 219 |
+
# ๋ฐฐ๊ฒฝ ์ ํ ์ ํ์ง
|
| 220 |
background_types = ["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํน์๋ฐฐ๊ฒฝ", "์ฃผ์ผ๋ฆฌ", "ํน์ํจ๊ณผ"]
|
| 221 |
|
| 222 |
+
# ์ปค์คํ
CSS ์ถ๊ฐ (์๋ณธ๊ณผ ๋์ผํ ์คํ์ผ ์ ์ง)
|
| 223 |
+
css = """
|
| 224 |
+
.gradio-container {
|
| 225 |
+
max-width: 1200px !important;
|
| 226 |
+
}
|
| 227 |
+
"""
|
| 228 |
+
|
| 229 |
+
with gr.Blocks(title="AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ", css=css, theme=gr.themes.Soft()) as app:
|
| 230 |
gr.Markdown("# AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ๋๊ตฌ")
|
| 231 |
|
| 232 |
# ๋น๋ฐ๋ฒํธ ์
๋ ฅ ํ๋
|
|
|
|
| 300 |
visible=False,
|
| 301 |
interactive=True
|
| 302 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
# ์์ฒญ์ฌํญ ์
๋ ฅ
|
| 305 |
request_text = gr.Textbox(
|
|
|
|
| 313 |
label="ํ์ง ๋ ๋ฒจ",
|
| 314 |
choices=["gpt", "flux"],
|
| 315 |
value="flux",
|
| 316 |
+
info="GPT: GPT ๋ชจ๋ธ (๊ณ ํ์ง), Flux: Flux ๋ชจ๋ธ (๋น ๋ฅธ ์ฒ๋ฆฌ + ๊ธฐ๋ณธ ํ์ง๊ฐ์ )"
|
| 317 |
)
|
| 318 |
|
| 319 |
aspect_ratio = gr.Dropdown(
|
|
|
|
| 336 |
)
|
| 337 |
|
| 338 |
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ
|
| 339 |
+
generate_prompt_btn = gr.Button("ํ๋กฌํํธ๋ง ์์ฑ", variant="secondary")
|
| 340 |
|
| 341 |
# ํธ์ง ๋ฒํผ
|
| 342 |
+
edit_btn = gr.Button("์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ", variant="primary")
|
| 343 |
|
| 344 |
with gr.Column():
|
| 345 |
with gr.Row():
|
|
|
|
| 358 |
info = gr.Textbox(label="์ฒ๋ฆฌ ์ ๋ณด", interactive=False)
|
| 359 |
error = gr.Textbox(label="์ค๋ฅ ๋ฉ์์ง", interactive=False, visible=True)
|
| 360 |
|
| 361 |
+
# ๋๋กญ๋ค์ด ๋ณ๊ฒฝ ์ด๋ฒคํธ
|
| 362 |
+
background_type.change(
|
| 363 |
+
fn=controller.update_dropdowns,
|
| 364 |
+
inputs=[background_type],
|
| 365 |
+
outputs=[simple_dropdown, studio_dropdown, nature_dropdown,
|
| 366 |
+
indoor_dropdown, special_dropdown, jewelry_dropdown, special_effects_dropdown]
|
| 367 |
+
)
|
| 368 |
+
|
| 369 |
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
| 370 |
generate_prompt_btn.click(
|
| 371 |
fn=controller.generate_prompt_only,
|
|
|
|
| 396 |
]
|
| 397 |
)
|
| 398 |
|
| 399 |
+
# ์ฑ ๋ก๋ ์ ์ด๊ธฐ ๋๋กญ๋ค์ด ์ค์
|
| 400 |
app.load(
|
| 401 |
fn=controller.update_dropdowns,
|
| 402 |
inputs=[background_type],
|
|
|
|
| 408 |
return app
|
| 409 |
|
| 410 |
except Exception as e:
|
| 411 |
+
logger.error(f"Gradio ์ธํฐํ์ด์ค ์์ฑ ์ค๋ฅ: {str(e)}")
|
| 412 |
logger.error(traceback.format_exc())
|
| 413 |
raise
|
| 414 |
|
|
|
|
| 421 |
os.makedirs("imgs", exist_ok=True)
|
| 422 |
logger.info("์ด๋ฏธ์ง ๋๋ ํ ๋ฆฌ ์ค๋น ์๋ฃ")
|
| 423 |
|
| 424 |
+
# API ์๋ํฌ์ธํธ ํ์ธ
|
| 425 |
+
if not os.environ.get("API_ENDPOINT"):
|
| 426 |
+
logger.error("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 427 |
+
print("\nโ ์ค๋ฅ: API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 428 |
+
print("ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ๊ฑฐ๋ .env ํ์ผ์ API_ENDPOINT๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.")
|
| 429 |
+
sys.exit(1)
|
| 430 |
+
|
| 431 |
app = create_gradio_interface()
|
| 432 |
logger.info("Gradio ์ฑ ์์")
|
| 433 |
+
app.launch(
|
| 434 |
+
share=True,
|
| 435 |
+
server_name="0.0.0.0",
|
| 436 |
+
server_port=7860,
|
| 437 |
+
show_error=True,
|
| 438 |
+
quiet=False
|
| 439 |
+
)
|
| 440 |
|
| 441 |
+
except KeyboardInterrupt:
|
| 442 |
+
logger.info("์ฌ์ฉ์์ ์ํด ์ ํ๋ฆฌ์ผ์ด์
์ด ์ค๋จ๋์์ต๋๋ค.")
|
| 443 |
except Exception as e:
|
| 444 |
+
logger.error(f"์ฑ ์คํ ์ค๋ฅ: {str(e)}")
|
| 445 |
+
logger.error(traceback.format_exc())
|
| 446 |
+
print(f"\nโ ์ฌ๊ฐํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
| 447 |
+
print("์์ธํ ๋ด์ฉ์ app.log ํ์ผ์ ํ์ธํด์ฃผ์ธ์.")
|