Spaces:
Running
on
Zero
Running
on
Zero
gokaygokay
commited on
Commit
•
5f1b905
1
Parent(s):
e8864dd
Update app.py
Browse files
app.py
CHANGED
@@ -102,43 +102,6 @@ download_file(
|
|
102 |
# Set up the device
|
103 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
104 |
|
105 |
-
# Load ControlNet model
|
106 |
-
controlnet = ControlNetModel.from_single_file(
|
107 |
-
"models/ControlNet/control_v11f1e_sd15_tile.pth", torch_dtype=torch.float16
|
108 |
-
)
|
109 |
-
safety_checker = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker")
|
110 |
-
|
111 |
-
# Load the Stable Diffusion pipeline with Juggernaut Reborn model
|
112 |
-
model_path = "models/models/Stable-diffusion/juggernaut_reborn.safetensors"
|
113 |
-
pipe = StableDiffusionControlNetImg2ImgPipeline.from_single_file(
|
114 |
-
model_path,
|
115 |
-
controlnet=controlnet,
|
116 |
-
torch_dtype=torch.float16,
|
117 |
-
use_safetensors=True,
|
118 |
-
safety_checker=safety_checker
|
119 |
-
)
|
120 |
-
|
121 |
-
# Load and set VAE
|
122 |
-
vae = AutoencoderKL.from_single_file(
|
123 |
-
"models/VAE/vae-ft-mse-840000-ema-pruned.safetensors",
|
124 |
-
torch_dtype=torch.float16
|
125 |
-
)
|
126 |
-
pipe.vae = vae
|
127 |
-
|
128 |
-
# Load embeddings and LoRA models
|
129 |
-
pipe.load_textual_inversion("models/embeddings/verybadimagenegative_v1.3.pt")
|
130 |
-
pipe.load_textual_inversion("models/embeddings/JuggernautNegative-neg.pt")
|
131 |
-
pipe.load_lora_weights("models/Lora/SDXLrender_v2.0.safetensors")
|
132 |
-
pipe.fuse_lora(lora_scale=0.5)
|
133 |
-
pipe.load_lora_weights("models/Lora/more_details.safetensors")
|
134 |
-
# Set up the scheduler
|
135 |
-
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
136 |
-
|
137 |
-
# Move the pipeline to the device and enable memory efficient attention
|
138 |
-
|
139 |
-
# Enable FreeU
|
140 |
-
pipe.enable_freeu(s1=0.9, s2=0.2, b1=1.3, b2=1.4)
|
141 |
-
|
142 |
class LazyRealESRGAN:
|
143 |
def __init__(self, device, scale):
|
144 |
self.device = device
|
@@ -217,51 +180,90 @@ def create_hdr_effect(original_image, hdr):
|
|
217 |
|
218 |
return hdr_image_pil
|
219 |
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
|
239 |
@spaces.GPU
|
240 |
def gradio_process_image(input_image, resolution, num_inference_steps, strength, hdr, guidance_scale):
|
241 |
-
pipe = pipe.to(device)
|
242 |
-
pipe.unet.set_attn_processor(AttnProcessor2_0())
|
243 |
prompt = "masterpiece, best quality, highres"
|
244 |
negative_prompt = "low quality, normal quality, ugly, blurry, blur, lowres, bad anatomy, bad hands, cropped, worst quality, verybadimagenegative_v1.3, JuggernautNegative-neg"
|
245 |
-
result = process_image(input_image, prompt, negative_prompt, resolution, num_inference_steps, guidance_scale, strength, hdr)
|
246 |
return result
|
247 |
-
|
248 |
-
# Simple options
|
249 |
-
simple_options = [
|
250 |
-
gr.Image(type="pil", label="Input Image"),
|
251 |
-
gr.Slider(minimum=2048, maximum=3072, step=512, value=2048, label="Resolution"),
|
252 |
-
gr.Slider(minimum=10, maximum=100, step=10, value=20, label="Inference Steps"),
|
253 |
-
gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.35, label="Strength"),
|
254 |
-
gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0, label="HDR"),
|
255 |
-
gr.Slider(minimum=1, maximum=10, step=0.1, value=3, label="Guidance Scale")
|
256 |
-
]
|
257 |
-
|
258 |
-
# Create the Gradio interface
|
259 |
-
iface = gr.Interface(
|
260 |
-
fn=gradio_process_image,
|
261 |
-
inputs=simple_options,
|
262 |
-
outputs=gr.Image(type="pil", label="Output Image"),
|
263 |
-
title="Image Processing with Stable Diffusion",
|
264 |
-
description="Upload an image and adjust the settings to process it using Stable Diffusion."
|
265 |
-
)
|
266 |
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
# Set up the device
|
103 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
class LazyRealESRGAN:
|
106 |
def __init__(self, device, scale):
|
107 |
self.device = device
|
|
|
180 |
|
181 |
return hdr_image_pil
|
182 |
|
183 |
+
class ImageProcessor:
|
184 |
+
def __init__(self):
|
185 |
+
self.pipe = self.setup_pipeline()
|
186 |
+
|
187 |
+
def setup_pipeline(self):
|
188 |
+
controlnet = ControlNetModel.from_single_file(
|
189 |
+
"models/ControlNet/control_v11f1e_sd15_tile.pth", torch_dtype=torch.float16
|
190 |
+
)
|
191 |
+
safety_checker = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker")
|
192 |
+
|
193 |
+
model_path = "models/models/Stable-diffusion/juggernaut_reborn.safetensors"
|
194 |
+
pipe = StableDiffusionControlNetImg2ImgPipeline.from_single_file(
|
195 |
+
model_path,
|
196 |
+
controlnet=controlnet,
|
197 |
+
torch_dtype=torch.float16,
|
198 |
+
use_safetensors=True,
|
199 |
+
safety_checker=safety_checker
|
200 |
+
)
|
201 |
+
|
202 |
+
vae = AutoencoderKL.from_single_file(
|
203 |
+
"models/VAE/vae-ft-mse-840000-ema-pruned.safetensors",
|
204 |
+
torch_dtype=torch.float16
|
205 |
+
)
|
206 |
+
pipe.vae = vae
|
207 |
+
|
208 |
+
pipe.load_textual_inversion("models/embeddings/verybadimagenegative_v1.3.pt")
|
209 |
+
pipe.load_textual_inversion("models/embeddings/JuggernautNegative-neg.pt")
|
210 |
+
pipe.load_lora_weights("models/Lora/SDXLrender_v2.0.safetensors")
|
211 |
+
pipe.fuse_lora(lora_scale=0.5)
|
212 |
+
pipe.load_lora_weights("models/Lora/more_details.safetensors")
|
213 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
214 |
+
pipe.enable_freeu(s1=0.9, s2=0.2, b1=1.3, b2=1.4)
|
215 |
+
|
216 |
+
return pipe
|
217 |
+
|
218 |
+
def process_image(self, input_image, prompt, negative_prompt, resolution=2048, num_inference_steps=50, guidance_scale=3, strength=0.35, hdr=0):
|
219 |
+
condition_image = resize_and_upscale(input_image, resolution)
|
220 |
+
condition_image = create_hdr_effect(condition_image, hdr)
|
221 |
+
|
222 |
+
result = self.pipe(
|
223 |
+
prompt=prompt,
|
224 |
+
negative_prompt=negative_prompt,
|
225 |
+
image=condition_image,
|
226 |
+
control_image=condition_image,
|
227 |
+
width=condition_image.size[0],
|
228 |
+
height=condition_image.size[1],
|
229 |
+
strength=strength,
|
230 |
+
num_inference_steps=num_inference_steps,
|
231 |
+
guidance_scale=guidance_scale,
|
232 |
+
generator=torch.manual_seed(0),
|
233 |
+
).images[0]
|
234 |
+
|
235 |
+
return result
|
236 |
+
|
237 |
+
# Create an instance of ImageProcessor
|
238 |
+
image_processor = ImageProcessor()
|
239 |
|
240 |
@spaces.GPU
|
241 |
def gradio_process_image(input_image, resolution, num_inference_steps, strength, hdr, guidance_scale):
|
242 |
+
image_processor.pipe = image_processor.pipe.to(device)
|
243 |
+
image_processor.pipe.unet.set_attn_processor(AttnProcessor2_0())
|
244 |
prompt = "masterpiece, best quality, highres"
|
245 |
negative_prompt = "low quality, normal quality, ugly, blurry, blur, lowres, bad anatomy, bad hands, cropped, worst quality, verybadimagenegative_v1.3, JuggernautNegative-neg"
|
246 |
+
result = image_processor.process_image(input_image, prompt, negative_prompt, resolution, num_inference_steps, guidance_scale, strength, hdr)
|
247 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
249 |
+
# Gradio interface
|
250 |
+
with gr.Blocks() as demo:
|
251 |
+
gr.Markdown("# Image Enhancement with Stable Diffusion")
|
252 |
+
with gr.Row():
|
253 |
+
with gr.Column():
|
254 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
255 |
+
run_button = gr.Button("Enhance Image")
|
256 |
+
with gr.Column():
|
257 |
+
output_image = gr.Image(type="pil", label="Enhanced Image")
|
258 |
+
with gr.Accordion("Advanced Options", open=False):
|
259 |
+
resolution = gr.Slider(minimum=512, maximum=2048, value=1024, step=64, label="Resolution")
|
260 |
+
num_inference_steps = gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Number of Inference Steps")
|
261 |
+
strength = gr.Slider(minimum=0, maximum=1, value=0.35, step=0.05, label="Strength")
|
262 |
+
hdr = gr.Slider(minimum=0, maximum=1, value=0, step=0.1, label="HDR Effect")
|
263 |
+
guidance_scale = gr.Slider(minimum=0, maximum=20, value=3, step=0.5, label="Guidance Scale")
|
264 |
+
|
265 |
+
run_button.click(fn=gradio_process_image,
|
266 |
+
inputs=[input_image, resolution, num_inference_steps, strength, hdr, guidance_scale],
|
267 |
+
outputs=output_image)
|
268 |
+
|
269 |
+
demo.launch(share=True)
|