Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ 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
|
@@ -11,7 +12,7 @@ from google.genai import types
|
|
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 |
|
@@ -19,6 +20,14 @@ 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๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
@@ -32,9 +41,12 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
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 = []
|
@@ -43,7 +55,8 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
43 |
parts.append(types.Part.from_text(text=prompt))
|
44 |
|
45 |
# ์ฌ์ฉ ๊ฐ๋ฅํ ์ด๋ฏธ์ง ์ถ๊ฐ
|
46 |
-
|
|
|
47 |
if img is not None:
|
48 |
# PIL ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ดํธ๋ก ๋ณํ
|
49 |
with tempfile.NamedTemporaryFile(suffix=".png") as tmp:
|
@@ -54,6 +67,11 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
54 |
|
55 |
# ์ด๋ฏธ์ง๋ฅผ ํํธ๋ก ์ถ๊ฐ
|
56 |
parts.append(types.Part.from_data(data=image_bytes, mime_type="image/png"))
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# ์์ฑ ์ค์
|
59 |
generate_content_config = types.GenerateContentConfig(
|
@@ -66,6 +84,7 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
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)],
|
@@ -73,9 +92,15 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
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)
|
@@ -88,39 +113,72 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
88 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
89 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
90 |
|
91 |
-
#
|
92 |
with gr.Blocks() as demo:
|
93 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
with gr.Row():
|
96 |
with gr.Column():
|
97 |
# 3๊ฐ์ ์ด๋ฏธ์ง ์
๋ ฅ
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
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)
|
|
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
import logging
|
6 |
+
import re
|
7 |
|
8 |
from google import genai
|
9 |
from google.genai import types
|
|
|
12 |
from dotenv import load_dotenv
|
13 |
load_dotenv()
|
14 |
|
15 |
+
# ๋ก๊น
์ค์
|
16 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
|
|
20 |
with open(file_name, "wb") as f:
|
21 |
f.write(data)
|
22 |
|
23 |
+
def preprocess_prompt(prompt):
|
24 |
+
"""
|
25 |
+
ํ๋กฌํํธ์ ์๋ #1, #2, #3 ์ฐธ์กฐ๋ฅผ ์ ์ ํ ํ
์คํธ๋ก ๋ณํ
|
26 |
+
"""
|
27 |
+
# #1, #2, #3 ์ฐธ์กฐ๋ฅผ ํ
์คํธ๋ก ๋ณํ
|
28 |
+
processed_prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง").replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง").replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
29 |
+
return processed_prompt
|
30 |
+
|
31 |
def process_images_with_prompt(image1, image2, image3, prompt):
|
32 |
"""
|
33 |
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
|
|
41 |
# Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
42 |
client = genai.Client(api_key=api_key)
|
43 |
|
44 |
+
# ํ๋กฌํํธ ์ฒ๋ฆฌ
|
45 |
if not prompt or not prompt.strip():
|
46 |
prompt = "์ด ์ด๋ฏธ์ง๋ค์ ํ์ฉํ์ฌ ์๋ก์ด ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
47 |
+
else:
|
48 |
+
# #1, #2, #3 ์ฐธ์กฐ ์ฒ๋ฆฌ
|
49 |
+
prompt = preprocess_prompt(prompt)
|
50 |
|
51 |
# ์ปจํ
์ธ ๋ฆฌ์คํธ ์์ฑ (์ด๋ฏธ์ง์ ํ๋กฌํํธ ๊ฒฐํฉ)
|
52 |
parts = []
|
|
|
55 |
parts.append(types.Part.from_text(text=prompt))
|
56 |
|
57 |
# ์ฌ์ฉ ๊ฐ๋ฅํ ์ด๋ฏธ์ง ์ถ๊ฐ
|
58 |
+
images_added = 0
|
59 |
+
for idx, img in enumerate([image1, image2, image3], 1):
|
60 |
if img is not None:
|
61 |
# PIL ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ดํธ๋ก ๋ณํ
|
62 |
with tempfile.NamedTemporaryFile(suffix=".png") as tmp:
|
|
|
67 |
|
68 |
# ์ด๋ฏธ์ง๋ฅผ ํํธ๋ก ์ถ๊ฐ
|
69 |
parts.append(types.Part.from_data(data=image_bytes, mime_type="image/png"))
|
70 |
+
images_added += 1
|
71 |
+
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
72 |
+
|
73 |
+
if images_added == 0:
|
74 |
+
return None, "์ ์ด๋ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
75 |
|
76 |
# ์์ฑ ์ค์
|
77 |
generate_content_config = types.GenerateContentConfig(
|
|
|
84 |
temp_path = tmp.name
|
85 |
|
86 |
# Gemini ๋ชจ๋ธ๋ก ์์ฒญ ์ ์ก
|
87 |
+
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}")
|
88 |
response = client.models.generate_content(
|
89 |
model="gemini-2.0-flash-exp-image-generation",
|
90 |
contents=[types.Content(role="user", parts=parts)],
|
|
|
92 |
)
|
93 |
|
94 |
# ์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ
|
95 |
+
image_found = False
|
96 |
for part in response.candidates[0].content.parts:
|
97 |
if hasattr(part, 'inline_data') and part.inline_data:
|
98 |
save_binary_file(temp_path, part.inline_data.data)
|
99 |
+
image_found = True
|
100 |
+
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
101 |
+
|
102 |
+
if not image_found:
|
103 |
+
return None, "API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ๋ค๋ฅธ ํ๋กฌํํธ๋ก ์๋ํด๋ณด์ธ์."
|
104 |
|
105 |
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐํ
|
106 |
result_img = Image.open(temp_path)
|
|
|
113 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
114 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
115 |
|
116 |
+
# Gradio ์ธํฐํ์ด์ค
|
117 |
with gr.Blocks() as demo:
|
118 |
+
gr.HTML(
|
119 |
+
"""
|
120 |
+
<div style="text-align: center; margin-bottom: 1rem;">
|
121 |
+
<h1>๊ฐ๋จํ ์ด๋ฏธ์ง ์์ฑ๊ธฐ</h1>
|
122 |
+
<p>์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์. #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.</p>
|
123 |
+
</div>
|
124 |
+
"""
|
125 |
+
)
|
126 |
|
127 |
with gr.Row():
|
128 |
with gr.Column():
|
129 |
# 3๊ฐ์ ์ด๋ฏธ์ง ์
๋ ฅ
|
130 |
+
with gr.Row():
|
131 |
+
image1_input = gr.Image(type="pil", label="#1", image_mode="RGB")
|
132 |
+
image2_input = gr.Image(type="pil", label="#2", image_mode="RGB")
|
133 |
+
image3_input = gr.Image(type="pil", label="#3", image_mode="RGB")
|
134 |
|
135 |
# ํ๋กฌํํธ ์
๋ ฅ
|
136 |
prompt_input = gr.Textbox(
|
137 |
lines=3,
|
138 |
+
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์. ์: '#1๊ณผ #2๋ฅผ ํฉ์ฑํด ์ฃผ์ธ์' ๋๋ '#1์ ์ธ๋ฌผ์ #3์ ๋ฐฐ๊ฒฝ์ ๋ฃ์ด์ฃผ์ธ์'",
|
139 |
label="ํ๋กฌํํธ"
|
140 |
)
|
141 |
|
142 |
+
# ์์ ํ๋กฌํํธ ๋ฒํผ๋ค
|
143 |
+
with gr.Row():
|
144 |
+
prompt1_btn = gr.Button("์์: #1 + #2 ํฉ์ฑ")
|
145 |
+
prompt2_btn = gr.Button("์์: #1์ ์ธ๋ฌผ + #2์ ๋ฐฐ๊ฒฝ")
|
146 |
+
prompt3_btn = gr.Button("์์: #3 ์คํ์ผ๋ก #1 ๋ณํ")
|
147 |
+
|
148 |
# ์์ฑ ๋ฒํผ
|
149 |
+
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ", variant="primary")
|
150 |
|
151 |
with gr.Column():
|
152 |
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
153 |
output_image = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง")
|
154 |
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
155 |
|
156 |
+
# ์์ ํ๋กฌํํธ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
157 |
+
prompt1_btn.click(lambda: "#1๊ณผ #2๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด ์ฃผ์ธ์", outputs=prompt_input)
|
158 |
+
prompt2_btn.click(lambda: "#1์ ์ธ๋ฌผ์ #2์ ๋ฐฐ๊ฒฝ์ ๋ฃ์ด์ฃผ์ธ์", outputs=prompt_input)
|
159 |
+
prompt3_btn.click(lambda: "#3์ ์คํ์ผ๋ก #1์ ๋ณํํด ์ฃผ์ธ์", outputs=prompt_input)
|
160 |
+
|
161 |
+
# ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
162 |
submit_btn.click(
|
163 |
fn=process_images_with_prompt,
|
164 |
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
165 |
outputs=[output_image, output_text],
|
166 |
)
|
167 |
|
168 |
+
gr.HTML(
|
169 |
+
"""
|
170 |
+
<div style="margin-top: 1rem; padding: 1rem; background-color: #f8f9fa; border-radius: 0.5rem;">
|
171 |
+
<h3>์ฌ์ฉ ๋ฐฉ๋ฒ:</h3>
|
172 |
+
<ul>
|
173 |
+
<li>์ด๋ฏธ์ง๋ฅผ #1, #2, #3 ์ฌ๋กฏ์ ์
๋ก๋ํ์ธ์</li>
|
174 |
+
<li>ํ๋กฌํํธ์์ #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค</li>
|
175 |
+
<li>์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์์ฃผ ์ฌ์ฉํ๋ ํ๋กฌํํธ ํ
ํ๋ฆฟ์ ์ ์ฉํ ์ ์์ต๋๋ค</li>
|
176 |
+
<li>๋ชจ๋ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ ํ์๋ ์์ผ๋ฉฐ, ํ์ํ ์ด๋ฏธ์ง๋ง ์
๋ก๋ํ์ธ์</li>
|
177 |
+
</ul>
|
178 |
+
</div>
|
179 |
+
"""
|
180 |
+
)
|
181 |
+
|
182 |
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
183 |
if __name__ == "__main__":
|
184 |
demo.launch(share=True)
|