Lifeinhockey commited on
Commit
9ba7c22
·
verified ·
1 Parent(s): 3395f61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -3,11 +3,14 @@ import numpy as np
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
- from rembg import remove
7
- from PIL import Image
 
 
 
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
 
12
  if torch.cuda.is_available():
13
  torch_dtype = torch.float16
@@ -21,7 +24,6 @@ MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
 
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
  def infer(
26
  model,
27
  prompt,
@@ -31,9 +33,9 @@ def infer(
31
  height,
32
  guidance_scale,
33
  num_inference_steps,
 
34
  progress=gr.Progress(track_tqdm=True),
35
  ):
36
-
37
  global model_repo_id
38
  if model != model_repo_id:
39
  print(model, model_repo_id)
@@ -52,6 +54,12 @@ def infer(
52
  generator=generator,
53
  ).images[0]
54
 
 
 
 
 
 
 
55
  return image, seed
56
 
57
 
@@ -113,14 +121,14 @@ with gr.Blocks(css=css) as demo:
113
  minimum=0.0,
114
  maximum=10.0,
115
  step=0.1,
116
- value=7.5, # Replace with defaults that work for your model
117
  )
118
  num_inference_steps = gr.Slider(
119
  label="Number of inference steps",
120
  minimum=1,
121
  maximum=100,
122
  step=1,
123
- value=30, # Replace with defaults that work for your model
124
  )
125
 
126
  with gr.Accordion("Advanced Settings", open=False):
@@ -130,7 +138,7 @@ with gr.Blocks(css=css) as demo:
130
  minimum=256,
131
  maximum=MAX_IMAGE_SIZE,
132
  step=32,
133
- value=512, # Replace with defaults that work for your model
134
  )
135
 
136
  height = gr.Slider(
@@ -138,9 +146,18 @@ with gr.Blocks(css=css) as demo:
138
  minimum=256,
139
  maximum=MAX_IMAGE_SIZE,
140
  step=32,
141
- value=512, # Replace with defaults that work for your model
142
  )
143
 
 
 
 
 
 
 
 
 
 
144
  gr.Examples(examples=examples, inputs=[prompt])
145
  gr.Examples(examples=examples_negative, inputs=[negative_prompt])
146
 
@@ -159,10 +176,11 @@ with gr.Blocks(css=css) as demo:
159
  height,
160
  guidance_scale,
161
  num_inference_steps,
 
162
  ],
163
  outputs=[result, seed],
164
  )
165
 
166
  if __name__ == "__main__":
167
  demo.launch()
168
-
 
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
+
7
+ ###########################################
8
+ from rembg import remove
9
+ from PIL import Image
10
+ ###############################################
11
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
+ model_repo_id = "stabilityai/sdxl-turbo"
14
 
15
  if torch.cuda.is_available():
16
  torch_dtype = torch.float16
 
24
  MAX_IMAGE_SIZE = 1024
25
 
26
 
 
27
  def infer(
28
  model,
29
  prompt,
 
33
  height,
34
  guidance_scale,
35
  num_inference_steps,
36
+ remove_bg, #################################################################
37
  progress=gr.Progress(track_tqdm=True),
38
  ):
 
39
  global model_repo_id
40
  if model != model_repo_id:
41
  print(model, model_repo_id)
 
54
  generator=generator,
55
  ).images[0]
56
 
57
+ #############################################################
58
+ # Если выбрано удаление фона
59
+ if remove_bg:
60
+ image = remove(image)
61
+ ##############################################################
62
+
63
  return image, seed
64
 
65
 
 
121
  minimum=0.0,
122
  maximum=10.0,
123
  step=0.1,
124
+ value=7.5,
125
  )
126
  num_inference_steps = gr.Slider(
127
  label="Number of inference steps",
128
  minimum=1,
129
  maximum=100,
130
  step=1,
131
+ value=30,
132
  )
133
 
134
  with gr.Accordion("Advanced Settings", open=False):
 
138
  minimum=256,
139
  maximum=MAX_IMAGE_SIZE,
140
  step=32,
141
+ value=512,
142
  )
143
 
144
  height = gr.Slider(
 
146
  minimum=256,
147
  maximum=MAX_IMAGE_SIZE,
148
  step=32,
149
+ value=512,
150
  )
151
 
152
+ #########################################################
153
+ # Добавляем Checkbox для удаления фона
154
+ remove_bg = gr.Checkbox(
155
+ label="Remove Background",
156
+ value=False,
157
+ interactive=True
158
+ )
159
+ ##########################################################
160
+
161
  gr.Examples(examples=examples, inputs=[prompt])
162
  gr.Examples(examples=examples_negative, inputs=[negative_prompt])
163
 
 
176
  height,
177
  guidance_scale,
178
  num_inference_steps,
179
+ remove_bg, # Добавляем remove_bg в inputs ###############################################
180
  ],
181
  outputs=[result, seed],
182
  )
183
 
184
  if __name__ == "__main__":
185
  demo.launch()
186
+