Update app.py
Browse files
app.py
CHANGED
@@ -29,7 +29,7 @@ def save_binary_file(file_name, data):
|
|
29 |
logger.debug(f"ํ์ผ ์ ์ฅ ์๋ฃ: {file_name}")
|
30 |
|
31 |
|
32 |
-
def generate(text, file_name, model="gemini-2.0-flash-exp-image-generation"):
|
33 |
logger.debug(f"generate ํจ์ ์์ - ํ
์คํธ: '{text}', ํ์ผ๋ช
: '{file_name}', ๋ชจ๋ธ: '{model}'")
|
34 |
|
35 |
try:
|
@@ -44,23 +44,54 @@ def generate(text, file_name, model="gemini-2.0-flash-exp-image-generation"):
|
|
44 |
client = genai.Client(api_key=effective_api_key)
|
45 |
logger.debug("Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์๋ฃ.")
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
]
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
# ์ปจํ
์ธ ๊ฐ์ฒด ์์ฑ: ํ์ผ URI์ ํ
์คํธ ํ๋กฌํํธ๋ฅผ ํจ๊ป ํฌํจ
|
54 |
contents = [
|
55 |
types.Content(
|
56 |
role="user",
|
57 |
-
parts=
|
58 |
-
types.Part.from_uri(
|
59 |
-
file_uri=files[0].uri,
|
60 |
-
mime_type=files[0].mime_type,
|
61 |
-
),
|
62 |
-
types.Part.from_text(text=text),
|
63 |
-
],
|
64 |
),
|
65 |
]
|
66 |
logger.debug(f"์ปจํ
์ธ ๊ฐ์ฒด ์์ฑ ์๋ฃ: {contents}")
|
@@ -104,7 +135,7 @@ def generate(text, file_name, model="gemini-2.0-flash-exp-image-generation"):
|
|
104 |
|
105 |
logger.debug(f"Raw chunk: {chunk}")
|
106 |
|
107 |
-
del
|
108 |
logger.debug("์
๋ก๋๋ ํ์ผ ์ ๋ณด ์ญ์ ์๋ฃ.")
|
109 |
return temp_path
|
110 |
|
@@ -113,19 +144,39 @@ def generate(text, file_name, model="gemini-2.0-flash-exp-image-generation"):
|
|
113 |
return None # ์ค๋ฅ ๋ฐ์ ์ None ๋ฐํ
|
114 |
|
115 |
|
116 |
-
def process_image_and_prompt(
|
117 |
logger.debug(f"process_image_and_prompt ํจ์ ์์ - ํ๋กฌํํธ: '{prompt}'")
|
118 |
try:
|
|
|
119 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
120 |
-
|
121 |
-
|
122 |
-
logger.debug(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
-
file_name = composite_path
|
125 |
input_text = prompt
|
126 |
model = "gemini-2.0-flash-exp-image-generation"
|
127 |
|
128 |
-
gemma_edited_image_path = generate(text=input_text,
|
|
|
|
|
|
|
|
|
129 |
|
130 |
if gemma_edited_image_path:
|
131 |
logger.debug(f"์ด๋ฏธ์ง ์์ฑ ์๋ฃ. ๊ฒฝ๋ก: {gemma_edited_image_path}")
|
@@ -157,11 +208,13 @@ with gr.Blocks() as demo:
|
|
157 |
</div>
|
158 |
"""
|
159 |
)
|
160 |
-
gr.Markdown("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ , ํธ์งํ ๋ด์ฉ์ ์
๋ ฅํ์ธ์.")
|
161 |
|
162 |
with gr.Row():
|
163 |
with gr.Column():
|
164 |
-
|
|
|
|
|
165 |
prompt_input = gr.Textbox(
|
166 |
lines=2,
|
167 |
placeholder="ํธ์งํ ๋ด์ฉ์ ์
๋ ฅํ์ธ์...",
|
@@ -173,17 +226,19 @@ with gr.Blocks() as demo:
|
|
173 |
|
174 |
submit_btn.click(
|
175 |
fn=process_image_and_prompt,
|
176 |
-
inputs=[
|
177 |
outputs=output_gallery,
|
178 |
)
|
179 |
|
180 |
# --- ํ
์คํธ ์ฝ๋ ---
|
181 |
# ํ
์คํธ์ฉ ๋๋ฏธ ์ด๋ฏธ์ง (์ค์ ์ด๋ฏธ์ง๋ก ๋์ฒด ๊ฐ๋ฅ)
|
182 |
-
|
183 |
-
|
|
|
|
|
184 |
|
185 |
logger.info("process_image_and_prompt ํจ์๋ฅผ ์ง์ ํธ์ถํฉ๋๋ค...")
|
186 |
-
result = process_image_and_prompt(
|
187 |
|
188 |
if result:
|
189 |
logger.info(f"์ง์ ํธ์ถ ์ฑ๊ณต. ๊ฒฐ๊ณผ: {result}")
|
|
|
29 |
logger.debug(f"ํ์ผ ์ ์ฅ ์๋ฃ: {file_name}")
|
30 |
|
31 |
|
32 |
+
def generate(text, file_name, background_file=None, style_file=None, model="gemini-2.0-flash-exp-image-generation"):
|
33 |
logger.debug(f"generate ํจ์ ์์ - ํ
์คํธ: '{text}', ํ์ผ๋ช
: '{file_name}', ๋ชจ๋ธ: '{model}'")
|
34 |
|
35 |
try:
|
|
|
44 |
client = genai.Client(api_key=effective_api_key)
|
45 |
logger.debug("Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ ์๋ฃ.")
|
46 |
|
47 |
+
# ์๋ณธ, ๋ฐฐ๊ฒฝ, ์คํ์ผ ์ด๋ฏธ์ง๋ฅผ ๊ฐ๊ฐ ์
๋ก๋ (์กด์ฌํ๋ ๊ฒฝ์ฐ)
|
48 |
+
uploaded_files = []
|
49 |
+
uploaded_files.append(client.files.upload(file=file_name))
|
50 |
+
logger.debug(f"์๋ณธ ํ์ผ ์
๋ก๋ ์๋ฃ. URI: {uploaded_files[0].uri}, MIME ํ์
: {uploaded_files[0].mime_type}")
|
51 |
+
|
52 |
+
if background_file is not None:
|
53 |
+
bg_file = client.files.upload(file=background_file)
|
54 |
+
uploaded_files.append(bg_file)
|
55 |
+
logger.debug(f"๋ฐฐ๊ฒฝ ํ์ผ ์
๋ก๋ ์๋ฃ. URI: {bg_file.uri}, MIME ํ์
: {bg_file.mime_type}")
|
56 |
+
if style_file is not None:
|
57 |
+
style_uploaded = client.files.upload(file=style_file)
|
58 |
+
uploaded_files.append(style_uploaded)
|
59 |
+
logger.debug(f"์คํ์ผ ํ์ผ ์
๋ก๋ ์๋ฃ. URI: {style_uploaded.uri}, MIME ํ์
: {style_uploaded.mime_type}")
|
60 |
+
|
61 |
+
# ์ปจํ
์ธ ๊ฐ์ฒด ์์ฑ: ์
๋ก๋ํ ์ด๋ฏธ์ง๋ค์ ์์๋๋ก(์๋ณธ, ๋ฐฐ๊ฒฝ, ์คํ์ผ) ์ถ๊ฐํ๊ณ ๋ง์ง๋ง์ ํ
์คํธ ํ๋กฌํํธ ์ถ๊ฐ
|
62 |
+
parts = []
|
63 |
+
# ์๋ณธ ์ด๋ฏธ์ง ํํธ
|
64 |
+
parts.append(
|
65 |
+
types.Part.from_uri(
|
66 |
+
file_uri=uploaded_files[0].uri,
|
67 |
+
mime_type=uploaded_files[0].mime_type,
|
68 |
+
)
|
69 |
+
)
|
70 |
+
# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ํํธ (์กด์ฌ ์)
|
71 |
+
if background_file is not None:
|
72 |
+
parts.append(
|
73 |
+
types.Part.from_uri(
|
74 |
+
file_uri=uploaded_files[1].uri,
|
75 |
+
mime_type=uploaded_files[1].mime_type,
|
76 |
+
)
|
77 |
+
)
|
78 |
+
# ์คํ์ผ ์ด๋ฏธ์ง ํํธ (์กด์ฌ ์)
|
79 |
+
if style_file is not None:
|
80 |
+
# ๋ฐฐ๊ฒฝ์ด ์๋ ๊ฒฝ์ฐ uploaded_files[1]๊ฐ ์คํ์ผ ์ด๋ฏธ์ง๊ฐ ๋จ์ ์ฃผ์
|
81 |
+
style_index = 2 if background_file is not None else 1
|
82 |
+
parts.append(
|
83 |
+
types.Part.from_uri(
|
84 |
+
file_uri=uploaded_files[style_index].uri,
|
85 |
+
mime_type=uploaded_files[style_index].mime_type,
|
86 |
+
)
|
87 |
+
)
|
88 |
+
# ๋ง์ง๋ง์ผ๋ก ํ
์คํธ ํํธ ์ถ๊ฐ
|
89 |
+
parts.append(types.Part.from_text(text=text))
|
90 |
|
|
|
91 |
contents = [
|
92 |
types.Content(
|
93 |
role="user",
|
94 |
+
parts=parts,
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
),
|
96 |
]
|
97 |
logger.debug(f"์ปจํ
์ธ ๊ฐ์ฒด ์์ฑ ์๋ฃ: {contents}")
|
|
|
135 |
|
136 |
logger.debug(f"Raw chunk: {chunk}")
|
137 |
|
138 |
+
del uploaded_files
|
139 |
logger.debug("์
๋ก๋๋ ํ์ผ ์ ๋ณด ์ญ์ ์๋ฃ.")
|
140 |
return temp_path
|
141 |
|
|
|
144 |
return None # ์ค๋ฅ ๋ฐ์ ์ None ๋ฐํ
|
145 |
|
146 |
|
147 |
+
def process_image_and_prompt(original_pil, prompt, background_pil=None, style_pil=None):
|
148 |
logger.debug(f"process_image_and_prompt ํจ์ ์์ - ํ๋กฌํํธ: '{prompt}'")
|
149 |
try:
|
150 |
+
# ์๋ณธ ์ด๋ฏธ์ง ์ ์ฅ
|
151 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
152 |
+
original_path = tmp.name
|
153 |
+
original_pil.save(original_path)
|
154 |
+
logger.debug(f"์๋ณธ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ: {original_path}")
|
155 |
+
|
156 |
+
# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์ ์ฅ (์ ๊ณต๋ ๊ฒฝ์ฐ)
|
157 |
+
background_path = None
|
158 |
+
if background_pil is not None:
|
159 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_bg:
|
160 |
+
background_path = tmp_bg.name
|
161 |
+
background_pil.save(background_path)
|
162 |
+
logger.debug(f"๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์ ์ฅ ๏ฟฝ๏ฟฝ๋ฃ: {background_path}")
|
163 |
+
|
164 |
+
# ์คํ์ผ ์ด๋ฏธ์ง ์ ์ฅ (์ ๊ณต๋ ๊ฒฝ์ฐ)
|
165 |
+
style_path = None
|
166 |
+
if style_pil is not None:
|
167 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_style:
|
168 |
+
style_path = tmp_style.name
|
169 |
+
style_pil.save(style_path)
|
170 |
+
logger.debug(f"์คํ์ผ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ: {style_path}")
|
171 |
|
|
|
172 |
input_text = prompt
|
173 |
model = "gemini-2.0-flash-exp-image-generation"
|
174 |
|
175 |
+
gemma_edited_image_path = generate(text=input_text,
|
176 |
+
file_name=original_path,
|
177 |
+
background_file=background_path,
|
178 |
+
style_file=style_path,
|
179 |
+
model=model)
|
180 |
|
181 |
if gemma_edited_image_path:
|
182 |
logger.debug(f"์ด๋ฏธ์ง ์์ฑ ์๋ฃ. ๊ฒฝ๋ก: {gemma_edited_image_path}")
|
|
|
208 |
</div>
|
209 |
"""
|
210 |
)
|
211 |
+
gr.Markdown("์๋ณธ, ๋ฐฐ๊ฒฝ, ์คํ์ผ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ , ํธ์งํ ๋ด์ฉ์ ์
๋ ฅํ์ธ์.")
|
212 |
|
213 |
with gr.Row():
|
214 |
with gr.Column():
|
215 |
+
original_input = gr.Image(type="pil", label="์๋ณธ ์ด๋ฏธ์ง", image_mode="RGBA")
|
216 |
+
background_input = gr.Image(type="pil", label="๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง", image_mode="RGBA")
|
217 |
+
style_input = gr.Image(type="pil", label="์คํ์ผ ์ด๋ฏธ์ง", image_mode="RGBA")
|
218 |
prompt_input = gr.Textbox(
|
219 |
lines=2,
|
220 |
placeholder="ํธ์งํ ๋ด์ฉ์ ์
๋ ฅํ์ธ์...",
|
|
|
226 |
|
227 |
submit_btn.click(
|
228 |
fn=process_image_and_prompt,
|
229 |
+
inputs=[original_input, prompt_input, background_input, style_input],
|
230 |
outputs=output_gallery,
|
231 |
)
|
232 |
|
233 |
# --- ํ
์คํธ ์ฝ๋ ---
|
234 |
# ํ
์คํธ์ฉ ๋๋ฏธ ์ด๋ฏธ์ง (์ค์ ์ด๋ฏธ์ง๋ก ๋์ฒด ๊ฐ๋ฅ)
|
235 |
+
dummy_original = Image.new("RGBA", (100, 100), color="red")
|
236 |
+
dummy_background = Image.new("RGBA", (100, 100), color="green")
|
237 |
+
dummy_style = Image.new("RGBA", (100, 100), color="blue")
|
238 |
+
dummy_prompt = "์ด๋ฏธ์ง ํธ์ง: ์๋ณธ์ ๋นจ๊ฐ, ๋ฐฐ๊ฒฝ์ ์ด๋ก, ์คํ์ผ์ ํ๋"
|
239 |
|
240 |
logger.info("process_image_and_prompt ํจ์๋ฅผ ์ง์ ํธ์ถํฉ๋๋ค...")
|
241 |
+
result = process_image_and_prompt(dummy_original, dummy_prompt, dummy_background, dummy_style)
|
242 |
|
243 |
if result:
|
244 |
logger.info(f"์ง์ ํธ์ถ ์ฑ๊ณต. ๊ฒฐ๊ณผ: {result}")
|