gokaygokay commited on
Commit
05f54e2
1 Parent(s): 4297781

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -31
app.py CHANGED
@@ -97,21 +97,34 @@ def create_hdr_effect(original_image, hdr):
97
  hdr_image_8bit = np.clip(hdr_image * 255, 0, 255).astype('uint8')
98
  return Image.fromarray(cv2.cvtColor(hdr_image_8bit, cv2.COLOR_BGR2RGB))
99
 
 
 
100
  class LazyLoadPipeline:
101
  def __init__(self):
102
  self.pipe = None
103
 
104
  def load(self):
105
  if self.pipe is None:
106
- self.pipe = self.setup_pipeline()
107
- if ENABLE_CPU_OFFLOAD:
108
- self.pipe.enable_model_cpu_offload()
109
- else:
110
- self.pipe.to(device)
111
- if USE_TORCH_COMPILE:
112
- self.pipe.unet = torch.compile(self.pipe.unet, mode="reduce-overhead", fullgraph=True)
 
 
 
 
 
 
 
 
 
 
113
 
114
  def setup_pipeline(self):
 
115
  controlnet = ControlNetModel.from_single_file(
116
  "models/ControlNet/control_v11f1e_sd15_tile.pth", torch_dtype=torch.float16
117
  )
@@ -132,6 +145,7 @@ class LazyLoadPipeline:
132
  )
133
  pipe.vae = vae
134
 
 
135
  pipe.load_textual_inversion("models/embeddings/verybadimagenegative_v1.3.pt")
136
  pipe.load_textual_inversion("models/embeddings/JuggernautNegative-neg.pt")
137
  pipe.load_lora_weights("models/Lora/SDXLrender_v2.0.safetensors")
@@ -150,31 +164,40 @@ lazy_pipe = LazyLoadPipeline()
150
 
151
  @spaces.GPU
152
  def gradio_process_image(input_image, resolution, num_inference_steps, strength, hdr, guidance_scale):
 
153
  torch.cuda.empty_cache()
154
- lazy_pipe.load()
155
- lazy_pipe.pipe.unet.set_attn_processor(AttnProcessor2_0())
156
-
157
- condition_image = resize_and_upscale(input_image, resolution)
158
- condition_image = create_hdr_effect(condition_image, hdr)
159
-
160
- prompt = "masterpiece, best quality, highres"
161
- negative_prompt = "low quality, normal quality, ugly, blurry, blur, lowres, bad anatomy, bad hands, cropped, worst quality, verybadimagenegative_v1.3, JuggernautNegative-neg"
162
-
163
- options = {
164
- "prompt": prompt,
165
- "negative_prompt": negative_prompt,
166
- "image": condition_image,
167
- "control_image": condition_image,
168
- "width": condition_image.size[0],
169
- "height": condition_image.size[1],
170
- "strength": strength,
171
- "num_inference_steps": num_inference_steps,
172
- "guidance_scale": guidance_scale,
173
- "generator": torch.Generator(device=device).manual_seed(0),
174
- }
175
-
176
- result = lazy_pipe(**options).images[0]
177
- return result
 
 
 
 
 
 
 
 
178
 
179
  # Gradio interface
180
  with gr.Blocks() as demo:
 
97
  hdr_image_8bit = np.clip(hdr_image * 255, 0, 255).astype('uint8')
98
  return Image.fromarray(cv2.cvtColor(hdr_image_8bit, cv2.COLOR_BGR2RGB))
99
 
100
+ import time
101
+
102
  class LazyLoadPipeline:
103
  def __init__(self):
104
  self.pipe = None
105
 
106
  def load(self):
107
  if self.pipe is None:
108
+ print("Starting to load the pipeline...")
109
+ start_time = time.time()
110
+ try:
111
+ self.pipe = self.setup_pipeline()
112
+ if ENABLE_CPU_OFFLOAD:
113
+ print("Enabling CPU offload...")
114
+ self.pipe.enable_model_cpu_offload()
115
+ else:
116
+ print(f"Moving pipeline to device: {device}")
117
+ self.pipe.to(device)
118
+ if USE_TORCH_COMPILE:
119
+ print("Compiling the model...")
120
+ self.pipe.unet = torch.compile(self.pipe.unet, mode="reduce-overhead", fullgraph=True)
121
+ print(f"Pipeline loaded successfully in {time.time() - start_time:.2f} seconds")
122
+ except Exception as e:
123
+ print(f"Error loading pipeline: {str(e)}")
124
+ raise
125
 
126
  def setup_pipeline(self):
127
+ print("Setting up the pipeline...")
128
  controlnet = ControlNetModel.from_single_file(
129
  "models/ControlNet/control_v11f1e_sd15_tile.pth", torch_dtype=torch.float16
130
  )
 
145
  )
146
  pipe.vae = vae
147
 
148
+ print("Loading textual inversions and LoRA weights...")
149
  pipe.load_textual_inversion("models/embeddings/verybadimagenegative_v1.3.pt")
150
  pipe.load_textual_inversion("models/embeddings/JuggernautNegative-neg.pt")
151
  pipe.load_lora_weights("models/Lora/SDXLrender_v2.0.safetensors")
 
164
 
165
  @spaces.GPU
166
  def gradio_process_image(input_image, resolution, num_inference_steps, strength, hdr, guidance_scale):
167
+ print("Starting image processing...")
168
  torch.cuda.empty_cache()
169
+ try:
170
+ lazy_pipe.load()
171
+ lazy_pipe.pipe.unet.set_attn_processor(AttnProcessor2_0())
172
+
173
+ print("Resizing and upscaling image...")
174
+ condition_image = resize_and_upscale(input_image, resolution)
175
+ print("Applying HDR effect...")
176
+ condition_image = create_hdr_effect(condition_image, hdr)
177
+
178
+ prompt = "masterpiece, best quality, highres"
179
+ negative_prompt = "low quality, normal quality, ugly, blurry, blur, lowres, bad anatomy, bad hands, cropped, worst quality, verybadimagenegative_v1.3, JuggernautNegative-neg"
180
+
181
+ options = {
182
+ "prompt": prompt,
183
+ "negative_prompt": negative_prompt,
184
+ "image": condition_image,
185
+ "control_image": condition_image,
186
+ "width": condition_image.size[0],
187
+ "height": condition_image.size[1],
188
+ "strength": strength,
189
+ "num_inference_steps": num_inference_steps,
190
+ "guidance_scale": guidance_scale,
191
+ "generator": torch.Generator(device=device).manual_seed(0),
192
+ }
193
+
194
+ print("Running inference...")
195
+ result = lazy_pipe(**options).images[0]
196
+ print("Image processing completed successfully")
197
+ return result
198
+ except Exception as e:
199
+ print(f"Error during image processing: {str(e)}")
200
+ raise gr.Error(f"An error occurred: {str(e)}")
201
 
202
  # Gradio interface
203
  with gr.Blocks() as demo: