Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -304,11 +304,15 @@ def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str
|
|
| 304 |
new_size = (int(img.width * ratio), int(img.height * ratio))
|
| 305 |
img = img.resize(new_size, Image.LANCZOS)
|
| 306 |
|
| 307 |
-
# λ©λͺ¨λ¦¬
|
| 308 |
-
|
| 309 |
-
torch.cuda.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
-
with torch.cuda.amp.autocast():
|
| 312 |
mask, bbox, time_log = _gpu_process(img, prompt)
|
| 313 |
masked_alpha = apply_mask(img, mask, defringe=True)
|
| 314 |
|
|
@@ -318,11 +322,12 @@ def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str
|
|
| 318 |
else:
|
| 319 |
combined = Image.alpha_composite(Image.new("RGBA", masked_alpha.size, "white"), masked_alpha)
|
| 320 |
|
| 321 |
-
clear_memory()
|
| 322 |
|
| 323 |
return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
|
| 324 |
except Exception as e:
|
| 325 |
clear_memory()
|
|
|
|
| 326 |
raise gr.Error(f"Processing failed: {str(e)}")
|
| 327 |
|
| 328 |
def on_change_bbox(prompts: dict[str, Any] | None):
|
|
@@ -621,69 +626,66 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 621 |
|
| 622 |
# UI κ΅¬μ± λΆλΆμ λ§μ§λ§μμ μμ ν
μ΄λΈ λΆλΆμ μμ
|
| 623 |
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
<
|
| 628 |
-
|
| 629 |
-
<
|
| 630 |
-
<
|
| 631 |
-
<
|
| 632 |
-
<
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
<
|
| 639 |
-
<
|
| 640 |
-
|
| 641 |
-
<
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
<
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
<
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
<
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
<
|
| 659 |
-
|
| 660 |
-
<
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
<
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
<
|
| 668 |
-
|
| 669 |
-
<
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
<
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
</table>
|
| 678 |
-
</div>
|
| 679 |
</div>
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
demo.queue(max_size=10) # ν ν¬κΈ° μ ν
|
| 683 |
demo.launch(
|
| 684 |
server_name="0.0.0.0",
|
| 685 |
server_port=7860,
|
| 686 |
-
share=
|
| 687 |
-
max_threads=2,
|
| 688 |
-
allowed_paths=[
|
| 689 |
)
|
|
|
|
| 304 |
new_size = (int(img.width * ratio), int(img.height * ratio))
|
| 305 |
img = img.resize(new_size, Image.LANCZOS)
|
| 306 |
|
| 307 |
+
# CUDA λ©λͺ¨λ¦¬ κ΄λ¦¬ μμ
|
| 308 |
+
try:
|
| 309 |
+
if torch.cuda.is_available():
|
| 310 |
+
current_device = torch.cuda.current_device()
|
| 311 |
+
torch.cuda.reset_peak_memory_stats(current_device)
|
| 312 |
+
except Exception as e:
|
| 313 |
+
print(f"CUDA memory stats reset 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 |
|
|
|
|
| 322 |
else:
|
| 323 |
combined = Image.alpha_composite(Image.new("RGBA", masked_alpha.size, "white"), masked_alpha)
|
| 324 |
|
| 325 |
+
clear_memory()
|
| 326 |
|
| 327 |
return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
|
| 328 |
except Exception as e:
|
| 329 |
clear_memory()
|
| 330 |
+
print(f"Processing error: {str(e)}")
|
| 331 |
raise gr.Error(f"Processing failed: {str(e)}")
|
| 332 |
|
| 333 |
def on_change_bbox(prompts: dict[str, Any] | None):
|
|
|
|
| 626 |
|
| 627 |
# UI κ΅¬μ± λΆλΆμ λ§μ§λ§μμ μμ ν
μ΄λΈ λΆλΆμ μμ
|
| 628 |
|
| 629 |
+
# μμ ν
μ΄λΈ HTML μμ
|
| 630 |
+
gr.HTML("""
|
| 631 |
+
<div style="margin-top: 2em; padding: 1em;">
|
| 632 |
+
<h2 style="text-align: center; color: #2196F3; margin-bottom: 1em;">Examples</h2>
|
| 633 |
+
<div class="examples-container" style="overflow-x: auto;">
|
| 634 |
+
<table style="width: 100%; border-collapse: collapse; margin: auto; background: white; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
|
| 635 |
+
<thead>
|
| 636 |
+
<tr style="background: #f7f7f7;">
|
| 637 |
+
<th style="padding: 1em; border: 1px solid #ddd; min-width: 120px;">Step</th>
|
| 638 |
+
<th style="padding: 1em; border: 1px solid #ddd; min-width: 200px;">Example 1</th>
|
| 639 |
+
<th style="padding: 1em; border: 1px solid #ddd; min-width: 200px;">Example 2</th>
|
| 640 |
+
</tr>
|
| 641 |
+
</thead>
|
| 642 |
+
<tbody>
|
| 643 |
+
<tr>
|
| 644 |
+
<td style="padding: 1em; border: 1px solid #ddd;">Upload Image</td>
|
| 645 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">
|
| 646 |
+
<img src="https://raw.githubusercontent.com/your-username/your-repo/main/examples/example1_input.jpg" style="max-width: 150px; height: auto;" alt="Example 1 Input">
|
| 647 |
+
</td>
|
| 648 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">
|
| 649 |
+
<img src="https://raw.githubusercontent.com/your-username/your-repo/main/examples/example2_input.jpg" style="max-width: 150px; height: auto;" alt="Example 2 Input">
|
| 650 |
+
</td>
|
| 651 |
+
</tr>
|
| 652 |
+
<tr>
|
| 653 |
+
<td style="padding: 1em; border: 1px solid #ddd;">Object Extract</td>
|
| 654 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">text</td>
|
| 655 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">sunglasses</td>
|
| 656 |
+
</tr>
|
| 657 |
+
<tr>
|
| 658 |
+
<td style="padding: 1em; border: 1px solid #ddd;">Background Prompt</td>
|
| 659 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">The Alps in Spring with Blossoming Flowers</td>
|
| 660 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">νμμ΄ beach</td>
|
| 661 |
+
</tr>
|
| 662 |
+
<tr>
|
| 663 |
+
<td style="padding: 1em; border: 1px solid #ddd;">Object Cutoff Image</td>
|
| 664 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">
|
| 665 |
+
<img src="https://raw.githubusercontent.com/your-username/your-repo/main/examples/example1_cutoff.jpg" style="max-width: 150px; height: auto;" alt="Example 1 Cutoff">
|
| 666 |
+
</td>
|
| 667 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">
|
| 668 |
+
<img src="https://raw.githubusercontent.com/your-username/your-repo/main/examples/example2_cutoff.jpg" style="max-width: 150px; height: auto;" alt="Example 2 Cutoff">
|
| 669 |
+
</td>
|
| 670 |
+
</tr>
|
| 671 |
+
<tr>
|
| 672 |
+
<td style="padding: 1em; border: 1px solid #ddd;">Result Image</td>
|
| 673 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">
|
| 674 |
+
<img src="https://raw.githubusercontent.com/your-username/your-repo/main/examples/example1_result.jpg" style="max-width: 150px; height: auto;" alt="Example 1 Result">
|
| 675 |
+
</td>
|
| 676 |
+
<td style="padding: 1em; border: 1px solid #ddd; text-align: center;">
|
| 677 |
+
<img src="https://raw.githubusercontent.com/your-username/your-repo/main/examples/example2_result.jpg" style="max-width: 150px; height: auto;" alt="Example 2 Result">
|
| 678 |
+
</td>
|
| 679 |
+
</tr>
|
| 680 |
+
</tbody>
|
| 681 |
+
</table>
|
|
|
|
|
|
|
| 682 |
</div>
|
| 683 |
+
</div>
|
| 684 |
+
""")
|
|
|
|
| 685 |
demo.launch(
|
| 686 |
server_name="0.0.0.0",
|
| 687 |
server_port=7860,
|
| 688 |
+
share=True, # κ³΅κ° λ§ν¬ νμ±ν
|
| 689 |
+
max_threads=2,
|
| 690 |
+
allowed_paths=[examples_path] # μμ λ μμ κ²½λ‘ μ¬μ©
|
| 691 |
)
|