Tonioesparza commited on
Commit
3371cbb
·
verified ·
1 Parent(s): d5e1c6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +213 -213
app.py CHANGED
@@ -1,213 +1,213 @@
1
- import gradio as gr
2
- import numpy as np
3
- import random
4
- #import spaces #[uncomment to use ZeroGPU]
5
- from diffusers import DiffusionPipeline
6
- import os
7
- from diffusers import DPMSolverSinglestepScheduler
8
- from PIL import Image, ImageDraw, ImageFont
9
- from diffusers.utils import make_image_grid
10
- import torch
11
- from PIL import Image
12
- from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
13
- from diffusers.utils import load_image
14
- from diffusers import StableDiffusionXLPipeline,StableDiffusionXLImg2ImgPipeline, DPMSolverMultistepScheduler, AutoencoderTiny, StableDiffusionXLControlNetPipeline, ControlNetModel
15
- from diffusers.utils import load_image
16
- from diffusers.image_processor import IPAdapterMaskProcessor
17
- import torch
18
- import os
19
- from transformers import CLIPVisionModelWithProjection, CLIPImageProcessor
20
- from diffusers.utils import make_image_grid
21
-
22
- device = "cuda" if torch.cuda.is_available() else "cpu"
23
- model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
24
-
25
- if torch.cuda.is_available():
26
- torch_dtype = torch.float16
27
- else:
28
- torch_dtype = torch.float32
29
-
30
- processor_mask = IPAdapterMaskProcessor()
31
- controlnets = [
32
- ControlNetModel.from_pretrained(
33
- "diffusers/controlnet-depth-sdxl-1.0",variant="fp16",use_safetensors=True,torch_dtype=torch.float16
34
- ),
35
- ControlNetModel.from_pretrained(
36
- "diffusers/controlnet-canny-sdxl-1.0", torch_dtype=torch.float16, use_safetensors=True,variant="fp16"
37
- ),
38
- ]
39
-
40
- ###load pipelines
41
-
42
- pipe_CN = StableDiffusionXLControlNetPipeline.from_pretrained("SG161222/RealVisXL_V5.0", torch_dtype=torch.float16,controlnet=controlnets, use_safetensors=True, variant='fp16')
43
- pipe_CN.vae = AutoencoderTiny.from_pretrained("madebyollin/taesdxl", torch_dtype=torch.float16)
44
- pipe_CN.scheduler=DPMSolverMultistepScheduler.from_pretrained("SG161222/RealVisXL_V5.0",subfolder="scheduler",use_karras_sigmas=True)
45
- pipe_CN.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
46
- pipe_CN.to("cuda")
47
-
48
- ##############################load loras
49
-
50
- pipe_CN.load_lora_weights(r'C:\Users\AntonioEsparzaGlisma\PycharmProjects\hB8\diffusers\examples\dreambooth\ourhood_training_dreambooth_lora_2_0', weight_name='pytorch_lora_weights.safetensors',adapter_name='ourhood')
51
- pipe_CN.fuse_lora()
52
-
53
- refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0",text_encoder_2=pipe_CN.text_encoder_2,vae=pipe_CN.vae,torch_dtype=torch.float16,use_safetensors=True,variant="fp16")
54
- refiner.to("cuda")
55
-
56
-
57
-
58
- MAX_SEED = np.iinfo(np.int32).max
59
- MAX_IMAGE_SIZE = 1024
60
-
61
- def ourhood_inference(prompt=str,num_inference_steps=int,scaffold=int,fracc=float):
62
-
63
- ###pro_encode = pipe_cn.encode_text(prompt)
64
-
65
- ### function has no formats defined
66
-
67
- scaff_dic={1:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_square_2.png",
68
- 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_depth_noroof_square.png",
69
- 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_depth_solo_square.png"},
70
- 2:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_C.png",
71
- 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/depth_C.png",
72
- 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/canny_C.png"},
73
- 3:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_B.png",
74
- 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/depth_B.png",
75
- 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/canny_B.png"}}
76
- ### mask init
77
-
78
- output_height = 1024
79
- output_width = 1024
80
-
81
- mask1 = load_image(scaff_dic[scaffold]['mask1'])
82
-
83
- masks = processor_mask.preprocess([mask1], height=output_height, width=output_width)
84
- masks = [masks.reshape(1, masks.shape[0], masks.shape[2], masks.shape[3])]
85
-
86
- ###ip_images init
87
-
88
- ip_img_1 = load_image("https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0")
89
-
90
- ip_images = [[ip_img_1]]
91
- pipe_CN.set_ip_adapter_scale([[0.7]])
92
-
93
- n_steps = num_inference_steps
94
-
95
- ###precomputed depth image
96
-
97
- depth_image = load_image(scaff_dic[scaffold]['depth_image'])
98
- canny_image = load_image(scaff_dic[scaffold]['canny_image'])
99
-
100
- images_CN = [depth_image, canny_image]
101
-
102
- ### inference
103
-
104
- results = pipe_CN(
105
- prompt=prompt,
106
- ip_adapter_image=ip_images,
107
- negative_prompt="deformed, ugly, wrong proportion, low res, worst quality, low quality,text,watermark",
108
- num_inference_steps=n_steps,
109
- num_images_per_prompt=1,
110
- denoising_end=fracc,
111
- image=images_CN,
112
- controlnet_conditioning_scale=[0.3, 0.45],
113
- cross_attention_kwargs={"ip_adapter_masks": masks}
114
- ).images[0]
115
-
116
-
117
- image = refiner(
118
- prompt=prompt,
119
- num_inference_steps=num_inference_steps,
120
- denoising_start=fracc,
121
- image=results[image_num],
122
- ).images[0]
123
-
124
- return image
125
-
126
-
127
-
128
- #@spaces.GPU #[uncomment to use ZeroGPU]
129
-
130
- examples = [
131
- "A photograph, of an Ourhood privacy booth, front view, in a warehouse eventspace environment, in the style of event photography, silken oak frame, checkered warm grey exterior fabric, checkered warm grey interior fabric, curtains, diner seating, pillows",
132
- "A photograph, of an Ourhood privacy booth, side view, in a warehouse eventspace environment, in the style of event photography, silken oak frame, taupe exterior fabric",
133
- "A photograph, of an Ourhood privacy booth, close-up, in a HolmrisB8_HQ office environment, in the style of makeshift photoshoot, silken oak frame, taupe exterior fabric, taupe interior fabric, pillows",
134
- "A rendering, of an Ourhood privacy booth, front view, in a Nordic atrium environment, in the style of Keyshot, silken oak frame, taupe exterior fabric, taupe interior fabric, diner seating"]
135
-
136
- css="""
137
- #col-container {
138
- margin: 0 auto;
139
- max-width: 640px;
140
- }
141
- """
142
-
143
- with gr.Blocks(css=css) as demo:
144
-
145
- with gr.Column(elem_id="col-container"):
146
- gr.Markdown(f"""
147
- # HB8-Ourhood inference test
148
- """)
149
-
150
- with gr.Row():
151
-
152
- prompt = gr.Text(
153
- label="Prompt",
154
- show_label=False,
155
- max_lines=1,
156
- placeholder="Enter your prompt",
157
- container=False,
158
- )
159
-
160
- run_button = gr.Button("Run", scale=0)
161
-
162
- result = gr.Image(label="Result", show_label=False)
163
-
164
- with gr.Accordion("Advanced Settings", open=False):
165
-
166
- perspective = gr.Text(
167
- label="perspective",
168
- max_lines=1,
169
- placeholder="Enter a negative prompt",
170
- visible=False,
171
- )
172
-
173
- seed = gr.Slider(
174
- label="tracking number (seed)",
175
- minimum=0,
176
- maximum=MAX_SEED,
177
- step=1,
178
- value=0,
179
- )
180
-
181
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
182
-
183
- with gr.Row():
184
-
185
- fracc = gr.Slider(
186
- label="refinement_scale",
187
- minimum=0.8,
188
- maximum=0.95,
189
- step=0.01,
190
- value=0.8, #Replace with defaults that work for your model
191
- )
192
-
193
- num_inference_steps = gr.Slider(
194
- label="Number of inference steps",
195
- minimum=35,
196
- maximum=50,
197
- step=1,
198
- value=35, #Replace with defaults that work for your model
199
- )
200
-
201
- gr.Examples(
202
- examples = examples,
203
- inputs = [prompt]
204
- )
205
- gr.on(
206
- triggers=[run_button.click, prompt.submit],
207
- fn = ourhood_inference,
208
- inputs = [prompt, num_inference_steps, perspective, fracc],
209
- outputs = [result, seed]
210
- )
211
-
212
- demo.queue().launch()
213
-
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ #import spaces #[uncomment to use ZeroGPU]
5
+ from diffusers import DiffusionPipeline
6
+ import os
7
+ from diffusers import DPMSolverSinglestepScheduler
8
+ from PIL import Image, ImageDraw, ImageFont
9
+ from diffusers.utils import make_image_grid
10
+ import torch
11
+ from PIL import Image
12
+ from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
13
+ from diffusers.utils import load_image
14
+ from diffusers import StableDiffusionXLPipeline,StableDiffusionXLImg2ImgPipeline, DPMSolverMultistepScheduler, AutoencoderTiny, StableDiffusionXLControlNetPipeline, ControlNetModel
15
+ from diffusers.utils import load_image
16
+ from diffusers.image_processor import IPAdapterMaskProcessor
17
+ import torch
18
+ import os
19
+ from transformers import CLIPVisionModelWithProjection, CLIPImageProcessor
20
+ from diffusers.utils import make_image_grid
21
+
22
+ device = "cuda" if torch.cuda.is_available() else "cpu"
23
+ model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
24
+
25
+ if torch.cuda.is_available():
26
+ torch_dtype = torch.float16
27
+ else:
28
+ torch_dtype = torch.float32
29
+
30
+ processor_mask = IPAdapterMaskProcessor()
31
+ controlnets = [
32
+ ControlNetModel.from_pretrained(
33
+ "diffusers/controlnet-depth-sdxl-1.0",variant="fp16",use_safetensors=True,torch_dtype=torch.float16
34
+ ),
35
+ ControlNetModel.from_pretrained(
36
+ "diffusers/controlnet-canny-sdxl-1.0", torch_dtype=torch.float16, use_safetensors=True,variant="fp16"
37
+ ),
38
+ ]
39
+
40
+ ###load pipelines
41
+
42
+ pipe_CN = StableDiffusionXLControlNetPipeline.from_pretrained("SG161222/RealVisXL_V5.0", torch_dtype=torch.float16,controlnet=controlnets, use_safetensors=True, variant='fp16')
43
+ pipe_CN.vae = AutoencoderTiny.from_pretrained("madebyollin/taesdxl", torch_dtype=torch.float16)
44
+ pipe_CN.scheduler=DPMSolverMultistepScheduler.from_pretrained("SG161222/RealVisXL_V5.0",subfolder="scheduler",use_karras_sigmas=True)
45
+ pipe_CN.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
46
+ pipe_CN.to("cuda")
47
+
48
+ ##############################load loras
49
+
50
+ pipe_CN.load_lora_weights('Tonioesparza/ourhood_training_dreambooth_lora_2_0', weight_name='pytorch_lora_weights.safetensors',adapter_name='ourhood')
51
+ pipe_CN.fuse_lora()
52
+
53
+ refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0",text_encoder_2=pipe_CN.text_encoder_2,vae=pipe_CN.vae,torch_dtype=torch.float16,use_safetensors=True,variant="fp16")
54
+ refiner.to("cuda")
55
+
56
+
57
+
58
+ MAX_SEED = np.iinfo(np.int32).max
59
+ MAX_IMAGE_SIZE = 1024
60
+
61
+ def ourhood_inference(prompt=str,num_inference_steps=int,scaffold=int,fracc=float):
62
+
63
+ ###pro_encode = pipe_cn.encode_text(prompt)
64
+
65
+ ### function has no formats defined
66
+
67
+ scaff_dic={1:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_square_2.png",
68
+ 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_depth_noroof_square.png",
69
+ 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_depth_solo_square.png"},
70
+ 2:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_C.png",
71
+ 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/depth_C.png",
72
+ 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/canny_C.png"},
73
+ 3:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_B.png",
74
+ 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/depth_B.png",
75
+ 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/canny_B.png"}}
76
+ ### mask init
77
+
78
+ output_height = 1024
79
+ output_width = 1024
80
+
81
+ mask1 = load_image(scaff_dic[scaffold]['mask1'])
82
+
83
+ masks = processor_mask.preprocess([mask1], height=output_height, width=output_width)
84
+ masks = [masks.reshape(1, masks.shape[0], masks.shape[2], masks.shape[3])]
85
+
86
+ ###ip_images init
87
+
88
+ ip_img_1 = load_image("https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0")
89
+
90
+ ip_images = [[ip_img_1]]
91
+ pipe_CN.set_ip_adapter_scale([[0.7]])
92
+
93
+ n_steps = num_inference_steps
94
+
95
+ ###precomputed depth image
96
+
97
+ depth_image = load_image(scaff_dic[scaffold]['depth_image'])
98
+ canny_image = load_image(scaff_dic[scaffold]['canny_image'])
99
+
100
+ images_CN = [depth_image, canny_image]
101
+
102
+ ### inference
103
+
104
+ results = pipe_CN(
105
+ prompt=prompt,
106
+ ip_adapter_image=ip_images,
107
+ negative_prompt="deformed, ugly, wrong proportion, low res, worst quality, low quality,text,watermark",
108
+ num_inference_steps=n_steps,
109
+ num_images_per_prompt=1,
110
+ denoising_end=fracc,
111
+ image=images_CN,
112
+ controlnet_conditioning_scale=[0.3, 0.45],
113
+ cross_attention_kwargs={"ip_adapter_masks": masks}
114
+ ).images[0]
115
+
116
+
117
+ image = refiner(
118
+ prompt=prompt,
119
+ num_inference_steps=num_inference_steps,
120
+ denoising_start=fracc,
121
+ image=results[image_num],
122
+ ).images[0]
123
+
124
+ return image
125
+
126
+
127
+
128
+ #@spaces.GPU #[uncomment to use ZeroGPU]
129
+
130
+ examples = [
131
+ "A photograph, of an Ourhood privacy booth, front view, in a warehouse eventspace environment, in the style of event photography, silken oak frame, checkered warm grey exterior fabric, checkered warm grey interior fabric, curtains, diner seating, pillows",
132
+ "A photograph, of an Ourhood privacy booth, side view, in a warehouse eventspace environment, in the style of event photography, silken oak frame, taupe exterior fabric",
133
+ "A photograph, of an Ourhood privacy booth, close-up, in a HolmrisB8_HQ office environment, in the style of makeshift photoshoot, silken oak frame, taupe exterior fabric, taupe interior fabric, pillows",
134
+ "A rendering, of an Ourhood privacy booth, front view, in a Nordic atrium environment, in the style of Keyshot, silken oak frame, taupe exterior fabric, taupe interior fabric, diner seating"]
135
+
136
+ css="""
137
+ #col-container {
138
+ margin: 0 auto;
139
+ max-width: 640px;
140
+ }
141
+ """
142
+
143
+ with gr.Blocks(css=css) as demo:
144
+
145
+ with gr.Column(elem_id="col-container"):
146
+ gr.Markdown(f"""
147
+ # HB8-Ourhood inference test
148
+ """)
149
+
150
+ with gr.Row():
151
+
152
+ prompt = gr.Text(
153
+ label="Prompt",
154
+ show_label=False,
155
+ max_lines=1,
156
+ placeholder="Enter your prompt",
157
+ container=False,
158
+ )
159
+
160
+ run_button = gr.Button("Run", scale=0)
161
+
162
+ result = gr.Image(label="Result", show_label=False)
163
+
164
+ with gr.Accordion("Advanced Settings", open=False):
165
+
166
+ perspective = gr.Text(
167
+ label="perspective",
168
+ max_lines=1,
169
+ placeholder="Enter a negative prompt",
170
+ visible=False,
171
+ )
172
+
173
+ seed = gr.Slider(
174
+ label="tracking number (seed)",
175
+ minimum=0,
176
+ maximum=MAX_SEED,
177
+ step=1,
178
+ value=0,
179
+ )
180
+
181
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
182
+
183
+ with gr.Row():
184
+
185
+ fracc = gr.Slider(
186
+ label="refinement_scale",
187
+ minimum=0.8,
188
+ maximum=0.95,
189
+ step=0.01,
190
+ value=0.8, #Replace with defaults that work for your model
191
+ )
192
+
193
+ num_inference_steps = gr.Slider(
194
+ label="Number of inference steps",
195
+ minimum=35,
196
+ maximum=50,
197
+ step=1,
198
+ value=35, #Replace with defaults that work for your model
199
+ )
200
+
201
+ gr.Examples(
202
+ examples = examples,
203
+ inputs = [prompt]
204
+ )
205
+ gr.on(
206
+ triggers=[run_button.click, prompt.submit],
207
+ fn = ourhood_inference,
208
+ inputs = [prompt, num_inference_steps, perspective, fracc],
209
+ outputs = [result, seed]
210
+ )
211
+
212
+ demo.queue().launch()
213
+