File size: 26,065 Bytes
00dd9de 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 83d7da1 5b349d3 8b8ac8e 5b349d3 8b8ac8e b86b06a 8b8ac8e 5b349d3 8b8ac8e 5b349d3 b86b06a 83d7da1 b86b06a 83d7da1 b86b06a 83d7da1 b86b06a 7b339a1 7de8013 7b339a1 7de8013 809e3e8 7de8013 7b339a1 7de8013 7b339a1 7de8013 7b339a1 b86b06a 7de8013 83d7da1 8b8ac8e 5b349d3 7de8013 5b349d3 7de8013 7b339a1 8b8ac8e 7de8013 5b349d3 7de8013 8b8ac8e 7b339a1 7de8013 809e3e8 7de8013 809e3e8 7de8013 8b8ac8e 7de8013 809e3e8 5b349d3 8b8ac8e 809e3e8 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 83d7da1 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 83d7da1 8b8ac8e 5b349d3 83d7da1 5b349d3 8b8ac8e 5b349d3 83d7da1 8b8ac8e 83d7da1 8b8ac8e 83d7da1 8b8ac8e 5b349d3 83d7da1 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 7de8013 5b349d3 7de8013 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 809e3e8 8b8ac8e 809e3e8 8b8ac8e 5b349d3 b86b06a 5b349d3 b86b06a 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e 5b349d3 8b8ac8e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
import os
import sys
import logging
import tempfile
import traceback
from typing import List, Tuple, Optional, Any
import gradio as gr
from gradio_client import Client, handle_file
from dotenv import load_dotenv
import time
# .env ํ์ผ ๋ก๋
load_dotenv()
# ๋ก๊น
์ค์ (API ์๋ํฌ์ธํธ ์ ๋ณด๋ ๋ก๊ทธ์ ๋จ๊ธฐ์ง ์์)
class SafeFormatter(logging.Formatter):
"""API ์๋ํฌ์ธํธ ์ ๋ณด๋ฅผ ๋ก๊ทธ์์ ์ ๊ฑฐํ๋ ์์ ํ ํฌ๋งคํฐ"""
def format(self, record):
# API_ENDPOINT ๊ด๋ จ ์ ๋ณด ํํฐ๋ง
msg = super().format(record)
# ๋ฏผ๊ฐํ ์ ๋ณด๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ๋ง์คํน
if "API_ENDPOINT" in msg or "happydoggg" in msg or "49493h" in msg:
return msg.replace(os.environ.get("API_ENDPOINT", ""), "[API_ENDPOINT_HIDDEN]")
return msg
# ๋ก๊ทธ ํธ๋ค๋ฌ ์ค์
file_handler = logging.FileHandler("app.log")
console_handler = logging.StreamHandler(sys.stdout)
formatter = SafeFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
logger = logging.getLogger("image-enhancer-control-tower")
logger.setLevel(logging.INFO)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
class GradioClientController:
"""ํ๊น
ํ์ด์ค ๊ทธ๋ผ๋์ค API ํด๋ผ์ด์ธํธ ์ปจํธ๋กค๋ฌ"""
def __init__(self):
self.client = None
self.api_endpoint = None
self.background_options = {} # ์บ์๋ ๋ฐฐ๊ฒฝ ์ต์
self._initialize_client()
def _initialize_client(self):
"""ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (์๋ํฌ์ธํธ ์ ๋ณด๋ ๋ก๊ทธ์ ๋จ๊ธฐ์ง ์์)"""
try:
self.api_endpoint = os.environ.get("API_ENDPOINT")
if not self.api_endpoint:
logger.error("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
raise ValueError("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ํ์ํฉ๋๋ค.")
# ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์๋ (์ฌ์๋ ๋ก์ง ํฌํจ)
max_retries = 3
for attempt in range(max_retries):
try:
self.client = Client(self.api_endpoint)
logger.info("API ํด๋ผ์ด์ธํธ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ด๊ธฐํ๋์์ต๋๋ค.")
# ์ด๊ธฐํ ์ฑ๊ณต ์ ์ฌํ ๋ฐฐ๊ฒฝ ์ต์
๋จผ์ ๋ก๋
self._load_simple_background()
return
except Exception as e:
if attempt < max_retries - 1:
logger.warning(f"ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์ฌ์๋ {attempt + 1}/{max_retries}")
time.sleep(2)
else:
raise e
except Exception as e:
logger.error(f"ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์คํจ: {str(e)}")
self.client = None
def _load_simple_background(self):
"""์ฌํ ๋ฐฐ๊ฒฝ ์ต์
๋ง ๋จผ์ ๋ก๋"""
try:
if self.client:
result = self.client.predict("์ฌํ ๋ฐฐ๊ฒฝ", api_name="/update_dropdowns")
if isinstance(result, (list, tuple)) and len(result) >= 7:
self.background_options["์ฌํ ๋ฐฐ๊ฒฝ"] = result
logger.info("์ฌํ ๋ฐฐ๊ฒฝ ์ต์
๋ก๋ ์๋ฃ")
return result[0] # ์ฌํ ๋ฐฐ๊ฒฝ ์ ํ์ง ๋ฐํ
except Exception as e:
logger.warning(f"์ฌํ ๋ฐฐ๊ฒฝ ์ต์
๋ก๋ ์คํจ: {str(e)}")
return []
def get_initial_dropdown_data(self, bg_type: str = "์ฌํ ๋ฐฐ๊ฒฝ") -> Tuple:
"""์ด๊ธฐ ๋๋กญ๋ค์ด ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ"""
try:
# ์บ์๋ ๋ฐ์ดํฐ ๋จผ์ ํ์ธ
if bg_type in self.background_options:
result = self.background_options[bg_type]
logger.info(f"์บ์์์ ์ด๊ธฐ ๋๋กญ๋ค์ด ๋ฐ์ดํฐ ๋ก๋: {bg_type}")
else:
# API ํธ์ถ๋ก ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
if self.client:
result = self.client.predict(bg_type, api_name="/update_dropdowns")
self.background_options[bg_type] = result # ์บ์์ ์ ์ฅ
logger.info(f"API์์ ์ด๊ธฐ ๋๋กญ๋ค์ด ๋ฐ์ดํฐ ๋ก๋: {bg_type}")
else:
logger.error("ํด๋ผ์ด์ธํธ๊ฐ ์ด๊ธฐํ๋์ง ์์")
return tuple([[] for _ in range(7)])
# ๊ฒฐ๊ณผ ๋ฐํ (์ค์ ์ ํ์ง๋ง)
if isinstance(result, (list, tuple)) and len(result) >= 7:
return result[:7]
else:
return tuple([[] for _ in range(7)])
except Exception as e:
logger.error(f"์ด๊ธฐ ๋๋กญ๋ค์ด ๋ฐ์ดํฐ ๋ก๋ ์คํจ: {str(e)}")
return tuple([[] for _ in range(7)])
def _parse_dropdown_result(self, result):
"""API ์๋ต์์ ์ค์ ์ ํ์ง ๋ฐ์ดํฐ๋ง ์ถ์ถ - ๋จ์ํ"""
try:
logger.info(f"์๋ต ํ์ฑ: {result}")
# API ๋ฌธ์์ ๋ฐ๋ฅด๋ฉด tuple of 7 elements๋ฅผ ๋ฐํ
# ๊ฐ ์์๋ Literal ํ์
์ ๋ฆฌ์คํธ์ฌ์ผ ํจ
if isinstance(result, (list, tuple)) and len(result) >= 7:
# ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋๋ก ๋ฐํ (API๊ฐ ์ด๋ฏธ ์ฌ๋ฐ๋ฅธ ํ์์ผ๋ก ๋ฐํํ๋ค๊ณ ๊ฐ์ )
return result[:7]
else:
logger.warning(f"์์ํ์ง ๋ชปํ ์๋ต ํ์: {type(result)}, length={len(result) if hasattr(result, '__len__') else 'N/A'}")
return [[] for _ in range(7)]
except Exception as e:
logger.error(f"๋๋กญ๋ค์ด ๊ฒฐ๊ณผ ํ์ฑ ์ค๋ฅ: {str(e)}")
return [[] for _ in range(7)]
def get_initial_simple_choices(self) -> list:
"""์ฌํ ๋ฐฐ๊ฒฝ ์ด๊ธฐ ์ ํ์ง - ํ๋์ฝ๋ฉ์ผ๋ก ํ
์คํธ"""
# API ๋ฌธ์์์ ํ์ธ๋ ์ค์ ์ ํ์ง๋ค
return [
'ํ์ดํธ ๊ธฐ๋ณธ', 'ํ์ ํฌํค', '๋ผ์ดํธ ๊ทธ๋ ์ด', '๊ทธ๋ ์ด ๊ทธ๋ผ๋ฐ์ด์
์คํฌํธ๋ผ์ดํธ',
'ํ๋ฆฌ๋ฏธ์ ๋๋ผ๋งํฑ ๋ธ๋', '๋ฅ๋ธ๋ฃจ ์ ๋ฆฌ๋ฐ์ฌ', 'ํ์คํ
๊ทธ๋ผ๋ฐ์ด์
', '์ค์นด์ด๋ธ๋ฃจ ํ์คํ
',
'๋ฒํฐ์๋ก์ฐ ํ์คํ
', '๋ธ๋ฃจ ์์', '๋ ๋ ์์'
]
def _ensure_client(self) -> bool:
"""ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ํ ํ์ธ ๋ฐ ์ฌ์ฐ๊ฒฐ"""
if self.client is None:
logger.info("ํด๋ผ์ด์ธํธ ์ฌ์ฐ๊ฒฐ ์๋")
self._initialize_client()
return self.client is not None
def update_dropdowns(self, bg_type: str) -> Tuple:
"""๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ฅธ ๋๋กญ๋ค์ด ์
๋ฐ์ดํธ - ๋จ์ํ"""
try:
logger.info(f"๋๋กญ๋ค์ด ์
๋ฐ์ดํธ: {bg_type}")
if not self._ensure_client():
logger.error("ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์คํจ")
return tuple([gr.update(visible=False) for _ in range(7)])
# API ํธ์ถ
result = self.client.predict(bg_type, api_name="/update_dropdowns")
logger.info(f"API ์๋ต: {result}")
# API ์๋ต์ ๊ทธ๋๋ก ์ฌ์ฉ
if isinstance(result, (list, tuple)) and len(result) >= 7:
updates = []
visibility_map = ["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํน์๋ฐฐ๊ฒฝ", "์ฃผ์ผ๋ฆฌ", "ํน์ํจ๊ณผ"]
for i, choices in enumerate(result[:7]):
is_visible = (bg_type == visibility_map[i])
if is_visible and isinstance(choices, list) and len(choices) > 0:
logger.info(f"{visibility_map[i]} ๋๋กญ๋ค์ด: {len(choices)}๊ฐ ์ ํ์ง")
updates.append(gr.update(
visible=True,
choices=choices,
value=choices[0], # ์ฒซ ๋ฒ์งธ ํญ๋ชฉ ์ ํ
interactive=True
))
else:
updates.append(gr.update(visible=False))
return tuple(updates)
else:
logger.error(f"์๋ชป๋ API ์๋ต: {result}")
return tuple([gr.update(visible=False) for _ in range(7)])
except Exception as e:
logger.error(f"๋๋กญ๋ค์ด ์
๋ฐ์ดํธ ์ค๋ฅ: {str(e)}")
return tuple([gr.update(visible=False) for _ in range(7)])
def generate_prompt_only(self, password: str, bg_type: str, simple: str, studio: str,
nature: str, indoor: str, special: str, jewelry: str,
special_effects: str, request_text: str, aspect_ratio: str) -> str:
"""ํ๋กฌํํธ๋ง ์์ฑ"""
try:
if not self._ensure_client():
return "ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์."
result = self.client.predict(
password,
bg_type,
simple or "",
studio or "",
nature or "",
indoor or "",
special or "",
jewelry or "",
special_effects or "",
request_text or "",
aspect_ratio,
api_name="/generate_prompt_with_password_check"
)
logger.info("ํ๋กฌํํธ ์์ฑ ์๋ฃ")
return result if result else "ํ๋กฌํํธ ์์ฑ์ ์คํจํ์ต๋๋ค."
except Exception as e:
logger.error(f"ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {str(e)}")
return f"ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {str(e)}"
def process_image(self, password: str, image, bg_type: str, simple: str, studio: str,
nature: str, indoor: str, special: str, jewelry: str, special_effects: str,
request_text: str, quality_level: str, aspect_ratio: str,
output_format: str, enable_enhancement: bool) -> Tuple:
"""์ด๋ฏธ์ง ์ฒ๋ฆฌ (ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ) - API ๋ฌธ์์ ์ ํํ ๋งค์นญ"""
temp_path = None
try:
if not self._ensure_client():
return ([], None, [], None, "", "", "ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.")
# ์ด๋ฏธ์ง ๊ฒ์ฆ
if image is None:
return ([], None, [], None, "", "", "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")
# ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
temp_path = tempfile.mktemp(suffix='.png')
try:
image.save(temp_path, format='PNG')
logger.info(f"์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ: {os.path.basename(temp_path)}")
except Exception as e:
logger.error(f"์ด๋ฏธ์ง ์ ์ฅ ์คํจ: {str(e)}")
return ([], None, [], None, "", "", "์ด๋ฏธ์ง ์ ์ฅ์ ์คํจํ์ต๋๋ค. ๋ค๋ฅธ ์ด๋ฏธ์ง๋ฅผ ์๋ํด๋ณด์ธ์.")
# API ํธ์ถ - ๋งค๊ฐ๋ณ์ ์์๋ฅผ API ๋ฌธ์์ ์ ํํ ๋ง์ถค
logger.info("์ด๋ฏธ์ง ์ฒ๋ฆฌ API ํธ์ถ ์์")
result = self.client.predict(
password, # password (str)
handle_file(temp_path), # param_1 (image file)
bg_type, # param_2 (๋ฐฐ๊ฒฝ ์ ํ)
simple or "", # param_3 (์ฌํ ๋ฐฐ๊ฒฝ)
studio or "", # param_4 (์คํ๋์ค ๋ฐฐ๊ฒฝ)
nature or "", # param_5 (์์ฐ ํ๊ฒฝ)
indoor or "", # param_6 (์ค๋ด ํ๊ฒฝ)
special or "", # param_7 (ํน์๋ฐฐ๊ฒฝ)
jewelry or "", # param_8 (์ฃผ์ผ๋ฆฌ)
special_effects or "", # param_9 (ํน์ํจ๊ณผ)
request_text or "", # param_10 (์์ฒญ์ฌํญ)
quality_level, # param_11 (ํ์ง ๋ ๋ฒจ)
aspect_ratio, # param_12 (์ข
ํก๋น)
output_format, # param_13 (์ด๋ฏธ์ง ํ์)
enable_enhancement, # param_14 (์ถ๊ฐ ํ์ง ๊ฐ์ )
api_name="/check_password"
)
logger.info("์ด๋ฏธ์ง ์ฒ๋ฆฌ API ํธ์ถ ์๋ฃ")
# ๊ฒฐ๊ณผ ๊ฒ์ฆ ๋ฐ ๋ฐํ - API ๋ฌธ์์ ๋ฐ๋ฅด๋ฉด tuple of 7 elements
if isinstance(result, (list, tuple)) and len(result) >= 7:
# [0] original_gallery, [1] original_file, [2] enhanced_gallery,
# [3] enhanced_file, [4] prompt_text, [5] info_text, [6] error_text
return result[:7] # ์ ํํ 7๊ฐ ์์๋ง ๋ฐํ
else:
logger.warning(f"API ์๋ต ํ์ ์ด์: type={type(result)}, length={len(result) if hasattr(result, '__len__') else 'N/A'}")
return ([], None, [], None, "", "", "API์์ ์ฌ๋ฐ๋ฅด์ง ์์ ์๋ต์ ๋ฐ์์ต๋๋ค.")
except Exception as e:
error_msg = str(e)
logger.error(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค๋ฅ: {error_msg}")
# ์ผ๋ฐ์ ์ธ ์ค๋ฅ ๋ฉ์์ง ๊ฐ์
if "timeout" in error_msg.lower():
user_error = "์์ฒญ ์ฒ๋ฆฌ ์๊ฐ์ด ์ด๊ณผ๋์์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์."
elif "connection" in error_msg.lower():
user_error = "์๋ฒ ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋คํธ์ํฌ ์ํ๋ฅผ ํ์ธํด์ฃผ์ธ์."
elif "unauthorized" in error_msg.lower() or "password" in error_msg.lower():
user_error = "์ธ์ฆ์ ์คํจํ์ต๋๋ค. ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํด์ฃผ์ธ์."
else:
user_error = f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {error_msg}"
return ([], None, [], None, "", "", user_error)
finally:
# ์์ ํ์ผ ์ ๋ฆฌ
if temp_path and os.path.exists(temp_path):
try:
os.remove(temp_path)
logger.info("์์ ํ์ผ ์ ๋ฆฌ ์๋ฃ")
except Exception as e:
logger.warning(f"์์ ํ์ผ ์ ๋ฆฌ ์คํจ: {str(e)}")
# ์ ์ญ ํด๋ผ์ด์ธํธ ์ปจํธ๋กค๋ฌ ์ธ์คํด์ค
controller = GradioClientController()
def create_gradio_interface():
"""Gradio ์ธํฐํ์ด์ค ์์ฑ"""
try:
logger.info("Gradio ์ธํฐํ์ด์ค ์์ฑ ์์")
# ๋ฐฐ๊ฒฝ ์ ํ ์ ํ์ง
background_types = ["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํน์๋ฐฐ๊ฒฝ", "์ฃผ์ผ๋ฆฌ", "ํน์ํจ๊ณผ"]
# ์ปค์คํ
CSS ์ถ๊ฐ (์๋ณธ๊ณผ ๋์ผํ ์คํ์ผ ์ ์ง)
css = """
.gradio-container {
max-width: 1200px !important;
}
"""
with gr.Blocks(title="AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ", css=css, theme=gr.themes.Soft()) as app:
gr.Markdown("# AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ๋๊ตฌ")
# ๋น๋ฐ๋ฒํธ ์
๋ ฅ ํ๋
password_box = gr.Textbox(
label="๋น๋ฐ๋ฒํธ",
type="password",
placeholder="์ฌ์ฉํ๋ ค๋ฉด ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์",
interactive=True
)
# ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ์ธํฐํ์ด์ค
with gr.Row():
with gr.Column():
# ์ํ ์ด๋ฏธ์ง ์
๋ก๋
image = gr.Image(label="์ํ ์ด๋ฏธ์ง ์
๋ก๋", type="pil")
with gr.Row():
with gr.Column():
background_type = gr.Radio(
choices=background_types,
label="๋ฐฐ๊ฒฝ ์ ํ",
value="์ฌํ ๋ฐฐ๊ฒฝ"
)
# ๋๋กญ๋ค์ด ์ปดํฌ๋ํธ๋ค - ์ฌํ ๋ฐฐ๊ฒฝ์ ํ๋์ฝ๋ฉ๋ ์ ํ์ง๋ก ์์
simple_dropdown = gr.Dropdown(
choices=controller.get_initial_simple_choices(),
value="ํ์ดํธ ๊ธฐ๋ณธ", # ๊ธฐ๋ณธ๊ฐ ์ค์
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ",
visible=True,
interactive=True
)
studio_dropdown = gr.Dropdown(
choices=[],
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
visible=False,
interactive=True
)
nature_dropdown = gr.Dropdown(
choices=[],
label="์์ฐ ํ๊ฒฝ ์ ํ",
visible=False,
interactive=True
)
indoor_dropdown = gr.Dropdown(
choices=[],
label="์ค๋ด ํ๊ฒฝ ์ ํ",
visible=False,
interactive=True
)
special_dropdown = gr.Dropdown(
choices=[],
label="ํน์๋ฐฐ๊ฒฝ ์ ํ",
visible=False,
interactive=True
)
jewelry_dropdown = gr.Dropdown(
choices=[],
label="์ฃผ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ํ",
visible=False,
interactive=True
)
special_effects_dropdown = gr.Dropdown(
choices=[],
label="ํน์ํจ๊ณผ ๋ฐฐ๊ฒฝ ์ ํ",
visible=False,
interactive=True
)
# ์์ฒญ์ฌํญ ์
๋ ฅ
request_text = gr.Textbox(
label="์์ฒญ์ฌํญ",
placeholder="์ํ ์ด๋ฏธ์ง์ ์ ์ฉํ ์คํ์ผ, ๋ถ์๊ธฐ, ํน๋ณ ์์ฒญ์ฌํญ ๋ฑ์ ์
๋ ฅํ์ธ์.",
lines=3
)
# ์๋ก์ด ์ต์
๋ค
quality_level = gr.Radio(
label="ํ์ง ๋ ๋ฒจ",
choices=["gpt", "flux"],
value="flux",
info="GPT: GPT ๋ชจ๋ธ (๊ณ ํ์ง), Flux: Flux ๋ชจ๋ธ (๋น ๋ฅธ ์ฒ๋ฆฌ + ๊ธฐ๋ณธ ํ์ง๊ฐ์ )"
)
aspect_ratio = gr.Dropdown(
label="์ข
ํก๋น",
choices=["1:1", "3:2", "2:3"],
value="1:1"
)
output_format = gr.Dropdown(
label="์ด๋ฏธ์ง ํ์",
choices=["jpg", "png"],
value="jpg"
)
# ํ์ง ๊ฐ์ ์ต์
enable_enhancement = gr.Checkbox(
label="์ถ๊ฐ ํ์ง ๊ฐ์ ",
value=False,
info="GPT: 1ํ ํ์ง๊ฐ์ , Flux: 2์ฐจ ํ์ง๊ฐ์ (๊ธฐ๋ณธ 1ํ + ์ถ๊ฐ 1ํ)"
)
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ
generate_prompt_btn = gr.Button("ํ๋กฌํํธ๋ง ์์ฑ", variant="secondary")
# ํธ์ง ๋ฒํผ
edit_btn = gr.Button("์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ", variant="primary")
with gr.Column():
with gr.Row():
with gr.Column():
gr.Markdown("## ํธ์ง๋ ์ด๋ฏธ์ง")
original_output = gr.Gallery(label="ํธ์ง ๊ฒฐ๊ณผ", preview=True)
original_download = gr.File(label="ํธ์ง ์ด๋ฏธ์ง ๋ค์ด๋ก๋", interactive=False)
with gr.Column():
gr.Markdown("## ํ์ง ๊ฐ์ ๋ ์ด๋ฏธ์ง")
enhanced_output = gr.Gallery(label="ํ์ง ๊ฐ์ ๊ฒฐ๊ณผ", preview=True)
enhanced_download = gr.File(label="๊ฐ์ ์ด๋ฏธ์ง ๋ค์ด๋ก๋", interactive=False)
# ํ๋กฌํํธ ์ถ๋ ฅ
prompt_output = gr.Textbox(label="์์ฑ๋ ํ๋กฌํํธ", lines=10, interactive=False)
info = gr.Textbox(label="์ฒ๋ฆฌ ์ ๋ณด", interactive=False)
error = gr.Textbox(label="์ค๋ฅ ๋ฉ์์ง", interactive=False, visible=True)
# ๋๋กญ๋ค์ด ๋ณ๊ฒฝ ์ด๋ฒคํธ - ์ฆ์ ์
๋ฐ์ดํธ
background_type.change(
fn=controller.update_dropdowns,
inputs=[background_type],
outputs=[simple_dropdown, studio_dropdown, nature_dropdown,
indoor_dropdown, special_dropdown, jewelry_dropdown, special_effects_dropdown],
queue=False # ํ๋ฅผ ์ฌ์ฉํ์ง ์์ ๋ ๋น ๋ฅธ ์๋ต
)
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
generate_prompt_btn.click(
fn=controller.generate_prompt_only,
inputs=[
password_box,
background_type,
simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, special_dropdown,
jewelry_dropdown, special_effects_dropdown,
request_text, aspect_ratio
],
outputs=[prompt_output]
)
# ํธ์ง ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
edit_btn.click(
fn=controller.process_image,
inputs=[
password_box,
image, background_type,
simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, special_dropdown,
jewelry_dropdown, special_effects_dropdown,
request_text, quality_level, aspect_ratio, output_format, enable_enhancement
],
outputs=[
original_output, original_download,
enhanced_output, enhanced_download,
prompt_output, info, error
]
)
# ์ฑ ๋ก๋ ์ ๋ชจ๋ ๋๋กญ๋ค์ด ์ด๊ธฐ ์ค์ (๋ ๊ฐ๋ ฅํ ์ด๊ธฐํ)
def initialize_all_dropdowns():
"""๋ชจ๋ ๋๋กญ๋ค์ด์ ์ด๊ธฐํํ๋ ํจ์"""
try:
logger.info("๋๋กญ๋ค์ด ์ด๊ธฐํ ์์")
updates = controller.update_dropdowns("์ฌํ ๋ฐฐ๊ฒฝ")
logger.info("๋๋กญ๋ค์ด ์ด๊ธฐํ ์๋ฃ")
return updates
except Exception as e:
logger.error(f"๋๋กญ๋ค์ด ์ด๊ธฐํ ์คํจ: {str(e)}")
return tuple([gr.update() for _ in range(7)])
app.load(
fn=initialize_all_dropdowns,
outputs=[simple_dropdown, studio_dropdown, nature_dropdown,
indoor_dropdown, special_dropdown, jewelry_dropdown, special_effects_dropdown]
)
logger.info("Gradio ์ธํฐํ์ด์ค ์์ฑ ์๋ฃ")
return app
except Exception as e:
logger.error(f"Gradio ์ธํฐํ์ด์ค ์์ฑ ์ค๋ฅ: {str(e)}")
logger.error(traceback.format_exc())
raise
# ์ฑ ์คํ
if __name__ == "__main__":
try:
logger.info("์ ํ๋ฆฌ์ผ์ด์
์์")
# imgs ๋๋ ํ ๋ฆฌ ํ์ธ/์์ฑ
os.makedirs("imgs", exist_ok=True)
logger.info("์ด๋ฏธ์ง ๋๋ ํ ๋ฆฌ ์ค๋น ์๋ฃ")
# API ์๋ํฌ์ธํธ ํ์ธ
if not os.environ.get("API_ENDPOINT"):
logger.error("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
print("\nโ ์ค๋ฅ: API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
print("ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ๊ฑฐ๋ .env ํ์ผ์ API_ENDPOINT๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.")
sys.exit(1)
app = create_gradio_interface()
logger.info("Gradio ์ฑ ์์")
app.launch(
share=True,
server_name="0.0.0.0",
server_port=7860,
show_error=True,
quiet=False
)
except KeyboardInterrupt:
logger.info("์ฌ์ฉ์์ ์ํด ์ ํ๋ฆฌ์ผ์ด์
์ด ์ค๋จ๋์์ต๋๋ค.")
except Exception as e:
logger.error(f"์ฑ ์คํ ์ค๋ฅ: {str(e)}")
logger.error(traceback.format_exc())
print(f"\nโ ์ฌ๊ฐํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
print("์์ธํ ๋ด์ฉ์ app.log ํ์ผ์ ํ์ธํด์ฃผ์ธ์.") |