DiZH797 commited on
Commit
894d4b7
·
verified ·
1 Parent(s): e8668d7

Update app.py

Browse files

added delete background

Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -5,6 +5,9 @@ import random
5
  # import spaces #[uncomment to use ZeroGPU]
6
  from diffusers import DiffusionPipeline
7
  from peft import PeftModel, PeftConfig
 
 
 
8
  import torch
9
  from typing import Optional
10
 
@@ -94,6 +97,7 @@ def infer(
94
  num_inference_steps: int = 20,
95
  scheduler_name: Optional[str] = None,
96
  lora_scale: float = 1.0,
 
97
  progress=gr.Progress(track_tqdm=True),
98
  ):
99
  # получаем/загружаем нужный pipe
@@ -132,6 +136,18 @@ def infer(
132
  generator=generator,
133
  ).images[0]
134
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  return image, seed
136
 
137
 
@@ -174,7 +190,7 @@ with gr.Blocks(css=css) as demo:
174
  minimum=0.0,
175
  maximum=3.0,
176
  step=0.1,
177
- value=0.7,
178
  visible=False, # Initially hidden
179
  )
180
 
@@ -192,6 +208,12 @@ with gr.Blocks(css=css) as demo:
192
  result = gr.Image(label="Result", show_label=False)
193
 
194
  with gr.Accordion("Advanced Settings", open=False):
 
 
 
 
 
 
195
  negative_prompt = gr.Text(
196
  label="Negative prompt",
197
  max_lines=1,
@@ -272,7 +294,8 @@ with gr.Blocks(css=css) as demo:
272
  guidance_scale,
273
  num_inference_steps,
274
  scheduler_select,
275
- lora_scale_slider
 
276
  ],
277
  outputs=[result, seed],
278
  )
 
5
  # import spaces #[uncomment to use ZeroGPU]
6
  from diffusers import DiffusionPipeline
7
  from peft import PeftModel, PeftConfig
8
+ from rembg import remove
9
+ from PIL import Image
10
+ import io
11
  import torch
12
  from typing import Optional
13
 
 
97
  num_inference_steps: int = 20,
98
  scheduler_name: Optional[str] = None,
99
  lora_scale: float = 1.0,
100
+ remove_background: bool = False,
101
  progress=gr.Progress(track_tqdm=True),
102
  ):
103
  # получаем/загружаем нужный pipe
 
136
  generator=generator,
137
  ).images[0]
138
 
139
+ if remove_background:
140
+ # Конвертируем PIL Image в bytes
141
+ img_byte_arr = io.BytesIO()
142
+ image.save(img_byte_arr, format='PNG')
143
+ img_byte_arr = img_byte_arr.getvalue()
144
+
145
+ # Удаляем фон
146
+ output_image = remove(img_byte_arr)
147
+
148
+ # Конвертируем обратно в PIL Image
149
+ image = Image.open(io.BytesIO(output_image))
150
+
151
  return image, seed
152
 
153
 
 
190
  minimum=0.0,
191
  maximum=3.0,
192
  step=0.1,
193
+ value=1.8,
194
  visible=False, # Initially hidden
195
  )
196
 
 
208
  result = gr.Image(label="Result", show_label=False)
209
 
210
  with gr.Accordion("Advanced Settings", open=False):
211
+ remove_background = gr.Checkbox(
212
+ label="Remove background from generated image",
213
+ value=False,
214
+ info="Use rembg to remove background from the generated image"
215
+ )
216
+
217
  negative_prompt = gr.Text(
218
  label="Negative prompt",
219
  max_lines=1,
 
294
  guidance_scale,
295
  num_inference_steps,
296
  scheduler_select,
297
+ lora_scale_slider,
298
+ remove_background
299
  ],
300
  outputs=[result, seed],
301
  )