Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import tempfile
|
|
5 |
from PIL import Image
|
6 |
import gradio as gr
|
7 |
import logging
|
8 |
-
|
9 |
|
10 |
from google import genai
|
11 |
from google.genai import types
|
@@ -27,6 +27,12 @@ def save_binary_file(file_name, data):
|
|
27 |
logger.debug(f"ํ์ผ ์ ์ฅ ์๋ฃ: {file_name}")
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def merge_images(person_img_path, product_img_path, background_img_path, prompt, model="gemini-2.0-flash-exp-image-generation"):
|
31 |
logger.debug(f"merge_images ํจ์ ์์ - ํ๋กฌํํธ: '{prompt}'")
|
32 |
|
@@ -42,37 +48,50 @@ def merge_images(person_img_path, product_img_path, background_img_path, prompt,
|
|
42 |
client = genai.Client(api_key=effective_api_key)
|
43 |
logger.debug("Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์๋ฃ.")
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
48 |
|
49 |
# ์ปจํ
์ธ ๋ฆฌ์คํธ ์์ฑ
|
50 |
-
|
51 |
|
52 |
# ์ฌ๋ ์ด๋ฏธ์ง ์ถ๊ฐ
|
53 |
-
|
54 |
-
types.Part.
|
|
|
|
|
|
|
55 |
)
|
56 |
|
57 |
# ์ํ ์ด๋ฏธ์ง ์ถ๊ฐ
|
58 |
-
|
59 |
-
types.Part.
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๊ฐ ์์ผ๋ฉด ์ถ๊ฐ
|
63 |
if background_img_path:
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
)
|
68 |
logger.debug("๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์ถ๊ฐ๋จ")
|
69 |
|
70 |
# ๋ง์ง๋ง์ ํ๋กฌํํธ ์ถ๊ฐ
|
71 |
-
|
72 |
types.Part.from_text(text=prompt)
|
73 |
)
|
74 |
|
75 |
-
logger.debug(f"์ปจํ
์ธ ๊ฐ์ฒด ์์ฑ ์๋ฃ: {len(
|
76 |
|
77 |
# ์์ฑ ์ค์
|
78 |
generate_content_config = types.GenerateContentConfig(
|
@@ -88,13 +107,13 @@ def merge_images(person_img_path, product_img_path, background_img_path, prompt,
|
|
88 |
temp_path = tmp.name
|
89 |
logger.debug(f"์์ ํ์ผ ์์ฑ๋จ: {temp_path}")
|
90 |
|
91 |
-
# ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ์์
|
92 |
response_stream = client.models.generate_content_stream(
|
93 |
model=model,
|
94 |
contents=[
|
95 |
types.Content(
|
96 |
role="user",
|
97 |
-
parts=
|
98 |
),
|
99 |
],
|
100 |
config=generate_content_config,
|
@@ -110,11 +129,11 @@ def merge_images(person_img_path, product_img_path, background_img_path, prompt,
|
|
110 |
logger.debug(f"chunk ์์ : {chunk}")
|
111 |
|
112 |
# ์๋ต ๊ฒ์ฆ
|
113 |
-
if not hasattr(chunk, 'candidates') or not chunk.candidates:
|
114 |
logger.warning("chunk์ candidates๊ฐ ์์ต๋๋ค. ๊ฑด๋๋๋๋ค.")
|
115 |
continue
|
116 |
|
117 |
-
if not hasattr(chunk.candidates[0], 'content') or
|
118 |
logger.warning("chunk.candidates[0]์ content๊ฐ ์์ต๋๋ค. ๊ฑด๋๋๋๋ค.")
|
119 |
continue
|
120 |
|
|
|
5 |
from PIL import Image
|
6 |
import gradio as gr
|
7 |
import logging
|
8 |
+
import io
|
9 |
|
10 |
from google import genai
|
11 |
from google.genai import types
|
|
|
27 |
logger.debug(f"ํ์ผ ์ ์ฅ ์๋ฃ: {file_name}")
|
28 |
|
29 |
|
30 |
+
def image_to_bytes(img_path):
|
31 |
+
"""ํ์ผ ๊ฒฝ๋ก์์ ์ด๋ฏธ์ง ๋ฐ์ดํธ ๋ฐ์ดํฐ ์ฝ๊ธฐ"""
|
32 |
+
with open(img_path, "rb") as f:
|
33 |
+
return f.read()
|
34 |
+
|
35 |
+
|
36 |
def merge_images(person_img_path, product_img_path, background_img_path, prompt, model="gemini-2.0-flash-exp-image-generation"):
|
37 |
logger.debug(f"merge_images ํจ์ ์์ - ํ๋กฌํํธ: '{prompt}'")
|
38 |
|
|
|
48 |
client = genai.Client(api_key=effective_api_key)
|
49 |
logger.debug("Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์๋ฃ.")
|
50 |
|
51 |
+
# ์ด๋ฏธ์ง ํ์ผ ์
๋ก๋
|
52 |
+
person_file = client.files.upload(file=person_img_path)
|
53 |
+
logger.debug(f"์ฌ๋ ์ด๋ฏธ์ง ์
๋ก๋ ์๋ฃ: {person_file.uri}")
|
54 |
+
|
55 |
+
product_file = client.files.upload(file=product_img_path)
|
56 |
+
logger.debug(f"์ํ ์ด๋ฏธ์ง ์
๋ก๋ ์๋ฃ: {product_file.uri}")
|
57 |
|
58 |
# ์ปจํ
์ธ ๋ฆฌ์คํธ ์์ฑ
|
59 |
+
parts = []
|
60 |
|
61 |
# ์ฌ๋ ์ด๋ฏธ์ง ์ถ๊ฐ
|
62 |
+
parts.append(
|
63 |
+
types.Part.from_uri(
|
64 |
+
file_uri=person_file.uri,
|
65 |
+
mime_type=person_file.mime_type
|
66 |
+
)
|
67 |
)
|
68 |
|
69 |
# ์ํ ์ด๋ฏธ์ง ์ถ๊ฐ
|
70 |
+
parts.append(
|
71 |
+
types.Part.from_uri(
|
72 |
+
file_uri=product_file.uri,
|
73 |
+
mime_type=product_file.mime_type
|
74 |
+
)
|
75 |
)
|
76 |
|
77 |
# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๊ฐ ์์ผ๋ฉด ์ถ๊ฐ
|
78 |
if background_img_path:
|
79 |
+
background_file = client.files.upload(file=background_img_path)
|
80 |
+
logger.debug(f"๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์
๋ก๋ ์๋ฃ: {background_file.uri}")
|
81 |
+
parts.append(
|
82 |
+
types.Part.from_uri(
|
83 |
+
file_uri=background_file.uri,
|
84 |
+
mime_type=background_file.mime_type
|
85 |
+
)
|
86 |
)
|
87 |
logger.debug("๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์ถ๊ฐ๋จ")
|
88 |
|
89 |
# ๋ง์ง๋ง์ ํ๋กฌํํธ ์ถ๊ฐ
|
90 |
+
parts.append(
|
91 |
types.Part.from_text(text=prompt)
|
92 |
)
|
93 |
|
94 |
+
logger.debug(f"์ปจํ
์ธ ๊ฐ์ฒด ์์ฑ ์๋ฃ: {len(parts)} ์์ดํ
")
|
95 |
|
96 |
# ์์ฑ ์ค์
|
97 |
generate_content_config = types.GenerateContentConfig(
|
|
|
107 |
temp_path = tmp.name
|
108 |
logger.debug(f"์์ ํ์ผ ์์ฑ๋จ: {temp_path}")
|
109 |
|
110 |
+
# ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ์์
|
111 |
response_stream = client.models.generate_content_stream(
|
112 |
model=model,
|
113 |
contents=[
|
114 |
types.Content(
|
115 |
role="user",
|
116 |
+
parts=parts,
|
117 |
),
|
118 |
],
|
119 |
config=generate_content_config,
|
|
|
129 |
logger.debug(f"chunk ์์ : {chunk}")
|
130 |
|
131 |
# ์๋ต ๊ฒ์ฆ
|
132 |
+
if not hasattr(chunk, 'candidates') or not chunk.candidates or len(chunk.candidates) == 0:
|
133 |
logger.warning("chunk์ candidates๊ฐ ์์ต๋๋ค. ๊ฑด๋๋๋๋ค.")
|
134 |
continue
|
135 |
|
136 |
+
if not hasattr(chunk.candidates[0], 'content') or chunk.candidates[0].content is None:
|
137 |
logger.warning("chunk.candidates[0]์ content๊ฐ ์์ต๋๋ค. ๊ฑด๋๋๋๋ค.")
|
138 |
continue
|
139 |
|