Update app.py
Browse files
app.py
CHANGED
@@ -98,7 +98,7 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
98 |
|
99 |
def generate_with_images(prompt, images):
|
100 |
"""
|
101 |
-
|
102 |
"""
|
103 |
try:
|
104 |
# API ํค ํ์ธ
|
@@ -151,10 +151,12 @@ def generate_with_images(prompt, images):
|
|
151 |
),
|
152 |
]
|
153 |
|
154 |
-
# ์์ฑ ์ค์
|
155 |
generate_content_config = types.GenerateContentConfig(
|
156 |
temperature=1,
|
157 |
-
|
|
|
|
|
158 |
)
|
159 |
|
160 |
# ์์ ํ์ผ ์์ฑ
|
@@ -162,48 +164,85 @@ def generate_with_images(prompt, images):
|
|
162 |
temp_path = tmp.name
|
163 |
|
164 |
# Gemini ๋ชจ๋ธ๋ก ์์ฒญ ์ ์ก
|
165 |
-
|
166 |
-
model="gemini-
|
167 |
contents=contents,
|
168 |
config=generate_content_config,
|
169 |
)
|
170 |
|
171 |
-
#
|
172 |
-
|
173 |
-
for
|
174 |
-
if
|
175 |
-
|
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 |
-
|
192 |
-
for file in file_uris:
|
193 |
-
try:
|
194 |
-
client.files.delete(fileLocation=file.uri)
|
195 |
-
except:
|
196 |
-
pass
|
197 |
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
|
206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
|
208 |
except Exception as e:
|
209 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
|
|
98 |
|
99 |
def generate_with_images(prompt, images):
|
100 |
"""
|
101 |
+
์๋ก์ด API ํธ์ถ ๋ฐฉ์์ผ๋ก ๊ตฌํ - ์๋ต ๋ชจ๋ฌ๋ฆฌํฐ ์์
|
102 |
"""
|
103 |
try:
|
104 |
# API ํค ํ์ธ
|
|
|
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 |
# ์์ ํ์ผ ์์ฑ
|
|
|
164 |
temp_path = tmp.name
|
165 |
|
166 |
# Gemini ๋ชจ๋ธ๋ก ์์ฒญ ์ ์ก
|
167 |
+
response = client.models.generate_content(
|
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 |
+
response_text += part.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
+
logger.info(f"์๋ต ํ
์คํธ: {response_text}")
|
|
|
|
|
|
|
|
|
|
|
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 |
+
for part in chunk.candidates[0].content.parts:
|
216 |
+
if hasattr(part, 'inline_data') and part.inline_data:
|
217 |
+
save_binary_file(temp_path, part.inline_data.data)
|
218 |
+
image_found = True
|
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("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|