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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import logging
|
| 6 |
+
import re
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
from google import genai
|
| 10 |
+
from google.genai import types
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
def save_binary_file(file_name, data):
|
| 18 |
+
with open(file_name, "wb") as f:
|
| 19 |
+
f.write(data)
|
| 20 |
+
|
| 21 |
+
def translate_prompt_to_english(prompt):
|
| 22 |
+
"""
|
| 23 |
+
์
๋ ฅ๋ ํ๋กฌํํธ์ ํ๊ธ์ด ํฌํจ๋์ด ์์ผ๋ฉด Geminiโ2.0โflash ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์์ด๋ก ๋ฒ์ญํฉ๋๋ค.
|
| 24 |
+
ํ๊ธ์ด ์์ผ๋ฉด ์๋ณธ ํ๋กฌํํธ๋ฅผ ๊ทธ๋๋ก ๋ฐํํฉ๋๋ค.
|
| 25 |
+
์ค์: #1, #2, #3 ํ๊ทธ๋ ๋ฒ์ญ ์ ํ์ ๋ฐ๋์ ๋ณด์กด๋์ด์ผ ํฉ๋๋ค.
|
| 26 |
+
"""
|
| 27 |
+
if not re.search("[๊ฐ-ํฃ]", prompt):
|
| 28 |
+
return prompt
|
| 29 |
+
|
| 30 |
+
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
|
| 31 |
+
prompt = prompt.replace("#2", "IMAGE_TAG_TWO")
|
| 32 |
+
prompt = prompt.replace("#3", "IMAGE_TAG_THREE")
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
| 36 |
+
if not api_key:
|
| 37 |
+
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 38 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
| 39 |
+
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
|
| 40 |
+
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
|
| 41 |
+
return prompt
|
| 42 |
+
|
| 43 |
+
client = genai.Client(api_key=api_key)
|
| 44 |
+
translation_prompt = f"""
|
| 45 |
+
Translate the following Korean text to English:
|
| 46 |
+
|
| 47 |
+
{prompt}
|
| 48 |
+
|
| 49 |
+
IMPORTANT: The tokens IMAGE_TAG_ONE, IMAGE_TAG_TWO, and IMAGE_TAG_THREE are special tags
|
| 50 |
+
and must be preserved exactly as is in your translation. Do not translate these tokens.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
logger.info(f"Translation prompt: {translation_prompt}")
|
| 54 |
+
response = client.models.generate_content(
|
| 55 |
+
model="gemini-2.0-flash",
|
| 56 |
+
contents=[translation_prompt],
|
| 57 |
+
config=types.GenerateContentConfig(
|
| 58 |
+
response_modalities=['Text'],
|
| 59 |
+
temperature=0.2,
|
| 60 |
+
top_p=0.95,
|
| 61 |
+
top_k=40,
|
| 62 |
+
max_output_tokens=512
|
| 63 |
+
)
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
translated_text = ""
|
| 67 |
+
for part in response.candidates[0].content.parts:
|
| 68 |
+
if hasattr(part, 'text') and part.text:
|
| 69 |
+
translated_text += part.text
|
| 70 |
+
|
| 71 |
+
if translated_text.strip():
|
| 72 |
+
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
|
| 73 |
+
translated_text = translated_text.replace("IMAGE_TAG_TWO", "#2")
|
| 74 |
+
translated_text = translated_text.replace("IMAGE_TAG_THREE", "#3")
|
| 75 |
+
logger.info(f"Translated text: {translated_text.strip()}")
|
| 76 |
+
return translated_text.strip()
|
| 77 |
+
else:
|
| 78 |
+
logger.warning("๋ฒ์ญ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ์๋ณธ ํ๋กฌํํธ ์ฌ์ฉ")
|
| 79 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
| 80 |
+
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
|
| 81 |
+
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
|
| 82 |
+
return prompt
|
| 83 |
+
except Exception as e:
|
| 84 |
+
logger.exception("๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์:")
|
| 85 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
| 86 |
+
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
|
| 87 |
+
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
|
| 88 |
+
return prompt
|
| 89 |
+
|
| 90 |
+
def preprocess_prompt(prompt, image1, image2, image3):
|
| 91 |
+
"""
|
| 92 |
+
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
|
| 93 |
+
"""
|
| 94 |
+
has_img1 = image1 is not None
|
| 95 |
+
has_img2 = image2 is not None
|
| 96 |
+
has_img3 = image3 is not None
|
| 97 |
+
|
| 98 |
+
if "#1" in prompt and not has_img1:
|
| 99 |
+
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
| 100 |
+
else:
|
| 101 |
+
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
| 102 |
+
|
| 103 |
+
if "#2" in prompt and not has_img2:
|
| 104 |
+
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
| 105 |
+
else:
|
| 106 |
+
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
| 107 |
+
|
| 108 |
+
if "#3" in prompt and not has_img3:
|
| 109 |
+
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
| 110 |
+
else:
|
| 111 |
+
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
| 112 |
+
|
| 113 |
+
if "1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ" in prompt:
|
| 114 |
+
desc_match = re.search(r'#1์ "(.*?)"์ผ๋ก ๋ฐ๊ฟ๋ผ', prompt)
|
| 115 |
+
if desc_match:
|
| 116 |
+
description = desc_match.group(1)
|
| 117 |
+
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ {description}์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์๋ณธ ์ด๋ฏธ์ง์ ์ฃผ์ ๋ด์ฉ์ ์ ์งํ๋ ์๋ก์ด ์คํ์ผ๊ณผ ๋ถ์๊ธฐ๋ก ์ฌํด์ํด์ฃผ์ธ์."
|
| 118 |
+
else:
|
| 119 |
+
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
| 120 |
+
|
| 121 |
+
elif "2. ๊ธ์์ง์ฐ๊ธฐ" in prompt:
|
| 122 |
+
text_match = re.search(r'#1์์ "(.*?)"๋ฅผ ์ง์๋ผ', prompt)
|
| 123 |
+
if text_match:
|
| 124 |
+
text_to_remove = text_match.group(1)
|
| 125 |
+
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ '{text_to_remove}' ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ํ
์คํธ๊ฐ ์๋ ๋ถ๋ถ์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ์ฑ์์ฃผ์ธ์."
|
| 126 |
+
else:
|
| 127 |
+
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ๊น๋ํ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
| 128 |
+
|
| 129 |
+
elif "4. ์ท๋ฐ๊พธ๊ธฐ" in prompt:
|
| 130 |
+
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ธ๋ฌผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์์์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์์์ ์คํ์ผ๊ณผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ฅด๋, ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ ์งํด์ฃผ์ธ์."
|
| 131 |
+
|
| 132 |
+
elif "5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ" in prompt:
|
| 133 |
+
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ฃผ์ ํผ์ฌ์ฒด๋ ์ ์งํ๊ณ , ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ํฉ์ฑํด์ฃผ์ธ์."
|
| 134 |
+
|
| 135 |
+
elif "6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)" in prompt:
|
| 136 |
+
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง(๋๋ ์ธ ๋ฒ์งธ ์ด๋ฏธ์ง)๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๊ณ , ํนํ ์ํ์ด ๋๋ณด์ด๋๋ก ์กฐํ๋กญ๊ฒ ํตํฉํด์ฃผ์ธ์."
|
| 137 |
+
|
| 138 |
+
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
| 139 |
+
return prompt
|
| 140 |
+
|
| 141 |
+
def generate_with_images(prompt, images, variation_index=0):
|
| 142 |
+
"""
|
| 143 |
+
API ํธ์ถ์ ํตํด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ณ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 144 |
+
variation_index๋ก ๋ค์ํ ๋ณํ๋ฅผ ์ค๋๋ค.
|
| 145 |
+
"""
|
| 146 |
+
try:
|
| 147 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
| 148 |
+
if not api_key:
|
| 149 |
+
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
| 150 |
+
|
| 151 |
+
client = genai.Client(api_key=api_key)
|
| 152 |
+
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
| 153 |
+
|
| 154 |
+
variation_suffixes = [
|
| 155 |
+
" Create this as the first variation. Do not add any text, watermarks, or labels to the image.",
|
| 156 |
+
" Create this as the second variation with more vivid colors. Do not add any text, watermarks, or labels to the image.",
|
| 157 |
+
" Create this as the third variation with a more creative style. Do not add any text, watermarks, or labels to the image.",
|
| 158 |
+
" Create this as the fourth variation with enhanced details. Do not add any text, watermarks, or labels to the image."
|
| 159 |
+
]
|
| 160 |
+
|
| 161 |
+
if variation_index < len(variation_suffixes):
|
| 162 |
+
prompt = prompt + variation_suffixes[variation_index]
|
| 163 |
+
else:
|
| 164 |
+
prompt = prompt + " Do not add any text, watermarks, or labels to the image."
|
| 165 |
+
|
| 166 |
+
contents = [prompt]
|
| 167 |
+
for idx, img in enumerate(images, 1):
|
| 168 |
+
if img is not None:
|
| 169 |
+
contents.append(img)
|
| 170 |
+
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
| 171 |
+
|
| 172 |
+
response = client.models.generate_content(
|
| 173 |
+
model="gemini-2.0-flash-exp-image-generation",
|
| 174 |
+
contents=contents,
|
| 175 |
+
config=types.GenerateContentConfig(
|
| 176 |
+
response_modalities=['Text', 'Image'],
|
| 177 |
+
temperature=1,
|
| 178 |
+
top_p=0.95,
|
| 179 |
+
top_k=40,
|
| 180 |
+
max_output_tokens=8192
|
| 181 |
+
)
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 185 |
+
temp_path = tmp.name
|
| 186 |
+
result_text = ""
|
| 187 |
+
image_found = False
|
| 188 |
+
for part in response.candidates[0].content.parts:
|
| 189 |
+
if hasattr(part, 'text') and part.text:
|
| 190 |
+
result_text += part.text
|
| 191 |
+
logger.info(f"์๋ต ํ
์คํธ: {part.text}")
|
| 192 |
+
elif hasattr(part, 'inline_data') and part.inline_data:
|
| 193 |
+
save_binary_file(temp_path, part.inline_data.data)
|
| 194 |
+
image_found = True
|
| 195 |
+
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
| 196 |
+
if not image_found:
|
| 197 |
+
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
| 198 |
+
result_img = Image.open(temp_path)
|
| 199 |
+
if result_img.mode == "RGBA":
|
| 200 |
+
result_img = result_img.convert("RGB")
|
| 201 |
+
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
| 202 |
+
except Exception as e:
|
| 203 |
+
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
| 204 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 205 |
+
|
| 206 |
+
def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0, max_retries=3):
|
| 207 |
+
"""
|
| 208 |
+
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ์ฌ ์ต์ข
์์ด ํ๋กฌํํธ(final_prompt)๋ฅผ ์์ฑํ ํ,
|
| 209 |
+
API๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค. ์๋ฌ ๋ฐ์ ์ ์ต๋ max_retries ํ์๋งํผ ์ฌ์๋ํฉ๋๋ค.
|
| 210 |
+
"""
|
| 211 |
+
retry_count = 0
|
| 212 |
+
last_error = None
|
| 213 |
+
|
| 214 |
+
while retry_count < max_retries:
|
| 215 |
+
try:
|
| 216 |
+
images = [image1, image2, image3]
|
| 217 |
+
valid_images = [img for img in images if img is not None]
|
| 218 |
+
if not valid_images:
|
| 219 |
+
return None, "์ ์ด๋ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
| 220 |
+
|
| 221 |
+
if prompt and prompt.strip():
|
| 222 |
+
processed_prompt = preprocess_prompt(prompt, image1, image2, image3)
|
| 223 |
+
if re.search("[๊ฐ-ํฃ]", processed_prompt):
|
| 224 |
+
final_prompt = translate_prompt_to_english(processed_prompt)
|
| 225 |
+
else:
|
| 226 |
+
final_prompt = processed_prompt
|
| 227 |
+
else:
|
| 228 |
+
if len(valid_images) == 1:
|
| 229 |
+
final_prompt = "Please creatively transform this image into a more vivid and artistic version. Do not include any text or watermarks in the generated image."
|
| 230 |
+
logger.info("Default prompt generated for single image")
|
| 231 |
+
elif len(valid_images) == 2:
|
| 232 |
+
final_prompt = "Please seamlessly composite these two images, integrating their key elements harmoniously into a single image. Do not include any text or watermarks in the generated image."
|
| 233 |
+
logger.info("Default prompt generated for two images")
|
| 234 |
+
else:
|
| 235 |
+
final_prompt = "Please creatively composite these three images, combining their main elements into a cohesive and natural scene. Do not include any text or watermarks in the generated image."
|
| 236 |
+
logger.info("Default prompt generated for three images")
|
| 237 |
+
|
| 238 |
+
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
| 239 |
+
if result_img is not None:
|
| 240 |
+
return result_img, status, final_prompt
|
| 241 |
+
else:
|
| 242 |
+
last_error = status
|
| 243 |
+
retry_count += 1
|
| 244 |
+
logger.warning(f"์ด๋ฏธ์ง ์์ฑ ์คํจ, ์ฌ์๋ {retry_count}/{max_retries}: {status}")
|
| 245 |
+
time.sleep(1)
|
| 246 |
+
except Exception as e:
|
| 247 |
+
last_error = str(e)
|
| 248 |
+
retry_count += 1
|
| 249 |
+
logger.exception(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์, ์ฌ์๋ {retry_count}/{max_retries}:")
|
| 250 |
+
time.sleep(1)
|
| 251 |
+
|
| 252 |
+
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt
|
| 253 |
+
|
| 254 |
+
def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progress()):
|
| 255 |
+
"""
|
| 256 |
+
์ฌ๋ฌ ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ฐจ๋ก๋๋ก ์์ฑํฉ๋๋ค.
|
| 257 |
+
"""
|
| 258 |
+
results = []
|
| 259 |
+
statuses = []
|
| 260 |
+
prompts = []
|
| 261 |
+
|
| 262 |
+
num_images = 4
|
| 263 |
+
max_retries = 3
|
| 264 |
+
|
| 265 |
+
progress(0, desc="์ด๋ฏธ์ง ์์ฑ ์ค๋น ์ค...")
|
| 266 |
+
|
| 267 |
+
for i in range(num_images):
|
| 268 |
+
progress((i / num_images), desc=f"{i+1}/{num_images} ์ด๋ฏธ์ง ์์ฑ ์ค...")
|
| 269 |
+
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt, i, max_retries)
|
| 270 |
+
|
| 271 |
+
if result_img is not None:
|
| 272 |
+
results.append(result_img)
|
| 273 |
+
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
| 274 |
+
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
| 275 |
+
else:
|
| 276 |
+
results.append(None)
|
| 277 |
+
statuses.append(f"์ด๋ฏธ์ง #{i+1} ์์ฑ ์คํจ: {status}")
|
| 278 |
+
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
| 279 |
+
|
| 280 |
+
time.sleep(1)
|
| 281 |
+
|
| 282 |
+
progress(1.0, desc="์ด๋ฏธ์ง ์์ฑ ์๋ฃ!")
|
| 283 |
+
|
| 284 |
+
while len(results) < 4:
|
| 285 |
+
results.append(None)
|
| 286 |
+
|
| 287 |
+
combined_status = "\n".join(statuses)
|
| 288 |
+
combined_prompts = "\n".join(prompts)
|
| 289 |
+
|
| 290 |
+
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
| 291 |
+
|
| 292 |
+
# =====================================================================
|
| 293 |
+
# ์๋๋ GFPGAN ์ฐธ์กฐ์ฝ๋๋ฅผ ํญ์ผ๋ก ์ถ๊ฐํ ๋ถ๋ถ (๊ธฐ๋ฅ ๊ตฌํ ๋ฐ UI ๊ฐ์ )
|
| 294 |
+
# ๊ธฐ์กด GFPGAN ๊ด๋ จ ์ฝ๋์์ version๊ณผ Rescaling factor ๊ด๋ จ UI๋ ์ ๊ฑฐํ๊ณ ,
|
| 295 |
+
# GFPGANv1.4๋ฅผ ๊ณ ์ ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
|
| 296 |
+
# =====================================================================
|
| 297 |
+
|
| 298 |
+
import sys
|
| 299 |
+
from torchvision.transforms import functional
|
| 300 |
+
sys.modules["torchvision.transforms.functional_tensor"] = functional
|
| 301 |
+
|
| 302 |
+
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
| 303 |
+
from gfpgan.utils import GFPGANer
|
| 304 |
+
from realesrgan.utils import RealESRGANer
|
| 305 |
+
|
| 306 |
+
import torch
|
| 307 |
+
import cv2
|
| 308 |
+
|
| 309 |
+
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋ (์ด๋ฏธ ์์ผ๋ฉด ๋ค์ด๋ก๋)
|
| 310 |
+
if not os.path.exists('realesr-general-x4v3.pth'):
|
| 311 |
+
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
| 312 |
+
if not os.path.exists('GFPGANv1.4.pth'):
|
| 313 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
| 314 |
+
if not os.path.exists('RestoreFormer.pth'):
|
| 315 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
|
| 316 |
+
|
| 317 |
+
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
| 318 |
+
model_path = 'realesr-general-x4v3.pth'
|
| 319 |
+
half = True if torch.cuda.is_available() else False
|
| 320 |
+
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
| 321 |
+
|
| 322 |
+
def upscaler(img, version, scale):
|
| 323 |
+
try:
|
| 324 |
+
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
| 325 |
+
if len(img.shape) == 3 and img.shape[2] == 4:
|
| 326 |
+
img_mode = 'RGBA'
|
| 327 |
+
elif len(img.shape) == 2:
|
| 328 |
+
img_mode = None
|
| 329 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 330 |
+
else:
|
| 331 |
+
img_mode = None
|
| 332 |
+
|
| 333 |
+
h, w = img.shape[0:2]
|
| 334 |
+
if h < 300:
|
| 335 |
+
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
| 336 |
+
|
| 337 |
+
face_enhancer = GFPGANer(
|
| 338 |
+
model_path=f'{version}.pth',
|
| 339 |
+
upscale=2,
|
| 340 |
+
arch='RestoreFormer' if version=='RestoreFormer' else 'clean',
|
| 341 |
+
channel_multiplier=2,
|
| 342 |
+
bg_upsampler=upsampler
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
try:
|
| 346 |
+
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
| 347 |
+
except RuntimeError as error:
|
| 348 |
+
print('์ค๋ฅ ๋ฐ์:', error)
|
| 349 |
+
|
| 350 |
+
try:
|
| 351 |
+
if scale != 2:
|
| 352 |
+
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
| 353 |
+
h, w = img.shape[0:2]
|
| 354 |
+
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
| 355 |
+
except Exception as error:
|
| 356 |
+
print('์๋ชป๋ ์ค์ผ์ผ ์
๋ ฅ:', error)
|
| 357 |
+
|
| 358 |
+
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 359 |
+
return output
|
| 360 |
+
except Exception as error:
|
| 361 |
+
print('์ ์ฒด ์์ธ ๋ฐ์:', error)
|
| 362 |
+
return None
|
| 363 |
+
|
| 364 |
+
# GFPGAN ์
์ค์ผ์ผ๋ฌ ํจ์ (๋ฒ์ ๊ณผ ์ค์ผ์ผ์ ๊ณ ์ )
|
| 365 |
+
def upscaler_korean(img):
|
| 366 |
+
return upscaler(img, "GFPGANv1.4", 2)
|
| 367 |
+
|
| 368 |
+
# =====================================================================
|
| 369 |
+
# Gradio Blocks๋ก ํญ UI ๊ตฌ์ฑ (Gradio 5.21 ๊ธฐ์ค)
|
| 370 |
+
# =====================================================================
|
| 371 |
+
|
| 372 |
+
with gr.Blocks() as demo:
|
| 373 |
+
with gr.Tabs():
|
| 374 |
+
with gr.TabItem("์ด์ปค๋จธ์ค ์ด๋ฏธ์ง ์์ฑ๊ธฐ"):
|
| 375 |
+
gr.HTML(
|
| 376 |
+
"""
|
| 377 |
+
<div style="text-align: center; margin-bottom: 1rem;">
|
| 378 |
+
<h1>์ด์ปค๋จธ์ค์ฉ ์ด๋ฏธ์ง ์์ฑ๊ธฐ</h1>
|
| 379 |
+
<p>์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ์์ ๋ฅผ ์ฐธ๊ฐํ์ฌ ํ๋กฌํํธ๋ฅผ ์์ ํ ํ "์ด๋ฏธ์ง ์์ฑ" ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฐจ๋ก๋ก 4์ฅ์ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.</p>
|
| 380 |
+
</div>
|
| 381 |
+
"""
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
with gr.Row():
|
| 385 |
+
with gr.Column(scale=1):
|
| 386 |
+
with gr.Row():
|
| 387 |
+
image1_input = gr.Image(type="pil", label="#1", image_mode="RGB", height=300, width=200)
|
| 388 |
+
image2_input = gr.Image(type="pil", label="#2", image_mode="RGB", height=300, width=200)
|
| 389 |
+
image3_input = gr.Image(type="pil", label="#3", image_mode="RGB", height=300, width=200)
|
| 390 |
+
prompt_input = gr.Textbox(
|
| 391 |
+
lines=3,
|
| 392 |
+
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ํฉ์ฑ๋ฉ๋๋ค.",
|
| 393 |
+
label="ํ๋กฌํํธ (์ ํ ์ฌํญ)"
|
| 394 |
+
)
|
| 395 |
+
with gr.Row():
|
| 396 |
+
image_change_btn1 = gr.Button("์ด๋ฏธ์ง ๋ณ๊ฒฝ-1")
|
| 397 |
+
image_change_btn2 = gr.Button("์ด๋ฏธ์ง ๋ณ๊ฒฝ-2")
|
| 398 |
+
text_remove_btn = gr.Button("๊ธ์ ์ง์ฐ๊ธฐ")
|
| 399 |
+
text_change_btn = gr.Button("๊ธ์ ๋ณ๊ฒฝํ๊ธฐ")
|
| 400 |
+
clothes_change_btn1 = gr.Button("๊ฐ์ ์ํ์ฐฉ์ฉ-1")
|
| 401 |
+
clothes_change_btn2 = gr.Button("๊ฐ์ ์ํ์ฐฉ์ฉ-2")
|
| 402 |
+
holding_product_btn = gr.Button("์ํ๋ค๊ณ ์๊ธฐ")
|
| 403 |
+
background_change_btn = gr.Button("๋ฐฐ๊ฒฝ ๋ฐ๊พธ๊ธฐ")
|
| 404 |
+
composite_product_btn = gr.Button("๋ถ๋ถ ์ง์ฐ๊ธฐ")
|
| 405 |
+
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ (4์ฅ)", variant="primary")
|
| 406 |
+
|
| 407 |
+
gr.Markdown(
|
| 408 |
+
"""
|
| 409 |
+
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
| 410 |
+
|
| 411 |
+
1. **์๋ ํฉ์ฑ**: ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ํ๋กฌํํธ๋ฅผ ๋น์๋๋ฉด ์๋์ผ๋ก ํฉ์ฑ๋ฉ๋๋ค.
|
| 412 |
+
2. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
|
| 413 |
+
3. **์ ํ ์ต์
**: ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ๋กฌํํธ ์
๋ ฅ๋์ ํ๊ตญ์ด ๋ฌธ๊ตฌ๋ก ์
๋ ฅํ์๋ฉด ๋ฉ๋๋ค.
|
| 414 |
+
4. **๋ค์ํ ์ด๋ฏธ์ง**: "์ด๋ฏธ์ง ์์ฑ" ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฐจ๋ก๋ก 4์ฅ์ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.
|
| 415 |
+
5. **์์ ์ ํ**: ๋ค์ํ ์์ ๋ฅผ ํตํด ๋ฏธ๋ฆฌ ํ
์คํธํด๋ณด์ธ์.
|
| 416 |
+
|
| 417 |
+
> **ํ**: ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.
|
| 418 |
+
"""
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
gr.Markdown("## ์์ ์ด๋ฏธ์ง")
|
| 422 |
+
examples = [
|
| 423 |
+
["down/๋ชจ๋ธ.jpg", None, None, "(#1์ ์ฌ์ฑ)์ด ์ด์ง ๋ค๋ก ๋์๋ณด๋ ๋ชจ์ต์ผ๋ก ์ต๋ํ ์ด์ seed๋ฅผ ์ ์งํํ
์์ฐ์ค๋ฝ๊ฒ ๋ณ๊ฒฝํ๋ผ."],
|
| 424 |
+
["down/์์ด๋ ๊ณ ๋ชจํ.png", None, None, "(#1 ๋ ๋ชจ๋ชจํ)์์ ์ฒญ์์์ด๋ ๊ณ ๋ง ๊ฒ์์ ๊ณ ๋๋ ๊ณ ๋ก ๋ณ๊ฒฝํ๊ณ ๋๋จธ์ง ๋ถ๋ถ์ seed๋ฅผ ๋ณ๊ฒฝ์ ํ์ง๋ง๋ผ."],
|
| 425 |
+
["down/์ค๊ตญ์ด.png", None, None, "(#1 ์ด๋ฏธ์ง)์ ์๋ ์ค๊ตญ์ด๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ๋ผ."],
|
| 426 |
+
["down/ํ
์คํธ.webp", None, None, '(#1์ ํ
์คํธ)๋ฅผ ์คํ์ผ์ ์ ์งํ์ฒด ํ
์คํธ๋ง "Hello"๋ก ๋ฐ๊ฟ๋ผ'],
|
| 427 |
+
["down/๋ชจ๋ธ.jpg", "down/์ ๊ธ๋ผ์ค.png", "down/์ฒญ๋ฐ์ง.png", "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)์ (#3์ ์ฒญ๋ฐ์ง)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ๊ฒ ์ฒ๋ผ ์์ฐ์ค๋ฝ๊ฒ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ."],
|
| 428 |
+
["down/๋ชจ๋ธ.jpg", "down/์ ๊ธ๋ผ์ค.png", "down/์นดํ์ ๊ฒฝ.png", "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ ๊ฒ์ฒ๋ผ (#3์ ์ฅ์)์์ ์์์ ์์ ์๋ ์์ฐ์ค๋ฌ์ด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ."],
|
| 429 |
+
["down/๋ชจ๋ธ.jpg", "down/์์ธ์.png", None, "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์์ธ์)์ ์ฌ์ฑ๋ชจ๋ธ์ด ํ๋ณดํ ์์ธ์์ ๋๋ณด์ด๊ฒ ๋ค๊ณ ์๋ ์์ฐ์ค๋ฌ์ด ๋ชจ์ต์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ."],
|
| 430 |
+
["down/๋ชจ๋ธ.jpg", "down/์นดํ์ ๊ฒฝ.png", None, "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด (#2 ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ)์ ์ฃผ์ ํผ์ฌ์ฒด๋ ๊ทธ๋๋ก ์ ์งํ์ฌ ์ด๋ฏธ์ง์ ๋ถ์๊ธฐ๊ฐ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์์ฑํ๋ผ."],
|
| 431 |
+
["down/์์ด๋ ๊ณ ๋ชจํ.png", None, None, "(#1์ ๋ ๊ณ ๋ชจํ)์์ ์ฒญ์์์ด๋ ๊ณ ๋ฅผ ์ ๊ฑฐํ ํ, ๊ทธ ์๋ฆฌ๋ฅผ ์ฃผ๋ณ ๋ฐฐ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์ฑ์์ฃผ์ธ์. ๋จ, ์ด๋ฏธ์ง์ ๋ค๋ฅธ ๋ถ๋ถ์ ์ฃผ์ ์์๋ ๋์ผํ๊ฒ ์ ์งํด ํด์ผํ๋ค."]
|
| 432 |
+
]
|
| 433 |
+
|
| 434 |
+
gr.Examples(
|
| 435 |
+
examples=examples,
|
| 436 |
+
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
| 437 |
+
elem_id="examples-grid"
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
with gr.Column(scale=1):
|
| 441 |
+
with gr.Row():
|
| 442 |
+
with gr.Column():
|
| 443 |
+
output_image1 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #1", height=600, width=450)
|
| 444 |
+
output_image3 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #3", height=600, width=450)
|
| 445 |
+
with gr.Column():
|
| 446 |
+
output_image2 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #2", height=600, width=450)
|
| 447 |
+
output_image4 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #4", height=600, width=450)
|
| 448 |
+
|
| 449 |
+
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง", lines=4)
|
| 450 |
+
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", visible=True, lines=4)
|
| 451 |
+
|
| 452 |
+
image_change_btn1.click(
|
| 453 |
+
fn=lambda: "(#1์ ์ฌ์ฑ)์ด ์ด์ง ๋ค๋ก ๋์๋ณด๋ ๋ชจ์ต์ผ๋ก ์ต๋ํ ์ด์ seed๋ฅผ ์ ์งํํ
์์ฐ์ค๋ฝ๊ฒ ๋ณ๊ฒฝํ๋ผ.",
|
| 454 |
+
inputs=[],
|
| 455 |
+
outputs=prompt_input
|
| 456 |
+
)
|
| 457 |
+
image_change_btn2.click(
|
| 458 |
+
fn=lambda: "(#1 ๋ ๋ชจ๋ชจํ)์์ ์ฒญ์์์ด๋ ๊ณ ๋ง ๊ฒ์์ ๊ณ ๋๋ ๊ณ ๋ก ๋ณ๊ฒฝํ๊ณ ๋๋จธ์ง ๋ถ๋ถ์ seed๋ฅผ ๋ณ๊ฒฝ์ ํ์ง๋ง๋ผ.",
|
| 459 |
+
inputs=[],
|
| 460 |
+
outputs=prompt_input
|
| 461 |
+
)
|
| 462 |
+
text_remove_btn.click(
|
| 463 |
+
fn=lambda: "(#1 ์ด๋ฏธ์ง)์ ์๋ ์ค๊ตญ์ด๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ๋ผ.",
|
| 464 |
+
inputs=[],
|
| 465 |
+
outputs=prompt_input
|
| 466 |
+
)
|
| 467 |
+
text_change_btn.click(
|
| 468 |
+
fn=lambda: '(#1์ ํ
์คํธ)๋ฅผ ์คํ์ผ์ ์ ์งํ์ฒด ํ
์คํธ๋ง "Hello"๋ก ๋ฐ๊ฟ๋ผ',
|
| 469 |
+
inputs=[],
|
| 470 |
+
outputs=prompt_input
|
| 471 |
+
)
|
| 472 |
+
clothes_change_btn1.click(
|
| 473 |
+
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๊ฒ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)์ (#3์ ์ฒญ๋ฐ์ง)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ๊ฒ ์ฒ๋ผ ์์ฐ์ค๋ฝ๊ฒ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ.",
|
| 474 |
+
inputs=[],
|
| 475 |
+
outputs=prompt_input
|
| 476 |
+
)
|
| 477 |
+
clothes_change_btn2.click(
|
| 478 |
+
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ ๊ฒ์ฒ๋ผ (#3์ ์ฅ์)์์ ์์์ ์์ ์๋ ์์ฐ์ค๋ฌ์ด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ.",
|
| 479 |
+
inputs=[],
|
| 480 |
+
outputs=prompt_input
|
| 481 |
+
)
|
| 482 |
+
holding_product_btn.click(
|
| 483 |
+
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์์ธ์)์ ์ฌ์ฑ๋ชจ๋ธ์ด ํ๋ณดํ ์์ธ์์ ๋๋ณด์ด๊ฒ ๋ค๊ณ ์๋ ์์ฐ์ค๋ฌ์ด ๋ชจ์ต์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ.",
|
| 484 |
+
inputs=[],
|
| 485 |
+
outputs=prompt_input
|
| 486 |
+
)
|
| 487 |
+
background_change_btn.click(
|
| 488 |
+
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด (#2 ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ)์ ์ฃผ์ ํผ์ฌ์ฒด๋ ๊ทธ๋๋ก ์ ์งํ์ฌ ๋ ์ด๋ฏธ์ง์ ๋ถ์๊ธฐ๊ฐ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์์ฑํ๋ผ.",
|
| 489 |
+
inputs=[],
|
| 490 |
+
outputs=prompt_input
|
| 491 |
+
)
|
| 492 |
+
composite_product_btn.click(
|
| 493 |
+
fn=lambda: "(#1์ ๋ ๊ณ ๋ชจํ)์์ ์ฒญ์์์ด๋ ๊ณ ๋ฅผ ์ ๊ฑฐํ ํ, ๊ทธ ์๋ฆฌ๋ฅผ ์ฃผ๋ณ ๋ฐฐ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์ฑ์์ฃผ์ธ์. ๋จ, ์ด๋ฏธ์ง์ ๋ค๋ฅธ ๋ถ๋ถ์ ์ฃผ์ ์์๋ ๋์ผํ๊ฒ ์ ์งํด ํด์ผํ๋ค.",
|
| 494 |
+
inputs=[],
|
| 495 |
+
outputs=prompt_input
|
| 496 |
+
)
|
| 497 |
+
submit_btn.click(
|
| 498 |
+
fn=generate_multiple_images,
|
| 499 |
+
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
| 500 |
+
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
| 501 |
+
)
|
| 502 |
+
|
| 503 |
+
with gr.TabItem("GFPGAN ์
์ค์ผ์ผ๋ฌ"):
|
| 504 |
+
gr.Markdown("<h1 style='text-align: center;'>GFPGAN ์
์ค์ผ์ผ๋ฌ ๋ฐ ๋ณต์</h1>")
|
| 505 |
+
gr.Markdown("์
๋ ฅ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด GFPGANv1.4 ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์ผ๊ตด ๋ณต์ ๋ฐ ์
์ค์ผ์ผ๋ง์ ์ํํฉ๋๋ค.")
|
| 506 |
+
with gr.Row():
|
| 507 |
+
gfpgan_input = gr.Image(type="filepath", label="์
๋ ฅ ์ด๋ฏธ์ง", image_mode="RGB")
|
| 508 |
+
gfpgan_output = gr.Image(type="numpy", label="์ถ๋ ฅ ์ด๋ฏธ์ง")
|
| 509 |
+
gfpgan_btn = gr.Button("์
์ค์ผ์ผ ๋ฐ ๋ณต์")
|
| 510 |
+
gfpgan_btn.click(fn=upscaler_korean, inputs=gfpgan_input, outputs=gfpgan_output)
|
| 511 |
+
|
| 512 |
+
demo.queue()
|
| 513 |
+
demo.launch()
|