ameerazam08 commited on
Commit
0dcfce6
·
verified ·
1 Parent(s): 9c085bb

Added ControlNet

Browse files
Files changed (1) hide show
  1. app.py +82 -9
app.py CHANGED
@@ -1,7 +1,6 @@
1
-
2
  import os
3
  donwload_repo_loc= "./models/image_encoder/"
4
- os.system("pip install -U peft")
5
  # os.system(f"wget -O {donwload_repo_loc}config.json https://huggingface.co/h94/IP-Adapter/resolve/main/sdxl_models/image_encoder/config.json?download=true")
6
  # os.system(f"wget -O {donwload_repo_loc}model.safetensors https://huggingface.co/h94/IP-Adapter/resolve/main/sdxl_models/image_encoder/model.safetensors?download=true")
7
  # os.system(f"wget -O {donwload_repo_loc}pytorch_model.bin https://huggingface.co/h94/IP-Adapter/resolve/main/sdxl_models/image_encoder/pytorch_model.bin?download=true")
@@ -9,12 +8,16 @@ os.system("pip install -U peft")
9
  import spaces
10
  import gradio as gr
11
  import torch
 
 
12
  from diffusers import StableDiffusionXLPipeline
 
13
  from PIL import Image
14
  from ip_adapter import IPAdapterXL
15
  base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
16
  device = "cuda"
17
 
 
18
  image_encoder_path = donwload_repo_loc #"sdxl_models/image_encoder"
19
  ip_ckpt = "./models/ip-adapter_sdxl.bin"
20
  # load SDXL pipeline
@@ -25,6 +28,50 @@ pipe = StableDiffusionXLPipeline.from_pretrained(
25
  )
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # generate image variations with only image prompt
29
  @spaces.GPU(enable_queue=True)
30
  def create_image(image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed):
@@ -67,12 +114,12 @@ This is a demo of https://github.com/InstantStyle/InstantStyle.
67
 
68
  block = gr.Blocks(css="footer {visibility: hidden}").queue(max_size=10)
69
  with block:
70
- with gr.Row():
 
71
 
72
- with gr.Column():
73
- # gr.Markdown("## <h1 align='center'>InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation </h1>")
74
- gr.Markdown(DESCRIPTION)
75
- with gr.Tabs():
76
  with gr.Row():
77
  with gr.Column():
78
  image_pil = gr.Image(label="Style Image", type='pil')
@@ -90,8 +137,34 @@ with block:
90
 
91
  generate_button.click(fn=create_image, inputs=[image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed],
92
  outputs=[generated_image])
 
 
93
 
94
- block.launch()
95
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
 
 
 
97
 
 
 
 
1
  import os
2
  donwload_repo_loc= "./models/image_encoder/"
3
+ # os.system("pip install -U peft")
4
  # os.system(f"wget -O {donwload_repo_loc}config.json https://huggingface.co/h94/IP-Adapter/resolve/main/sdxl_models/image_encoder/config.json?download=true")
5
  # os.system(f"wget -O {donwload_repo_loc}model.safetensors https://huggingface.co/h94/IP-Adapter/resolve/main/sdxl_models/image_encoder/model.safetensors?download=true")
6
  # os.system(f"wget -O {donwload_repo_loc}pytorch_model.bin https://huggingface.co/h94/IP-Adapter/resolve/main/sdxl_models/image_encoder/pytorch_model.bin?download=true")
 
8
  import spaces
9
  import gradio as gr
10
  import torch
11
+ import numpy as np
12
+ import cv2
13
  from diffusers import StableDiffusionXLPipeline
14
+ from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline
15
  from PIL import Image
16
  from ip_adapter import IPAdapterXL
17
  base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
18
  device = "cuda"
19
 
20
+
21
  image_encoder_path = donwload_repo_loc #"sdxl_models/image_encoder"
22
  ip_ckpt = "./models/ip-adapter_sdxl.bin"
23
  # load SDXL pipeline
 
28
  )
29
 
30
 
31
+ controlnet_path = "diffusers/controlnet-canny-sdxl-1.0"
32
+ controlnet = ControlNetModel.from_pretrained(controlnet_path, use_safetensors=False, torch_dtype=torch.float16).to(device)
33
+
34
+ contronet_pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
35
+ base_model_path,
36
+ controlnet=controlnet,
37
+ torch_dtype=torch.float16,
38
+ add_watermarker=False,
39
+ )
40
+
41
+
42
+
43
+ @spaces.GPU(enable_queue=True)
44
+ def create_image_controlnet(image_pil,input_image,target,prompt,n_prompt,scale, control_scale, guidance_scale,num_samples,num_inference_steps,seed):
45
+ # load ip-adapter
46
+ ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device, target_blocks=["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"])
47
+
48
+ image_pil=image_pil.resize((512, 512))
49
+ cv_input_image = pil_to_cv2(input_image)
50
+ detected_map = cv2.Canny(cv_input_image, 50, 200)
51
+ canny_map = Image.fromarray(cv2.cvtColor(detected_map, cv2.COLOR_BGR2RGB))
52
+
53
+ images = ip_model.generate(pil_image=image_pil,
54
+ prompt=prompt,
55
+ negative_prompt=n_prompt,
56
+ scale=scale,
57
+ guidance_scale=guidance_scale,
58
+ num_samples=num_samples,
59
+ num_inference_steps=num_inference_steps,
60
+ seed=seed,
61
+ image=canny_map,
62
+ controlnet_conditioning_scale=control_scale,
63
+ )
64
+ del ip_model
65
+
66
+ return images
67
+
68
+ def pil_to_cv2(image_pil):
69
+ image_np = np.array(image_pil)
70
+ image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
71
+
72
+ return image_cv2
73
+
74
+
75
  # generate image variations with only image prompt
76
  @spaces.GPU(enable_queue=True)
77
  def create_image(image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed):
 
114
 
115
  block = gr.Blocks(css="footer {visibility: hidden}").queue(max_size=10)
116
  with block:
117
+ with gr.Tab("Instant Syle"):
118
+ with gr.Row():
119
 
120
+ with gr.Column():
121
+ # gr.Markdown("## <h1 align='center'>InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation </h1>")
122
+ gr.Markdown(DESCRIPTION)
 
123
  with gr.Row():
124
  with gr.Column():
125
  image_pil = gr.Image(label="Style Image", type='pil')
 
137
 
138
  generate_button.click(fn=create_image, inputs=[image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed],
139
  outputs=[generated_image])
140
+ with gr.Tab("Image stylization Style"):
141
+ with gr.Row():
142
 
143
+ with gr.Column():
144
+ gr.Markdown("""
145
+ # Imagestylization-Style: Free Lunch towards Style-Preserving in Text-to-Image Generation
146
+ **Demo by [ameer azam] - [Twitter](https://twitter.com/Ameerazam18) - [GitHub](https://github.com/AMEERAZAM08)) - [Hugging Face](https://huggingface.co/ameerazam08)**
147
+ This is a demo of https://github.com/InstantStyle/InstantStyle.
148
+ """)
149
+ with gr.Row():
150
+ with gr.Column():
151
+ src_image_pil = gr.Image(label="Source Image", type='pil')
152
+ with gr.Column():
153
+ image_pil = gr.Image(label="Style Image", type='pil')
154
+ prompt = gr.Textbox(label="Prompt",value="masterpiece, best quality, high quality")
155
+ n_prompt = gr.Textbox(label="Neg Prompt",value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry")
156
+ scale = gr.Slider(minimum=0,maximum=2.0, step=0.01,value=1.0, label="scale")
157
+ control_scale = gr.Slider(minimum=0,maximum=1.0, step=0.01,value=0.6, label="controlnet conditioning scale")
158
+ guidance_scale = gr.Slider(minimum=1,maximum=15.0, step=0.01,value=5.0, label="guidance scale")
159
+ num_samples= gr.Slider(minimum=1,maximum=4.0, step=1.0,value=1.0, label="num samples")
160
+ num_inference_steps = gr.Slider(minimum=5,maximum=50.0, step=1.0,value=30, label="num inference steps")
161
+ seed = gr.Slider(minimum=-1000000,maximum=1000000,value=1, step=1, label="Seed Value")
162
+ generate_button = gr.Button("Generate Image")
163
+ with gr.Column():
164
+ generated_image = gr.Gallery(label="Generated Image")
165
 
166
+ generate_button.click(fn=create_image_controlnet,
167
+ inputs=[image_pil,src_image_pil,prompt,n_prompt,scale, control_scale, guidance_scale,num_samples,num_inference_steps,seed],
168
+ outputs=[generated_image])
169
 
170
+ block.launch()