Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,11 @@ from transformers import AutoModelForImageSegmentation
|
|
| 6 |
import torch
|
| 7 |
from torchvision import transforms
|
| 8 |
|
| 9 |
-
# ๋ชจ๋ธ
|
| 10 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 11 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
| 12 |
)
|
| 13 |
-
#
|
| 14 |
|
| 15 |
transform_image = transforms.Compose(
|
| 16 |
[
|
|
@@ -22,20 +22,16 @@ transform_image = transforms.Compose(
|
|
| 22 |
|
| 23 |
@spaces.GPU
|
| 24 |
def fn(image):
|
| 25 |
-
# GPU ํ ๋น ์ ๋ชจ๋ธ์ CUDA๋ก ์ด๋
|
| 26 |
-
birefnet.to("cuda")
|
| 27 |
im = load_img(image, output_type="pil")
|
| 28 |
im = im.convert("RGB")
|
| 29 |
origin = im.copy()
|
| 30 |
processed_image = process(im)
|
| 31 |
-
# ์์
์๋ฃ ํ ๋ชจ๋ธ์ CPU๋ก ์ด๋
|
| 32 |
-
birefnet.to("cpu")
|
| 33 |
return (processed_image, origin)
|
| 34 |
|
| 35 |
def process(image):
|
| 36 |
image_size = image.size
|
| 37 |
-
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
| 38 |
-
#
|
| 39 |
with torch.no_grad():
|
| 40 |
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
| 41 |
pred = preds[0].squeeze()
|
|
@@ -46,13 +42,11 @@ def process(image):
|
|
| 46 |
|
| 47 |
@spaces.GPU
|
| 48 |
def process_file(f):
|
| 49 |
-
birefnet.to("cuda")
|
| 50 |
name_path = f.rsplit(".", 1)[0] + ".png"
|
| 51 |
im = load_img(f, output_type="pil")
|
| 52 |
im = im.convert("RGB")
|
| 53 |
transparent = process(im)
|
| 54 |
transparent.save(name_path)
|
| 55 |
-
birefnet.to("cpu")
|
| 56 |
return name_path
|
| 57 |
|
| 58 |
slider1 = ImageSlider(label="Processed Image", type="pil")
|
|
@@ -62,7 +56,7 @@ image_file_upload = gr.Image(label="Upload an image", type="filepath")
|
|
| 62 |
url_input = gr.Textbox(label="Paste an image URL")
|
| 63 |
output_file = gr.File(label="Output PNG File")
|
| 64 |
|
| 65 |
-
#
|
| 66 |
chameleon = load_img("butterfly.jpg", output_type="pil")
|
| 67 |
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
| 68 |
|
|
|
|
| 6 |
import torch
|
| 7 |
from torchvision import transforms
|
| 8 |
|
| 9 |
+
# ๋ชจ๋ธ ๋ก๋ฉ ๋ฐ GPU ํ๊ฒฝ ์ค์
|
| 10 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 11 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
| 12 |
)
|
| 13 |
+
birefnet.to("cuda") # ZeroGPU ํ๊ฒฝ์์๋ GPU("cuda") ์ฌ์ฉ
|
| 14 |
|
| 15 |
transform_image = transforms.Compose(
|
| 16 |
[
|
|
|
|
| 22 |
|
| 23 |
@spaces.GPU
|
| 24 |
def fn(image):
|
|
|
|
|
|
|
| 25 |
im = load_img(image, output_type="pil")
|
| 26 |
im = im.convert("RGB")
|
| 27 |
origin = im.copy()
|
| 28 |
processed_image = process(im)
|
|
|
|
|
|
|
| 29 |
return (processed_image, origin)
|
| 30 |
|
| 31 |
def process(image):
|
| 32 |
image_size = image.size
|
| 33 |
+
input_images = transform_image(image).unsqueeze(0).to("cuda") # ์
๋ ฅ๋ GPU๋ก ์ ๋ฌ
|
| 34 |
+
# ์์ธก ์ํ
|
| 35 |
with torch.no_grad():
|
| 36 |
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
| 37 |
pred = preds[0].squeeze()
|
|
|
|
| 42 |
|
| 43 |
@spaces.GPU
|
| 44 |
def process_file(f):
|
|
|
|
| 45 |
name_path = f.rsplit(".", 1)[0] + ".png"
|
| 46 |
im = load_img(f, output_type="pil")
|
| 47 |
im = im.convert("RGB")
|
| 48 |
transparent = process(im)
|
| 49 |
transparent.save(name_path)
|
|
|
|
| 50 |
return name_path
|
| 51 |
|
| 52 |
slider1 = ImageSlider(label="Processed Image", type="pil")
|
|
|
|
| 56 |
url_input = gr.Textbox(label="Paste an image URL")
|
| 57 |
output_file = gr.File(label="Output PNG File")
|
| 58 |
|
| 59 |
+
# Example images
|
| 60 |
chameleon = load_img("butterfly.jpg", output_type="pil")
|
| 61 |
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
| 62 |
|