Kims12 commited on
Commit
842ce77
ยท
verified ยท
1 Parent(s): e7c00bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -11
app.py CHANGED
@@ -6,11 +6,11 @@ from transformers import AutoModelForImageSegmentation
6
  import torch
7
  from torchvision import transforms
8
 
9
- # ๋ชจ๋ธ์„ ์ „์—ญ์œผ๋กœ ๋กœ๋“œ (๊ธฐ๋ณธ์ ์œผ๋กœ CPU์— ๋กœ๋“œ)
10
  birefnet = AutoModelForImageSegmentation.from_pretrained(
11
  "ZhengPeng7/BiRefNet", trust_remote_code=True
12
  )
13
- # GPU ํ™˜๊ฒฝ์—์„œ๋งŒ ๋ชจ๋ธ์„ GPU๋กœ ์ด๋™
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
- # Prediction
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