kinsung commited on
Commit
02abcd9
1 Parent(s): 75119ed

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +191 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import os
3
+ import gradio as gr
4
+ from PIL import Image
5
+ import random
6
+ from diffusers import (
7
+ DiffusionPipeline,
8
+ AutoencoderKL,
9
+ StableDiffusionControlNetPipeline,
10
+ ControlNetModel,
11
+ StableDiffusionLatentUpscalePipeline,
12
+ StableDiffusionImg2ImgPipeline,
13
+ StableDiffusionControlNetImg2ImgPipeline,
14
+ DPMSolverMultistepScheduler, # <-- Added import
15
+ EulerDiscreteScheduler # <-- Added import
16
+ )
17
+ from share_btn import community_icon_html, loading_icon_html, share_js
18
+ from gallery_history import fetch_gallery_history, show_gallery_history
19
+ from illusion_style import css
20
+
21
+ BASE_MODEL = "SG161222/Realistic_Vision_V5.1_noVAE"
22
+
23
+ # Initialize both pipelines
24
+ vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16)
25
+ #init_pipe = DiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V5.1_noVAE", torch_dtype=torch.float16)
26
+ controlnet = ControlNetModel.from_pretrained("monster-labs/control_v1p_sd15_qrcode_monster", torch_dtype=torch.float16)#, torch_dtype=torch.float16)
27
+ main_pipe = StableDiffusionControlNetPipeline.from_pretrained(
28
+ BASE_MODEL,
29
+ controlnet=controlnet,
30
+ vae=vae,
31
+ safety_checker=None,
32
+ torch_dtype=torch.float16,
33
+ ).to("cuda")
34
+ #main_pipe.unet = torch.compile(main_pipe.unet, mode="reduce-overhead", fullgraph=True)
35
+ #main_pipe.unet.to(memory_format=torch.channels_last)
36
+ #main_pipe.unet = torch.compile(main_pipe.unet, mode="reduce-overhead", fullgraph=True)
37
+ #model_id = "stabilityai/sd-x2-latent-upscaler"
38
+ image_pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(BASE_MODEL, unet=main_pipe.unet, vae=vae, controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16).to("cuda")
39
+ #image_pipe.unet = torch.compile(image_pipe.unet, mode="reduce-overhead", fullgraph=True)
40
+ #upscaler = StableDiffusionLatentUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
41
+ #upscaler.to("cuda")
42
+
43
+
44
+ # Sampler map
45
+ SAMPLER_MAP = {
46
+ "DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
47
+ "Euler": lambda config: EulerDiscreteScheduler.from_config(config),
48
+ }
49
+
50
+ def center_crop_resize(img, output_size=(512, 512)):
51
+ width, height = img.size
52
+
53
+ # Calculate dimensions to crop to the center
54
+ new_dimension = min(width, height)
55
+ left = (width - new_dimension)/2
56
+ top = (height - new_dimension)/2
57
+ right = (width + new_dimension)/2
58
+ bottom = (height + new_dimension)/2
59
+
60
+ # Crop and resize
61
+ img = img.crop((left, top, right, bottom))
62
+ img = img.resize(output_size)
63
+
64
+ return img
65
+
66
+ def common_upscale(samples, width, height, upscale_method, crop=False):
67
+ if crop == "center":
68
+ old_width = samples.shape[3]
69
+ old_height = samples.shape[2]
70
+ old_aspect = old_width / old_height
71
+ new_aspect = width / height
72
+ x = 0
73
+ y = 0
74
+ if old_aspect > new_aspect:
75
+ x = round((old_width - old_width * (new_aspect / old_aspect)) / 2)
76
+ elif old_aspect < new_aspect:
77
+ y = round((old_height - old_height * (old_aspect / new_aspect)) / 2)
78
+ s = samples[:,:,y:old_height-y,x:old_width-x]
79
+ else:
80
+ s = samples
81
+
82
+ return torch.nn.functional.interpolate(s, size=(height, width), mode=upscale_method)
83
+
84
+ def upscale(samples, upscale_method, scale_by):
85
+ #s = samples.copy()
86
+ width = round(samples["images"].shape[3] * scale_by)
87
+ height = round(samples["images"].shape[2] * scale_by)
88
+ s = common_upscale(samples["images"], width, height, upscale_method, "disabled")
89
+ return (s)
90
+
91
+ # Inference function
92
+ def inference(
93
+ control_image: Image.Image,
94
+ prompt: str,
95
+ negative_prompt: str,
96
+ guidance_scale: float = 8.0,
97
+ controlnet_conditioning_scale: float = 1,
98
+ control_guidance_start: float = 1,
99
+ control_guidance_end: float = 1,
100
+ upscaler_strength: float = 0.5,
101
+ seed: int = -1,
102
+ sampler = "DPM++ Karras SDE",
103
+ progress = gr.Progress(track_tqdm=True)
104
+ ):
105
+ if prompt is None or prompt == "":
106
+ raise gr.Error("Prompt is required")
107
+
108
+ # Generate the initial image
109
+ #init_image = init_pipe(prompt).images[0]
110
+
111
+ # Rest of your existing code
112
+ control_image_small = center_crop_resize(control_image)
113
+ main_pipe.scheduler = SAMPLER_MAP[sampler](main_pipe.scheduler.config)
114
+ my_seed = random.randint(0, 2**32 - 1) if seed == -1 else seed
115
+ generator = torch.manual_seed(my_seed)
116
+
117
+ out = main_pipe(
118
+ prompt=prompt,
119
+ negative_prompt=negative_prompt,
120
+ image=control_image_small,
121
+ guidance_scale=float(guidance_scale),
122
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale),
123
+ generator=generator,
124
+ control_guidance_start=float(control_guidance_start),
125
+ control_guidance_end=float(control_guidance_end),
126
+ num_inference_steps=30,
127
+ output_type="latent"
128
+ )
129
+ control_image_large = center_crop_resize(control_image, (1024, 1024))
130
+ upscaled_latents = upscale(out, "nearest-exact", 2)
131
+ out_image = image_pipe(
132
+ prompt=prompt,
133
+ negative_prompt=negative_prompt,
134
+ control_image=control_image_large,
135
+ image=upscaled_latents,
136
+ guidance_scale=float(guidance_scale),
137
+ generator=generator,
138
+ num_inference_steps=30,
139
+ strength=upscaler_strength,
140
+ control_guidance_start=float(control_guidance_start),
141
+ control_guidance_end=float(control_guidance_end),
142
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale)
143
+ )
144
+ return out_image["images"][0], gr.update(visible=True), my_seed
145
+
146
+ #return out
147
+
148
+ with gr.Blocks(css=css) as app:
149
+ gr.Markdown(
150
+ '''
151
+ <center>
152
+ <span font-size:16px;"></span>
153
+ </center>
154
+ '''
155
+ )
156
+
157
+ with gr.Row():
158
+ with gr.Column():
159
+ control_image = gr.Image(label="Upload QR code", type="pil", elem_id="control_image")
160
+ controlnet_conditioning_scale = gr.Slider(minimum=0.0, maximum=2.0, step=0.01, value=1.2, label="Prompt strength", elem_id="illusion_strength")
161
+
162
+ prompt = gr.Textbox(label="Prompt", elem_id="prompt")
163
+ negative_prompt = gr.Textbox(label="Negative Prompt", value="low quality", elem_id="negative_prompt")
164
+ with gr.Accordion(label="Advanced Options", open=False):
165
+ guidance_scale = gr.Slider(minimum=0.0, maximum=50.0, step=0.25, value=6, label="Readabilty to Creative Scale")
166
+ sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="Euler")
167
+ control_start = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0, visible=False, label="Start of ControlNet")
168
+ control_end = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1, visible=False, label="End of ControlNet")
169
+ strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1, visible=False, label="Strength of the upscaler")
170
+ seed = gr.Slider(minimum=-1, maximum=9999999999, step=1, value=-1, label="Seed", info="-1 means random seed")
171
+ used_seed = gr.Number(label="Last seed used",visible=False,interactive=False)
172
+ run_btn = gr.Button("Run")
173
+ with gr.Column():
174
+ result_image = gr.Image(label="QR Code", interactive=False,show_share_button=False, elem_id="output")
175
+
176
+
177
+ prompt.submit(
178
+ inference,
179
+ inputs=[control_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, control_start, control_end, strength, seed, sampler],
180
+ outputs=[result_image, seed]
181
+ )
182
+ run_btn.click(
183
+ inference,
184
+ inputs=[control_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, control_start, control_end, strength, seed, sampler],
185
+ outputs=[result_image, seed]
186
+ )
187
+
188
+ app.queue(max_size=21)
189
+
190
+ if __name__ == "__main__":
191
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ diffusers
2
+ transformers
3
+ accelerate
4
+ torch
5
+ xformers
6
+ gradio
7
+ Pillow
8
+ qrcode
9
+ filelock