Kims12 commited on
Commit
a923ce9
ยท
verified ยท
1 Parent(s): 3e624fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -37
app.py CHANGED
@@ -98,7 +98,7 @@ def preprocess_prompt(prompt, image1, image2, image3):
98
 
99
  def generate_with_images(prompt, images):
100
  """
101
- ๋ฌธ์ œ๊ฐ€ ๋œ API ํ˜ธ์ถœ ๋ถ€๋ถ„์„ ์ƒˆ๋กœ์šด ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„
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
- response_modalities=["image"],
 
 
158
  )
159
 
160
  # ์ž„์‹œ ํŒŒ์ผ ์ƒ์„ฑ
@@ -162,48 +164,85 @@ def generate_with_images(prompt, images):
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์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ”„๋กฌํ”„ํŠธ๋กœ ์‹œ๋„ํ•ด๋ณด์„ธ์š”."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
 
201
- # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€ ๋ฐ˜ํ™˜
202
- result_img = Image.open(temp_path)
203
- if result_img.mode == "RGBA":
204
- result_img = result_img.convert("RGB")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
 
206
- return result_img, "์ด๋ฏธ์ง€๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
 
 
 
 
 
 
 
 
 
 
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("์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")