Emilichka commited on
Commit
c4db5c9
·
1 Parent(s): 97d44ef
Files changed (1) hide show
  1. app.py +37 -32
app.py CHANGED
@@ -7,7 +7,9 @@ from typing import Optional
7
  from diffusers import StableDiffusionPipeline, StableDiffusionControlNetPipeline
8
  from diffusers import ControlNetModel
9
  from peft import PeftModel, LoraConfig
10
- from PIL import Image
 
 
11
  import cv2
12
 
13
  import torch
@@ -46,6 +48,7 @@ def infer(
46
  width,
47
  height,
48
  lscale=0.0,
 
49
  controlnet_enabled=False,
50
  controlnet_strength=0.0,
51
  controlnet_mode=None,
@@ -59,33 +62,32 @@ def infer(
59
  num_inference_steps : Optional[int] = 20,
60
  progress=gr.Progress(track_tqdm=True),
61
  ):
62
- # if model_id != "CompVis/stable-diffusion-v1-4":
63
- # raise ValueError("The submitted model is not supported")
64
  generator = torch.Generator().manual_seed(seed)
65
 
66
  if controlnet_enabled:
67
  if not controlnet_image :
68
  raise ValueError("controlnet_enabled set to True, but controlnet_image not given")
69
  else:
70
- controlnet_model = ControlNetModel.from_pretrained(CONTROL_MODE_MODEL.get(controlnet_mode))
71
  if model_id == "SD-v1-5 + Lora" :
72
  pipe=StableDiffusionControlNetPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5",controlnet=controlnet_model, torch_dtype=torch_dtype)
73
- pipe.unet = PeftModel.from_pretrained("Emilichcka/diffusion_lora_funny_cat", "./unet", torch_dtype=torch_dtype)
74
- pipe.text_encoder = PeftModel.from_pretrained("Emilichcka/diffusion_lora_funny_cat", "./text_encoder", torch_dtype=torch_dtype)
75
 
76
  else:
77
  pipe=StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=controlnet_model, torch_dtype=torch_dtype)
78
  else:
79
  if model_id == "SD-v1-5 + Lora" :
80
  pipe=StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5",torch_dtype=torch_dtype)
81
- pipe.unet = PeftModel.from_pretrained("Emilichcka/diffusion_lora_funny_cat", "./unet", torch_dtype=torch_dtype)
82
- pipe.text_encoder = PeftModel.from_pretrained("Emilichcka/diffusion_lora_funny_cat", "./text_encoder", torch_dtype=torch_dtype)
83
  else:
84
  pipe=StableDiffusionPipeline.from_pretrained(model_id)
85
 
86
  if ip_adapter_enabled:
87
  ip_adapter_scale = float(ip_adapter_scale)
88
- pipe.load_ip_adapter("h94/IP-Adapter",subfolder="models", weight_name="ip-adapter-plus_sd15.bin")
89
  pipe.set_ip_adapter_scale(ip_adapter_scale)
90
 
91
  if controlnet_image!= None:
@@ -97,26 +99,27 @@ def infer(
97
  controlnet_image = cv2.Canny(controlnet_image, low_threshold, high_threshold)
98
  controlnet_image = controlnet_image[:, :, None]
99
  controlnet_image = np.concatenate([controlnet_image, controlnet_image, controlnet_image], axis=2)
100
- controlnet_image = Image.fromarray(controlnet_image)
101
 
102
  pipe = pipe.to(device)
103
 
104
- try:
105
- image = pipe(
106
- prompt=prompt,
107
- image=controlnet_image,
108
- negative_prompt=negative_prompt,
109
- guidance_scale=guidance_scale,
110
- num_inference_steps=num_inference_steps,
111
- width=width,
112
- height=height,
113
- generator=generator,
114
- ross_attention_kwargs={"scale": float(lscale)},
115
- controlnet_conditioning_scale=controlnet_strength,
116
- ip_adapter_image=ip_adapter_image,
117
- ).images[0]
118
- except Exception as e:
119
- raise gr.Error(f"Ошибка при генерации изображения: {e}")
 
120
 
121
  return image, seed
122
 
@@ -138,6 +141,7 @@ default_model_id_choice = [
138
  "stable-diffusion-v1-5/stable-diffusion-v1-5",
139
  "CompVis/stable-diffusion-v1-4",
140
  "SD-v1-5 + Lora",
 
141
 
142
  ]
143
 
@@ -155,7 +159,7 @@ with gr.Blocks(css=css) as demo:
155
  model_id = gr.Dropdown(
156
  label="Model Selection",
157
  choices=default_model_id_choice,
158
- value="CompVis/stable-diffusion-v1-4",
159
  )
160
 
161
  seed = gr.Slider(
@@ -180,6 +184,7 @@ with gr.Blocks(css=css) as demo:
180
  result = gr.Image(label="Result", show_label=False)
181
 
182
  with gr.Row():
 
183
  controlnet_enabled = gr.Checkbox(label="Enable ControlNet", value=False)
184
  ip_adapter_enabled = gr.Checkbox(label="Enable IP-Adapter", value=False)
185
 
@@ -218,7 +223,6 @@ with gr.Blocks(css=css) as demo:
218
  with gr.Accordion("Advanced Settings", open=False):
219
  negative_prompt = gr.Text(
220
  label="Negative prompt",
221
- value="deformed, ugly,low res, worst quality, low quality",
222
  max_lines=1,
223
  placeholder="Enter a negative prompt",
224
  )
@@ -238,7 +242,7 @@ with gr.Blocks(css=css) as demo:
238
  minimum=256,
239
  maximum=MAX_IMAGE_SIZE,
240
  step=32,
241
- value=1024, # Replace with defaults that work for your model
242
  )
243
 
244
  height = gr.Slider(
@@ -246,7 +250,7 @@ with gr.Blocks(css=css) as demo:
246
  minimum=256,
247
  maximum=MAX_IMAGE_SIZE,
248
  step=32,
249
- value=1024, # Replace with defaults that work for your model
250
  )
251
 
252
  with gr.Row():
@@ -255,7 +259,7 @@ with gr.Blocks(css=css) as demo:
255
  minimum=0.0,
256
  maximum=10.0,
257
  step=0.1,
258
- value=7.0, # Replace with defaults that work for your model
259
  )
260
 
261
  num_inference_steps = gr.Slider(
@@ -263,7 +267,7 @@ with gr.Blocks(css=css) as demo:
263
  minimum=1,
264
  maximum=50,
265
  step=1,
266
- value=20, # Replace with defaults that work for your model
267
  )
268
 
269
  gr.Examples(examples=examples, inputs=[prompt])
@@ -277,6 +281,7 @@ with gr.Blocks(css=css) as demo:
277
  width,
278
  height,
279
  lora_scale,
 
280
  controlnet_enabled,
281
  controlNet_strength,
282
  controlNet_mode,
 
7
  from diffusers import StableDiffusionPipeline, StableDiffusionControlNetPipeline
8
  from diffusers import ControlNetModel
9
  from peft import PeftModel, LoraConfig
10
+ from rembg import new_session, remove
11
+
12
+ from PIL import Image as PILImage
13
  import cv2
14
 
15
  import torch
 
48
  width,
49
  height,
50
  lscale=0.0,
51
+ remove_background=False,
52
  controlnet_enabled=False,
53
  controlnet_strength=0.0,
54
  controlnet_mode=None,
 
62
  num_inference_steps : Optional[int] = 20,
63
  progress=gr.Progress(track_tqdm=True),
64
  ):
65
+
 
66
  generator = torch.Generator().manual_seed(seed)
67
 
68
  if controlnet_enabled:
69
  if not controlnet_image :
70
  raise ValueError("controlnet_enabled set to True, but controlnet_image not given")
71
  else:
72
+ controlnet_model = ControlNetModel.from_pretrained(CONTROL_MODE_MODEL.get(controlnet_mode),torch_dtype=torch_dtype)
73
  if model_id == "SD-v1-5 + Lora" :
74
  pipe=StableDiffusionControlNetPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5",controlnet=controlnet_model, torch_dtype=torch_dtype)
75
+ pipe.unet = PeftModel.from_pretrained(pipe.unet , "Emilichcka/diffusion_lora_funny_cat", subfolder="unet", torch_dtype=torch_dtype)
76
+ pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder,"Emilichcka/diffusion_lora_funny_cat", subfolder="text_encoder", torch_dtype=torch_dtype)
77
 
78
  else:
79
  pipe=StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=controlnet_model, torch_dtype=torch_dtype)
80
  else:
81
  if model_id == "SD-v1-5 + Lora" :
82
  pipe=StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5",torch_dtype=torch_dtype)
83
+ pipe.unet = PeftModel.from_pretrained(pipe.unet , "Emilichcka/diffusion_lora_funny_cat", subfolder="unet", torch_dtype=torch_dtype)
84
+ pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder,"Emilichcka/diffusion_lora_funny_cat", subfolder="text_encoder", torch_dtype=torch_dtype)
85
  else:
86
  pipe=StableDiffusionPipeline.from_pretrained(model_id)
87
 
88
  if ip_adapter_enabled:
89
  ip_adapter_scale = float(ip_adapter_scale)
90
+ pipe.load_ip_adapter("h94/IP-Adapter",subfolder="models", weight_name="ip-adapter-plus_sd15.bin", torch_dtype=torch_dtype)
91
  pipe.set_ip_adapter_scale(ip_adapter_scale)
92
 
93
  if controlnet_image!= None:
 
99
  controlnet_image = cv2.Canny(controlnet_image, low_threshold, high_threshold)
100
  controlnet_image = controlnet_image[:, :, None]
101
  controlnet_image = np.concatenate([controlnet_image, controlnet_image, controlnet_image], axis=2)
102
+ controlnet_image = PILImage.fromarray(controlnet_image)
103
 
104
  pipe = pipe.to(device)
105
 
106
+
107
+ image = pipe(
108
+ prompt=prompt,
109
+ image=controlnet_image,
110
+ negative_prompt=negative_prompt,
111
+ guidance_scale=guidance_scale,
112
+ num_inference_steps=num_inference_steps,
113
+ width=width,
114
+ height=height,
115
+ generator=generator,
116
+ ross_attention_kwargs={"scale": float(lscale)},
117
+ controlnet_conditioning_scale=controlnet_strength,
118
+ ip_adapter_image=ip_adapter_image,
119
+ ).images[0]
120
+
121
+ if remove_background:
122
+ image = remove(image)
123
 
124
  return image, seed
125
 
 
141
  "stable-diffusion-v1-5/stable-diffusion-v1-5",
142
  "CompVis/stable-diffusion-v1-4",
143
  "SD-v1-5 + Lora",
144
+ "nota-ai/bk-sdm-small",
145
 
146
  ]
147
 
 
159
  model_id = gr.Dropdown(
160
  label="Model Selection",
161
  choices=default_model_id_choice,
162
+ value="SD-v1-5 + Lora",
163
  )
164
 
165
  seed = gr.Slider(
 
184
  result = gr.Image(label="Result", show_label=False)
185
 
186
  with gr.Row():
187
+ remove_background = gr.Checkbox(label="Remove Background", value=False)
188
  controlnet_enabled = gr.Checkbox(label="Enable ControlNet", value=False)
189
  ip_adapter_enabled = gr.Checkbox(label="Enable IP-Adapter", value=False)
190
 
 
223
  with gr.Accordion("Advanced Settings", open=False):
224
  negative_prompt = gr.Text(
225
  label="Negative prompt",
 
226
  max_lines=1,
227
  placeholder="Enter a negative prompt",
228
  )
 
242
  minimum=256,
243
  maximum=MAX_IMAGE_SIZE,
244
  step=32,
245
+ value=512, # Replace with defaults that work for your model
246
  )
247
 
248
  height = gr.Slider(
 
250
  minimum=256,
251
  maximum=MAX_IMAGE_SIZE,
252
  step=32,
253
+ value=512, # Replace with defaults that work for your model
254
  )
255
 
256
  with gr.Row():
 
259
  minimum=0.0,
260
  maximum=10.0,
261
  step=0.1,
262
+ value=10.0, # Replace with defaults that work for your model
263
  )
264
 
265
  num_inference_steps = gr.Slider(
 
267
  minimum=1,
268
  maximum=50,
269
  step=1,
270
+ value=30, # Replace with defaults that work for your model
271
  )
272
 
273
  gr.Examples(examples=examples, inputs=[prompt])
 
281
  width,
282
  height,
283
  lora_scale,
284
+ remove_background,
285
  controlnet_enabled,
286
  controlNet_strength,
287
  controlNet_mode,