Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -20,20 +20,24 @@ 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:
|
@@ -481,21 +485,61 @@ button.primary:hover {
|
|
481 |
}
|
482 |
"""
|
483 |
|
484 |
-
###--------------ZERO GPU 필수/ 메모리 관리 공통 --------------------###
|
485 |
-
def clear_memory():
|
486 |
-
gc.collect()
|
487 |
-
if torch.cuda.is_available():
|
488 |
-
try:
|
489 |
-
torch.cuda.empty_cache()
|
490 |
-
torch.cuda.synchronize()
|
491 |
-
except:
|
492 |
-
pass
|
493 |
|
|
|
|
|
|
|
494 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
495 |
|
496 |
-
#
|
497 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
498 |
|
|
|
499 |
# UI 구성
|
500 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
501 |
gr.HTML("""
|
@@ -505,46 +549,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
505 |
</div>
|
506 |
""")
|
507 |
|
508 |
-
|
509 |
-
|
510 |
-
# 예제 설명 섹션 추가
|
511 |
-
gr.HTML("""
|
512 |
-
<div style="margin-top: 50px; padding: 20px; background-color: #f8f9fa; border-radius: 10px;">
|
513 |
-
<h2 style="text-align: center; color: #2196F3; margin-bottom: 30px;">How It Works: Step by Step Example</h2>
|
514 |
-
|
515 |
-
<div style="display: flex; justify-content: space-around; align-items: center; flex-wrap: wrap; gap: 20px;">
|
516 |
-
<div style="text-align: center; flex: 1; min-width: 250px; max-width: 300px;">
|
517 |
-
<img src="file/aa1.png" style="width: 100%; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
518 |
-
<h3 style="color: #333; margin: 15px 0;">Step 1: Original Image</h3>
|
519 |
-
<p style="color: #666;">Upload your original image that contains the object you want to extract.</p>
|
520 |
-
</div>
|
521 |
-
|
522 |
-
<div style="text-align: center; flex: 1; min-width: 250px; max-width: 300px;">
|
523 |
-
<img src="file/aa2.png" style="width: 100%; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
524 |
-
<h3 style="color: #333; margin: 15px 0;">Step 2: Extracted Object</h3>
|
525 |
-
<p style="color: #666;">Use prompts to identify and extract specific objects from your image.</p>
|
526 |
-
</div>
|
527 |
-
|
528 |
-
<div style="text-align: center; flex: 1; min-width: 250px; max-width: 300px;">
|
529 |
-
<img src="file/aa3.png" style="width: 100%; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
530 |
-
<h3 style="color: #333; margin: 15px 0;">Step 3: Final Result</h3>
|
531 |
-
<p style="color: #666;">The extracted object is placed on an AI-generated background based on your description.</p>
|
532 |
-
</div>
|
533 |
-
</div>
|
534 |
-
|
535 |
-
<div style="margin-top: 30px; text-align: center; padding: 20px; background-color: #e3f2fd; border-radius: 8px;">
|
536 |
-
<h4 style="color: #1976D2; margin-bottom: 10px;">Key Features:</h4>
|
537 |
-
<ul style="list-style: none; padding: 0;">
|
538 |
-
<li style="margin: 5px 0;">✨ Precise object extraction using AI</li>
|
539 |
-
<li style="margin: 5px 0;">🎨 Custom background generation</li>
|
540 |
-
<li style="margin: 5px 0;">🔄 Flexible object positioning and sizing</li>
|
541 |
-
<li style="margin: 5px 0;">📐 Multiple aspect ratio support</li>
|
542 |
-
</ul>
|
543 |
-
</div>
|
544 |
-
</div>
|
545 |
-
""")
|
546 |
-
|
547 |
-
# 나머지 기존 코드...
|
548 |
|
549 |
with gr.Row():
|
550 |
with gr.Column(scale=1):
|
|
|
20 |
from diffusers import FluxPipeline
|
21 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
22 |
import gc
|
23 |
+
import base64
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# GPU 설정
|
27 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # 명시적으로 cuda:0 지정
|
28 |
|
29 |
+
###--------------ZERO GPU 필수/ 메모리 관리 공통 --------------------###
|
30 |
+
def clear_memory():
|
31 |
+
gc.collect()
|
32 |
+
if torch.cuda.is_available():
|
33 |
+
try:
|
34 |
+
torch.cuda.empty_cache()
|
35 |
+
torch.cuda.synchronize()
|
36 |
+
except:
|
37 |
+
pass
|
38 |
+
|
39 |
+
###---------------------------------------------------------------
|
40 |
+
|
41 |
# GPU 설정을 try-except로 감싸기
|
42 |
if torch.cuda.is_available():
|
43 |
try:
|
|
|
485 |
}
|
486 |
"""
|
487 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
488 |
|
489 |
+
def get_image_base64(image_path):
|
490 |
+
with open(image_path, "rb") as image_file:
|
491 |
+
return base64.b64encode(image_file.read()).decode()
|
492 |
|
493 |
+
# 이미지를 Base64로 변환
|
494 |
+
try:
|
495 |
+
example_img1 = get_image_base64("aa1.png")
|
496 |
+
example_img2 = get_image_base64("aa2.png")
|
497 |
+
example_img3 = get_image_base64("aa3.png")
|
498 |
+
except Exception as e:
|
499 |
+
print(f"Error loading example images: {e}")
|
500 |
+
example_img1 = example_img2 = example_img3 = ""
|
501 |
|
502 |
+
# HTML 템플릿 수정
|
503 |
+
example_html = f"""
|
504 |
+
<div style="margin-top: 50px; padding: 20px; background-color: #f8f9fa; border-radius: 10px;">
|
505 |
+
<h2 style="text-align: center; color: #2196F3; margin-bottom: 30px;">How It Works: Step by Step Guide</h2>
|
506 |
+
|
507 |
+
<div style="display: flex; justify-content: space-around; align-items: center; flex-wrap: wrap; gap: 20px;">
|
508 |
+
<div style="text-align: center; flex: 1; min-width: 250px; max-width: 300px;">
|
509 |
+
<img src="data:image/png;base64,{example_img1}"
|
510 |
+
style="width: 100%; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
511 |
+
<h3 style="color: #333; margin: 15px 0;">Step 1: Original Image</h3>
|
512 |
+
<p style="color: #666;">Upload your original image containing the object you want to extract.</p>
|
513 |
+
</div>
|
514 |
+
|
515 |
+
<div style="text-align: center; flex: 1; min-width: 250px; max-width: 300px;">
|
516 |
+
<img src="data:image/png;base64,{example_img2}"
|
517 |
+
style="width: 100%; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
518 |
+
<h3 style="color: #333; margin: 15px 0;">Step 2: Object Extraction</h3>
|
519 |
+
<p style="color: #666;">AI automatically detects and extracts the specified object.</p>
|
520 |
+
</div>
|
521 |
+
|
522 |
+
<div style="text-align: center; flex: 1; min-width: 250px; max-width: 300px;">
|
523 |
+
<img src="data:image/png;base64,{example_img3}"
|
524 |
+
style="width: 100%; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
525 |
+
<h3 style="color: #333; margin: 15px 0;">Step 3: Final Result</h3>
|
526 |
+
<p style="color: #666;">The extracted object is placed on an AI-generated background.</p>
|
527 |
+
</div>
|
528 |
+
</div>
|
529 |
+
|
530 |
+
<div style="margin-top: 30px; text-align: center; padding: 20px; background-color: #e3f2fd; border-radius: 8px;">
|
531 |
+
<h4 style="color: #1976D2; margin-bottom: 10px;">Key Features:</h4>
|
532 |
+
<ul style="list-style: none; padding: 0;">
|
533 |
+
<li style="margin: 5px 0;">✨ Advanced AI-powered object detection and extraction</li>
|
534 |
+
<li style="margin: 5px 0;">🎨 Custom background generation with text prompts</li>
|
535 |
+
<li style="margin: 5px 0;">🔄 Flexible object positioning and sizing options</li>
|
536 |
+
<li style="margin: 5px 0;">📐 Multiple aspect ratio support for various use cases</li>
|
537 |
+
</ul>
|
538 |
+
</div>
|
539 |
+
</div>
|
540 |
+
"""
|
541 |
|
542 |
+
|
543 |
# UI 구성
|
544 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
545 |
gr.HTML("""
|
|
|
549 |
</div>
|
550 |
""")
|
551 |
|
552 |
+
# 예제 섹션 추가
|
553 |
+
gr.HTML(example_html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
554 |
|
555 |
with gr.Row():
|
556 |
with gr.Column(scale=1):
|