Tonioesparza commited on
Commit
d8c1145
·
verified ·
1 Parent(s): cc4b13d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +236 -142
app.py CHANGED
@@ -1,142 +1,236 @@
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 torch
7
-
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
10
-
11
- if torch.cuda.is_available():
12
- torch_dtype = torch.float16
13
- else:
14
- torch_dtype = torch.float32
15
-
16
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
- pipe = pipe.to(device)
18
-
19
- MAX_SEED = np.iinfo(np.int32).max
20
- MAX_IMAGE_SIZE = 1024
21
-
22
- #@spaces.GPU #[uncomment to use ZeroGPU]
23
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
24
-
25
- if randomize_seed:
26
- seed = random.randint(0, MAX_SEED)
27
-
28
- generator = torch.Generator().manual_seed(seed)
29
-
30
- image = pipe(
31
- prompt = prompt,
32
- negative_prompt = negative_prompt,
33
- guidance_scale = guidance_scale,
34
- num_inference_steps = num_inference_steps,
35
- width = width,
36
- height = height,
37
- generator = generator
38
- ).images[0]
39
-
40
- return image, seed
41
-
42
- examples = [
43
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
44
- "An astronaut riding a green horse",
45
- "A delicious ceviche cheesecake slice",
46
- ]
47
-
48
- css="""
49
- #col-container {
50
- margin: 0 auto;
51
- max-width: 640px;
52
- }
53
- """
54
-
55
- with gr.Blocks(css=css) as demo:
56
-
57
- with gr.Column(elem_id="col-container"):
58
- gr.Markdown(f"""
59
- # Text-to-Image Gradio Template
60
- """)
61
-
62
- with gr.Row():
63
-
64
- prompt = gr.Text(
65
- label="Prompt",
66
- show_label=False,
67
- max_lines=1,
68
- placeholder="Enter your prompt",
69
- container=False,
70
- )
71
-
72
- run_button = gr.Button("Run", scale=0)
73
-
74
- result = gr.Image(label="Result", show_label=False)
75
-
76
- with gr.Accordion("Advanced Settings", open=False):
77
-
78
- negative_prompt = gr.Text(
79
- label="Negative prompt",
80
- max_lines=1,
81
- placeholder="Enter a negative prompt",
82
- visible=False,
83
- )
84
-
85
- seed = gr.Slider(
86
- label="Seed",
87
- minimum=0,
88
- maximum=MAX_SEED,
89
- step=1,
90
- value=0,
91
- )
92
-
93
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
-
95
- with gr.Row():
96
-
97
- width = gr.Slider(
98
- label="Width",
99
- minimum=256,
100
- maximum=MAX_IMAGE_SIZE,
101
- step=32,
102
- value=1024, #Replace with defaults that work for your model
103
- )
104
-
105
- height = gr.Slider(
106
- label="Height",
107
- minimum=256,
108
- maximum=MAX_IMAGE_SIZE,
109
- step=32,
110
- value=1024, #Replace with defaults that work for your model
111
- )
112
-
113
- with gr.Row():
114
-
115
- guidance_scale = gr.Slider(
116
- label="Guidance scale",
117
- minimum=0.0,
118
- maximum=10.0,
119
- step=0.1,
120
- value=0.0, #Replace with defaults that work for your model
121
- )
122
-
123
- num_inference_steps = gr.Slider(
124
- label="Number of inference steps",
125
- minimum=1,
126
- maximum=50,
127
- step=1,
128
- value=2, #Replace with defaults that work for your model
129
- )
130
-
131
- gr.Examples(
132
- examples = examples,
133
- inputs = [prompt]
134
- )
135
- gr.on(
136
- triggers=[run_button.click, prompt.submit],
137
- fn = infer,
138
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
139
- outputs = [result, seed]
140
- )
141
-
142
- demo.queue().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from diffusers import StableDiffusionXLControlNetInpaintPipeline
4
+ from diffusers import StableDiffusionXLImg2ImgPipeline, DPMSolverMultistepScheduler, AutoencoderTiny, StableDiffusionXLControlNetPipeline, ControlNetModel
5
+ from diffusers.utils import load_image
6
+ from diffusers.image_processor import IPAdapterMaskProcessor
7
+ import torch
8
+ import os
9
+ from transformers import CLIPVisionModelWithProjection, CLIPImageProcessor
10
+ from diffusers.utils import make_image_grid
11
+ from diffusers import DPMSolverSDEScheduler
12
+
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 1024
16
+ device = "cuda" if torch.cuda.is_available() else "cpu"
17
+
18
+ if torch.cuda.is_available():
19
+ torch_dtype = torch.float16
20
+ else:
21
+ torch_dtype = torch.float32
22
+
23
+ processor_mask = IPAdapterMaskProcessor()
24
+ controlnets = [
25
+ ControlNetModel.from_pretrained(
26
+ "diffusers/controlnet-depth-sdxl-1.0",variant="fp16",use_safetensors=True,torch_dtype=torch.float16
27
+ ),
28
+ ControlNetModel.from_pretrained(
29
+ "diffusers/controlnet-canny-sdxl-1.0", torch_dtype=torch.float16, use_safetensors=True,variant="fp16"
30
+ ),
31
+ ]
32
+
33
+ def ourhood_inference(prompt1=str,num_inference_steps=int,scaffold=int):
34
+
35
+ ###pro_encode = pipe_cn.encode_text(prompt)
36
+
37
+ ###pro_encode = pipe_CN.encode_text(prompt)[2]
38
+
39
+
40
+ ### function has no formats defined
41
+
42
+ scaff_dic={1:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_square_2.png",
43
+ 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_depth_noroof_square.png",
44
+ 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_depth_solo_square.png"},
45
+ 2:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_C.png",
46
+ 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/depth_C.png",
47
+ 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/canny_C.png"},
48
+ 3:{'mask1':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/mask_in_B.png",
49
+ 'depth_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/depth_B.png",
50
+ 'canny_image':"https://huggingface.co/Tonioesparza/ourhood_training_dreambooth_lora_2_0/blob/main/canny_B.png"}}
51
+
52
+
53
+ pipe_CN = StableDiffusionXLControlNetPipeline.from_pretrained("SG161222/RealVisXL_V5.0", torch_dtype=torch.float16,controlnet=controlnets[0], use_safetensors=True, variant='fp16')
54
+ pipe_CN.vae = AutoencoderTiny.from_pretrained("madebyollin/taesdxl", torch_dtype=torch.float16)
55
+ pipe_CN.scheduler=DPMSolverSDEScheduler.from_pretrained("SG161222/RealVisXL_V5.0",subfolder="scheduler",use_karras_sigmas=True)
56
+ ###pipe_CN.scheduler=DPMSolverMultistepScheduler.from_pretrained("SG161222/RealVisXL_V5.0",subfolder="scheduler",use_karras_sigmas=True)
57
+ ###pipe_CN.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
58
+ pipe_CN.to("cuda")
59
+
60
+ ##############################load loras
61
+
62
+ pipe_CN.load_lora_weights('CreativesCombined/hb8_cases_dreambooth_lora_test_1_14', weight_name='pytorch_lora_weights.safetensors',adapter_name='cases')
63
+ ###pipe_CN.fuse_lora()
64
+
65
+ output_height = 1024
66
+ output_width = 1024
67
+ mask1 = load_image(scaff_dic[scaffold]['mask1'])
68
+ masks = processor_mask.preprocess([mask1], height=output_height, width=output_width)
69
+ masks = [masks.reshape(1, masks.shape[0], masks.shape[2], masks.shape[3])]
70
+ ###ip_images init
71
+ ###ip_img_1 = load_image(r"C:\Users\AntonioEsparzaGlisma\PycharmProjects\hB8\Cases\a-place-to_210930_HAY_A-PLACE-TO_091-768x1024.png")
72
+ ###ip_images = [[ip_img_1]]
73
+ pipe_CN.set_ip_adapter_scale([[0.7]])
74
+ n_steps = num_inference_steps
75
+ ###precomputed depth image
76
+ depth_image = load_image(scaff_dic[scaffold]['depth_image'])
77
+ canny_image = load_image(scaff_dic[scaffold]['canny_image'])
78
+ images_CN = [depth_image, canny_image]
79
+
80
+ neg1 = 'text,watermark'
81
+ prompt2 = 'Photorealistic rendering, of an OurHood privacy booth, with a silken oak frame, hickory stained melange polyester fabric, windows'
82
+ neg2 = 'curtains, pillows'
83
+ generator = torch.Generator(device="cuda").manual_seed(seed)
84
+
85
+ results = pipe_CN(
86
+ prompt=prompt1,
87
+ ###ip_adapter_image=ip_images,
88
+ negative_prompt=neg1,
89
+ num_inference_steps=n_steps,
90
+ num_images_per_prompt=1,
91
+ generator=generator,
92
+ denoising_end=0.9,
93
+ image=images_CN[0],
94
+ output_type="latent",
95
+ control_guidance_end=0.25,
96
+ controlnet_conditioning_scale=0.5,
97
+ ).images[0]
98
+
99
+ 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")
100
+ refiner.to("cuda")
101
+
102
+ del pipe_CN
103
+ torch.cuda.empty_cache()
104
+
105
+ image = refiner(
106
+ prompt=prompt1,
107
+ num_inference_steps=n_steps,
108
+ denoising_start=0.8,
109
+ image=results).images[0]
110
+
111
+ del refiner
112
+ torch.cuda.empty_cache()
113
+
114
+ pipe_IN = StableDiffusionXLControlNetInpaintPipeline.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1",controlnet=controlnets, torch_dtype=torch.float16, variant="fp16").to("cuda")
115
+ pipe_IN.load_lora_weights('Tonioesparza/ourhood_training_dreambooth_lora_2_0', weight_name='pytorch_lora_weights.safetensors',adapter_name='ourhood')
116
+ pipe_IN.to("cuda")
117
+
118
+ image = pipe_IN(
119
+ prompt=prompt2,
120
+ negative_prompt=neg2,
121
+ image=image,
122
+ mask_image=mask1,
123
+ num_inference_steps=n_steps,
124
+ strength=0.95,
125
+ control_guidance_end=[0.3,0.9],
126
+ controlnet_conditioning_scale=[0.3, 0.45],
127
+ control_image=images_CN,
128
+ generator=generator,
129
+ ).images[0]
130
+
131
+ image.show()
132
+ del pipe_IN
133
+ torch.cuda.empty_cache()
134
+
135
+ return image
136
+
137
+
138
+
139
+
140
+ """
141
+ image = refiner(
142
+ prompt=prompt,
143
+ num_inference_steps=40,
144
+ denoising_start=0.8,
145
+ image=image,
146
+ ).images[0]
147
+ """
148
+
149
+ #@spaces.GPU #[uncomment to use ZeroGPU]
150
+
151
+ examples = [
152
+ "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",
153
+ "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",
154
+ "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",
155
+ "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"]
156
+
157
+ css="""
158
+ #col-container {
159
+ margin: 0 auto;
160
+ max-width: 640px;
161
+ }
162
+ """
163
+
164
+ with gr.Blocks(css=css) as demo:
165
+
166
+ with gr.Column(elem_id="col-container"):
167
+ gr.Markdown(f"""
168
+ # HB8-Ourhood inference test
169
+ """)
170
+
171
+ with gr.Row():
172
+
173
+ prompt = gr.Text(
174
+ label="Prompt",
175
+ show_label=False,
176
+ max_lines=1,
177
+ placeholder="Enter your prompt",
178
+ container=False,
179
+ )
180
+
181
+ run_button = gr.Button("Run", scale=0)
182
+
183
+
184
+ result = gr.Image(label="Result", show_label=False)
185
+
186
+ with gr.Accordion("Advanced Settings", open=False):
187
+
188
+ perspective = gr.Slider(
189
+ label="perspective",
190
+ minimum=1,
191
+ maximum=3,
192
+ step=1,
193
+ value=1,
194
+ )
195
+
196
+ seed = gr.Slider(
197
+ label="tracking number (seed)",
198
+ minimum=0,
199
+ maximum=MAX_SEED,
200
+ step=1,
201
+ value=0,
202
+ )
203
+
204
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
205
+
206
+ with gr.Row():
207
+
208
+ fracc = gr.Slider(
209
+ label="¨seed",
210
+ minimum=0,
211
+ maximum=9999,
212
+ step=1,
213
+ value=0, #Replace with defaults that work for your model
214
+ )
215
+
216
+ num_inference_steps = gr.Slider(
217
+ label="Number of inference steps",
218
+ minimum=35,
219
+ maximum=50,
220
+ step=1,
221
+ value=35, #Replace with defaults that work for your model
222
+ )
223
+
224
+ gr.Examples(
225
+ examples = examples,
226
+ inputs = [prompt]
227
+ )
228
+ gr.on(
229
+ triggers=[run_button.click, prompt.submit],
230
+ fn = ourhood_inference,
231
+ inputs = [prompt, num_inference_steps, perspective],
232
+ outputs = [result]
233
+ )
234
+
235
+ demo.queue().launch()
236
+