Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,7 @@ logger = logging.getLogger(__name__)
|
|
21 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
22 |
|
23 |
# Gemini API ์ด๊ธฐํ
|
24 |
-
genai.
|
25 |
|
26 |
# ๋ฐฐ๊ฒฝ JSON ํ์ผ ๊ฒฝ๋ก ์ค์ - ์๋ ๊ฒฝ๋ก ์ฌ์ฉ
|
27 |
BACKGROUNDS_DIR = "./background"
|
@@ -144,20 +144,26 @@ def generate_prompt_with_gemini(product_name, background_info, additional_info="
|
|
144 |
8. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.
|
145 |
ํ๊ตญ์ด ์
๋ ฅ ๋ด์ฉ์ ์์ด๋ก ์ ์ ํ ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์.
|
146 |
"""
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
154 |
temperature=0.7,
|
155 |
top_p=0.95,
|
156 |
top_k=64,
|
157 |
max_output_tokens=1024,
|
158 |
)
|
159 |
)
|
160 |
-
|
|
|
161 |
if "--ar 1:1" not in response_text:
|
162 |
response_text = response_text.rstrip(".") + ". --ar 1:1 --s 750 --q 2"
|
163 |
return response_text
|
@@ -171,13 +177,6 @@ def translate_prompt_to_english(prompt):
|
|
171 |
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
|
172 |
|
173 |
try:
|
174 |
-
api_key = os.environ.get("GEMINI_API_KEY")
|
175 |
-
if not api_key:
|
176 |
-
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
177 |
-
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
178 |
-
return prompt
|
179 |
-
|
180 |
-
client = genai.Client(api_key=api_key)
|
181 |
translation_prompt = f"""
|
182 |
Translate the following Korean text to English:
|
183 |
|
@@ -188,11 +187,10 @@ def translate_prompt_to_english(prompt):
|
|
188 |
"""
|
189 |
|
190 |
logger.info(f"Translation prompt: {translation_prompt}")
|
191 |
-
response =
|
192 |
model="gemini-2.0-flash",
|
193 |
contents=[translation_prompt],
|
194 |
-
|
195 |
-
response_modalities=['Text'],
|
196 |
temperature=0.2,
|
197 |
top_p=0.95,
|
198 |
top_k=40,
|
@@ -200,10 +198,7 @@ def translate_prompt_to_english(prompt):
|
|
200 |
)
|
201 |
)
|
202 |
|
203 |
-
translated_text =
|
204 |
-
for part in response.candidates[0].content.parts:
|
205 |
-
if hasattr(part, 'text') and part.text:
|
206 |
-
translated_text += part.text
|
207 |
|
208 |
if translated_text.strip():
|
209 |
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
|
@@ -218,24 +213,11 @@ def translate_prompt_to_english(prompt):
|
|
218 |
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
219 |
return prompt
|
220 |
|
221 |
-
def preprocess_prompt(prompt, image1):
|
222 |
-
has_img1 = image1 is not None
|
223 |
-
|
224 |
-
if "#1" in prompt and not has_img1:
|
225 |
-
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
226 |
-
else:
|
227 |
-
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
228 |
-
|
229 |
-
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
230 |
-
return prompt
|
231 |
-
|
232 |
def generate_with_images(prompt, images, variation_index=0):
|
233 |
try:
|
234 |
-
|
235 |
-
if not api_key:
|
236 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
237 |
|
238 |
-
client = genai.Client(api_key=api_key)
|
239 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
240 |
|
241 |
variation_suffixes = [
|
@@ -253,14 +235,14 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
253 |
contents = [prompt]
|
254 |
for idx, img in enumerate(images, 1):
|
255 |
if img is not None:
|
|
|
256 |
contents.append(img)
|
257 |
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
258 |
|
259 |
-
response =
|
260 |
model="gemini-2.0-flash-exp-image-generation",
|
261 |
contents=contents,
|
262 |
-
|
263 |
-
response_modalities=['Text', 'Image'],
|
264 |
temperature=1,
|
265 |
top_p=0.95,
|
266 |
top_k=40,
|
@@ -273,6 +255,7 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
273 |
temp_path = tmp.name
|
274 |
result_text = ""
|
275 |
image_found = False
|
|
|
276 |
for part in response.candidates[0].content.parts:
|
277 |
if hasattr(part, 'text') and part.text:
|
278 |
result_text += part.text
|
@@ -281,6 +264,7 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
281 |
save_binary_file(temp_path, part.inline_data.data)
|
282 |
image_found = True
|
283 |
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
|
|
284 |
if not image_found:
|
285 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
286 |
|
@@ -522,6 +506,7 @@ with gr.Blocks() as demo:
|
|
522 |
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown]
|
523 |
)
|
524 |
|
|
|
525 |
# ํ๋กฌํํธ ์์ฑ ํจ์
|
526 |
def generate_output(image, bg_type, simple, studio, nature, indoor, abstract, product_text, additional_text):
|
527 |
if image is None:
|
@@ -573,6 +558,6 @@ with gr.Blocks() as demo:
|
|
573 |
inputs=[image1_input, prompt_output],
|
574 |
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
575 |
)
|
576 |
-
|
577 |
demo.queue()
|
578 |
demo.launch()
|
|
|
21 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
22 |
|
23 |
# Gemini API ์ด๊ธฐํ
|
24 |
+
genai_client = genai.Client(api_key=GEMINI_API_KEY)
|
25 |
|
26 |
# ๋ฐฐ๊ฒฝ JSON ํ์ผ ๊ฒฝ๋ก ์ค์ - ์๋ ๊ฒฝ๋ก ์ฌ์ฉ
|
27 |
BACKGROUNDS_DIR = "./background"
|
|
|
144 |
8. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.
|
145 |
ํ๊ตญ์ด ์
๋ ฅ ๋ด์ฉ์ ์์ด๋ก ์ ์ ํ ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์.
|
146 |
"""
|
147 |
+
response = genai_client.models.generate_content(
|
148 |
+
model="gemini-2.0-flash",
|
149 |
+
contents=[
|
150 |
+
{
|
151 |
+
"role": "user",
|
152 |
+
"parts": [
|
153 |
+
{"text": generate_system_instruction()},
|
154 |
+
{"text": prompt_request}
|
155 |
+
]
|
156 |
+
}
|
157 |
+
],
|
158 |
+
generation_config=types.GenerationConfig(
|
159 |
temperature=0.7,
|
160 |
top_p=0.95,
|
161 |
top_k=64,
|
162 |
max_output_tokens=1024,
|
163 |
)
|
164 |
)
|
165 |
+
|
166 |
+
response_text = response.candidates[0].content.parts[0].text.strip()
|
167 |
if "--ar 1:1" not in response_text:
|
168 |
response_text = response_text.rstrip(".") + ". --ar 1:1 --s 750 --q 2"
|
169 |
return response_text
|
|
|
177 |
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
|
178 |
|
179 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
translation_prompt = f"""
|
181 |
Translate the following Korean text to English:
|
182 |
|
|
|
187 |
"""
|
188 |
|
189 |
logger.info(f"Translation prompt: {translation_prompt}")
|
190 |
+
response = genai_client.models.generate_content(
|
191 |
model="gemini-2.0-flash",
|
192 |
contents=[translation_prompt],
|
193 |
+
generation_config=types.GenerationConfig(
|
|
|
194 |
temperature=0.2,
|
195 |
top_p=0.95,
|
196 |
top_k=40,
|
|
|
198 |
)
|
199 |
)
|
200 |
|
201 |
+
translated_text = response.candidates[0].content.parts[0].text
|
|
|
|
|
|
|
202 |
|
203 |
if translated_text.strip():
|
204 |
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
|
|
|
213 |
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
214 |
return prompt
|
215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
def generate_with_images(prompt, images, variation_index=0):
|
217 |
try:
|
218 |
+
if not GEMINI_API_KEY:
|
|
|
219 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
220 |
|
|
|
221 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
222 |
|
223 |
variation_suffixes = [
|
|
|
235 |
contents = [prompt]
|
236 |
for idx, img in enumerate(images, 1):
|
237 |
if img is not None:
|
238 |
+
# PIL ์ด๋ฏธ์ง๋ฅผ ํํธ๋ก ๋ณํ
|
239 |
contents.append(img)
|
240 |
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
241 |
|
242 |
+
response = genai_client.models.generate_content(
|
243 |
model="gemini-2.0-flash-exp-image-generation",
|
244 |
contents=contents,
|
245 |
+
generation_config=types.GenerationConfig(
|
|
|
246 |
temperature=1,
|
247 |
top_p=0.95,
|
248 |
top_k=40,
|
|
|
255 |
temp_path = tmp.name
|
256 |
result_text = ""
|
257 |
image_found = False
|
258 |
+
|
259 |
for part in response.candidates[0].content.parts:
|
260 |
if hasattr(part, 'text') and part.text:
|
261 |
result_text += part.text
|
|
|
264 |
save_binary_file(temp_path, part.inline_data.data)
|
265 |
image_found = True
|
266 |
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
267 |
+
|
268 |
if not image_found:
|
269 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
270 |
|
|
|
506 |
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown]
|
507 |
)
|
508 |
|
509 |
+
|
510 |
# ํ๋กฌํํธ ์์ฑ ํจ์
|
511 |
def generate_output(image, bg_type, simple, studio, nature, indoor, abstract, product_text, additional_text):
|
512 |
if image is None:
|
|
|
558 |
inputs=[image1_input, prompt_output],
|
559 |
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
560 |
)
|
561 |
+
|
562 |
demo.queue()
|
563 |
demo.launch()
|