Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,8 @@ 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
|
@@ -92,13 +94,14 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
92 |
elif "์ ๋ถ์์์ผ๋ก ๋ฐ๊ฟ๋ผ" in prompt or "๋ฅผ ๋ถ์์์ผ๋ก ๋ฐ๊ฟ๋ผ" in prompt:
|
93 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ถ์์ ํค์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ ์ฒด์ ์ธ ์์์ ๋ถ์ ๊ณ์ด๋ก ์กฐ์ ํ๊ณ ์์ฐ์ค๋ฌ์ด ๋๋์ ์ ์งํด์ฃผ์ธ์."
|
94 |
|
95 |
-
#
|
|
|
96 |
|
97 |
return prompt
|
98 |
|
99 |
def generate_with_images(prompt, images):
|
100 |
"""
|
101 |
-
|
102 |
"""
|
103 |
try:
|
104 |
# API ํค ํ์ธ
|
@@ -111,138 +114,57 @@ def generate_with_images(prompt, images):
|
|
111 |
|
112 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}")
|
113 |
|
114 |
-
#
|
115 |
-
|
116 |
-
file_uris = []
|
117 |
|
118 |
-
#
|
|
|
|
|
|
|
119 |
for idx, img in enumerate(images, 1):
|
120 |
if img is not None:
|
121 |
-
|
122 |
-
|
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 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
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 |
-
# ์์ฑ ์ค์ - response_modalities ์ ๊ฑฐ
|
155 |
-
generate_content_config = types.GenerateContentConfig(
|
156 |
-
temperature=1,
|
157 |
-
top_p=0.95,
|
158 |
-
top_k=40,
|
159 |
-
max_output_tokens=8192,
|
160 |
)
|
161 |
|
162 |
# ์์ ํ์ผ ์์ฑ
|
163 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
164 |
temp_path = tmp.name
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
model="gemini-pro-vision", # ๋ชจ๋ธ์ gemini-pro-vision์ผ๋ก ๋ณ๊ฒฝ
|
169 |
-
contents=contents,
|
170 |
-
config=generate_content_config,
|
171 |
-
)
|
172 |
|
173 |
-
#
|
174 |
-
response_text = ""
|
175 |
for part in response.candidates[0].content.parts:
|
176 |
if hasattr(part, 'text') and part.text:
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
-
|
180 |
-
|
181 |
-
# ์ด์ ์ด ํ
์คํธ ์๋ต์ ์ฌ์ฉํ์ฌ gemini-2.0-flash-exp-image-generation ๋ชจ๋ธ๋ก ์ด๋ฏธ์ง ์์ฑ
|
182 |
-
# ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ง ์ฌ์ฉ (์ฌ๋ฌ ์ด๋ฏธ์ง ์ฌ์ฉ์ ๋ณต์กํด์ง ์ ์์)
|
183 |
-
if len(temp_image_paths) > 0:
|
184 |
-
with open(temp_image_paths[0], 'rb') as f:
|
185 |
-
image_data = f.read()
|
186 |
-
|
187 |
-
# gemini-2.0-flash-exp-image-generation ๋ชจ๋ธ์ ์ฌ์ฉํ ๋จ์ผ ์ด๋ฏธ์ง ์์ฒญ
|
188 |
-
response_stream = client.models.generate_content_stream(
|
189 |
-
model="gemini-2.0-flash-exp-image-generation",
|
190 |
-
contents=[
|
191 |
-
types.Content(
|
192 |
-
role="user",
|
193 |
-
parts=[
|
194 |
-
types.Part.from_data(
|
195 |
-
mime_type="image/png",
|
196 |
-
data=image_data
|
197 |
-
),
|
198 |
-
types.Part.from_text(text=f"{prompt} {response_text}")
|
199 |
-
]
|
200 |
-
)
|
201 |
-
],
|
202 |
-
config=types.GenerateContentConfig(
|
203 |
-
temperature=1,
|
204 |
-
response_mime_type="image/png",
|
205 |
-
)
|
206 |
-
)
|
207 |
-
|
208 |
-
# ์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ
|
209 |
-
image_found = False
|
210 |
-
for chunk in response_stream:
|
211 |
-
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
212 |
-
logger.warning("์ฒญํฌ์ ๋ฐ์ดํฐ๊ฐ ์์. ๊ฑด๋๋.")
|
213 |
-
continue
|
214 |
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
220 |
|
221 |
-
|
222 |
-
for path in temp_image_paths:
|
223 |
-
try:
|
224 |
-
os.unlink(path)
|
225 |
-
except:
|
226 |
-
pass
|
227 |
-
|
228 |
-
# ํ์ผ URI ์ ๋ฆฌ
|
229 |
-
for file in file_uris:
|
230 |
-
try:
|
231 |
-
client.files.delete(fileLocation=file.uri)
|
232 |
-
except:
|
233 |
-
pass
|
234 |
-
|
235 |
-
if not image_found:
|
236 |
-
return None, "API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ๋ค๋ฅธ ํ๋กฌํํธ๋ก ์๋ํด๋ณด์ธ์."
|
237 |
-
|
238 |
-
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐํ
|
239 |
-
result_img = Image.open(temp_path)
|
240 |
-
if result_img.mode == "RGBA":
|
241 |
-
result_img = result_img.convert("RGB")
|
242 |
-
|
243 |
-
return result_img, "์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค."
|
244 |
-
else:
|
245 |
-
return None, "์ฒ๋ฆฌํ ์ด๋ฏธ์ง๊ฐ ์์ต๋๋ค."
|
246 |
|
247 |
except Exception as e:
|
248 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
|
|
4 |
import gradio as gr
|
5 |
import logging
|
6 |
import re
|
7 |
+
import io
|
8 |
+
from io import BytesIO
|
9 |
|
10 |
from google import genai
|
11 |
from google.genai import types
|
|
|
94 |
elif "์ ๋ถ์์์ผ๋ก ๋ฐ๊ฟ๋ผ" in prompt or "๋ฅผ ๋ถ์์์ผ๋ก ๋ฐ๊ฟ๋ผ" in prompt:
|
95 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ถ์์ ํค์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ ์ฒด์ ์ธ ์์์ ๋ถ์ ๊ณ์ด๋ก ์กฐ์ ํ๊ณ ์์ฐ์ค๋ฌ์ด ๋๋์ ์ ์งํด์ฃผ์ธ์."
|
96 |
|
97 |
+
# ๋ช
ํํ ์ด๋ฏธ์ง ์์ฑ ์์ฒญ ์ถ๊ฐ
|
98 |
+
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
99 |
|
100 |
return prompt
|
101 |
|
102 |
def generate_with_images(prompt, images):
|
103 |
"""
|
104 |
+
๊ณต์ ๋ฌธ์์ ๊ธฐ๋ฐํ ์ฌ๋ฐ๋ฅธ API ํธ์ถ ๋ฐฉ์ ๊ตฌํ
|
105 |
"""
|
106 |
try:
|
107 |
# API ํค ํ์ธ
|
|
|
114 |
|
115 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}")
|
116 |
|
117 |
+
# ์ปจํ
์ธ ์ค๋น
|
118 |
+
contents = []
|
|
|
119 |
|
120 |
+
# ํ
์คํธ ํ๋กฌํํธ ์ถ๊ฐ
|
121 |
+
contents.append(prompt)
|
122 |
+
|
123 |
+
# ์ด๋ฏธ์ง ์ถ๊ฐ
|
124 |
for idx, img in enumerate(images, 1):
|
125 |
if img is not None:
|
126 |
+
contents.append(img)
|
127 |
+
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
+
# ์์ฑ ์ค์ - ๊ณต์ ๋ฌธ์์ ๋ฐ๋ผ responseModalities ์ค์
|
130 |
+
response = client.models.generate_content(
|
131 |
+
model="gemini-2.0-flash-exp-image-generation",
|
132 |
+
contents=contents,
|
133 |
+
config=types.GenerateContentConfig(
|
134 |
+
response_modalities=['Text', 'Image'],
|
135 |
+
temperature=1,
|
136 |
+
top_p=0.95,
|
137 |
+
top_k=40,
|
138 |
+
max_output_tokens=8192
|
139 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
)
|
141 |
|
142 |
# ์์ ํ์ผ ์์ฑ
|
143 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
144 |
temp_path = tmp.name
|
145 |
|
146 |
+
result_text = ""
|
147 |
+
image_found = False
|
|
|
|
|
|
|
|
|
148 |
|
149 |
+
# ์๋ต ์ฒ๋ฆฌ
|
|
|
150 |
for part in response.candidates[0].content.parts:
|
151 |
if hasattr(part, 'text') and part.text:
|
152 |
+
result_text += part.text
|
153 |
+
logger.info(f"์๋ต ํ
์คํธ: {part.text}")
|
154 |
+
elif hasattr(part, 'inline_data') and part.inline_data:
|
155 |
+
save_binary_file(temp_path, part.inline_data.data)
|
156 |
+
image_found = True
|
157 |
+
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
158 |
|
159 |
+
if not image_found:
|
160 |
+
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
+
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐํ
|
163 |
+
result_img = Image.open(temp_path)
|
164 |
+
if result_img.mode == "RGBA":
|
165 |
+
result_img = result_img.convert("RGB")
|
|
|
166 |
|
167 |
+
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
except Exception as e:
|
170 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|