fffiloni commited on
Commit
7f39ca4
1 Parent(s): 672cfcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -45
app.py CHANGED
@@ -12,69 +12,84 @@ import torch
12
  import numpy as np
13
  import cv2
14
 
15
-
16
- controlnet = ControlNetModel.from_pretrained(
17
- "diffusers/controlnet-canny-sdxl-1.0",
18
- torch_dtype=torch.float16
19
- )
20
  #vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
21
- pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
22
- "stabilityai/stable-diffusion-xl-base-1.0",
23
- controlnet=controlnet,
24
- #vae=vae,
25
- torch_dtype=torch.float16,
26
- variant="fp16",
27
- use_safetensors=True
28
- )
29
- pipe.to("cuda")
30
 
31
  generator = torch.Generator(device="cuda")
32
 
33
  #pipe.enable_model_cpu_offload()
34
 
35
- def infer(model_name, image_in, prompt, controlnet_conditioning_scale, guidance_scale, seed):
36
- custom_model = model_name
 
37
 
38
- # This is where you load your trained weights
39
- pipe.load_lora_weights(custom_model, weight_name="pytorch_lora_weights.safetensors", use_auth_token=True)
40
 
41
  prompt = prompt
42
  negative_prompt = "extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured"
43
 
44
- image = load_image(image_in)
45
-
46
- #controlnet_conditioning_scale = 0.25 # recommended for good generalization
47
-
48
- image = np.array(image)
49
- image = cv2.Canny(image, 100, 200)
50
- image = image[:, :, None]
51
- image = np.concatenate([image, image, image], axis=2)
52
- image = Image.fromarray(image)
53
-
54
- lora_scale= 0.9
55
-
56
-
57
-
58
- images = pipe(
59
- prompt,
60
- negative_prompt=negative_prompt,
61
- image=image,
62
- controlnet_conditioning_scale=controlnet_conditioning_scale,
63
- guidance_scale = guidance_scale,
64
- num_inference_steps=50,
65
- generator=generator.manual_seed(seed),
66
- cross_attention_kwargs={"scale": lora_scale}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  ).images
68
 
69
- images[0].save(f"hug_lab.png")
70
 
71
- return f"hug_lab.png"
72
 
73
  with gr.Blocks() as demo:
74
  with gr.Column():
 
75
  model_name = gr.Textbox(label="Model to use", placeholder="username/my_model")
76
  image_in = gr.Image(source="upload", type="filepath")
77
- prompt = gr.Textbox(label="Prompt")
 
78
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=7.5, type="float")
79
  controlnet_conditioning_scale = gr.Slider(label="Controlnet conditioning Scale", minimum=0.1, maximum=0.9, step=0.01, value=0.5, type="float")
80
  seed = gr.Slider(label="seed", minimum=0, maximum=500000, step=1, value=42)
@@ -84,7 +99,7 @@ with gr.Blocks() as demo:
84
 
85
  submit_btn.click(
86
  fn = infer,
87
- inputs = [model_name, image_in, prompt, controlnet_conditioning_scale, guidance_scale, seed],
88
  outputs = [result]
89
  )
90
 
 
12
  import numpy as np
13
  import cv2
14
 
 
 
 
 
 
15
  #vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
 
 
 
 
 
 
 
 
 
16
 
17
  generator = torch.Generator(device="cuda")
18
 
19
  #pipe.enable_model_cpu_offload()
20
 
21
+ def infer(use_custom_model, model_name, image_in, prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, seed):
22
+ if use_custom_model:
23
+ custom_model = model_name
24
 
25
+ # This is where you load your trained weights
26
+ pipe.load_lora_weights(custom_model, weight_name="pytorch_lora_weights.safetensors", use_auth_token=True)
27
 
28
  prompt = prompt
29
  negative_prompt = "extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured"
30
 
31
+ if preprocessor == "canny":
32
+
33
+ controlnet = ControlNetModel.from_pretrained(
34
+ "diffusers/controlnet-canny-sdxl-1.0",
35
+ torch_dtype=torch.float16
36
+ )
37
+
38
+ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
39
+ "stabilityai/stable-diffusion-xl-base-1.0",
40
+ controlnet=controlnet,
41
+ #vae=vae,
42
+ torch_dtype=torch.float16,
43
+ variant="fp16",
44
+ use_safetensors=True
45
+ )
46
+ pipe.to("cuda")
47
+
48
+ image = load_image(image_in)
49
+
50
+ image = np.array(image)
51
+ image = cv2.Canny(image, 100, 200)
52
+ image = image[:, :, None]
53
+ image = np.concatenate([image, image, image], axis=2)
54
+ image = Image.fromarray(image)
55
+
56
+ if use_custom_model:
57
+ lora_scale= 0.9
58
+
59
+ images = pipe(
60
+ prompt,
61
+ negative_prompt=negative_prompt,
62
+ image=image,
63
+ preprocessor=preprocessor,
64
+ controlnet_conditioning_scale=controlnet_conditioning_scale,
65
+ guidance_scale = guidance_scale,
66
+ num_inference_steps=50,
67
+ generator=generator.manual_seed(seed),
68
+ cross_attention_kwargs={"scale": lora_scale}
69
+ ).images
70
+ else:
71
+ images = pipe(
72
+ prompt,
73
+ negative_prompt=negative_prompt,
74
+ image=image,
75
+ preprocessor=preprocessor,
76
+ controlnet_conditioning_scale=controlnet_conditioning_scale,
77
+ guidance_scale = guidance_scale,
78
+ num_inference_steps=50,
79
+ generator=generator.manual_seed(seed),
80
  ).images
81
 
82
+ images[0].save(f"result.png")
83
 
84
+ return f"result.png"
85
 
86
  with gr.Blocks() as demo:
87
  with gr.Column():
88
+ use_custom_model = gr.Checkbox(label="Use a custom model ?", value=False)
89
  model_name = gr.Textbox(label="Model to use", placeholder="username/my_model")
90
  image_in = gr.Image(source="upload", type="filepath")
91
+ prompt = gr.Textbox(label="Prompt"),
92
+ preprocessor = gr.Dropdown(label="Preprocessor", choices=["canny"], value="canny")
93
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=7.5, type="float")
94
  controlnet_conditioning_scale = gr.Slider(label="Controlnet conditioning Scale", minimum=0.1, maximum=0.9, step=0.01, value=0.5, type="float")
95
  seed = gr.Slider(label="seed", minimum=0, maximum=500000, step=1, value=42)
 
99
 
100
  submit_btn.click(
101
  fn = infer,
102
+ inputs = [use_custom_model, model_name, image_in, prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, seed],
103
  outputs = [result]
104
  )
105