Update app.py
Browse files
app.py
CHANGED
|
@@ -156,6 +156,28 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
| 156 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
| 157 |
return prompt
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
def generate_with_images(prompt, images, variation_index=0):
|
| 160 |
# ๊ธฐ์กด ํจ์ ์ ์ง
|
| 161 |
try:
|
|
@@ -211,17 +233,9 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
| 211 |
if not image_found:
|
| 212 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
| 213 |
|
| 214 |
-
#
|
| 215 |
result_img = Image.open(temp_path)
|
| 216 |
-
|
| 217 |
-
result_img = result_img.convert("RGB") # RGBA๋ฅผ RGB๋ก ๋ณํ (JPG๋ ์ํ ์ฑ๋์ ์ง์ํ์ง ์์)
|
| 218 |
-
|
| 219 |
-
# ์ด๋ฏธ์ง๋ฅผ JPG๋ก ์ ์ฅ
|
| 220 |
-
jpg_temp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False).name
|
| 221 |
-
result_img.save(jpg_temp, format="JPEG", quality=95)
|
| 222 |
-
|
| 223 |
-
# JPG ์ด๋ฏธ์ง๋ฅผ ๋ค์ ๋ก๋
|
| 224 |
-
result_img = Image.open(jpg_temp)
|
| 225 |
|
| 226 |
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
| 227 |
except Exception as e:
|
|
@@ -229,7 +243,7 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
| 229 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 230 |
|
| 231 |
def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0, max_retries=3):
|
| 232 |
-
# ๊ธฐ์กด ํจ์ ์ ์ง
|
| 233 |
retry_count = 0
|
| 234 |
last_error = None
|
| 235 |
|
|
@@ -259,6 +273,8 @@ def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0
|
|
| 259 |
|
| 260 |
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
| 261 |
if result_img is not None:
|
|
|
|
|
|
|
| 262 |
return result_img, status, final_prompt
|
| 263 |
else:
|
| 264 |
last_error = status
|
|
@@ -274,7 +290,7 @@ def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0
|
|
| 274 |
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt
|
| 275 |
|
| 276 |
def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progress()):
|
| 277 |
-
#
|
| 278 |
results = []
|
| 279 |
statuses = []
|
| 280 |
prompts = []
|
|
@@ -289,6 +305,8 @@ def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progres
|
|
| 289 |
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt, i, max_retries)
|
| 290 |
|
| 291 |
if result_img is not None:
|
|
|
|
|
|
|
| 292 |
results.append(result_img)
|
| 293 |
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
| 294 |
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
|
@@ -309,6 +327,7 @@ def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progres
|
|
| 309 |
|
| 310 |
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
| 311 |
|
|
|
|
| 312 |
# GFPGAN ๊ด๋ จ ์ฝ๋ ์ ์ง
|
| 313 |
import sys
|
| 314 |
from torchvision.transforms import functional
|
|
|
|
| 156 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
| 157 |
return prompt
|
| 158 |
|
| 159 |
+
def convert_to_jpg(pil_image):
|
| 160 |
+
"""์ด๋ฏธ์ง๋ฅผ JPG ํ์์ผ๋ก ๋ณํํฉ๋๋ค."""
|
| 161 |
+
import tempfile
|
| 162 |
+
from PIL import Image
|
| 163 |
+
import io
|
| 164 |
+
|
| 165 |
+
if pil_image is None:
|
| 166 |
+
return None
|
| 167 |
+
|
| 168 |
+
# RGBA ์ด๋ฏธ์ง๋ฅผ RGB๋ก ๋ณํ (JPG๋ ์ํ ์ฑ๋์ ์ง์ํ์ง ์์)
|
| 169 |
+
if pil_image.mode == "RGBA":
|
| 170 |
+
pil_image = pil_image.convert("RGB")
|
| 171 |
+
|
| 172 |
+
# ์ด๋ฏธ์ง๋ฅผ JPG๋ก ๋ณํ
|
| 173 |
+
jpg_buffer = io.BytesIO()
|
| 174 |
+
pil_image.save(jpg_buffer, format="JPEG", quality=95)
|
| 175 |
+
jpg_buffer.seek(0)
|
| 176 |
+
|
| 177 |
+
# JPG ์ด๋ฏธ์ง ๋ค์ ๋ก๋
|
| 178 |
+
jpg_image = Image.open(jpg_buffer)
|
| 179 |
+
return jpg_image
|
| 180 |
+
|
| 181 |
def generate_with_images(prompt, images, variation_index=0):
|
| 182 |
# ๊ธฐ์กด ํจ์ ์ ์ง
|
| 183 |
try:
|
|
|
|
| 233 |
if not image_found:
|
| 234 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
| 235 |
|
| 236 |
+
# ์ด๋ฏธ์ง๋ฅผ JPG๋ก ๋ณํ
|
| 237 |
result_img = Image.open(temp_path)
|
| 238 |
+
result_img = convert_to_jpg(result_img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
| 241 |
except Exception as e:
|
|
|
|
| 243 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 244 |
|
| 245 |
def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0, max_retries=3):
|
| 246 |
+
# ๊ธฐ์กด ํจ์ ๋ด์ฉ ์ ์ง
|
| 247 |
retry_count = 0
|
| 248 |
last_error = None
|
| 249 |
|
|
|
|
| 273 |
|
| 274 |
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
| 275 |
if result_img is not None:
|
| 276 |
+
# ์ด๋ฏธ์ง๋ฅผ JPG๋ก ๋ณํํ์ฌ ๋ฐํ
|
| 277 |
+
result_img = convert_to_jpg(result_img)
|
| 278 |
return result_img, status, final_prompt
|
| 279 |
else:
|
| 280 |
last_error = status
|
|
|
|
| 290 |
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt
|
| 291 |
|
| 292 |
def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progress()):
|
| 293 |
+
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ค์ด JPG๋ก ๋ณํ๋๋๋ก ์์
|
| 294 |
results = []
|
| 295 |
statuses = []
|
| 296 |
prompts = []
|
|
|
|
| 305 |
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt, i, max_retries)
|
| 306 |
|
| 307 |
if result_img is not None:
|
| 308 |
+
# ์ด๋ฏธ์ง๋ฅผ JPG๋ก ๋ณํ
|
| 309 |
+
result_img = convert_to_jpg(result_img)
|
| 310 |
results.append(result_img)
|
| 311 |
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
| 312 |
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
|
|
|
| 327 |
|
| 328 |
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
| 329 |
|
| 330 |
+
|
| 331 |
# GFPGAN ๊ด๋ จ ์ฝ๋ ์ ์ง
|
| 332 |
import sys
|
| 333 |
from torchvision.transforms import functional
|