Update app.py
Browse files
app.py
CHANGED
@@ -1,320 +1,147 @@
|
|
1 |
import os
|
2 |
import sys
|
|
|
|
|
3 |
import logging
|
4 |
import tempfile
|
5 |
import traceback
|
6 |
-
|
|
|
7 |
import gradio as gr
|
8 |
-
from gradio_client import Client
|
9 |
from dotenv import load_dotenv
|
10 |
-
import time
|
11 |
|
12 |
-
#
|
13 |
load_dotenv()
|
14 |
|
15 |
-
# ๋ก๊น
์ค์ (
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
console_handler.setFormatter(formatter)
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
logger.
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
self.background_options = {} # ์บ์๋ ๋ฐฐ๊ฒฝ ์ต์
|
45 |
-
self._initialize_client()
|
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 |
-
result = self.client.predict("์ฌํ ๋ฐฐ๊ฒฝ", api_name="/update_dropdowns")
|
80 |
-
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
81 |
-
self.background_options["์ฌํ ๋ฐฐ๊ฒฝ"] = result
|
82 |
-
logger.info("์ฌํ ๋ฐฐ๊ฒฝ ์ต์
๋ก๋ ์๋ฃ")
|
83 |
-
return result[0] # ์ฌํ ๋ฐฐ๊ฒฝ ์ ํ์ง ๋ฐํ
|
84 |
-
except Exception as e:
|
85 |
-
logger.warning(f"์ฌํ ๋ฐฐ๊ฒฝ ์ต์
๋ก๋ ์คํจ: {str(e)}")
|
86 |
-
return []
|
87 |
-
def get_initial_dropdown_data(self, bg_type: str = "์ฌํ ๋ฐฐ๊ฒฝ") -> Tuple:
|
88 |
-
"""์ด๊ธฐ ๋๋กญ๋ค์ด ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ"""
|
89 |
-
try:
|
90 |
-
# ์บ์๋ ๋ฐ์ดํฐ ๋จผ์ ๏ฟฝ๏ฟฝ์ธ
|
91 |
-
if bg_type in self.background_options:
|
92 |
-
result = self.background_options[bg_type]
|
93 |
-
logger.info(f"์บ์์์ ์ด๊ธฐ ๋๋กญ๋ค์ด ๋ฐ์ดํฐ ๋ก๋: {bg_type}")
|
94 |
-
else:
|
95 |
-
# API ํธ์ถ๋ก ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
|
96 |
-
if self.client:
|
97 |
-
result = self.client.predict(bg_type, api_name="/update_dropdowns")
|
98 |
-
self.background_options[bg_type] = result # ์บ์์ ์ ์ฅ
|
99 |
-
logger.info(f"API์์ ์ด๊ธฐ ๋๋กญ๋ค์ด ๋ฐ์ดํฐ ๋ก๋: {bg_type}")
|
100 |
-
else:
|
101 |
-
logger.error("ํด๋ผ์ด์ธํธ๊ฐ ์ด๊ธฐํ๋์ง ์์")
|
102 |
-
return tuple([[] for _ in range(7)])
|
103 |
-
|
104 |
-
# ๊ฒฐ๊ณผ ๋ฐํ (์ค์ ์ ํ์ง๋ง)
|
105 |
-
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
106 |
-
return result[:7]
|
107 |
-
else:
|
108 |
-
return tuple([[] for _ in range(7)])
|
109 |
-
|
110 |
-
except Exception as e:
|
111 |
-
logger.error(f"์ด๊ธฐ ๋๋กญ๋ค์ด ๋ฐ์ดํฐ ๋ก๋ ์คํจ: {str(e)}")
|
112 |
-
return tuple([[] for _ in range(7)])
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
return result[:7]
|
124 |
-
else:
|
125 |
-
logger.warning(f"์์ํ์ง ๋ชปํ ์๋ต ํ์: {type(result)}, length={len(result) if hasattr(result, '__len__') else 'N/A'}")
|
126 |
-
return [[] for _ in range(7)]
|
127 |
-
|
128 |
-
except Exception as e:
|
129 |
-
logger.error(f"๋๋กญ๋ค์ด ๊ฒฐ๊ณผ ํ์ฑ ์ค๋ฅ: {str(e)}")
|
130 |
-
return [[] for _ in range(7)]
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
if not self._ensure_client():
|
154 |
-
logger.error("ํด๏ฟฝ๏ฟฝ์ด์ธํธ ์ฐ๊ฒฐ ์คํจ")
|
155 |
-
return tuple([gr.update(visible=False) for _ in range(7)])
|
156 |
-
|
157 |
-
# API ํธ์ถ
|
158 |
-
result = self.client.predict(bg_type, api_name="/update_dropdowns")
|
159 |
-
logger.info(f"API ์๋ต: {result}")
|
160 |
-
|
161 |
-
# API ์๋ต์ ๊ทธ๋๋ก ์ฌ์ฉ
|
162 |
-
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
163 |
-
updates = []
|
164 |
-
visibility_map = ["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํน์๋ฐฐ๊ฒฝ", "์ฃผ์ผ๋ฆฌ", "ํน์ํจ๊ณผ"]
|
165 |
-
|
166 |
-
for i, choices in enumerate(result[:7]):
|
167 |
-
is_visible = (bg_type == visibility_map[i])
|
168 |
-
|
169 |
-
if is_visible and isinstance(choices, list) and len(choices) > 0:
|
170 |
-
logger.info(f"{visibility_map[i]} ๋๋กญ๋ค์ด: {len(choices)}๊ฐ ์ ํ์ง")
|
171 |
-
updates.append(gr.update(
|
172 |
-
visible=True,
|
173 |
-
choices=choices,
|
174 |
-
value=choices[0], # ์ฒซ ๋ฒ์งธ ํญ๋ชฉ ์ ํ
|
175 |
-
interactive=True
|
176 |
-
))
|
177 |
-
else:
|
178 |
-
updates.append(gr.update(visible=False))
|
179 |
-
|
180 |
-
return tuple(updates)
|
181 |
-
else:
|
182 |
-
logger.error(f"์๋ชป๋ API ์๋ต: {result}")
|
183 |
-
return tuple([gr.update(visible=False) for _ in range(7)])
|
184 |
-
|
185 |
-
except Exception as e:
|
186 |
-
logger.error(f"๋๋กญ๋ค์ด ์
๋ฐ์ดํธ ์ค๋ฅ: {str(e)}")
|
187 |
-
return tuple([gr.update(visible=False) for _ in range(7)])
|
188 |
-
|
189 |
-
def generate_prompt_only(self, password: str, bg_type: str, simple: str, studio: str,
|
190 |
-
nature: str, indoor: str, special: str, jewelry: str,
|
191 |
-
special_effects: str, request_text: str, aspect_ratio: str) -> str:
|
192 |
-
"""ํ๋กฌํํธ๋ง ์์ฑ"""
|
193 |
-
try:
|
194 |
-
if not self._ensure_client():
|
195 |
-
return "ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
196 |
-
|
197 |
-
result = self.client.predict(
|
198 |
-
password,
|
199 |
-
bg_type,
|
200 |
-
simple or "",
|
201 |
-
studio or "",
|
202 |
-
nature or "",
|
203 |
-
indoor or "",
|
204 |
-
special or "",
|
205 |
-
jewelry or "",
|
206 |
-
special_effects or "",
|
207 |
-
request_text or "",
|
208 |
-
aspect_ratio,
|
209 |
-
api_name="/generate_prompt_with_password_check"
|
210 |
-
)
|
211 |
-
|
212 |
-
logger.info("ํ๋กฌํํธ ์์ฑ ์๋ฃ")
|
213 |
-
return result if result else "ํ๋กฌํํธ ์์ฑ์ ์คํจํ์ต๋๋ค."
|
214 |
-
|
215 |
-
except Exception as e:
|
216 |
-
logger.error(f"ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {str(e)}")
|
217 |
-
return f"ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {str(e)}"
|
218 |
-
|
219 |
-
def process_image(self, password: str, image, bg_type: str, simple: str, studio: str,
|
220 |
-
nature: str, indoor: str, special: str, jewelry: str, special_effects: str,
|
221 |
-
request_text: str, quality_level: str, aspect_ratio: str,
|
222 |
-
output_format: str, enable_enhancement: bool) -> Tuple:
|
223 |
-
"""์ด๋ฏธ์ง ์ฒ๋ฆฌ (ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ) - API ๋ฌธ์์ ์ ํํ ๋งค์นญ"""
|
224 |
-
temp_path = None
|
225 |
-
try:
|
226 |
-
if not self._ensure_client():
|
227 |
-
return ([], None, [], None, "", "", "ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์.")
|
228 |
-
|
229 |
-
# ์ด๋ฏธ์ง ๊ฒ์ฆ
|
230 |
-
if image is None:
|
231 |
-
return ([], None, [], None, "", "", "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")
|
232 |
-
|
233 |
-
# ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
234 |
-
temp_path = tempfile.mktemp(suffix='.png')
|
235 |
-
try:
|
236 |
-
image.save(temp_path, format='PNG')
|
237 |
-
logger.info(f"์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ: {os.path.basename(temp_path)}")
|
238 |
-
except Exception as e:
|
239 |
-
logger.error(f"์ด๋ฏธ์ง ์ ์ฅ ์คํจ: {str(e)}")
|
240 |
-
return ([], None, [], None, "", "", "์ด๋ฏธ์ง ์ ์ฅ์ ์คํจํ์ต๋๋ค. ๋ค๋ฅธ ์ด๋ฏธ์ง๋ฅผ ์๋ํด๋ณด์ธ์.")
|
241 |
-
|
242 |
-
# API ํธ์ถ - ๋งค๊ฐ๋ณ์ ์์๋ฅผ API ๋ฌธ์์ ์ ํํ ๋ง์ถค
|
243 |
-
logger.info("์ด๋ฏธ์ง ์ฒ๋ฆฌ API ํธ์ถ ์์")
|
244 |
-
result = self.client.predict(
|
245 |
-
password, # password (str)
|
246 |
-
handle_file(temp_path), # param_1 (image file)
|
247 |
-
bg_type, # param_2 (๋ฐฐ๊ฒฝ ์ ํ)
|
248 |
-
simple or "", # param_3 (์ฌํ ๋ฐฐ๊ฒฝ)
|
249 |
-
studio or "", # param_4 (์คํ๋์ค ๋ฐฐ๊ฒฝ)
|
250 |
-
nature or "", # param_5 (์์ฐ ํ๊ฒฝ)
|
251 |
-
indoor or "", # param_6 (์ค๋ด ํ๊ฒฝ)
|
252 |
-
special or "", # param_7 (ํน์๋ฐฐ๊ฒฝ)
|
253 |
-
jewelry or "", # param_8 (์ฃผ์ผ๋ฆฌ)
|
254 |
-
special_effects or "", # param_9 (ํน์ํจ๊ณผ)
|
255 |
-
request_text or "", # param_10 (์์ฒญ์ฌํญ)
|
256 |
-
quality_level, # param_11 (ํ์ง ๋ ๋ฒจ)
|
257 |
-
aspect_ratio, # param_12 (์ข
ํก๋น)
|
258 |
-
output_format, # param_13 (์ด๋ฏธ์ง ํ์)
|
259 |
-
enable_enhancement, # param_14 (์ถ๊ฐ ํ์ง ๊ฐ์ )
|
260 |
-
api_name="/check_password"
|
261 |
-
)
|
262 |
-
|
263 |
-
logger.info("์ด๋ฏธ์ง ์ฒ๋ฆฌ API ํธ์ถ ์๋ฃ")
|
264 |
-
|
265 |
-
# ๊ฒฐ๊ณผ ๊ฒ์ฆ ๋ฐ ๋ฐํ - API ๋ฌธ์์ ๋ฐ๋ฅด๋ฉด tuple of 7 elements
|
266 |
-
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
267 |
-
# [0] original_gallery, [1] original_file, [2] enhanced_gallery,
|
268 |
-
# [3] enhanced_file, [4] prompt_text, [5] info_text, [6] error_text
|
269 |
-
return result[:7] # ์ ํํ 7๊ฐ ์์๋ง ๋ฐํ
|
270 |
-
else:
|
271 |
-
logger.warning(f"API ์๋ต ํ์ ์ด์: type={type(result)}, length={len(result) if hasattr(result, '__len__') else 'N/A'}")
|
272 |
-
return ([], None, [], None, "", "", "API์์ ์ฌ๋ฐ๋ฅด์ง ์์ ์๋ต์ ๋ฐ์์ต๋๋ค.")
|
273 |
-
|
274 |
-
except Exception as e:
|
275 |
-
error_msg = str(e)
|
276 |
-
logger.error(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค๋ฅ: {error_msg}")
|
277 |
-
|
278 |
-
# ์ผ๋ฐ์ ์ธ ์ค๋ฅ ๋ฉ์์ง ๊ฐ์
|
279 |
-
if "timeout" in error_msg.lower():
|
280 |
-
user_error = "์์ฒญ ์ฒ๋ฆฌ ์๊ฐ์ด ์ด๊ณผ๋์์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
281 |
-
elif "connection" in error_msg.lower():
|
282 |
-
user_error = "์๋ฒ ์ฐ๊ฒฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋คํธ์ํฌ ์ํ๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
283 |
-
elif "unauthorized" in error_msg.lower() or "password" in error_msg.lower():
|
284 |
-
user_error = "์ธ์ฆ์ ์คํจํ์ต๋๋ค. ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
285 |
-
else:
|
286 |
-
user_error = f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {error_msg}"
|
287 |
-
|
288 |
-
return ([], None, [], None, "", "", user_error)
|
289 |
-
|
290 |
-
finally:
|
291 |
-
# ์์ ํ์ผ ์ ๋ฆฌ
|
292 |
-
if temp_path and os.path.exists(temp_path):
|
293 |
-
try:
|
294 |
-
os.remove(temp_path)
|
295 |
-
logger.info("์์ ํ์ผ ์ ๋ฆฌ ์๋ฃ")
|
296 |
-
except Exception as e:
|
297 |
-
logger.warning(f"์์ ํ์ผ ์ ๋ฆฌ ์คํจ: {str(e)}")
|
298 |
|
299 |
-
|
300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
|
|
|
302 |
def create_gradio_interface():
|
303 |
-
"""Gradio ์ธํฐํ์ด์ค ์์ฑ"""
|
304 |
try:
|
305 |
-
logger.info("Gradio
|
306 |
-
|
307 |
-
# ๋ฐฐ๊ฒฝ ์ ํ ์ ํ์ง
|
308 |
-
background_types = ["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํน์๋ฐฐ๊ฒฝ", "์ฃผ์ผ๋ฆฌ", "ํน์ํจ๊ณผ"]
|
309 |
-
|
310 |
-
# ์ปค์คํ
CSS ์ถ๊ฐ (์๋ณธ๊ณผ ๋์ผํ ์ค๏ฟฝ๏ฟฝ์ผ ์ ์ง)
|
311 |
-
css = """
|
312 |
-
.gradio-container {
|
313 |
-
max-width: 1200px !important;
|
314 |
-
}
|
315 |
-
"""
|
316 |
-
|
317 |
-
with gr.Blocks(title="AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ", css=css, theme=gr.themes.Soft()) as app:
|
318 |
gr.Markdown("# AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ๋๊ตฌ")
|
319 |
|
320 |
# ๋น๋ฐ๋ฒํธ ์
๋ ฅ ํ๋
|
@@ -334,61 +161,85 @@ def create_gradio_interface():
|
|
334 |
with gr.Row():
|
335 |
with gr.Column():
|
336 |
background_type = gr.Radio(
|
337 |
-
choices=
|
338 |
label="๋ฐฐ๊ฒฝ ์ ํ",
|
339 |
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
340 |
)
|
341 |
|
342 |
-
# ๋๋กญ๋ค์ด ์ปดํฌ๋ํธ๋ค
|
343 |
simple_dropdown = gr.Dropdown(
|
344 |
-
choices=
|
345 |
-
value="ํ์ดํธ ๊ธฐ๋ณธ",
|
346 |
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ",
|
347 |
visible=True,
|
348 |
interactive=True
|
349 |
)
|
350 |
|
351 |
studio_dropdown = gr.Dropdown(
|
352 |
-
choices=[],
|
|
|
353 |
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
354 |
visible=False,
|
355 |
interactive=True
|
356 |
)
|
357 |
|
358 |
nature_dropdown = gr.Dropdown(
|
359 |
-
choices=[],
|
|
|
360 |
label="์์ฐ ํ๊ฒฝ ์ ํ",
|
361 |
visible=False,
|
362 |
interactive=True
|
363 |
)
|
364 |
|
365 |
indoor_dropdown = gr.Dropdown(
|
366 |
-
choices=[],
|
|
|
367 |
label="์ค๋ด ํ๊ฒฝ ์ ํ",
|
368 |
visible=False,
|
369 |
interactive=True
|
370 |
)
|
371 |
|
372 |
special_dropdown = gr.Dropdown(
|
373 |
-
choices=[],
|
|
|
374 |
label="ํน์๋ฐฐ๊ฒฝ ์ ํ",
|
375 |
visible=False,
|
376 |
interactive=True
|
377 |
)
|
378 |
|
379 |
jewelry_dropdown = gr.Dropdown(
|
380 |
-
choices=[],
|
|
|
381 |
label="์ฃผ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ํ",
|
382 |
visible=False,
|
383 |
interactive=True
|
384 |
)
|
385 |
|
386 |
special_effects_dropdown = gr.Dropdown(
|
387 |
-
choices=[],
|
|
|
388 |
label="ํน์ํจ๊ณผ ๋ฐฐ๊ฒฝ ์ ํ",
|
389 |
visible=False,
|
390 |
interactive=True
|
391 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
|
393 |
# ์์ฒญ์ฌํญ ์
๋ ฅ
|
394 |
request_text = gr.Textbox(
|
@@ -402,7 +253,7 @@ def create_gradio_interface():
|
|
402 |
label="ํ์ง ๋ ๋ฒจ",
|
403 |
choices=["gpt", "flux"],
|
404 |
value="flux",
|
405 |
-
info="GPT: GPT ๋ชจ๋ธ (๊ณ ํ์ง),
|
406 |
)
|
407 |
|
408 |
aspect_ratio = gr.Dropdown(
|
@@ -423,12 +274,13 @@ def create_gradio_interface():
|
|
423 |
value=False,
|
424 |
info="GPT: 1ํ ํ์ง๊ฐ์ , Flux: 2์ฐจ ํ์ง๊ฐ์ (๊ธฐ๋ณธ 1ํ + ์ถ๊ฐ 1ํ)"
|
425 |
)
|
|
|
426 |
|
427 |
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ
|
428 |
-
generate_prompt_btn = gr.Button("ํ๋กฌํํธ๋ง ์์ฑ"
|
429 |
|
430 |
# ํธ์ง ๋ฒํผ
|
431 |
-
edit_btn = gr.Button("์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ "
|
432 |
|
433 |
with gr.Column():
|
434 |
with gr.Row():
|
@@ -447,18 +299,40 @@ def create_gradio_interface():
|
|
447 |
info = gr.Textbox(label="์ฒ๋ฆฌ ์ ๋ณด", interactive=False)
|
448 |
error = gr.Textbox(label="์ค๋ฅ ๋ฉ์์ง", interactive=False, visible=True)
|
449 |
|
450 |
-
#
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
458 |
|
459 |
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
460 |
generate_prompt_btn.click(
|
461 |
-
fn=
|
462 |
inputs=[
|
463 |
password_box,
|
464 |
background_type,
|
@@ -471,7 +345,7 @@ def create_gradio_interface():
|
|
471 |
|
472 |
# ํธ์ง ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
473 |
edit_btn.click(
|
474 |
-
fn=
|
475 |
inputs=[
|
476 |
password_box,
|
477 |
image, background_type,
|
@@ -485,63 +359,99 @@ def create_gradio_interface():
|
|
485 |
prompt_output, info, error
|
486 |
]
|
487 |
)
|
488 |
-
|
489 |
-
# ์ฑ ๋ก๋ ์ ๋ชจ๋ ๋๋กญ๋ค์ด ์ด๊ธฐ ์ค์ (๋ ๊ฐ๋ ฅํ ์ด๊ธฐํ)
|
490 |
-
def initialize_all_dropdowns():
|
491 |
-
"""๋ชจ๋ ๋๋กญ๋ค์ด์ ์ด๊ธฐํํ๋ ํจ์"""
|
492 |
-
try:
|
493 |
-
logger.info("๋๋กญ๋ค์ด ์ด๊ธฐํ ์์")
|
494 |
-
updates = controller.update_dropdowns("์ฌํ ๋ฐฐ๊ฒฝ")
|
495 |
-
logger.info("๋๋กญ๋ค์ด ์ด๊ธฐํ ์๋ฃ")
|
496 |
-
return updates
|
497 |
-
except Exception as e:
|
498 |
-
logger.error(f"๋๋กญ๋ค์ด ์ด๊ธฐํ ์คํจ: {str(e)}")
|
499 |
-
return tuple([gr.update() for _ in range(7)])
|
500 |
-
|
501 |
-
app.load(
|
502 |
-
fn=initialize_all_dropdowns,
|
503 |
-
outputs=[simple_dropdown, studio_dropdown, nature_dropdown,
|
504 |
-
indoor_dropdown, special_dropdown, jewelry_dropdown, special_effects_dropdown]
|
505 |
-
)
|
506 |
|
507 |
-
logger.info("Gradio
|
508 |
return app
|
509 |
-
|
510 |
except Exception as e:
|
511 |
-
logger.error(f"
|
512 |
logger.error(traceback.format_exc())
|
513 |
raise
|
514 |
|
515 |
# ์ฑ ์คํ
|
516 |
if __name__ == "__main__":
|
517 |
try:
|
518 |
-
logger.info("
|
519 |
|
520 |
# imgs ๋๋ ํ ๋ฆฌ ํ์ธ/์์ฑ
|
521 |
os.makedirs("imgs", exist_ok=True)
|
522 |
logger.info("์ด๋ฏธ์ง ๋๋ ํ ๋ฆฌ ์ค๋น ์๋ฃ")
|
523 |
|
524 |
-
# API ์๋ํฌ์ธํธ ํ์ธ
|
525 |
-
if not os.environ.get("API_ENDPOINT"):
|
526 |
-
logger.error("API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
527 |
-
print("\nโ ์ค๋ฅ: API_ENDPOINT ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
528 |
-
print("ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ๊ฑฐ๋ .env ํ์ผ์ API_ENDPOINT๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.")
|
529 |
-
sys.exit(1)
|
530 |
-
|
531 |
app = create_gradio_interface()
|
532 |
-
logger.info("Gradio
|
533 |
-
app.launch(
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
|
|
|
|
540 |
|
541 |
-
|
542 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
543 |
except Exception as e:
|
544 |
-
logger.error(f"
|
545 |
-
|
546 |
-
|
547 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
+
import base64
|
4 |
+
import io
|
5 |
import logging
|
6 |
import tempfile
|
7 |
import traceback
|
8 |
+
import requests
|
9 |
+
from PIL import Image
|
10 |
import gradio as gr
|
11 |
+
from gradio_client import Client
|
12 |
from dotenv import load_dotenv
|
|
|
13 |
|
14 |
+
# ํ๊ฒฝ๋ณ์ ๋ก๋
|
15 |
load_dotenv()
|
16 |
|
17 |
+
# ๋ก๊น
์ค์ (๋ฏผ๊ฐํ ์ ๋ณด ์ ์ธ)
|
18 |
+
logging.basicConfig(
|
19 |
+
level=logging.INFO,
|
20 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
21 |
+
handlers=[
|
22 |
+
logging.FileHandler("app.log"),
|
23 |
+
logging.StreamHandler(sys.stdout)
|
24 |
+
]
|
25 |
+
)
|
26 |
+
logger = logging.getLogger("image-enhancer-control-tower")
|
27 |
|
28 |
+
# API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (ํ๊ฒฝ๋ณ์์์)
|
29 |
+
API_ENDPOINT = os.environ.get("API_ENDPOINT")
|
30 |
+
if not API_ENDPOINT:
|
31 |
+
logger.error("API_ENDPOINT environment variable is not set")
|
32 |
+
sys.exit(1)
|
|
|
33 |
|
34 |
+
try:
|
35 |
+
# ๋ก๊ทธ์ ์๋ํฌ์ธํธ ์ ๋ณด ์ถ๋ ฅํ์ง ์์
|
36 |
+
client = Client(API_ENDPOINT)
|
37 |
+
logger.info("API client initialized successfully")
|
38 |
+
except Exception as e:
|
39 |
+
logger.error(f"Failed to initialize API client: {e}")
|
40 |
+
sys.exit(1)
|
41 |
|
42 |
+
# backgrounds.py ์ ์ฒด ๋ด์ฉ์ ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์ค๊ธฐ
|
43 |
+
def load_backgrounds_from_env():
|
44 |
+
"""ํ๊ฒฝ๋ณ์์์ backgrounds.py ์ ์ฒด ๋ด์ฉ ๋ก๋"""
|
45 |
+
backgrounds_code = os.environ.get("BACKGROUNDS_DATA", "")
|
46 |
+
if not backgrounds_code:
|
47 |
+
logger.warning("BACKGROUNDS_DATA environment variable is empty")
|
|
|
|
|
48 |
|
49 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
50 |
+
temp_filename = temp_file.name
|
51 |
+
logger.info(f"Created temporary file: {temp_filename}")
|
52 |
+
|
53 |
+
# Gradio ์
๋ก๋ ํ์ผ ์ฒ๋ฆฌ
|
54 |
+
if isinstance(uploaded_file, str): # ์ด๋ฏธ ํ์ผ ๊ฒฝ๋ก์ธ ๊ฒฝ์ฐ
|
55 |
+
logger.info(f"Uploaded file is already a path: {uploaded_file}")
|
56 |
+
return uploaded_file
|
57 |
+
|
58 |
+
# PIL Image ์ฒ๋ฆฌ
|
59 |
+
if isinstance(uploaded_file, Image.Image):
|
60 |
+
logger.info("Uploaded file is a PIL Image")
|
61 |
+
uploaded_file.save(temp_filename, format="PNG")
|
62 |
+
return temp_filename
|
63 |
+
|
64 |
+
# ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ ์ฒ๋ฆฌ
|
65 |
+
with open(temp_filename, "wb") as f:
|
66 |
+
if hasattr(uploaded_file, "read"): # ํ์ผ ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ
|
67 |
+
logger.info("Processing file object")
|
68 |
+
content = uploaded_file.read()
|
69 |
+
f.write(content)
|
70 |
+
logger.info(f"Wrote {len(content)} bytes to {temp_filename}")
|
71 |
+
else: # ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ์ธ ๊ฒฝ์ฐ
|
72 |
+
logger.info("Processing binary data")
|
73 |
+
f.write(uploaded_file)
|
74 |
+
logger.info(f"Wrote data to {temp_filename}")
|
75 |
+
|
76 |
+
return temp_filename
|
77 |
+
except Exception as e:
|
78 |
+
logger.error(f"Error saving uploaded file: {e}")
|
79 |
+
logger.error(traceback.format_exc())
|
80 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
# API ํธ์ถ ํจ์๋ค (ํด๋ผ์ด์ธํธ๋ฅผ ํตํด ์๋ณธ API ํธ์ถ)
|
83 |
+
def api_update_dropdowns(bg_type):
|
84 |
+
"""๋๋กญ๋ค์ด ์
๋ฐ์ดํธ API ํธ์ถ"""
|
85 |
+
try:
|
86 |
+
result = client.predict(bg_type=bg_type, api_name="/update_dropdowns")
|
87 |
+
return result
|
88 |
+
except Exception as e:
|
89 |
+
logger.error(f"Error calling update_dropdowns API: {e}")
|
90 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
def api_generate_prompt_with_password_check(password, bg_type, simple, studio, nature, indoor, special, jewelry, special_effects, request_text, aspect_ratio):
|
93 |
+
"""ํ๋กฌํํธ ์์ฑ API ํธ์ถ"""
|
94 |
+
try:
|
95 |
+
result = client.predict(
|
96 |
+
password=password,
|
97 |
+
bg_type=bg_type,
|
98 |
+
simple=simple,
|
99 |
+
studio=studio,
|
100 |
+
nature=nature,
|
101 |
+
indoor=indoor,
|
102 |
+
special=special,
|
103 |
+
jewelry=jewelry,
|
104 |
+
special_effects=special_effects,
|
105 |
+
request_text=request_text,
|
106 |
+
aspect_ratio=aspect_ratio,
|
107 |
+
api_name="/generate_prompt_with_password_check"
|
108 |
+
)
|
109 |
+
return result
|
110 |
+
except Exception as e:
|
111 |
+
logger.error(f"Error calling generate_prompt API: {e}")
|
112 |
+
return f"API ํธ์ถ ์ค๋ฅ: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
+
def api_check_password(password, image_file, bg_type, simple, studio, nature, indoor, special, jewelry, special_effects, request_text, quality_level, aspect_ratio, output_format, enable_enhancement):
|
115 |
+
"""์ด๋ฏธ์ง ์ฒ๋ฆฌ API ํธ์ถ"""
|
116 |
+
try:
|
117 |
+
result = client.predict(
|
118 |
+
password=password,
|
119 |
+
param_1=image_file,
|
120 |
+
param_2=bg_type,
|
121 |
+
param_3=simple,
|
122 |
+
param_4=studio,
|
123 |
+
param_5=nature,
|
124 |
+
param_6=indoor,
|
125 |
+
param_7=special,
|
126 |
+
param_8=jewelry,
|
127 |
+
param_9=special_effects,
|
128 |
+
param_10=request_text,
|
129 |
+
param_11=quality_level,
|
130 |
+
param_12=aspect_ratio,
|
131 |
+
param_13=output_format,
|
132 |
+
param_14=enable_enhancement,
|
133 |
+
api_name="/check_password"
|
134 |
+
)
|
135 |
+
return result
|
136 |
+
except Exception as e:
|
137 |
+
logger.error(f"Error calling check_password API: {e}")
|
138 |
+
return None, None, None, None, "", "", f"API ํธ์ถ ์ค๋ฅ: {str(e)}"
|
139 |
|
140 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ (์์ ํ ๋์ผํ UI)
|
141 |
def create_gradio_interface():
|
|
|
142 |
try:
|
143 |
+
logger.info("Creating Gradio interface")
|
144 |
+
with gr.Blocks(title="AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ") as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
gr.Markdown("# AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ๋๊ตฌ")
|
146 |
|
147 |
# ๋น๋ฐ๋ฒํธ ์
๋ ฅ ํ๋
|
|
|
161 |
with gr.Row():
|
162 |
with gr.Column():
|
163 |
background_type = gr.Radio(
|
164 |
+
choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํน์๋ฐฐ๊ฒฝ", "์ฃผ์ผ๋ฆฌ", "ํน์ํจ๊ณผ"],
|
165 |
label="๋ฐฐ๊ฒฝ ์ ํ",
|
166 |
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
167 |
)
|
168 |
|
169 |
+
# ๋๋กญ๋ค์ด ์ปดํฌ๋ํธ๋ค
|
170 |
simple_dropdown = gr.Dropdown(
|
171 |
+
choices=list(SIMPLE_BACKGROUNDS.keys()) if SIMPLE_BACKGROUNDS else ["ํ์ดํธ ๊ธฐ๋ณธ"],
|
172 |
+
value=list(SIMPLE_BACKGROUNDS.keys())[0] if SIMPLE_BACKGROUNDS else "ํ์ดํธ ๊ธฐ๋ณธ",
|
173 |
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ",
|
174 |
visible=True,
|
175 |
interactive=True
|
176 |
)
|
177 |
|
178 |
studio_dropdown = gr.Dropdown(
|
179 |
+
choices=list(STUDIO_BACKGROUNDS.keys()) if STUDIO_BACKGROUNDS else ["์ฐ๋
น์ ์ฅ๋ฏธ ์ ์"],
|
180 |
+
value=list(STUDIO_BACKGROUNDS.keys())[0] if STUDIO_BACKGROUNDS else "์ฐ๋
น์ ์ฅ๋ฏธ ์ ์",
|
181 |
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
182 |
visible=False,
|
183 |
interactive=True
|
184 |
)
|
185 |
|
186 |
nature_dropdown = gr.Dropdown(
|
187 |
+
choices=list(NATURE_BACKGROUNDS.keys()) if NATURE_BACKGROUNDS else ["์์ ํ๋๊ฐ ์๋ ํด๋ณ"],
|
188 |
+
value=list(NATURE_BACKGROUNDS.keys())[0] if NATURE_BACKGROUNDS else "์์ ํ๋๊ฐ ์๋ ํด๋ณ",
|
189 |
label="์์ฐ ํ๊ฒฝ ์ ํ",
|
190 |
visible=False,
|
191 |
interactive=True
|
192 |
)
|
193 |
|
194 |
indoor_dropdown = gr.Dropdown(
|
195 |
+
choices=list(INDOOR_BACKGROUNDS.keys()) if INDOOR_BACKGROUNDS else ["๊ธฐ๋ณธ ์ฑ
์"],
|
196 |
+
value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else "๊ธฐ๋ณธ ์ฑ
์",
|
197 |
label="์ค๋ด ํ๊ฒฝ ์ ํ",
|
198 |
visible=False,
|
199 |
interactive=True
|
200 |
)
|
201 |
|
202 |
special_dropdown = gr.Dropdown(
|
203 |
+
choices=list(SPECIAL_BACKGROUNDS.keys()) if SPECIAL_BACKGROUNDS else ["๋ค์ด๋น ๋นํฐ์ง ํ๋ก๋ด ๋ฒฝ์ง"],
|
204 |
+
value=list(SPECIAL_BACKGROUNDS.keys())[0] if SPECIAL_BACKGROUNDS else "๋ค์ด๋น ๋นํฐ์ง ํ๋ก๋ด ๋ฒฝ์ง",
|
205 |
label="ํน์๋ฐฐ๊ฒฝ ์ ํ",
|
206 |
visible=False,
|
207 |
interactive=True
|
208 |
)
|
209 |
|
210 |
jewelry_dropdown = gr.Dropdown(
|
211 |
+
choices=list(JEWELRY_BACKGROUNDS.keys()) if JEWELRY_BACKGROUNDS else ["ํ์ดํธ ๋ฏธ๋ฌ ์คํ ๋ผ์ดํธ"],
|
212 |
+
value=list(JEWELRY_BACKGROUNDS.keys())[0] if JEWELRY_BACKGROUNDS else "ํ์ดํธ ๋ฏธ๋ฌ ์คํ ๋ผ์ดํธ",
|
213 |
label="์ฃผ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ํ",
|
214 |
visible=False,
|
215 |
interactive=True
|
216 |
)
|
217 |
|
218 |
special_effects_dropdown = gr.Dropdown(
|
219 |
+
choices=list(SPECIAL_EFFECTS_BACKGROUNDS.keys()) if SPECIAL_EFFECTS_BACKGROUNDS else ["๋ธ๋ฃจ๋ธ๋ ํฐ ๋ฌผ๋ฐฉ์ธ ํจ๊ณผ"],
|
220 |
+
value=list(SPECIAL_EFFECTS_BACKGROUNDS.keys())[0] if SPECIAL_EFFECTS_BACKGROUNDS else "๋ธ๋ฃจ๋ธ๋ ํฐ ๋ฌผ๋ฐฉ์ธ ํจ๊ณผ",
|
221 |
label="ํน์ํจ๊ณผ ๋ฐฐ๊ฒฝ ์ ํ",
|
222 |
visible=False,
|
223 |
interactive=True
|
224 |
)
|
225 |
+
|
226 |
+
# ๋๋กญ๋ค์ด ๋ณ๊ฒฝ ํจ์
|
227 |
+
def update_dropdowns(bg_type):
|
228 |
+
return {
|
229 |
+
simple_dropdown: gr.update(visible=(bg_type == "์ฌํ ๋ฐฐ๊ฒฝ")),
|
230 |
+
studio_dropdown: gr.update(visible=(bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ")),
|
231 |
+
nature_dropdown: gr.update(visible=(bg_type == "์์ฐ ํ๊ฒฝ")),
|
232 |
+
indoor_dropdown: gr.update(visible=(bg_type == "์ค๋ด ํ๊ฒฝ")),
|
233 |
+
special_dropdown: gr.update(visible=(bg_type == "ํน์๋ฐฐ๊ฒฝ")),
|
234 |
+
jewelry_dropdown: gr.update(visible=(bg_type == "์ฃผ์ผ๋ฆฌ")),
|
235 |
+
special_effects_dropdown: gr.update(visible=(bg_type == "ํน์ํจ๊ณผ"))
|
236 |
+
}
|
237 |
+
|
238 |
+
background_type.change(
|
239 |
+
fn=update_dropdowns,
|
240 |
+
inputs=[background_type],
|
241 |
+
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, special_dropdown, jewelry_dropdown, special_effects_dropdown]
|
242 |
+
)
|
243 |
|
244 |
# ์์ฒญ์ฌํญ ์
๋ ฅ
|
245 |
request_text = gr.Textbox(
|
|
|
253 |
label="ํ์ง ๋ ๋ฒจ",
|
254 |
choices=["gpt", "flux"],
|
255 |
value="flux",
|
256 |
+
info="GPT: GPT ๋ชจ๋ธ (๊ณ ํ์ง), ์ผ๋ฐ: Flux ๋ชจ๋ธ (๋น ๋ฅธ ์ฒ๋ฆฌ + ๊ธฐ๋ณธ ํ์ง๊ฐ์ )"
|
257 |
)
|
258 |
|
259 |
aspect_ratio = gr.Dropdown(
|
|
|
274 |
value=False,
|
275 |
info="GPT: 1ํ ํ์ง๊ฐ์ , Flux: 2์ฐจ ํ์ง๊ฐ์ (๊ธฐ๋ณธ 1ํ + ์ถ๊ฐ 1ํ)"
|
276 |
)
|
277 |
+
enhancement_level = gr.Slider(label="ํ์ง ๊ฐ์ ๋ ๋ฒจ", minimum=1, maximum=4, value=2, step=1, visible=False)
|
278 |
|
279 |
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ
|
280 |
+
generate_prompt_btn = gr.Button("ํ๋กฌํํธ๋ง ์์ฑ")
|
281 |
|
282 |
# ํธ์ง ๋ฒํผ
|
283 |
+
edit_btn = gr.Button("์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ")
|
284 |
|
285 |
with gr.Column():
|
286 |
with gr.Row():
|
|
|
299 |
info = gr.Textbox(label="์ฒ๋ฆฌ ์ ๋ณด", interactive=False)
|
300 |
error = gr.Textbox(label="์ค๋ฅ ๋ฉ์์ง", interactive=False, visible=True)
|
301 |
|
302 |
+
# ํ๋กฌํํธ๋ง ์์ฑํ๋ ํจ์ (API ํธ์ถ)
|
303 |
+
def generate_prompt_wrapper(password, bg_type, simple, studio, nature, indoor, special, jewelry, special_effects, request_text, aspect_ratio):
|
304 |
+
return api_generate_prompt_with_password_check(
|
305 |
+
password, bg_type, simple, studio, nature, indoor, special, jewelry, special_effects, request_text, aspect_ratio
|
306 |
+
)
|
307 |
+
|
308 |
+
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ํจ์ (API ํธ์ถ)
|
309 |
+
def process_image_wrapper(password, image, bg_type, simple, studio, nature, indoor, special, jewelry, special_effects, request_text, quality_level, aspect_ratio, output_format, enable_enhancement):
|
310 |
+
if image is None:
|
311 |
+
return [], None, [], None, "", "", "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ผ ํฉ๋๋ค."
|
312 |
+
|
313 |
+
# ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
314 |
+
temp_path = save_uploaded_file(image)
|
315 |
+
if temp_path is None:
|
316 |
+
return [], None, [], None, "", "", "์ด๋ฏธ์ง ์ฒ๋ฆฌ์ ์คํจํ์ต๋๋ค."
|
317 |
+
|
318 |
+
try:
|
319 |
+
# API ํธ์ถ
|
320 |
+
result = api_check_password(
|
321 |
+
password, temp_path, bg_type, simple, studio, nature, indoor, special, jewelry, special_effects,
|
322 |
+
request_text, quality_level, aspect_ratio, output_format, enable_enhancement
|
323 |
+
)
|
324 |
+
return result
|
325 |
+
finally:
|
326 |
+
# ์์ ํ์ผ ์ ๋ฆฌ
|
327 |
+
if os.path.exists(temp_path):
|
328 |
+
try:
|
329 |
+
os.remove(temp_path)
|
330 |
+
except:
|
331 |
+
pass
|
332 |
|
333 |
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
334 |
generate_prompt_btn.click(
|
335 |
+
fn=generate_prompt_wrapper,
|
336 |
inputs=[
|
337 |
password_box,
|
338 |
background_type,
|
|
|
345 |
|
346 |
# ํธ์ง ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
347 |
edit_btn.click(
|
348 |
+
fn=process_image_wrapper,
|
349 |
inputs=[
|
350 |
password_box,
|
351 |
image, background_type,
|
|
|
359 |
prompt_output, info, error
|
360 |
]
|
361 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
|
363 |
+
logger.info("Gradio interface created successfully")
|
364 |
return app
|
|
|
365 |
except Exception as e:
|
366 |
+
logger.error(f"Error creating Gradio interface: {e}")
|
367 |
logger.error(traceback.format_exc())
|
368 |
raise
|
369 |
|
370 |
# ์ฑ ์คํ
|
371 |
if __name__ == "__main__":
|
372 |
try:
|
373 |
+
logger.info("Starting control tower application")
|
374 |
|
375 |
# imgs ๋๋ ํ ๋ฆฌ ํ์ธ/์์ฑ
|
376 |
os.makedirs("imgs", exist_ok=True)
|
377 |
logger.info("์ด๋ฏธ์ง ๋๋ ํ ๋ฆฌ ์ค๋น ์๋ฃ")
|
378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
app = create_gradio_interface()
|
380 |
+
logger.info("Launching Gradio app")
|
381 |
+
app.launch(share=True)
|
382 |
+
except Exception as e:
|
383 |
+
logger.error(f"Error running app: {e}")
|
384 |
+
logger.error(traceback.format_exc()), None, None, None, None, None, None
|
385 |
+
|
386 |
+
try:
|
387 |
+
# ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์จ ์ฝ๋ ์คํํ์ฌ ๋์
๋๋ฆฌ๋ค ์ถ์ถ
|
388 |
+
local_vars = {}
|
389 |
+
exec(backgrounds_code, {}, local_vars)
|
390 |
|
391 |
+
return (
|
392 |
+
local_vars.get("SIMPLE_BACKGROUNDS", {}),
|
393 |
+
local_vars.get("STUDIO_BACKGROUNDS", {}),
|
394 |
+
local_vars.get("NATURE_BACKGROUNDS", {}),
|
395 |
+
local_vars.get("INDOOR_BACKGROUNDS", {}),
|
396 |
+
local_vars.get("SPECIAL_BACKGROUNDS", {}),
|
397 |
+
local_vars.get("JEWELRY_BACKGROUNDS", {}),
|
398 |
+
local_vars.get("SPECIAL_EFFECTS_BACKGROUNDS", {})
|
399 |
+
)
|
400 |
except Exception as e:
|
401 |
+
logger.error(f"Failed to parse BACKGROUNDS_DATA: {e}")
|
402 |
+
return None, None, None, None, None, None, None
|
403 |
+
|
404 |
+
# ํ๊ฒฝ๋ณ์์์ ๋ฐฐ๊ฒฝ ๋์
๋๋ฆฌ๋ค ๋ก๋
|
405 |
+
(SIMPLE_BACKGROUNDS, STUDIO_BACKGROUNDS, NATURE_BACKGROUNDS,
|
406 |
+
INDOOR_BACKGROUNDS, SPECIAL_BACKGROUNDS, JEWELRY_BACKGROUNDS,
|
407 |
+
SPECIAL_EFFECTS_BACKGROUNDS) = load_backgrounds_from_env()
|
408 |
+
|
409 |
+
# ๋ง์ฝ ํ๊ฒฝ๋ณ์๊ฐ ๋น์ด์๋ค๋ฉด ๊ธฐ๋ณธ๊ฐ ์ฌ์ฉ
|
410 |
+
if not SIMPLE_BACKGROUNDS:
|
411 |
+
SIMPLE_BACKGROUNDS = {
|
412 |
+
"ํ์ดํธ ๊ธฐ๋ณธ": "clean white background with soft lighting",
|
413 |
+
"ํ์ ํฌํค": "light gray background with minimal shadows",
|
414 |
+
"๋ผ์ดํธ ๊ทธ๋ ์ด": "soft light gray backdrop with even illumination"
|
415 |
+
}
|
416 |
+
|
417 |
+
if not STUDIO_BACKGROUNDS:
|
418 |
+
STUDIO_BACKGROUNDS = {
|
419 |
+
"์ฐ๋
น์ ์ฅ๋ฏธ ์ ์": "soft green background with rose garden elements",
|
420 |
+
"์ฐ๋ถํ ์ฅ๋ฏธ ๋๋ฆฌ์": "pink rose with marble surface backdrop"
|
421 |
+
}
|
422 |
+
|
423 |
+
if not NATURE_BACKGROUNDS:
|
424 |
+
NATURE_BACKGROUNDS = {
|
425 |
+
"์์ ํ๋๊ฐ ์๋ ํด๋ณ": "serene beach with gentle waves",
|
426 |
+
"์ด๋ํด๋ณ": "tropical beach setting with palm trees"
|
427 |
+
}
|
428 |
+
|
429 |
+
if not INDOOR_BACKGROUNDS:
|
430 |
+
INDOOR_BACKGROUNDS = {
|
431 |
+
"๊ธฐ๋ณธ ์ฑ
์": "clean modern desk setup",
|
432 |
+
"๋น์ด ๋น์น๋ ๊ฑฐ์ค": "bright living room with natural light"
|
433 |
+
}
|
434 |
+
|
435 |
+
if not SPECIAL_BACKGROUNDS:
|
436 |
+
SPECIAL_BACKGROUNDS = {
|
437 |
+
"๋ค์ด๋น ๋นํฐ์ง ํ๋ก๋ด ๋ฒฝ์ง": "navy vintage floral wallpaper background"
|
438 |
+
}
|
439 |
+
|
440 |
+
if not JEWELRY_BACKGROUNDS:
|
441 |
+
JEWELRY_BACKGROUNDS = {
|
442 |
+
"ํ์ดํธ ๋ฏธ๋ฌ ์คํ ๋ผ์ดํธ": "white mirror surface with spotlight"
|
443 |
+
}
|
444 |
+
|
445 |
+
if not SPECIAL_EFFECTS_BACKGROUNDS:
|
446 |
+
SPECIAL_EFFECTS_BACKGROUNDS = {
|
447 |
+
"๋ธ๋ฃจ๋ธ๋ ํฐ ๋ฌผ๋ฐฉ์ธ ํจ๊ณผ": "blue-black background with water droplet effects"
|
448 |
+
}
|
449 |
+
|
450 |
+
# ์์ ํ์ผ ์ ์ฅ ํจ์
|
451 |
+
def save_uploaded_file(uploaded_file, suffix='.png'):
|
452 |
+
try:
|
453 |
+
logger.info(f"Processing uploaded file: {type(uploaded_file)}")
|
454 |
+
|
455 |
+
if uploaded_file is None:
|
456 |
+
logger.warning("Uploaded file is None")
|
457 |
+
return
|