Kims12 commited on
Commit
c17a6b6
ยท
verified ยท
1 Parent(s): f41a559

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -39
app.py CHANGED
@@ -108,8 +108,8 @@ def generate_with_images(prompt, images, max_retries=2):
108
 
109
  logger.info(f"Gemini API ์š”์ฒญ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: {prompt} (์‹œ๋„: {retries+1}/{max_retries+1})")
110
 
111
- # ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ (์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ)
112
- contents = []
113
 
114
  # ์ด๋ฏธ์ง€ ํŒŒํŠธ ์ถ”๊ฐ€
115
  for idx, img in enumerate(images, 1):
@@ -120,34 +120,39 @@ def generate_with_images(prompt, images, max_retries=2):
120
  image_bytes = buffered.getvalue()
121
 
122
  # ์ด๋ฏธ์ง€ ํŒŒํŠธ ์ƒ์„ฑ
123
- contents.append({
124
- 'inline_data': {
125
- 'mime_type': 'image/png',
126
- 'data': image_bytes
127
  }
128
  })
129
  logger.info(f"์ด๋ฏธ์ง€ #{idx} ์ถ”๊ฐ€๋จ")
130
 
131
  # ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ ์ถ”๊ฐ€
132
- contents.append({
133
  'text': prompt
134
  })
135
 
136
- # ์ƒ์„ฑ ์„ค์ • - ๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ผ responseModalities ์„ค์ •
137
- response = client.models.generate_content(
138
- model="gemini-2.0-flash-exp-image-generation",
139
- contents=contents,
140
- config=types.GenerateContentConfig(
141
- response_modalities=['Text', 'Image'],
142
- temperature=1,
143
- top_p=0.95,
144
- top_k=40,
145
- max_output_tokens=8192
146
- )
147
- )
 
 
 
 
 
148
 
149
  # ์‘๋‹ต ์œ ํšจ์„ฑ ํ™•์ธ
150
- if not response or not response.candidates:
151
  if retries < max_retries:
152
  retries += 1
153
  logger.warning(f"์œ ํšจํ•œ ์‘๋‹ต์„ ๋ฐ›์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์žฌ์‹œ๋„ ์ค‘... ({retries}/{max_retries})")
@@ -161,27 +166,21 @@ def generate_with_images(prompt, images, max_retries=2):
161
  result_text = ""
162
  image_found = False
163
 
164
- # ์‘๋‹ต ์ฒ˜๋ฆฌ
165
- for candidate in response.candidates:
166
- if not candidate.content:
167
- continue
168
-
169
- # ํ…์ŠคํŠธ ์ถ”์ถœ
170
- if candidate.content.text:
171
- result_text += candidate.content.text
172
- logger.info(f"์‘๋‹ต ํ…์ŠคํŠธ: {candidate.content.text}")
173
-
174
- # ์ด๋ฏธ์ง€ ์ถ”์ถœ
175
- for part in candidate.content.parts:
176
- if hasattr(part, 'inline_data') and part.inline_data:
177
- save_binary_file(temp_path, part.inline_data.data)
178
- image_found = True
179
- logger.info("์‘๋‹ต์—์„œ ์ด๋ฏธ์ง€ ์ถ”์ถœ ์„ฑ๊ณต")
180
- break
181
-
182
- if image_found:
183
  break
184
 
 
 
 
 
185
  if not image_found:
186
  if retries < max_retries:
187
  retries += 1
 
108
 
109
  logger.info(f"Gemini API ์š”์ฒญ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: {prompt} (์‹œ๋„: {retries+1}/{max_retries+1})")
110
 
111
+ # ์ด๋ฏธ์ง€ ๋ฐ ํ…์ŠคํŠธ ์ปจํ…์ธ  ์ค€๋น„
112
+ parts = []
113
 
114
  # ์ด๋ฏธ์ง€ ํŒŒํŠธ ์ถ”๊ฐ€
115
  for idx, img in enumerate(images, 1):
 
120
  image_bytes = buffered.getvalue()
121
 
122
  # ์ด๋ฏธ์ง€ ํŒŒํŠธ ์ƒ์„ฑ
123
+ parts.append({
124
+ 'inlineData': {
125
+ 'mimeType': 'image/png',
126
+ 'data': base64.b64encode(image_bytes).decode('utf-8')
127
  }
128
  })
129
  logger.info(f"์ด๋ฏธ์ง€ #{idx} ์ถ”๊ฐ€๋จ")
130
 
131
  # ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ ์ถ”๊ฐ€
132
+ parts.append({
133
  'text': prompt
134
  })
135
 
136
+ # ๋ชจ๋ธ ์ƒ์„ฑ ์š”์ฒญ
137
+ request = {
138
+ 'contents': [{
139
+ 'parts': parts
140
+ }],
141
+ 'generation_config': {
142
+ 'temperature': 1.0,
143
+ 'topP': 0.95,
144
+ 'topK': 40,
145
+ 'maxOutputTokens': 8192,
146
+ 'responseMimeType': 'image/png'
147
+ }
148
+ }
149
+
150
+ # ์ด๋ฏธ์ง€ ์ƒ์„ฑ API ํ˜ธ์ถœ
151
+ model = client.get_model('gemini-pro-vision')
152
+ response = model.generate_content(request)
153
 
154
  # ์‘๋‹ต ์œ ํšจ์„ฑ ํ™•์ธ
155
+ if not response or not response.parts:
156
  if retries < max_retries:
157
  retries += 1
158
  logger.warning(f"์œ ํšจํ•œ ์‘๋‹ต์„ ๋ฐ›์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์žฌ์‹œ๋„ ์ค‘... ({retries}/{max_retries})")
 
166
  result_text = ""
167
  image_found = False
168
 
169
+ # ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ํ™•์ธ ๋ฐ ์ €์žฅ
170
+ for part in response.parts:
171
+ if hasattr(part, 'mime_type') and part.mime_type.startswith('image/'):
172
+ # base64 ๋””์ฝ”๋”ฉ ๋ฐ ์ €์žฅ
173
+ img_data = base64.b64decode(part.data)
174
+ with open(temp_path, 'wb') as f:
175
+ f.write(img_data)
176
+ image_found = True
177
+ logger.info("์‘๋‹ต์—์„œ ์ด๋ฏธ์ง€ ์ถ”์ถœ ์„ฑ๊ณต")
 
 
 
 
 
 
 
 
 
 
178
  break
179
 
180
+ # ํ…์ŠคํŠธ ์ถ”์ถœ (์˜ต์…˜)
181
+ if hasattr(response, 'text'):
182
+ result_text = response.text
183
+
184
  if not image_found:
185
  if retries < max_retries:
186
  retries += 1