Abdullah-Habib commited on
Commit
85b1357
·
1 Parent(s): 46b2abb

added remove bg

Browse files
Files changed (2) hide show
  1. app.py +11 -4
  2. requirements.txt +2 -1
app.py CHANGED
@@ -3,6 +3,8 @@ import json
3
  import logging
4
  import torch
5
  import base64
 
 
6
  from io import BytesIO
7
  from PIL import Image
8
  from diffusers import (
@@ -35,8 +37,13 @@ def image_to_base64(image: Image) -> str:
35
  img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
36
  return img_base64
37
 
38
-
39
-
 
 
 
 
 
40
  def update_selection(evt: gr.SelectData):
41
  selected_lora = loras[evt.index]
42
  new_placeholder = f"Type a prompt for {selected_lora['title']}"
@@ -115,8 +122,8 @@ def run_lora(prompt, negative_prompt, cfg_scale, steps, scheduler, seed, width,
115
 
116
  # Unload LoRA weights
117
  pipe.unload_lora_weights()
118
-
119
- return image_to_base64(image)
120
 
121
  with gr.Blocks(theme=gr.themes.Soft()) as app:
122
 
 
3
  import logging
4
  import torch
5
  import base64
6
+ import rembg
7
+ import numpy as np
8
  from io import BytesIO
9
  from PIL import Image
10
  from diffusers import (
 
37
  img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
38
  return img_base64
39
 
40
+ def remove_bg(image: Image):
41
+ input_array_bg = np.array(image)
42
+ # Apply background removal using rembg
43
+ output_array_bg = rembg.remove(input_array_bg)
44
+ # Create a PIL Image from the output array
45
+ return Image.fromarray(output_array_bg)
46
+
47
  def update_selection(evt: gr.SelectData):
48
  selected_lora = loras[evt.index]
49
  new_placeholder = f"Type a prompt for {selected_lora['title']}"
 
122
 
123
  # Unload LoRA weights
124
  pipe.unload_lora_weights()
125
+ image_without_bg = remove_bg(image)
126
+ return image_to_base64(image_without_bg)
127
 
128
  with gr.Blocks(theme=gr.themes.Soft()) as app:
129
 
requirements.txt CHANGED
@@ -2,4 +2,5 @@ torch
2
  diffusers
3
  spaces
4
  transformers
5
- peft
 
 
2
  diffusers
3
  spaces
4
  transformers
5
+ peft
6
+ rembg