Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from PIL import Image
|
|
4 |
import gradio as gr
|
5 |
import logging
|
6 |
import re
|
7 |
-
import io
|
8 |
|
9 |
from google import genai
|
10 |
from google.genai import types
|
@@ -25,6 +24,7 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
25 |
"""
|
26 |
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
|
27 |
"""
|
|
|
28 |
# ์ด๋ฏธ์ง ์๋ ์ฐธ์กฐ ํ์ธ ๋ฐ ์ฒ๋ฆฌ
|
29 |
has_img1 = image1 is not None
|
30 |
has_img2 = image2 is not None
|
@@ -96,9 +96,9 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
96 |
|
97 |
return prompt
|
98 |
|
99 |
-
def
|
100 |
"""
|
101 |
-
|
102 |
"""
|
103 |
try:
|
104 |
# API ํค ํ์ธ
|
@@ -109,86 +109,91 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
109 |
# Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
110 |
client = genai.Client(api_key=api_key)
|
111 |
|
112 |
-
|
113 |
-
images = [image1, image2, image3]
|
114 |
-
valid_images = [img for img in images if img is not None]
|
115 |
|
116 |
-
|
117 |
-
|
|
|
118 |
|
119 |
-
#
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
else:
|
132 |
-
# ํ๋กฌํํธ ์ ์ฒ๋ฆฌ ๋ฐ ๊ธฐ๋ฅ ๋ช
๋ น ํด์
|
133 |
-
prompt = preprocess_prompt(prompt, image1, image2, image3)
|
134 |
|
135 |
-
# ์ปจํ
์ธ
|
136 |
parts = []
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
# ํ
์คํธ ํ๋กฌํํธ ์ถ๊ฐ
|
139 |
-
parts.append(
|
140 |
-
"text": prompt
|
141 |
-
})
|
142 |
|
143 |
-
#
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
# ์ด๋ฏธ์ง๋ฅผ ํํธ๋ก ์ถ๊ฐ (genai.types.Part ๋์ ์ง์ ๋์
๋๋ฆฌ ์ฌ์ฉ)
|
152 |
-
parts.append({
|
153 |
-
"inline_data": {
|
154 |
-
"mime_type": "image/png",
|
155 |
-
"data": img_bytes
|
156 |
-
}
|
157 |
-
})
|
158 |
-
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
159 |
|
160 |
# ์์ฑ ์ค์
|
161 |
-
generate_content_config =
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
|
166 |
# ์์ ํ์ผ ์์ฑ
|
167 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
168 |
temp_path = tmp.name
|
169 |
|
170 |
# Gemini ๋ชจ๋ธ๋ก ์์ฒญ ์ ์ก
|
171 |
-
|
172 |
-
|
173 |
-
# API ์์ฒญ ํ์ ์์
|
174 |
-
response = client.models.generate_content(
|
175 |
model="gemini-2.0-flash-exp-image-generation",
|
176 |
-
contents=
|
177 |
-
|
178 |
-
"parts": parts
|
179 |
-
}],
|
180 |
-
generation_config=generate_content_config
|
181 |
)
|
182 |
|
183 |
# ์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ
|
184 |
image_found = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
-
#
|
187 |
-
for
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
if not image_found:
|
194 |
return None, "API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ๋ค๋ฅธ ํ๋กฌํํธ๋ก ์๋ํด๋ณด์ธ์."
|
@@ -204,7 +209,42 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
204 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
205 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
206 |
|
207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
def update_prompt_from_function(function_choice, custom_text=""):
|
209 |
function_templates = {
|
210 |
"1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ": f'#1์ "{custom_text if custom_text else "์ํ๋ ์ค๋ช
"}"์ผ๋ก ๋ฐ๊ฟ๋ผ',
|
@@ -218,7 +258,7 @@ def update_prompt_from_function(function_choice, custom_text=""):
|
|
218 |
|
219 |
return function_templates.get(function_choice, "")
|
220 |
|
221 |
-
# Gradio ์ธํฐํ์ด์ค
|
222 |
with gr.Blocks() as demo:
|
223 |
gr.HTML(
|
224 |
"""
|
|
|
4 |
import gradio as gr
|
5 |
import logging
|
6 |
import re
|
|
|
7 |
|
8 |
from google import genai
|
9 |
from google.genai import types
|
|
|
24 |
"""
|
25 |
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
|
26 |
"""
|
27 |
+
# ๊ธฐ์กด preprocess_prompt ํจ์ ์ฝ๋ ์ ์ง
|
28 |
# ์ด๋ฏธ์ง ์๋ ์ฐธ์กฐ ํ์ธ ๋ฐ ์ฒ๋ฆฌ
|
29 |
has_img1 = image1 is not None
|
30 |
has_img2 = image2 is not None
|
|
|
96 |
|
97 |
return prompt
|
98 |
|
99 |
+
def generate_with_images(prompt, images):
|
100 |
"""
|
101 |
+
๋ฌธ์ ๊ฐ ๋ API ํธ์ถ ๋ถ๋ถ์ ์๋ก์ด ๋ฐฉ์์ผ๋ก ๊ตฌํ
|
102 |
"""
|
103 |
try:
|
104 |
# API ํค ํ์ธ
|
|
|
109 |
# Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
110 |
client = genai.Client(api_key=api_key)
|
111 |
|
112 |
+
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}")
|
|
|
|
|
113 |
|
114 |
+
# ์์ ํ์ผ ์์ฑ ๋ฐ ์ฒ๋ฆฌ
|
115 |
+
temp_image_paths = []
|
116 |
+
file_uris = []
|
117 |
|
118 |
+
# ์ด๋ฏธ์ง ํ์ผ ์ ์ฅ ๋ฐ ์
๋ก๋
|
119 |
+
for idx, img in enumerate(images, 1):
|
120 |
+
if img is not None:
|
121 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
122 |
+
temp_path = tmp.name
|
123 |
+
img.save(temp_path, format="PNG")
|
124 |
+
temp_image_paths.append(temp_path)
|
125 |
+
|
126 |
+
# ํ์ผ ์
๋ก๋
|
127 |
+
uploaded_file = client.files.upload(file=temp_path)
|
128 |
+
file_uris.append(uploaded_file)
|
129 |
+
logger.info(f"์ด๋ฏธ์ง #{idx} ์
๋ก๋๋จ: {uploaded_file.uri}")
|
|
|
|
|
|
|
130 |
|
131 |
+
# ์ปจํ
์ธ ํํธ ๊ตฌ์ฑ
|
132 |
parts = []
|
133 |
|
134 |
+
# ์ด๋ฏธ์ง ํํธ ์ถ๊ฐ
|
135 |
+
for file in file_uris:
|
136 |
+
parts.append(
|
137 |
+
types.Part.from_uri(
|
138 |
+
file_uri=file.uri,
|
139 |
+
mime_type=file.mime_type,
|
140 |
+
)
|
141 |
+
)
|
142 |
+
|
143 |
# ํ
์คํธ ํ๋กฌํํธ ์ถ๊ฐ
|
144 |
+
parts.append(types.Part.from_text(text=prompt))
|
|
|
|
|
145 |
|
146 |
+
# ์์ฒญ ๋ฉ์์ง ๊ตฌ์ฑ
|
147 |
+
contents = [
|
148 |
+
types.Content(
|
149 |
+
role="user",
|
150 |
+
parts=parts,
|
151 |
+
),
|
152 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
# ์์ฑ ์ค์
|
155 |
+
generate_content_config = types.GenerateContentConfig(
|
156 |
+
temperature=1,
|
157 |
+
response_modalities=["image"],
|
158 |
+
)
|
159 |
|
160 |
# ์์ ํ์ผ ์์ฑ
|
161 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
162 |
temp_path = tmp.name
|
163 |
|
164 |
# Gemini ๋ชจ๋ธ๋ก ์์ฒญ ์ ์ก
|
165 |
+
response_stream = client.models.generate_content_stream(
|
|
|
|
|
|
|
166 |
model="gemini-2.0-flash-exp-image-generation",
|
167 |
+
contents=contents,
|
168 |
+
config=generate_content_config,
|
|
|
|
|
|
|
169 |
)
|
170 |
|
171 |
# ์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ
|
172 |
image_found = False
|
173 |
+
for chunk in response_stream:
|
174 |
+
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
175 |
+
logger.warning("์ฒญํฌ์ ๋ฐ์ดํฐ๊ฐ ์์. ๊ฑด๋๋.")
|
176 |
+
continue
|
177 |
+
|
178 |
+
for part in chunk.candidates[0].content.parts:
|
179 |
+
if hasattr(part, 'inline_data') and part.inline_data:
|
180 |
+
save_binary_file(temp_path, part.inline_data.data)
|
181 |
+
image_found = True
|
182 |
+
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
183 |
|
184 |
+
# ์์ ํ์ผ ์ ๋ฆฌ
|
185 |
+
for path in temp_image_paths:
|
186 |
+
try:
|
187 |
+
os.unlink(path)
|
188 |
+
except:
|
189 |
+
pass
|
190 |
+
|
191 |
+
# ํ์ผ URI ์ ๋ฆฌ
|
192 |
+
for file in file_uris:
|
193 |
+
try:
|
194 |
+
client.files.delete(fileLocation=file.uri)
|
195 |
+
except:
|
196 |
+
pass
|
197 |
|
198 |
if not image_found:
|
199 |
return None, "API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ๋ค๋ฅธ ํ๋กฌํํธ๋ก ์๋ํด๋ณด์ธ์."
|
|
|
209 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
210 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
211 |
|
212 |
+
def process_images_with_prompt(image1, image2, image3, prompt):
|
213 |
+
"""
|
214 |
+
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
215 |
+
"""
|
216 |
+
try:
|
217 |
+
# ์ด๋ฏธ์ง ๊ฐ์ ํ์ธ
|
218 |
+
images = [image1, image2, image3]
|
219 |
+
valid_images = [img for img in images if img is not None]
|
220 |
+
|
221 |
+
if not valid_images:
|
222 |
+
return None, "์ ์ด๋ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
223 |
+
|
224 |
+
# ํ๋กฌํํธ ์ฒ๋ฆฌ
|
225 |
+
if not prompt or not prompt.strip():
|
226 |
+
# ํ๋กฌํํธ๊ฐ ์์ผ๋ฉด ์
๋ก๋๋ ์ด๋ฏธ์ง ์์ ๋ฐ๋ผ ์๋ ํฉ์ฑ ํ๋กฌํํธ ์์ฑ
|
227 |
+
if len(valid_images) == 1:
|
228 |
+
prompt = "์ด ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
229 |
+
logger.info("๋จ์ผ ์ด๋ฏธ์ง ํ๋กฌํํธ ์๋ ์์ฑ")
|
230 |
+
elif len(valid_images) == 2:
|
231 |
+
prompt = "์ด ๋ ์ด๋ฏธ์ง๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ ์ด๋ฏธ์ง์ ์์๋ฅผ ์กฐํ๋กญ๊ฒ ํตํฉํ์ฌ ํ๋์ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
232 |
+
logger.info("๋ ์ด๋ฏธ์ง ํฉ์ฑ ํ๋กฌํํธ ์๋ ์์ฑ")
|
233 |
+
else:
|
234 |
+
prompt = "์ด ์ธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ํฉ์ฑํด์ฃผ์ธ์. ๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๋ ์์ฐ์ค๋ฝ๊ณ ์ผ๊ด๋ ํ๋์ ์ฅ๋ฉด์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
235 |
+
logger.info("์ธ ์ด๋ฏธ์ง ํฉ์ฑ ํ๋กฌํํธ ์๋ ์์ฑ")
|
236 |
+
else:
|
237 |
+
# ํ๋กฌํํธ ์ ์ฒ๋ฆฌ ๋ฐ ๊ธฐ๋ฅ ๋ช
๋ น ํด์
|
238 |
+
prompt = preprocess_prompt(prompt, image1, image2, image3)
|
239 |
+
|
240 |
+
# ์๋ก์ด API ํธ์ถ ๋ฐฉ์ ์ฌ์ฉ
|
241 |
+
return generate_with_images(prompt, valid_images)
|
242 |
+
|
243 |
+
except Exception as e:
|
244 |
+
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
245 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
246 |
+
|
247 |
+
# ๊ธฐ๋ฅ ์ ํ ์ฝ๋ฐฑ (๊ธฐ์กด ์ฝ๋ ์ ์ง)
|
248 |
def update_prompt_from_function(function_choice, custom_text=""):
|
249 |
function_templates = {
|
250 |
"1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ": f'#1์ "{custom_text if custom_text else "์ํ๋ ์ค๋ช
"}"์ผ๋ก ๋ฐ๊ฟ๋ผ',
|
|
|
258 |
|
259 |
return function_templates.get(function_choice, "")
|
260 |
|
261 |
+
# Gradio ์ธํฐํ์ด์ค (๊ธฐ์กด ๏ฟฝ๏ฟฝ๏ฟฝ๋ ์ ์ง)
|
262 |
with gr.Blocks() as demo:
|
263 |
gr.HTML(
|
264 |
"""
|