Update app.py
Browse files
app.py
CHANGED
@@ -1,372 +1,273 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import google.generativeai as genai
|
3 |
-
from PIL import Image
|
4 |
import os
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
genai
|
|
|
12 |
|
13 |
-
|
14 |
-
BACKGROUNDS_DIR = "./background"
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
print(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ์ต๋๋ค: {BACKGROUNDS_DIR}")
|
24 |
-
else:
|
25 |
-
print(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๊ฐ ์ด๋ฏธ ์กด์ฌํฉ๋๋ค: {BACKGROUNDS_DIR}")
|
26 |
-
try:
|
27 |
-
for file in os.listdir(BACKGROUNDS_DIR):
|
28 |
-
print(f"๋ฐ๊ฒฌ๋ ํ์ผ: {file}")
|
29 |
-
except Exception as e:
|
30 |
-
print(f"๋๋ ํ ๋ฆฌ ๋ด์ฉ์ ๋์ดํ๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
try:
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
except Exception as e:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
# ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ๋ก๋
|
51 |
-
SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json")
|
52 |
-
STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json")
|
53 |
-
NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json")
|
54 |
-
INDOOR_BACKGROUNDS = load_background_json("indoor_backgrounds.json")
|
55 |
-
ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json")
|
56 |
-
|
57 |
-
# ๋ฐฐ๊ฒฝ์ด ๋ก๋๋์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ ์ค์
|
58 |
-
if not SIMPLE_BACKGROUNDS:
|
59 |
-
SIMPLE_BACKGROUNDS = {"ํ์ดํธ ๋ฐฐ๊ฒฝ": "white background"}
|
60 |
-
if not STUDIO_BACKGROUNDS:
|
61 |
-
STUDIO_BACKGROUNDS = {"์ ํ ์ฌ์ง ์คํ๋์ค": "product photography studio"}
|
62 |
-
if not NATURE_BACKGROUNDS:
|
63 |
-
NATURE_BACKGROUNDS = {"์ด๋ ํด๋ณ": "tropical beach"}
|
64 |
-
if not INDOOR_BACKGROUNDS:
|
65 |
-
INDOOR_BACKGROUNDS = {"๋ชจ๋ ๋ฆฌ๋น๋ฃธ": "modern living room"}
|
66 |
-
if not ABSTRACT_BACKGROUNDS:
|
67 |
-
ABSTRACT_BACKGROUNDS = {"๋ค์จ ์กฐ๋ช
": "neon lights"}
|
68 |
|
69 |
-
def
|
70 |
-
|
71 |
-
์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ์ํ๋ช
, ๋ฐฐ๊ฒฝ ์ ํ, ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ฐํ์ผ๋ก ๋ฏธ๋์ ๋(Midjourney)์ ์ฌ์ฉํ ์ ์๋
|
72 |
-
์์ธํ๊ณ ์ ๋ฌธ์ ์ธ ํ๋กฌํํธ๋ฅผ ์์ด๋ก ์์ฑํด์ฃผ์ธ์.
|
73 |
-
๋ค์ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋์ ๋ฐ๋ผ์ผ ํฉ๋๋ค:
|
74 |
|
75 |
-
|
|
|
|
|
|
|
76 |
|
77 |
-
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
4. ์ด๋ฏธ์ง ๋น์จ์ ์ ํํ 1:1(์ ์ฌ๊ฐํ) ํ์์ผ๋ก ์ง์ ํฉ๋๋ค. ํ๋กฌํํธ์ "square format", "1:1 ratio" ๋๋ "aspect ratio 1:1"์ ๋ช
์์ ์ผ๋ก ํฌํจํฉ๋๋ค.
|
86 |
-
|
87 |
-
5. ์ํ์ ๋ฐ๋์ ์ ์ฌ๊ฐํ ๊ตฌ๋์ ์ ์ค์์ ๋ฐฐ์น๋์ด์ผ ํฉ๋๋ค.
|
88 |
-
|
89 |
-
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ ํฌ๊ฒ ์ฐจ์งํ๋๋ก ํฉ๋๋ค.
|
90 |
-
|
91 |
-
7. ์ํ ์ด๋ฏธ์ง ์ปท์์(#1)์ ๊ธฐ๋ณธ ํํ์ ์์์ ์ ์งํ๋ฉด์, ์ ํํ ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ๋๋๋ก ํฉ๋๋ค.
|
92 |
-
|
93 |
-
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
์ ์ด๋ฏธ์ง๋ฅผ ์ํ ๋ค์ ํ๊ฒฝ ์์๋ค์ ํฌํจํ์ธ์:
|
94 |
-
- ์ํ๊ณผ ์ด์ธ๋ฆฌ๋ ์ฃผ๋ณ ํ๊ฒฝ/๋ฐฐ๊ฒฝ ์์๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ํ์ฅํ ์ฃผ๋ณ์ ๊ฝ์ด๋ ํ๋ธ, ์๋ฃ ์ ํ ์์ ๊ณผ์ผ, ์ ์์ ํ ๊ทผ์ฒ์ ํ๋์ ์ํ ๋ฑ.
|
95 |
-
- ํ๊ฒฝ์ ์กฐ๋ช
ํจ๊ณผ(๋ฆผ ๋ผ์ดํธ, ๋ฐฑ๋ผ์ดํธ, ์ํํธ๋ฐ์ค ๋ฑ)๋ฅผ ์ค๋ช
ํฉ๋๋ค.
|
96 |
-
- ์ํ์ด ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ์กด์ฌํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ์ ์ ํ ๊ทธ๋ฆผ์์ ๋น ํํ์ ํฌํจํฉ๋๋ค.
|
97 |
-
- ์ํ์ ์ฉ๋๋ ์ฅ์ ์ ๊ฐ์ ์ ์ผ๋ก ์์ํ๋ ๋ฐฐ๊ฒฝ ์์๋ฅผ ํฌํจํฉ๋๋ค.
|
98 |
-
- ํ๋กํ์
๋ํ ์์
์ฌ์ง ํจ๊ณผ(์ ํ์ ํผ์ฌ๊ณ ์ฌ๋, ์ํํธ ํฌ์ปค์ค, ์คํ๋์ค ์กฐ๋ช
๋ฑ)๋ฅผ ๋ช
์ํฉ๋๋ค.
|
99 |
-
|
100 |
-
9. ํ๋กฌํํธ์ ๋ค์ ์์๋ค์ ๋ช
์์ ์ผ๋ก ํฌํจํ์ธ์:
|
101 |
-
- "highly detailed commercial photography"
|
102 |
-
- "award-winning product photography"
|
103 |
-
- "professional advertising imagery"
|
104 |
-
- "studio quality"
|
105 |
-
- "magazine advertisement quality"
|
106 |
-
|
107 |
-
10. ๋ฐฐ๊ฒฝ ํ๊ฒฝ ์์๋ฅผ ์ํ ์นดํ
๊ณ ๋ฆฌ์ ๋ง๊ฒ ์ ํํฉ๋๋ค:
|
108 |
-
- ์คํจ์ผ์ด ์ ํ: ๊นจ๋ํ ์์ค ์ ๋ฐ, ์ฐ์ํ ํ์ฅ๋, ์คํ ๊ฐ์ ํ๊ฒฝ ๋ฑ
|
109 |
-
- ์๋ฃ ์ ํ: ์ธ๋ จ๋ ํ
์ด๋ธ, ํํฐ ํ๊ฒฝ, ์ผ์ธ ํผํฌ๋ ์ฅ๋ฉด ๋ฑ
|
110 |
-
- ์ ์ ์ ํ: ์ธ๋ จ๋ ์์
๊ณต๊ฐ, ํ๋์ ์ธ ๊ฑฐ์ค, ๋ฏธ๋๋ฉํ ์ฑ
์ ๋ฑ
|
111 |
-
- ํจ์
/์๋ฅ: ์ธ๋ จ๋ ์ผ๋ฃธ, ๋์ ๊ฑฐ๋ฆฌ, ์๋ ๊ฐ์คํ ๋ผ์ดํ์คํ์ผ ํ๊ฒฝ ๋ฑ
|
112 |
-
- ์ํ ์ ํ: ๊น๋ํ ์ฃผ๋ฐฉ, ์ํ, ์๋ฆฌ ํ๊ฒฝ ๋ฑ
|
113 |
-
|
114 |
-
11. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ ํํ ๋ฐ์ํฉ๋๋ค.
|
115 |
-
|
116 |
-
12. ํ๋กฌํํธ๋ ๋ฏธ๋์ ๋ AI์ ์ต์ ํ๋์ด์ผ ํฉ๋๋ค.
|
117 |
-
|
118 |
-
13. ํ๋กฌํํธ ๋์ "--ar 1:1 --s 750 --q 2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค.
|
119 |
|
120 |
-
|
121 |
-
""
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
5. ํ๊ฒฝ๊ณผ์ ์์ฐ์ค๋ฌ์ด ํตํฉ์ ์ํ ์กฐ๋ช
ํจ๊ณผ์ ๊ทธ๋ฆผ์๋ ํฌํจํด์ฃผ์ธ์.
|
140 |
-
6. ์ํ์ ๋ ๋๋ณด์ด๊ฒ ํ๋ ๋ฐฐ๊ฒฝ ํ๊ฒฝ์ ์ค๋ช
ํด์ฃผ์ธ์.
|
141 |
-
7. ๊ณ ๊ธ์ค๋ฌ์ด ์์
๊ด๊ณ ํ์ง์ ์ด๋ฏธ์ง๊ฐ ๋๋๋ก ํ๊ฒฝ ์ค๋ช
์ ํด์ฃผ์ธ์.
|
142 |
-
8. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.
|
143 |
|
144 |
-
|
145 |
-
""
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
response = model.generate_content(
|
151 |
-
prompt_request,
|
152 |
-
generation_config=genai.types.GenerationConfig(
|
153 |
-
temperature=0.7,
|
154 |
top_p=0.95,
|
155 |
-
top_k=
|
156 |
-
max_output_tokens=
|
157 |
)
|
158 |
)
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
except Exception as e:
|
164 |
-
|
|
|
165 |
|
166 |
-
def
|
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 |
-
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
202 |
-
visible=False,
|
203 |
-
interactive=True
|
204 |
-
)
|
205 |
-
|
206 |
-
# ์์ฐ ํ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
207 |
-
nature_dropdown = gr.Dropdown(
|
208 |
-
choices=list(NATURE_BACKGROUNDS.keys()),
|
209 |
-
value=list(NATURE_BACKGROUNDS.keys())[0] if NATURE_BACKGROUNDS else None,
|
210 |
-
label="์์ฐ ํ๊ฒฝ ์ ํ",
|
211 |
-
visible=False,
|
212 |
-
interactive=True
|
213 |
-
)
|
214 |
-
|
215 |
-
# ์ค๋ด ํ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
216 |
-
indoor_dropdown = gr.Dropdown(
|
217 |
-
choices=list(INDOOR_BACKGROUNDS.keys()),
|
218 |
-
value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else None,
|
219 |
-
label="์ค๋ด ํ๊ฒฝ ์ ํ",
|
220 |
-
visible=False,
|
221 |
-
interactive=True
|
222 |
-
)
|
223 |
-
|
224 |
-
# ์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
225 |
-
abstract_dropdown = gr.Dropdown(
|
226 |
-
choices=list(ABSTRACT_BACKGROUNDS.keys()),
|
227 |
-
value=list(ABSTRACT_BACKGROUNDS.keys())[0] if ABSTRACT_BACKGROUNDS else None,
|
228 |
-
label="์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ",
|
229 |
-
visible=False,
|
230 |
-
interactive=True
|
231 |
-
)
|
232 |
|
233 |
-
|
234 |
-
additional_info = gr.Textbox(
|
235 |
-
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)",
|
236 |
-
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ์ ์ธ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์ ๋ฑ",
|
237 |
lines=3,
|
238 |
-
|
|
|
239 |
)
|
240 |
|
241 |
-
|
|
|
|
|
242 |
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
return {
|
252 |
-
simple_dropdown: gr.update(visible=(bg_type == "์ฌํ ๋ฐฐ๊ฒฝ")),
|
253 |
-
studio_dropdown: gr.update(visible=(bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ")),
|
254 |
-
nature_dropdown: gr.update(visible=(bg_type == "์์ฐ ํ๊ฒฝ")),
|
255 |
-
indoor_dropdown: gr.update(visible=(bg_type == "์ค๋ด ํ๊ฒฝ")),
|
256 |
-
abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"))
|
257 |
-
}
|
258 |
-
|
259 |
-
# ๋ฐฐ๊ฒฝ ์ ํ ๋ณ๊ฒฝ ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
260 |
-
background_type.change(
|
261 |
-
fn=update_dropdowns,
|
262 |
-
inputs=[background_type],
|
263 |
-
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown]
|
264 |
-
)
|
265 |
-
|
266 |
-
# ์ ํ๋ ๋ฐฐ๊ฒฝ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
267 |
-
def get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract):
|
268 |
-
if bg_type == "์ฌํ ๋ฐฐ๊ฒฝ":
|
269 |
-
return {
|
270 |
-
"category": "์ฌํ ๋ฐฐ๊ฒฝ",
|
271 |
-
"name": simple,
|
272 |
-
"english": SIMPLE_BACKGROUNDS.get(simple, "white background")
|
273 |
-
}
|
274 |
-
elif bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ":
|
275 |
-
return {
|
276 |
-
"category": "์คํ๋์ค ๋ฐฐ๊ฒฝ",
|
277 |
-
"name": studio,
|
278 |
-
"english": STUDIO_BACKGROUNDS.get(studio, "product photography studio")
|
279 |
-
}
|
280 |
-
elif bg_type == "์์ฐ ํ๊ฒฝ":
|
281 |
-
return {
|
282 |
-
"category": "์์ฐ ํ๊ฒฝ",
|
283 |
-
"name": nature,
|
284 |
-
"english": NATURE_BACKGROUNDS.get(nature, "natural environment")
|
285 |
-
}
|
286 |
-
elif bg_type == "์ค๋ด ํ๊ฒฝ":
|
287 |
-
return {
|
288 |
-
"category": "์ค๋ด ํ๊ฒฝ",
|
289 |
-
"name": indoor,
|
290 |
-
"english": INDOOR_BACKGROUNDS.get(indoor, "indoor environment")
|
291 |
-
}
|
292 |
-
elif bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ":
|
293 |
-
return {
|
294 |
-
"category": "์ถ์/ํน์ ๋ฐฐ๊ฒฝ",
|
295 |
-
"name": abstract,
|
296 |
-
"english": ABSTRACT_BACKGROUNDS.get(abstract, "abstract background")
|
297 |
-
}
|
298 |
-
else:
|
299 |
-
return {
|
300 |
-
"category": "๊ธฐ๋ณธ ๋ฐฐ๊ฒฝ",
|
301 |
-
"name": "ํ์ดํธ ๋ฐฐ๊ฒฝ",
|
302 |
-
"english": "white background"
|
303 |
-
}
|
304 |
-
|
305 |
-
# ํ๋กฌํํธ ์์ฑ ํจ์
|
306 |
-
def generate_output(image, bg_type, simple, studio, nature, indoor, abstract, product_text, additional_text):
|
307 |
-
if image is None:
|
308 |
-
gr.Warning("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")
|
309 |
-
return "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ ํ ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
310 |
-
|
311 |
-
product_text = product_text.strip() or "์ ํ"
|
312 |
-
|
313 |
-
# ๋ฐฐ๊ฒฝ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
314 |
-
background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract)
|
315 |
-
|
316 |
-
try:
|
317 |
-
prompt = generate_prompt_with_gemini(product_text, background_info, additional_text)
|
318 |
-
|
319 |
-
if not GEMINI_API_KEY:
|
320 |
-
gr.Warning("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ ์ค์ ํด์ฃผ์ธ์.")
|
321 |
-
prompt = """
|
322 |
-
[Gemini API ํค ๋๋ฝ]
|
323 |
-
API ํค ์ค์ ๋ฐฉ๋ฒ:
|
324 |
-
1. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY="your-api-key"
|
325 |
-
2. ์ฝ๋ ๋ด ์ง์ ์
๋ ฅ: GEMINI_API_KEY = "your-api-key"
|
326 |
-
ํค ๋ฐ๊ธ: https://makersuite.google.com/
|
327 |
-
"""
|
328 |
-
return prompt, image, "API ํค๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค."
|
329 |
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
"""
|
340 |
-
|
341 |
-
return prompt, image, preview
|
342 |
-
except Exception as e:
|
343 |
-
error_msg = f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
344 |
-
gr.Error(error_msg)
|
345 |
-
return error_msg, image, "์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค."
|
346 |
-
|
347 |
-
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
348 |
-
submit_btn.click(
|
349 |
-
fn=generate_output,
|
350 |
-
inputs=[
|
351 |
-
image_input,
|
352 |
-
background_type,
|
353 |
-
simple_dropdown,
|
354 |
-
studio_dropdown,
|
355 |
-
nature_dropdown,
|
356 |
-
indoor_dropdown,
|
357 |
-
abstract_dropdown,
|
358 |
-
product_name,
|
359 |
-
additional_info
|
360 |
-
],
|
361 |
-
outputs=[
|
362 |
-
prompt_output,
|
363 |
-
image_preview,
|
364 |
-
preview_html
|
365 |
-
]
|
366 |
-
)
|
367 |
|
368 |
-
|
|
|
|
|
|
|
|
|
|
|
369 |
|
370 |
-
|
371 |
-
|
372 |
-
app.launch()
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import tempfile
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
+
import logging
|
6 |
+
import re
|
7 |
+
import time
|
8 |
|
9 |
+
from google import genai
|
10 |
+
from google.genai import types
|
11 |
+
from dotenv import load_dotenv
|
12 |
|
13 |
+
load_dotenv()
|
|
|
14 |
|
15 |
+
# Logging setup
|
16 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
17 |
+
logger = logging.getLogger(__name__)
|
18 |
|
19 |
+
def save_binary_file(file_name, data):
|
20 |
+
with open(file_name, "wb") as f:
|
21 |
+
f.write(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
def translate_prompt_to_english(prompt):
|
24 |
+
if not re.search("[๊ฐ-ํฃ]", prompt):
|
25 |
+
return prompt
|
26 |
+
|
27 |
+
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
|
28 |
+
|
29 |
try:
|
30 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
31 |
+
if not api_key:
|
32 |
+
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
33 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
34 |
+
return prompt
|
35 |
+
|
36 |
+
client = genai.Client(api_key=api_key)
|
37 |
+
translation_prompt = f"""
|
38 |
+
Translate the following Korean text to English:
|
39 |
+
|
40 |
+
{prompt}
|
41 |
+
|
42 |
+
IMPORTANT: The token IMAGE_TAG_ONE is a special tag
|
43 |
+
and must be preserved exactly as is in your translation. Do not translate this token.
|
44 |
+
"""
|
45 |
+
|
46 |
+
logger.info(f"Translation prompt: {translation_prompt}")
|
47 |
+
response = client.models.generate_content(
|
48 |
+
model="gemini-2.0-flash",
|
49 |
+
contents=[translation_prompt],
|
50 |
+
config=types.GenerateContentConfig(
|
51 |
+
response_modalities=['Text'],
|
52 |
+
temperature=0.2,
|
53 |
+
top_p=0.95,
|
54 |
+
top_k=40,
|
55 |
+
max_output_tokens=512
|
56 |
+
)
|
57 |
+
)
|
58 |
+
|
59 |
+
translated_text = ""
|
60 |
+
for part in response.candidates[0].content.parts:
|
61 |
+
if hasattr(part, 'text') and part.text:
|
62 |
+
translated_text += part.text
|
63 |
+
|
64 |
+
if translated_text.strip():
|
65 |
+
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
|
66 |
+
logger.info(f"Translated text: {translated_text.strip()}")
|
67 |
+
return translated_text.strip()
|
68 |
+
else:
|
69 |
+
logger.warning("๋ฒ์ญ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ์๋ณธ ํ๋กฌํํธ ์ฌ์ฉ")
|
70 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
71 |
+
return prompt
|
72 |
except Exception as e:
|
73 |
+
logger.exception("๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์:")
|
74 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
75 |
+
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
def preprocess_prompt(prompt, image1):
|
78 |
+
has_img1 = image1 is not None
|
|
|
|
|
|
|
79 |
|
80 |
+
if "#1" in prompt and not has_img1:
|
81 |
+
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
82 |
+
else:
|
83 |
+
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
84 |
|
85 |
+
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
86 |
+
return prompt
|
87 |
|
88 |
+
def generate_with_images(prompt, images, variation_index=0):
|
89 |
+
try:
|
90 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
91 |
+
if not api_key:
|
92 |
+
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
+
client = genai.Client(api_key=api_key)
|
95 |
+
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
96 |
|
97 |
+
variation_suffixes = [
|
98 |
+
" Create this as the first variation. Do not add any text, watermarks, or labels to the image.",
|
99 |
+
" Create this as the second variation with more vivid colors. Do not add any text, watermarks, or labels to the image.",
|
100 |
+
" Create this as the third variation with a more creative style. Do not add any text, watermarks, or labels to the image.",
|
101 |
+
" Create this as the fourth variation with enhanced details. Do not add any text, watermarks, or labels to the image."
|
102 |
+
]
|
103 |
+
|
104 |
+
if variation_index < len(variation_suffixes):
|
105 |
+
prompt = prompt + variation_suffixes[variation_index]
|
106 |
+
else:
|
107 |
+
prompt = prompt + " Do not add any text, watermarks, or labels to the image."
|
108 |
|
109 |
+
contents = [prompt]
|
110 |
+
for idx, img in enumerate(images, 1):
|
111 |
+
if img is not None:
|
112 |
+
contents.append(img)
|
113 |
+
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
response = client.models.generate_content(
|
116 |
+
model="gemini-2.0-flash-exp-image-generation",
|
117 |
+
contents=contents,
|
118 |
+
config=types.GenerateContentConfig(
|
119 |
+
response_modalities=['Text', 'Image'],
|
120 |
+
temperature=1,
|
|
|
|
|
|
|
|
|
121 |
top_p=0.95,
|
122 |
+
top_k=40,
|
123 |
+
max_output_tokens=8192
|
124 |
)
|
125 |
)
|
126 |
+
|
127 |
+
# ์์ ํ์ผ์ ํญ์ JPG ํ์ฅ์๋ก ์์ฑ
|
128 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
|
129 |
+
temp_path = tmp.name
|
130 |
+
result_text = ""
|
131 |
+
image_found = False
|
132 |
+
for part in response.candidates[0].content.parts:
|
133 |
+
if hasattr(part, 'text') and part.text:
|
134 |
+
result_text += part.text
|
135 |
+
logger.info(f"์๋ต ํ
์คํธ: {part.text}")
|
136 |
+
elif hasattr(part, 'inline_data') and part.inline_data:
|
137 |
+
save_binary_file(temp_path, part.inline_data.data)
|
138 |
+
image_found = True
|
139 |
+
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
140 |
+
if not image_found:
|
141 |
+
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
142 |
+
|
143 |
+
result_img = Image.open(temp_path)
|
144 |
+
if result_img.mode == "RGBA":
|
145 |
+
result_img = result_img.convert("RGB") # JPG๋ ํฌ๋ช
๋๋ฅผ ์ง์ํ์ง ์์ผ๋ฏ๋ก RGB๋ก ๋ณํ
|
146 |
+
|
147 |
+
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ JPG๋ก ์ ์ฅ
|
148 |
+
result_img.save(temp_path, format="JPEG", quality=95)
|
149 |
+
|
150 |
+
# ํ์ผ ๊ฒฝ๋ก ๋ฐํ
|
151 |
+
return temp_path, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
152 |
except Exception as e:
|
153 |
+
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
154 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
155 |
|
156 |
+
def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3):
|
157 |
+
retry_count = 0
|
158 |
+
last_error = None
|
159 |
+
|
160 |
+
while retry_count < max_retries:
|
161 |
+
try:
|
162 |
+
images = [image1]
|
163 |
+
valid_images = [img for img in images if img is not None]
|
164 |
+
if not valid_images:
|
165 |
+
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
166 |
+
|
167 |
+
if prompt and prompt.strip():
|
168 |
+
processed_prompt = preprocess_prompt(prompt, image1)
|
169 |
+
if re.search("[๊ฐ-ํฃ]", processed_prompt):
|
170 |
+
final_prompt = translate_prompt_to_english(processed_prompt)
|
171 |
+
else:
|
172 |
+
final_prompt = processed_prompt
|
173 |
+
else:
|
174 |
+
final_prompt = "Please creatively transform this image into a more vivid and artistic version. Do not include any text or watermarks in the generated image."
|
175 |
+
logger.info("Default prompt generated for single image")
|
176 |
+
|
177 |
+
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
178 |
+
if result_img is not None:
|
179 |
+
return result_img, status, final_prompt
|
180 |
+
else:
|
181 |
+
last_error = status
|
182 |
+
retry_count += 1
|
183 |
+
logger.warning(f"์ด๋ฏธ์ง ์์ฑ ์คํจ, ์ฌ์๋ {retry_count}/{max_retries}: {status}")
|
184 |
+
time.sleep(1)
|
185 |
+
except Exception as e:
|
186 |
+
last_error = str(e)
|
187 |
+
retry_count += 1
|
188 |
+
logger.exception(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์, ์ฌ์๋ {retry_count}/{max_retries}:")
|
189 |
+
time.sleep(1)
|
190 |
+
|
191 |
+
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt
|
192 |
+
|
193 |
+
def generate_multiple_images(image1, prompt, progress=gr.Progress()):
|
194 |
+
results = []
|
195 |
+
statuses = []
|
196 |
+
prompts = []
|
197 |
+
|
198 |
+
num_images = 4
|
199 |
+
max_retries = 3
|
200 |
+
|
201 |
+
progress(0, desc="์ด๋ฏธ์ง ์์ฑ ์ค๋น ์ค...")
|
202 |
+
|
203 |
+
for i in range(num_images):
|
204 |
+
progress((i / num_images), desc=f"{i+1}/{num_images} ์ด๋ฏธ์ง ์์ฑ ์ค...")
|
205 |
+
result_img, status, final_prompt = process_images_with_prompt(image1, prompt, i, max_retries)
|
206 |
|
207 |
+
if result_img is not None:
|
208 |
+
results.append(result_img)
|
209 |
+
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
210 |
+
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
211 |
+
else:
|
212 |
+
results.append(None)
|
213 |
+
statuses.append(f"์ด๋ฏธ์ง #{i+1} ์์ฑ ์คํจ: {status}")
|
214 |
+
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
215 |
|
216 |
+
time.sleep(1)
|
217 |
+
|
218 |
+
progress(1.0, desc="์ด๋ฏธ์ง ์์ฑ ์๋ฃ!")
|
219 |
+
|
220 |
+
while len(results) < 4:
|
221 |
+
results.append(None)
|
222 |
+
|
223 |
+
combined_status = "\n".join(statuses)
|
224 |
+
combined_prompts = "\n".join(prompts)
|
225 |
+
|
226 |
+
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
227 |
+
|
228 |
+
# UI ๊ตฌ์ฑ
|
229 |
+
with gr.Blocks() as demo:
|
230 |
+
with gr.Column():
|
231 |
+
with gr.Group():
|
232 |
+
with gr.Column():
|
233 |
+
with gr.Row():
|
234 |
+
image1_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋", image_mode="RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
+
prompt_input = gr.Textbox(
|
|
|
|
|
|
|
237 |
lines=3,
|
238 |
+
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์. '#1'์ผ๋ก ์
๋ก๋ํ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.",
|
239 |
+
label="ํ๋กฌํํธ ์
๋ ฅ"
|
240 |
)
|
241 |
|
242 |
+
with gr.Row():
|
243 |
+
submit_single_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (1์ฅ)')
|
244 |
+
submit_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (4์ฅ)')
|
245 |
|
246 |
+
with gr.Column():
|
247 |
+
with gr.Row():
|
248 |
+
with gr.Column():
|
249 |
+
output_image1 = gr.Image(label="์ด๋ฏธ์ง #1", type="filepath")
|
250 |
+
output_image3 = gr.Image(label="์ด๋ฏธ์ง #3", type="filepath")
|
251 |
+
with gr.Column():
|
252 |
+
output_image2 = gr.Image(label="์ด๋ฏธ์ง #2", type="filepath")
|
253 |
+
output_image4 = gr.Image(label="์ด๋ฏธ์ง #4", type="filepath")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
|
255 |
+
output_text = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=2)
|
256 |
+
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", lines=2)
|
257 |
+
|
258 |
+
# ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
259 |
+
submit_single_btn.click(
|
260 |
+
fn=lambda image1, prompt: process_images_with_prompt(image1, prompt, 0),
|
261 |
+
inputs=[image1_input, prompt_input],
|
262 |
+
outputs=[output_image1, output_text, prompt_display],
|
263 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
+
# 4์ฅ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
266 |
+
submit_btn.click(
|
267 |
+
fn=generate_multiple_images,
|
268 |
+
inputs=[image1_input, prompt_input],
|
269 |
+
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
270 |
+
)
|
271 |
|
272 |
+
demo.queue()
|
273 |
+
demo.launch()
|
|