Emilichka commited on
Commit
97d44ef
·
1 Parent(s): fb83555

final_app_py

Browse files
Files changed (1) hide show
  1. app.py +192 -38
app.py CHANGED
@@ -1,52 +1,122 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
 
5
  # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
 
 
 
 
 
7
  import torch
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
14
  else:
15
  torch_dtype = torch.float32
16
 
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
 
20
  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
- prompt,
27
- negative_prompt,
28
- seed,
29
- randomize_seed,
30
  width,
31
  height,
32
- guidance_scale,
33
- num_inference_steps,
 
 
 
 
 
 
 
 
 
 
34
  progress=gr.Progress(track_tqdm=True),
35
  ):
36
- if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
-
39
  generator = torch.Generator().manual_seed(seed)
40
 
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  return image, seed
52
 
@@ -60,14 +130,42 @@ examples = [
60
  css = """
61
  #col-container {
62
  margin: 0 auto;
63
- max-width: 640px;
64
  }
65
  """
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  with gr.Blocks(css=css) as demo:
68
  with gr.Column(elem_id="col-container"):
69
  gr.Markdown(" # Text-to-Image Gradio Template")
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  with gr.Row():
72
  prompt = gr.Text(
73
  label="Prompt",
@@ -81,24 +179,59 @@ with gr.Blocks(css=css) as demo:
81
 
82
  result = gr.Image(label="Result", show_label=False)
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  with gr.Accordion("Advanced Settings", open=False):
85
  negative_prompt = gr.Text(
86
  label="Negative prompt",
 
87
  max_lines=1,
88
  placeholder="Enter a negative prompt",
89
- visible=False,
90
  )
91
 
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
 
 
98
  )
99
-
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
  with gr.Row():
103
  width = gr.Slider(
104
  label="Width",
@@ -122,7 +255,7 @@ with gr.Blocks(css=css) as demo:
122
  minimum=0.0,
123
  maximum=10.0,
124
  step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
  )
127
 
128
  num_inference_steps = gr.Slider(
@@ -130,25 +263,46 @@ with gr.Blocks(css=css) as demo:
130
  minimum=1,
131
  maximum=50,
132
  step=1,
133
- value=2, # Replace with defaults that work for your model
134
  )
135
 
136
  gr.Examples(examples=examples, inputs=[prompt])
 
137
  gr.on(
138
  triggers=[run_button.click, prompt.submit],
139
  fn=infer,
140
  inputs=[
141
  prompt,
142
  negative_prompt,
143
- seed,
144
- randomize_seed,
145
  width,
146
  height,
 
 
 
 
 
 
 
 
 
 
147
  guidance_scale,
148
  num_inference_steps,
149
  ],
150
  outputs=[result, seed],
151
  )
152
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  if __name__ == "__main__":
154
- demo.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
+ from typing import Optional
5
 
6
  # import spaces #[uncomment to use ZeroGPU]
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
14
 
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
16
 
17
  if torch.cuda.is_available():
18
  torch_dtype = torch.float16
19
  else:
20
  torch_dtype = torch.float32
21
 
22
+ import os
23
+ # import torch
24
 
25
  MAX_SEED = np.iinfo(np.int32).max
26
  MAX_IMAGE_SIZE = 1024
27
 
28
 
29
+ CONTROL_MODE_MODEL = {
30
+ "Canny Ege Detection" : "lllyasviel/control_v11p_sd15_canny",
31
+ "Pixel to Pixel": "lllyasviel/control_v11e_sd15_ip2p",
32
+ "M-LSD Line detection" : "lllyasviel/control_v11p_sd15_mlsd",
33
+ "HED edge detection (soft edge)" : "lllyasviel/control_sd15_hed",
34
+ "Midas depth estimationn" : "lllyasviel/control_v11f1p_sd15_depth",
35
+ "Surface Normal Estimation" : "lllyasviel/control_v11p_sd15_normalbae",
36
+ "Scribble-Based Generation" : "lllyasviel/control_v11p_sd15_scribble",
37
+ "Semantic segmentation" : "lllyasviel/control_v11p_sd15_seg",
38
+ "OpenPose pose detection" : "lllyasviel/control_v11p_sd15_openpose",
39
+ "Line Art Generation": "lllyasviel/control_v11p_sd15_lineart",
40
+ }
41
+
42
  # @spaces.GPU #[uncomment to use ZeroGPU]
43
  def infer(
44
+ prompt: str,
45
+ negative_prompt : str,
 
 
46
  width,
47
  height,
48
+ lscale=0.0,
49
+ controlnet_enabled=False,
50
+ controlnet_strength=0.0,
51
+ controlnet_mode=None,
52
+ controlnet_image=None,
53
+ ip_adapter_enabled=False,
54
+ ip_adapter_scale=0.0,
55
+ ip_adapter_image=None,
56
+ model_id: Optional[str] = "CompVis/stable-diffusion-v1-4",
57
+ seed: Optional[int] = 42,
58
+ guidance_scale : Optional[int] = 7,
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:
92
+ controlnet_image = np.array(controlnet_image)
93
+
94
+ low_threshold = 100
95
+ high_threshold = 200
96
+
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
 
 
130
  css = """
131
  #col-container {
132
  margin: 0 auto;
133
+ max-width: 880px;
134
  }
135
  """
136
 
137
+ 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
+
144
+ def update_controlnet_visibility(controlnet_enabled):
145
+ return gr.update(visible=controlnet_enabled), gr.update(visible=controlnet_enabled), gr.update(visible=controlnet_enabled)
146
+
147
+ def update_ip_adapter_visibility(ip_adapter_enabled):
148
+ return gr.update(visible=ip_adapter_enabled), gr.update(visible=ip_adapter_enabled)
149
+
150
  with gr.Blocks(css=css) as demo:
151
  with gr.Column(elem_id="col-container"):
152
  gr.Markdown(" # Text-to-Image Gradio Template")
153
 
154
+ with gr.Row():
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(
162
+ label="Seed",
163
+ minimum=0,
164
+ maximum=MAX_SEED,
165
+ step=1,
166
+ value=42,
167
+ )
168
+
169
  with gr.Row():
170
  prompt = gr.Text(
171
  label="Prompt",
 
179
 
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
+
186
+ with gr.Accordion("ControlNet Settings", open=False):
187
+ gr.Markdown("Enable ControlNet to use settings", visible=True)
188
+ with gr.Row():
189
+ controlNet_strength = gr.Slider(
190
+ label="ControlNet scale",
191
+ minimum=0.0, maximum=1.0, step=0.05, value=0.75,
192
+ visible=False,
193
+ interactive=True,
194
+ )
195
+
196
+ controlNet_mode = gr.Dropdown(
197
+ label="ControlNet Mode",
198
+ choices=list(CONTROL_MODE_MODEL.keys()),
199
+ visible=False,
200
+ interactive=True,
201
+ )
202
+
203
+ controlNet_image = gr.Image(label="ControlNet Image", type="pil",
204
+ interactive=True, visible=False)
205
+
206
+ with gr.Accordion("IP-Adapter Settings", open=False):
207
+ gr.Markdown("Enable IP-Adapter to use settings", visible=True)
208
+ with gr.Row():
209
+ ip_adapter_scale = gr.Slider(
210
+ label="IP-Adapter Scale",
211
+ minimum=0.0, maximum=2.0, step=0.05, value=1.0,
212
+ visible=False,
213
+ interactive=True,
214
+ )
215
+
216
+ ip_adapter_image = gr.Image(label="IP-Adapter Image", type="pil",interactive=True, visible=False)
217
+
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
  )
225
 
226
+ lora_scale = gr.Slider(
227
+ label="LoRA Scale",
228
+ minimum=0.0,
229
+ maximum=2.0,
230
+ step=0.1,
231
+ value=1.0,
232
+ info="Adjust the influence of the LoRA weights",
233
+ interactive=True,
234
  )
 
 
 
235
  with gr.Row():
236
  width = gr.Slider(
237
  label="Width",
 
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
  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])
270
+
271
  gr.on(
272
  triggers=[run_button.click, prompt.submit],
273
  fn=infer,
274
  inputs=[
275
  prompt,
276
  negative_prompt,
 
 
277
  width,
278
  height,
279
+ lora_scale,
280
+ controlnet_enabled,
281
+ controlNet_strength,
282
+ controlNet_mode,
283
+ controlNet_image,
284
+ ip_adapter_enabled,
285
+ ip_adapter_scale,
286
+ ip_adapter_image,
287
+ model_id,
288
+ seed,
289
  guidance_scale,
290
  num_inference_steps,
291
  ],
292
  outputs=[result, seed],
293
  )
294
 
295
+ controlnet_enabled.change(
296
+ fn=update_controlnet_visibility,
297
+ inputs=[controlnet_enabled],
298
+ outputs=[controlNet_strength, controlNet_mode, controlNet_image],
299
+ )
300
+
301
+ ip_adapter_enabled.change(
302
+ fn=update_ip_adapter_visibility,
303
+ inputs=[ip_adapter_enabled],
304
+ outputs=[ip_adapter_scale, ip_adapter_image],
305
+ )
306
+
307
  if __name__ == "__main__":
308
+ demo.launch(share=True)