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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -117
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
- ์ƒˆ๋กœ์šด API ํ˜ธ์ถœ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ - ์‘๋‹ต ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ ์ˆ˜์ •
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
- 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
- # ์ƒ์„ฑ ์„ค์ • - 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
- # 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("์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
 
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("์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")