Update app.py
Browse files
app.py
CHANGED
@@ -1,223 +1,126 @@
|
|
1 |
-
import json
|
2 |
import os
|
3 |
-
import time
|
4 |
import tempfile
|
5 |
from PIL import Image
|
6 |
import gradio as gr
|
7 |
import logging
|
8 |
-
import io
|
9 |
|
10 |
from google import genai
|
11 |
from google.genai import types
|
12 |
|
13 |
-
#
|
14 |
from dotenv import load_dotenv
|
15 |
load_dotenv()
|
16 |
|
17 |
-
# ๋ก๊น
์ค์
|
18 |
-
logging.basicConfig(level=logging.
|
19 |
-
format='%(asctime)s - %(levelname)s - %(message)s')
|
20 |
logger = logging.getLogger(__name__)
|
21 |
|
22 |
-
|
23 |
def save_binary_file(file_name, data):
|
24 |
-
logger.debug(f"ํ์ผ์ ์ด์ง ๋ฐ์ดํฐ ์ ์ฅ ์ค: {file_name}")
|
25 |
with open(file_name, "wb") as f:
|
26 |
f.write(data)
|
27 |
-
logger.debug(f"ํ์ผ ์ ์ฅ ์๋ฃ: {file_name}")
|
28 |
-
|
29 |
|
30 |
-
def
|
31 |
-
|
32 |
-
|
|
|
33 |
try:
|
34 |
-
# API
|
35 |
-
|
36 |
-
if
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
logger.debug("Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์๋ฃ.")
|
44 |
-
|
45 |
-
# ํ๋กฌํํธ ์ฒ๋ฆฌ
|
46 |
if not prompt or not prompt.strip():
|
47 |
-
prompt = "
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
logger.debug(f"์ปจํ
์ธ ๊ฐ์ฒด ์์ฑ ์๋ฃ: {len(parts)} ์์ดํ
")
|
59 |
-
|
60 |
# ์์ฑ ์ค์
|
61 |
generate_content_config = types.GenerateContentConfig(
|
62 |
temperature=1,
|
63 |
-
|
64 |
-
top_k=40,
|
65 |
-
max_output_tokens=8192,
|
66 |
-
response_modalities=["image", "text"],
|
67 |
)
|
68 |
-
|
69 |
-
|
70 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
71 |
temp_path = tmp.name
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
contents=[
|
78 |
-
types.Content(
|
79 |
-
role="user",
|
80 |
-
parts=parts,
|
81 |
-
),
|
82 |
-
],
|
83 |
config=generate_content_config,
|
84 |
)
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
response_text = ""
|
91 |
-
|
92 |
-
for chunk in response_stream:
|
93 |
-
logger.debug(f"chunk ์์ : {chunk}")
|
94 |
-
|
95 |
-
# ์๋ต ๊ฒ์ฆ
|
96 |
-
if not hasattr(chunk, 'candidates') or not chunk.candidates or len(chunk.candidates) == 0:
|
97 |
-
logger.warning("chunk์ candidates๊ฐ ์์ต๋๋ค. ๊ฑด๋๋๋๋ค.")
|
98 |
-
continue
|
99 |
-
|
100 |
-
if not hasattr(chunk.candidates[0], 'content') or chunk.candidates[0].content is None:
|
101 |
-
logger.warning("chunk.candidates[0]์ content๊ฐ ์์ต๋๋ค. ๊ฑด๋๋๋๋ค.")
|
102 |
-
continue
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
response_text += part.text
|
111 |
-
logger.info(f"์์ ๋ ํ
์คํธ: {part.text}")
|
112 |
-
elif hasattr(part, 'inline_data') and part.inline_data:
|
113 |
-
save_binary_file(temp_path, part.inline_data.data)
|
114 |
-
logger.info(f"MIME ํ์
{part.inline_data.mime_type}์ ํ์ผ์ด ์ ์ฅ๋จ: {temp_path}")
|
115 |
-
image_saved = True
|
116 |
-
|
117 |
-
if not image_saved:
|
118 |
-
logger.warning("์ด๋ฏธ์ง๊ฐ ์์ฑ๋์ง ์์์ต๋๋ค.")
|
119 |
-
return None, response_text or "์ด๋ฏธ์ง๊ฐ ์์ฑ๋์ง ์์์ต๋๋ค. ๋ค๋ฅธ ํ๋กฌํํธ๋ก ์๋ํด๋ณด์ธ์."
|
120 |
-
|
121 |
-
logger.debug("์ด๋ฏธ์ง ์์ฑ ์๋ฃ.")
|
122 |
-
return temp_path, response_text
|
123 |
-
|
124 |
except Exception as e:
|
125 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
126 |
-
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
127 |
-
|
128 |
-
|
129 |
-
def process_image_generation(person_pil, product_pil, background_pil, prompt):
|
130 |
-
logger.debug(f"process_image_generation ํจ์ ์์ - ํ๋กฌํํธ: '{prompt}'")
|
131 |
-
try:
|
132 |
-
# ์ด๋ฏธ์ง๋ค์ด ์ ๊ณต๋์๋ค๋ฉด ๊ธฐ์กด ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ
|
133 |
-
if person_pil is not None or product_pil is not None or background_pil is not None:
|
134 |
-
return process_images_and_prompt(person_pil, product_pil, background_pil, prompt)
|
135 |
-
|
136 |
-
# ์ด๋ฏธ์ง ์์ด ํ๋กฌํํธ๋ง ์๋ ๊ฒฝ์ฐ
|
137 |
-
result_path, response_text = generate_image_from_prompt(prompt)
|
138 |
-
|
139 |
-
# ์ด๋ฏธ์ง ๋ฐํ ๋ฐ ์ฒ๋ฆฌ
|
140 |
-
if result_path:
|
141 |
-
logger.debug(f"์ด๋ฏธ์ง ์์ฑ ์๋ฃ. ๊ฒฝ๋ก: {result_path}")
|
142 |
-
try:
|
143 |
-
result_img = Image.open(result_path)
|
144 |
-
if result_img.mode == "RGBA":
|
145 |
-
result_img = result_img.convert("RGB")
|
146 |
-
|
147 |
-
return [result_img], response_text or "์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค."
|
148 |
-
except Exception as e:
|
149 |
-
logger.exception(f"๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ก๋ ์ค ์ค๋ฅ: {str(e)}")
|
150 |
-
return [], f"๊ฒฐ๊ณผ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {str(e)}"
|
151 |
-
else:
|
152 |
-
logger.error("generate_image_from_prompt ํจ์์์ None ๋ฐํ๋จ.")
|
153 |
-
return [], response_text or "์ด๋ฏธ์ง ์์ฑ์ ์คํจํ์ต๋๋ค. ๋ค๋ฅธ ํ๋กฌํํธ๋ก ์๋ํด๋ณด์ธ์."
|
154 |
|
155 |
-
|
156 |
-
logger.exception("process_image_generation ํจ์์์ ์ค๋ฅ ๋ฐ์:")
|
157 |
-
return [], f"์ค๋ฅ ๋ฐ์: {str(e)}" # ์ค๋ฅ ์ ๋น ๋ฆฌ์คํธ์ ์ค๋ฅ ๋ฉ์์ง ๋ฐํ
|
158 |
-
|
159 |
-
|
160 |
-
# --- Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ (๊ธฐ์กด ์ฝ๋๋ ๋์ผํ๊ฒ ์ ์ง) ---
|
161 |
with gr.Blocks() as demo:
|
162 |
-
gr.HTML(
|
163 |
-
|
164 |
-
<div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
|
165 |
-
<div style="background-color: var(--block-background-fill); border-radius: 8px">
|
166 |
-
<img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" style="width: 100px; height: 100px;">
|
167 |
-
</div>
|
168 |
-
<div>
|
169 |
-
<h1>Gemini๋ฅผ ์ด์ฉํ ์ด๋ฏธ์ง ์์ฑ</h1>
|
170 |
-
<p>์ฌ๋, ์ํ, ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ํฉ์ฑํ๊ฑฐ๋ ํ
์คํธ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.</p>
|
171 |
-
</div>
|
172 |
-
</div>
|
173 |
-
"""
|
174 |
-
)
|
175 |
-
gr.Markdown("์ฌ๋ ์ด๋ฏธ์ง, ์ํ ์ด๋ฏธ์ง, ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ฑฐ๋ ํ
์คํธ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.")
|
176 |
-
|
177 |
with gr.Row():
|
178 |
with gr.Column():
|
179 |
-
# ์ด๋ฏธ์ง ์
๋ ฅ
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
|
184 |
-
# ํ๋กฌํํธ ์
๋ ฅ
|
185 |
prompt_input = gr.Textbox(
|
186 |
lines=3,
|
187 |
-
placeholder="
|
188 |
-
label="
|
189 |
)
|
|
|
|
|
190 |
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ")
|
191 |
|
192 |
with gr.Column():
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
196 |
submit_btn.click(
|
197 |
-
fn=
|
198 |
-
inputs=[
|
199 |
-
outputs=[
|
200 |
)
|
201 |
|
202 |
-
|
203 |
-
<div style="margin-top: 20px; padding: 10px; background-color: #f8f9fa; border-radius: 8px;">
|
204 |
-
<h3>์ฌ์ฉ ๋ฐฉ๋ฒ:</h3>
|
205 |
-
<ul>
|
206 |
-
<li><strong>ํ
์คํธ๋ก ์ด๋ฏธ์ง ์์ฑ:</strong> ์ค๋ช
๋ง ์
๋ ฅํ๊ณ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ์ ๋๋ฅด์ธ์.</li>
|
207 |
-
<li><strong>์ด๋ฏธ์ง ํฉ์ฑ:</strong> ์ฌ๋, ์ํ, ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์ ํ์ ์ผ๋ก ์
๋ก๋ํ๊ณ ํฉ์ฑ ๋ฐฉ๋ฒ์ ์ค๋ช
ํด์ฃผ์ธ์.</li>
|
208 |
-
<li><strong>ํ๋กฌํํธ ํ:</strong> ๊ตฌ์ฒด์ ์ด๊ณ ์์ํ ์ค๋ช
์ผ์๋ก ๋ ์ข์ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.</li>
|
209 |
-
<li><strong>์์ ํ๋กฌํํธ:</strong>
|
210 |
-
<ul>
|
211 |
-
<li>ํ์ ํ ์นดํ์์ ๋
ธํธ๋ถ์ผ๋ก ์์
ํ๋ ํฌ๋ฆฌ์์ดํฐ</li>
|
212 |
-
<li>ํด๋ณ๊ฐ์์ ๊ณ ๊ธ ์ ๊ธ๋ผ์ค๋ฅผ ๋ผ๊ณ ํฌ์ฆ ์ทจํ๋ ๋ชจ๋ธ</li>
|
213 |
-
<li>ํ๋์ ์ธ ์ฃผ๋ฐฉ์์ ์ต์ ๋ธ๋ ๋๋ก ์ค๋ฌด๋๋ฅผ ๋ง๋๋ ์๋ฆฌ์ฌ</li>
|
214 |
-
</ul>
|
215 |
-
</li>
|
216 |
-
<li><strong>์ธ์ด ํ:</strong> ํ๊ตญ์ด์ ์์ด๋ฅผ ํผํฉํด ์ฌ์ฉํ๋ฉด ๋ ์ ํํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.</li>
|
217 |
-
</ul>
|
218 |
-
</div>
|
219 |
-
""")
|
220 |
-
|
221 |
-
# --- ์คํ ---
|
222 |
if __name__ == "__main__":
|
223 |
demo.launch(share=True)
|
|
|
|
|
1 |
import os
|
|
|
2 |
import tempfile
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
import logging
|
|
|
6 |
|
7 |
from google import genai
|
8 |
from google.genai import types
|
9 |
|
10 |
+
# ํ๊ฒฝ๋ณ์ ๋ก๋
|
11 |
from dotenv import load_dotenv
|
12 |
load_dotenv()
|
13 |
|
14 |
+
# ๊ฐ๋จํ ๋ก๊น
์ค์
|
15 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
16 |
logger = logging.getLogger(__name__)
|
17 |
|
|
|
18 |
def save_binary_file(file_name, data):
|
|
|
19 |
with open(file_name, "wb") as f:
|
20 |
f.write(data)
|
|
|
|
|
21 |
|
22 |
+
def process_images_with_prompt(image1, image2, image3, prompt):
|
23 |
+
"""
|
24 |
+
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
25 |
+
"""
|
26 |
try:
|
27 |
+
# API ํค ํ์ธ
|
28 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
29 |
+
if not api_key:
|
30 |
+
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
31 |
+
|
32 |
+
# Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
33 |
+
client = genai.Client(api_key=api_key)
|
34 |
+
|
35 |
+
# ํ๋กฌํํธ ๊ธฐ๋ณธ๊ฐ ์ค์
|
|
|
|
|
|
|
36 |
if not prompt or not prompt.strip():
|
37 |
+
prompt = "์ด ์ด๋ฏธ์ง๋ค์ ํ์ฉํ์ฌ ์๋ก์ด ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
38 |
|
39 |
+
# ์ปจํ
์ธ ๋ฆฌ์คํธ ์์ฑ (์ด๋ฏธ์ง์ ํ๋กฌํํธ ๊ฒฐํฉ)
|
40 |
+
parts = []
|
41 |
+
|
42 |
+
# ํ
์คํธ ํ๋กฌํํธ ์ถ๊ฐ
|
43 |
+
parts.append(types.Part.from_text(text=prompt))
|
44 |
+
|
45 |
+
# ์ฌ์ฉ ๊ฐ๋ฅํ ์ด๋ฏธ์ง ์ถ๊ฐ
|
46 |
+
for img in [image1, image2, image3]:
|
47 |
+
if img is not None:
|
48 |
+
# PIL ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ดํธ๋ก ๋ณํ
|
49 |
+
with tempfile.NamedTemporaryFile(suffix=".png") as tmp:
|
50 |
+
img_path = tmp.name
|
51 |
+
img.save(img_path, format="PNG")
|
52 |
+
with open(img_path, "rb") as f:
|
53 |
+
image_bytes = f.read()
|
54 |
+
|
55 |
+
# ์ด๋ฏธ์ง๋ฅผ ํํธ๋ก ์ถ๊ฐ
|
56 |
+
parts.append(types.Part.from_data(data=image_bytes, mime_type="image/png"))
|
57 |
|
|
|
|
|
58 |
# ์์ฑ ์ค์
|
59 |
generate_content_config = types.GenerateContentConfig(
|
60 |
temperature=1,
|
61 |
+
response_modalities=["image"],
|
|
|
|
|
|
|
62 |
)
|
63 |
+
|
64 |
+
# ์์ ํ์ผ ์์ฑ
|
65 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
66 |
temp_path = tmp.name
|
67 |
+
|
68 |
+
# Gemini ๋ชจ๋ธ๋ก ์์ฒญ ์ ์ก
|
69 |
+
response = client.models.generate_content(
|
70 |
+
model="gemini-2.0-flash-exp-image-generation",
|
71 |
+
contents=[types.Content(role="user", parts=parts)],
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
config=generate_content_config,
|
73 |
)
|
74 |
|
75 |
+
# ์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ
|
76 |
+
for part in response.candidates[0].content.parts:
|
77 |
+
if hasattr(part, 'inline_data') and part.inline_data:
|
78 |
+
save_binary_file(temp_path, part.inline_data.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐํ
|
81 |
+
result_img = Image.open(temp_path)
|
82 |
+
if result_img.mode == "RGBA":
|
83 |
+
result_img = result_img.convert("RGB")
|
84 |
|
85 |
+
return result_img, "์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค."
|
86 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
except Exception as e:
|
88 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
89 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
+
# ๊ฐ์ํ๋ Gradio ์ธํฐํ์ด์ค
|
|
|
|
|
|
|
|
|
|
|
92 |
with gr.Blocks() as demo:
|
93 |
+
gr.HTML("<h1>๊ฐ๋จํ ์ด๋ฏธ์ง ์์ฑ๊ธฐ</h1><p>์ด๋ฏธ์ง 3๊ฐ์ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์</p>")
|
94 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
with gr.Row():
|
96 |
with gr.Column():
|
97 |
+
# 3๊ฐ์ ์ด๋ฏธ์ง ์
๋ ฅ
|
98 |
+
image1_input = gr.Image(type="pil", label="์ด๋ฏธ์ง 1", image_mode="RGB")
|
99 |
+
image2_input = gr.Image(type="pil", label="์ด๋ฏธ์ง 2", image_mode="RGB")
|
100 |
+
image3_input = gr.Image(type="pil", label="์ด๋ฏธ์ง 3", image_mode="RGB")
|
101 |
|
102 |
+
# ํ๋กฌํํธ ์
๋ ฅ
|
103 |
prompt_input = gr.Textbox(
|
104 |
lines=3,
|
105 |
+
placeholder="์ด ์ด๋ฏธ์ง๋ค์ ์ด๋ป๊ฒ ๋ณํํ ์ง ์ค๋ช
ํด์ฃผ์ธ์",
|
106 |
+
label="ํ๋กฌํํธ"
|
107 |
)
|
108 |
+
|
109 |
+
# ์์ฑ ๋ฒํผ
|
110 |
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ")
|
111 |
|
112 |
with gr.Column():
|
113 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
114 |
+
output_image = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง")
|
115 |
+
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
116 |
+
|
117 |
+
# ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
118 |
submit_btn.click(
|
119 |
+
fn=process_images_with_prompt,
|
120 |
+
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
121 |
+
outputs=[output_image, output_text],
|
122 |
)
|
123 |
|
124 |
+
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
if __name__ == "__main__":
|
126 |
demo.launch(share=True)
|