Spaces:
Running
on
Zero
Running
on
Zero
Update app-backup.py
Browse files- app-backup.py +284 -66
app-backup.py
CHANGED
|
@@ -18,14 +18,55 @@ from refiners.fluxion.utils import no_grad
|
|
| 18 |
from refiners.solutions import BoxSegmenter
|
| 19 |
from transformers import GroundingDinoForObjectDetection, GroundingDinoProcessor
|
| 20 |
from diffusers import FluxPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
BoundingBox = tuple[int, int, int, int]
|
| 23 |
|
| 24 |
pillow_heif.register_heif_opener()
|
| 25 |
pillow_heif.register_avif_opener()
|
| 26 |
|
| 27 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 28 |
-
|
| 29 |
# HF ํ ํฐ ์ค์
|
| 30 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 31 |
if HF_TOKEN is None:
|
|
@@ -50,9 +91,12 @@ assert isinstance(gd_model, GroundingDinoForObjectDetection)
|
|
| 50 |
# FLUX ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
| 51 |
pipe = FluxPipeline.from_pretrained(
|
| 52 |
"black-forest-labs/FLUX.1-dev",
|
| 53 |
-
torch_dtype=torch.
|
| 54 |
use_auth_token=HF_TOKEN
|
| 55 |
)
|
|
|
|
|
|
|
|
|
|
| 56 |
pipe.load_lora_weights(
|
| 57 |
hf_hub_download(
|
| 58 |
"ByteDance/Hyper-SD",
|
|
@@ -61,7 +105,13 @@ pipe.load_lora_weights(
|
|
| 61 |
)
|
| 62 |
)
|
| 63 |
pipe.fuse_lora(lora_scale=0.125)
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
class timer:
|
| 67 |
def __init__(self, method_name="timed process"):
|
|
@@ -135,80 +185,151 @@ def calculate_dimensions(aspect_ratio: str, base_size: int = 512) -> tuple[int,
|
|
| 135 |
return base_size * 4 // 3, base_size
|
| 136 |
return base_size, base_size
|
| 137 |
|
|
|
|
| 138 |
def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
|
| 139 |
-
"""๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์์ฑ ํจ์"""
|
| 140 |
try:
|
| 141 |
-
# ์ ํ๋ ๋น์จ์ ๋ฐ๋ผ ํฌ๊ธฐ ๊ณ์ฐ
|
| 142 |
width, height = calculate_dimensions(aspect_ratio)
|
| 143 |
-
|
| 144 |
-
# 8์ ๋ฐฐ์๋ก ์กฐ์
|
| 145 |
width, height = adjust_size_to_multiple_of_8(width, height)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
with timer("Background generation"):
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
return image
|
| 157 |
except Exception as e:
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
def combine_with_background(foreground: Image.Image, background: Image.Image
|
|
|
|
| 162 |
"""์ ๊ฒฝ๊ณผ ๋ฐฐ๊ฒฝ ํฉ์ฑ ํจ์"""
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
@spaces.GPU
|
| 167 |
def _gpu_process(img: Image.Image, prompt: str | BoundingBox | None) -> tuple[Image.Image, BoundingBox | None, list[str]]:
|
| 168 |
time_log: list[str] = []
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
t0 = time.time()
|
| 171 |
-
|
| 172 |
-
time_log.append(f"
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
bbox = prompt
|
| 178 |
-
t0 = time.time()
|
| 179 |
-
mask = segmenter(img, bbox)
|
| 180 |
-
time_log.append(f"segment: {time.time() - t0}")
|
| 181 |
-
return mask, bbox, time_log
|
| 182 |
|
| 183 |
def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str | None = None, aspect_ratio: str = "1:1") -> tuple[tuple[Image.Image, Image.Image, Image.Image], gr.DownloadButton]:
|
| 184 |
try:
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
if bg_prompt:
|
| 196 |
background = generate_background(bg_prompt, aspect_ratio)
|
| 197 |
-
combined =
|
| 198 |
else:
|
| 199 |
combined = Image.alpha_composite(Image.new("RGBA", masked_alpha.size, "white"), masked_alpha)
|
| 200 |
|
| 201 |
-
|
| 202 |
-
bbox = thresholded.getbbox()
|
| 203 |
-
to_dl = masked_alpha.crop(bbox)
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
|
| 210 |
-
|
| 211 |
except Exception as e:
|
|
|
|
|
|
|
| 212 |
raise gr.Error(f"Processing failed: {str(e)}")
|
| 213 |
|
| 214 |
def on_change_bbox(prompts: dict[str, Any] | None):
|
|
@@ -218,19 +339,47 @@ def on_change_bbox(prompts: dict[str, Any] | None):
|
|
| 218 |
def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
|
| 219 |
return gr.update(interactive=bool(img and prompt))
|
| 220 |
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
try:
|
| 223 |
if img is None or prompt.strip() == "":
|
| 224 |
raise gr.Error("Please provide both image and prompt")
|
| 225 |
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
|
| 228 |
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
return results[1], results[2]
|
| 231 |
except Exception as e:
|
|
|
|
| 232 |
raise gr.Error(str(e))
|
| 233 |
-
|
|
|
|
|
|
|
| 234 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
| 235 |
try:
|
| 236 |
if img is None or box_input.strip() == "":
|
|
@@ -270,7 +419,7 @@ def update_box_button(img, box_input):
|
|
| 270 |
return gr.update(interactive=False, variant="secondary")
|
| 271 |
|
| 272 |
|
| 273 |
-
#
|
| 274 |
css = """
|
| 275 |
footer {display: none}
|
| 276 |
.main-title {
|
|
@@ -321,14 +470,27 @@ button.primary {
|
|
| 321 |
button.primary:hover {
|
| 322 |
background: #1976D2;
|
| 323 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
"""
|
| 325 |
|
| 326 |
-
# UI
|
|
|
|
|
|
|
|
|
|
| 327 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 328 |
gr.HTML("""
|
| 329 |
<div class="main-title">
|
| 330 |
-
<h1>๐จ
|
| 331 |
-
<p>Extract objects
|
| 332 |
</div>
|
| 333 |
""")
|
| 334 |
|
|
@@ -359,12 +521,51 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 359 |
visible=True,
|
| 360 |
scale=1
|
| 361 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
process_btn = gr.Button(
|
| 363 |
"Process",
|
| 364 |
variant="primary",
|
| 365 |
interactive=False
|
| 366 |
)
|
| 367 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
with gr.Column(scale=1):
|
| 369 |
with gr.Row():
|
| 370 |
combined_image = gr.Image(
|
|
@@ -396,23 +597,40 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 396 |
queue=False
|
| 397 |
)
|
| 398 |
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
bg_prompt.change(
|
| 404 |
-
fn=
|
| 405 |
inputs=bg_prompt,
|
| 406 |
-
outputs=aspect_ratio,
|
| 407 |
queue=False
|
| 408 |
)
|
| 409 |
|
| 410 |
process_btn.click(
|
| 411 |
fn=process_prompt,
|
| 412 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
outputs=[combined_image, extracted_image],
|
| 414 |
queue=True
|
| 415 |
)
|
| 416 |
|
| 417 |
-
|
| 418 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
from refiners.solutions import BoxSegmenter
|
| 19 |
from transformers import GroundingDinoForObjectDetection, GroundingDinoProcessor
|
| 20 |
from diffusers import FluxPipeline
|
| 21 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 22 |
+
import gc
|
| 23 |
+
|
| 24 |
+
def clear_memory():
|
| 25 |
+
"""๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ํจ์"""
|
| 26 |
+
gc.collect()
|
| 27 |
+
try:
|
| 28 |
+
if torch.cuda.is_available():
|
| 29 |
+
with torch.cuda.device(0): # ๋ช
์์ ์ผ๋ก device 0 ์ฌ์ฉ
|
| 30 |
+
torch.cuda.empty_cache()
|
| 31 |
+
except:
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
# GPU ์ค์
|
| 35 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
| 36 |
+
|
| 37 |
+
# GPU ์ค์ ์ try-except๋ก ๊ฐ์ธ๊ธฐ
|
| 38 |
+
if torch.cuda.is_available():
|
| 39 |
+
try:
|
| 40 |
+
with torch.cuda.device(0):
|
| 41 |
+
torch.cuda.empty_cache()
|
| 42 |
+
torch.backends.cudnn.benchmark = True
|
| 43 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 44 |
+
except:
|
| 45 |
+
print("Warning: Could not configure CUDA settings")
|
| 46 |
+
|
| 47 |
+
# ๋ฒ์ญ ๋ชจ๋ธ ์ด๊ธฐํ
|
| 48 |
+
model_name = "Helsinki-NLP/opus-mt-ko-en"
|
| 49 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 50 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to('cpu')
|
| 51 |
+
translator = pipeline("translation", model=model, tokenizer=tokenizer, device=-1)
|
| 52 |
+
|
| 53 |
+
def translate_to_english(text: str) -> str:
|
| 54 |
+
"""ํ๊ธ ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ"""
|
| 55 |
+
try:
|
| 56 |
+
if any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text):
|
| 57 |
+
translated = translator(text, max_length=128)[0]['translation_text']
|
| 58 |
+
print(f"Translated '{text}' to '{translated}'")
|
| 59 |
+
return translated
|
| 60 |
+
return text
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Translation error: {str(e)}")
|
| 63 |
+
return text
|
| 64 |
|
| 65 |
BoundingBox = tuple[int, int, int, int]
|
| 66 |
|
| 67 |
pillow_heif.register_heif_opener()
|
| 68 |
pillow_heif.register_avif_opener()
|
| 69 |
|
|
|
|
|
|
|
| 70 |
# HF ํ ํฐ ์ค์
|
| 71 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 72 |
if HF_TOKEN is None:
|
|
|
|
| 91 |
# FLUX ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
| 92 |
pipe = FluxPipeline.from_pretrained(
|
| 93 |
"black-forest-labs/FLUX.1-dev",
|
| 94 |
+
torch_dtype=torch.float16,
|
| 95 |
use_auth_token=HF_TOKEN
|
| 96 |
)
|
| 97 |
+
pipe.enable_attention_slicing(slice_size="auto")
|
| 98 |
+
|
| 99 |
+
# LoRA ๊ฐ์ค์น ๋ก๋
|
| 100 |
pipe.load_lora_weights(
|
| 101 |
hf_hub_download(
|
| 102 |
"ByteDance/Hyper-SD",
|
|
|
|
| 105 |
)
|
| 106 |
)
|
| 107 |
pipe.fuse_lora(lora_scale=0.125)
|
| 108 |
+
|
| 109 |
+
# GPU ์ค์ ์ try-except๋ก ๊ฐ์ธ๊ธฐ
|
| 110 |
+
try:
|
| 111 |
+
if torch.cuda.is_available():
|
| 112 |
+
pipe = pipe.to("cuda:0") # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"Warning: Could not move pipeline to CUDA: {str(e)}")
|
| 115 |
|
| 116 |
class timer:
|
| 117 |
def __init__(self, method_name="timed process"):
|
|
|
|
| 185 |
return base_size * 4 // 3, base_size
|
| 186 |
return base_size, base_size
|
| 187 |
|
| 188 |
+
@spaces.GPU(duration=20) # 40์ด์์ 20์ด๋ก ๊ฐ์
|
| 189 |
def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
|
|
|
|
| 190 |
try:
|
|
|
|
| 191 |
width, height = calculate_dimensions(aspect_ratio)
|
|
|
|
|
|
|
| 192 |
width, height = adjust_size_to_multiple_of_8(width, height)
|
| 193 |
|
| 194 |
+
max_size = 768
|
| 195 |
+
if width > max_size or height > max_size:
|
| 196 |
+
ratio = max_size / max(width, height)
|
| 197 |
+
width = int(width * ratio)
|
| 198 |
+
height = int(height * ratio)
|
| 199 |
+
width, height = adjust_size_to_multiple_of_8(width, height)
|
| 200 |
+
|
| 201 |
with timer("Background generation"):
|
| 202 |
+
try:
|
| 203 |
+
with torch.inference_mode():
|
| 204 |
+
image = pipe(
|
| 205 |
+
prompt=prompt,
|
| 206 |
+
width=width,
|
| 207 |
+
height=height,
|
| 208 |
+
num_inference_steps=8,
|
| 209 |
+
guidance_scale=4.0
|
| 210 |
+
).images[0]
|
| 211 |
+
except Exception as e:
|
| 212 |
+
print(f"Pipeline error: {str(e)}")
|
| 213 |
+
return Image.new('RGB', (width, height), 'white')
|
| 214 |
|
| 215 |
return image
|
| 216 |
except Exception as e:
|
| 217 |
+
print(f"Background generation error: {str(e)}")
|
| 218 |
+
return Image.new('RGB', (512, 512), 'white')
|
| 219 |
+
|
| 220 |
+
def create_position_grid():
|
| 221 |
+
return """
|
| 222 |
+
<div class="position-grid" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 150px; margin: auto;">
|
| 223 |
+
<button class="position-btn" data-pos="top-left">โ</button>
|
| 224 |
+
<button class="position-btn" data-pos="top-center">โ</button>
|
| 225 |
+
<button class="position-btn" data-pos="top-right">โ</button>
|
| 226 |
+
<button class="position-btn" data-pos="middle-left">โ</button>
|
| 227 |
+
<button class="position-btn" data-pos="middle-center">โข</button>
|
| 228 |
+
<button class="position-btn" data-pos="middle-right">โ</button>
|
| 229 |
+
<button class="position-btn" data-pos="bottom-left">โ</button>
|
| 230 |
+
<button class="position-btn" data-pos="bottom-center" data-default="true">โ</button>
|
| 231 |
+
<button class="position-btn" data-pos="bottom-right">โ</button>
|
| 232 |
+
</div>
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
+
def calculate_object_position(position: str, bg_size: tuple[int, int], obj_size: tuple[int, int]) -> tuple[int, int]:
|
| 236 |
+
"""์ค๋ธ์ ํธ์ ์์น ๊ณ์ฐ"""
|
| 237 |
+
bg_width, bg_height = bg_size
|
| 238 |
+
obj_width, obj_height = obj_size
|
| 239 |
+
|
| 240 |
+
positions = {
|
| 241 |
+
"top-left": (0, 0),
|
| 242 |
+
"top-center": ((bg_width - obj_width) // 2, 0),
|
| 243 |
+
"top-right": (bg_width - obj_width, 0),
|
| 244 |
+
"middle-left": (0, (bg_height - obj_height) // 2),
|
| 245 |
+
"middle-center": ((bg_width - obj_width) // 2, (bg_height - obj_height) // 2),
|
| 246 |
+
"middle-right": (bg_width - obj_width, (bg_height - obj_height) // 2),
|
| 247 |
+
"bottom-left": (0, bg_height - obj_height),
|
| 248 |
+
"bottom-center": ((bg_width - obj_width) // 2, bg_height - obj_height),
|
| 249 |
+
"bottom-right": (bg_width - obj_width, bg_height - obj_height)
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
return positions.get(position, positions["bottom-center"])
|
| 253 |
|
| 254 |
+
def resize_object(image: Image.Image, scale_percent: float) -> Image.Image:
|
| 255 |
+
"""์ค๋ธ์ ํธ ํฌ๊ธฐ ์กฐ์ """
|
| 256 |
+
width = int(image.width * scale_percent / 100)
|
| 257 |
+
height = int(image.height * scale_percent / 100)
|
| 258 |
+
return image.resize((width, height), Image.Resampling.LANCZOS)
|
| 259 |
|
| 260 |
+
def combine_with_background(foreground: Image.Image, background: Image.Image,
|
| 261 |
+
position: str = "bottom-center", scale_percent: float = 100) -> Image.Image:
|
| 262 |
"""์ ๊ฒฝ๊ณผ ๋ฐฐ๊ฒฝ ํฉ์ฑ ํจ์"""
|
| 263 |
+
# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์ค๋น
|
| 264 |
+
result = background.convert('RGBA')
|
| 265 |
+
|
| 266 |
+
# ์ค๋ธ์ ํธ ํฌ๊ธฐ ์กฐ์
|
| 267 |
+
scaled_foreground = resize_object(foreground, scale_percent)
|
| 268 |
+
|
| 269 |
+
# ์ค๋ธ์ ํธ ์์น ๊ณ์ฐ
|
| 270 |
+
x, y = calculate_object_position(position, result.size, scaled_foreground.size)
|
| 271 |
+
|
| 272 |
+
# ํฉ์ฑ
|
| 273 |
+
result.paste(scaled_foreground, (x, y), scaled_foreground)
|
| 274 |
+
return result
|
| 275 |
|
| 276 |
+
@spaces.GPU(duration=30) # 120์ด์์ 30์ด๋ก ๊ฐ์
|
| 277 |
def _gpu_process(img: Image.Image, prompt: str | BoundingBox | None) -> tuple[Image.Image, BoundingBox | None, list[str]]:
|
| 278 |
time_log: list[str] = []
|
| 279 |
+
try:
|
| 280 |
+
if isinstance(prompt, str):
|
| 281 |
+
t0 = time.time()
|
| 282 |
+
bbox = gd_detect(img, prompt)
|
| 283 |
+
time_log.append(f"detect: {time.time() - t0}")
|
| 284 |
+
if not bbox:
|
| 285 |
+
print(time_log[0])
|
| 286 |
+
raise gr.Error("No object detected")
|
| 287 |
+
else:
|
| 288 |
+
bbox = prompt
|
| 289 |
t0 = time.time()
|
| 290 |
+
mask = segmenter(img, bbox)
|
| 291 |
+
time_log.append(f"segment: {time.time() - t0}")
|
| 292 |
+
return mask, bbox, time_log
|
| 293 |
+
except Exception as e:
|
| 294 |
+
print(f"GPU process error: {str(e)}")
|
| 295 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str | None = None, aspect_ratio: str = "1:1") -> tuple[tuple[Image.Image, Image.Image, Image.Image], gr.DownloadButton]:
|
| 298 |
try:
|
| 299 |
+
# ์
๋ ฅ ์ด๋ฏธ์ง ํฌ๊ธฐ ์ ํ
|
| 300 |
+
max_size = 1024
|
| 301 |
+
if img.width > max_size or img.height > max_size:
|
| 302 |
+
ratio = max_size / max(img.width, img.height)
|
| 303 |
+
new_size = (int(img.width * ratio), int(img.height * ratio))
|
| 304 |
+
img = img.resize(new_size, Image.LANCZOS)
|
| 305 |
+
|
| 306 |
+
# CUDA ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ ์์
|
| 307 |
+
try:
|
| 308 |
+
if torch.cuda.is_available():
|
| 309 |
+
current_device = torch.cuda.current_device()
|
| 310 |
+
with torch.cuda.device(current_device):
|
| 311 |
+
torch.cuda.empty_cache()
|
| 312 |
+
except Exception as e:
|
| 313 |
+
print(f"CUDA memory management failed: {e}")
|
| 314 |
|
| 315 |
+
with torch.cuda.amp.autocast(enabled=torch.cuda.is_available()):
|
| 316 |
+
mask, bbox, time_log = _gpu_process(img, prompt)
|
| 317 |
+
masked_alpha = apply_mask(img, mask, defringe=True)
|
| 318 |
|
| 319 |
if bg_prompt:
|
| 320 |
background = generate_background(bg_prompt, aspect_ratio)
|
| 321 |
+
combined = background
|
| 322 |
else:
|
| 323 |
combined = Image.alpha_composite(Image.new("RGBA", masked_alpha.size, "white"), masked_alpha)
|
| 324 |
|
| 325 |
+
clear_memory()
|
|
|
|
|
|
|
| 326 |
|
| 327 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp:
|
| 328 |
+
combined.save(temp.name)
|
| 329 |
+
return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
|
|
|
|
|
|
|
|
|
|
| 330 |
except Exception as e:
|
| 331 |
+
clear_memory()
|
| 332 |
+
print(f"Processing error: {str(e)}")
|
| 333 |
raise gr.Error(f"Processing failed: {str(e)}")
|
| 334 |
|
| 335 |
def on_change_bbox(prompts: dict[str, Any] | None):
|
|
|
|
| 339 |
def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
|
| 340 |
return gr.update(interactive=bool(img and prompt))
|
| 341 |
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
| 345 |
+
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
| 346 |
+
scale_percent: float = 100) -> tuple[Image.Image, Image.Image]:
|
| 347 |
try:
|
| 348 |
if img is None or prompt.strip() == "":
|
| 349 |
raise gr.Error("Please provide both image and prompt")
|
| 350 |
|
| 351 |
+
print(f"Processing with position: {position}, scale: {scale_percent}")
|
| 352 |
+
|
| 353 |
+
try:
|
| 354 |
+
prompt = translate_to_english(prompt)
|
| 355 |
+
if bg_prompt:
|
| 356 |
+
bg_prompt = translate_to_english(bg_prompt)
|
| 357 |
+
except Exception as e:
|
| 358 |
+
print(f"Translation error (continuing with original text): {str(e)}")
|
| 359 |
+
|
| 360 |
results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
|
| 361 |
|
| 362 |
+
if bg_prompt:
|
| 363 |
+
try:
|
| 364 |
+
combined = combine_with_background(
|
| 365 |
+
foreground=results[2],
|
| 366 |
+
background=results[1],
|
| 367 |
+
position=position,
|
| 368 |
+
scale_percent=scale_percent
|
| 369 |
+
)
|
| 370 |
+
print(f"Combined image created with position: {position}")
|
| 371 |
+
return combined, results[2]
|
| 372 |
+
except Exception as e:
|
| 373 |
+
print(f"Combination error: {str(e)}")
|
| 374 |
+
return results[1], results[2]
|
| 375 |
+
|
| 376 |
return results[1], results[2]
|
| 377 |
except Exception as e:
|
| 378 |
+
print(f"Error in process_prompt: {str(e)}")
|
| 379 |
raise gr.Error(str(e))
|
| 380 |
+
finally:
|
| 381 |
+
clear_memory()
|
| 382 |
+
|
| 383 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
| 384 |
try:
|
| 385 |
if img is None or box_input.strip() == "":
|
|
|
|
| 419 |
return gr.update(interactive=False, variant="secondary")
|
| 420 |
|
| 421 |
|
| 422 |
+
# CSS ์ ์
|
| 423 |
css = """
|
| 424 |
footer {display: none}
|
| 425 |
.main-title {
|
|
|
|
| 470 |
button.primary:hover {
|
| 471 |
background: #1976D2;
|
| 472 |
}
|
| 473 |
+
.position-btn {
|
| 474 |
+
transition: all 0.3s ease;
|
| 475 |
+
}
|
| 476 |
+
.position-btn:hover {
|
| 477 |
+
background-color: #e3f2fd;
|
| 478 |
+
}
|
| 479 |
+
.position-btn.selected {
|
| 480 |
+
background-color: #2196F3;
|
| 481 |
+
color: white;
|
| 482 |
+
}
|
| 483 |
"""
|
| 484 |
|
| 485 |
+
# UI ๊ตฌ์ฑ
|
| 486 |
+
# UI ๊ตฌ์ฑ ๋ถ๋ถ์์ process_btn์ ์๋ก ์ด๋ํ๊ณ position_grid.click ๋ถ๋ถ ์ ๊ฑฐ
|
| 487 |
+
|
| 488 |
+
# UI ๊ตฌ์ฑ
|
| 489 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 490 |
gr.HTML("""
|
| 491 |
<div class="main-title">
|
| 492 |
+
<h1>๐จGiniGen Canvas</h1>
|
| 493 |
+
<p>AI Integrated Image Creator: Extract objects, generate backgrounds, and adjust ratios and positions to create complete images with AI.</p>
|
| 494 |
</div>
|
| 495 |
""")
|
| 496 |
|
|
|
|
| 521 |
visible=True,
|
| 522 |
scale=1
|
| 523 |
)
|
| 524 |
+
|
| 525 |
+
with gr.Row(visible=False) as object_controls:
|
| 526 |
+
with gr.Column(scale=1):
|
| 527 |
+
with gr.Row():
|
| 528 |
+
position = gr.State(value="bottom-center")
|
| 529 |
+
btn_top_left = gr.Button("โ")
|
| 530 |
+
btn_top_center = gr.Button("โ")
|
| 531 |
+
btn_top_right = gr.Button("โ")
|
| 532 |
+
with gr.Row():
|
| 533 |
+
btn_middle_left = gr.Button("โ")
|
| 534 |
+
btn_middle_center = gr.Button("โข")
|
| 535 |
+
btn_middle_right = gr.Button("โ")
|
| 536 |
+
with gr.Row():
|
| 537 |
+
btn_bottom_left = gr.Button("โ")
|
| 538 |
+
btn_bottom_center = gr.Button("โ")
|
| 539 |
+
btn_bottom_right = gr.Button("โ")
|
| 540 |
+
with gr.Column(scale=1):
|
| 541 |
+
scale_slider = gr.Slider(
|
| 542 |
+
minimum=10,
|
| 543 |
+
maximum=200,
|
| 544 |
+
value=50,
|
| 545 |
+
step=5,
|
| 546 |
+
label="Object Size (%)"
|
| 547 |
+
)
|
| 548 |
+
|
| 549 |
process_btn = gr.Button(
|
| 550 |
"Process",
|
| 551 |
variant="primary",
|
| 552 |
interactive=False
|
| 553 |
)
|
| 554 |
|
| 555 |
+
# ๊ฐ ๋ฒํผ์ ๋ํ ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
| 556 |
+
def update_position(new_position):
|
| 557 |
+
return new_position
|
| 558 |
+
|
| 559 |
+
btn_top_left.click(fn=lambda: update_position("top-left"), outputs=position)
|
| 560 |
+
btn_top_center.click(fn=lambda: update_position("top-center"), outputs=position)
|
| 561 |
+
btn_top_right.click(fn=lambda: update_position("top-right"), outputs=position)
|
| 562 |
+
btn_middle_left.click(fn=lambda: update_position("middle-left"), outputs=position)
|
| 563 |
+
btn_middle_center.click(fn=lambda: update_position("middle-center"), outputs=position)
|
| 564 |
+
btn_middle_right.click(fn=lambda: update_position("middle-right"), outputs=position)
|
| 565 |
+
btn_bottom_left.click(fn=lambda: update_position("bottom-left"), outputs=position)
|
| 566 |
+
btn_bottom_center.click(fn=lambda: update_position("bottom-center"), outputs=position)
|
| 567 |
+
btn_bottom_right.click(fn=lambda: update_position("bottom-right"), outputs=position)
|
| 568 |
+
|
| 569 |
with gr.Column(scale=1):
|
| 570 |
with gr.Row():
|
| 571 |
combined_image = gr.Image(
|
|
|
|
| 597 |
queue=False
|
| 598 |
)
|
| 599 |
|
| 600 |
+
def update_controls(bg_prompt):
|
| 601 |
+
"""๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์
๋ ฅ ์ฌ๋ถ์ ๋ฐ๋ผ ์ปจํธ๋กค ํ์ ์
๋ฐ์ดํธ"""
|
| 602 |
+
is_visible = bool(bg_prompt)
|
| 603 |
+
return [
|
| 604 |
+
gr.update(visible=is_visible), # aspect_ratio
|
| 605 |
+
gr.update(visible=is_visible), # object_controls
|
| 606 |
+
]
|
| 607 |
|
| 608 |
bg_prompt.change(
|
| 609 |
+
fn=update_controls,
|
| 610 |
inputs=bg_prompt,
|
| 611 |
+
outputs=[aspect_ratio, object_controls],
|
| 612 |
queue=False
|
| 613 |
)
|
| 614 |
|
| 615 |
process_btn.click(
|
| 616 |
fn=process_prompt,
|
| 617 |
+
inputs=[
|
| 618 |
+
input_image,
|
| 619 |
+
text_prompt,
|
| 620 |
+
bg_prompt,
|
| 621 |
+
aspect_ratio,
|
| 622 |
+
position,
|
| 623 |
+
scale_slider
|
| 624 |
+
],
|
| 625 |
outputs=[combined_image, extracted_image],
|
| 626 |
queue=True
|
| 627 |
)
|
| 628 |
|
| 629 |
+
|
| 630 |
+
demo.queue(max_size=10) # ํ ํฌ๊ธฐ ์ ํ
|
| 631 |
+
demo.launch(
|
| 632 |
+
server_name="0.0.0.0",
|
| 633 |
+
server_port=7860,
|
| 634 |
+
share=False,
|
| 635 |
+
max_threads=2 # ์ค๋ ๋ ์ ์ ํ
|
| 636 |
+
)
|