AIhgenerator commited on
Commit
c792663
·
verified ·
1 Parent(s): 33b7269

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +50 -49
handler.py CHANGED
@@ -11,26 +11,26 @@ class EndpointHandler:
11
  self.model_refiner = "stabilityai/stable-diffusion-xl-refiner-1.0"
12
 
13
  # # Load the VAE model
14
- # self.vae = AutoencoderKL.from_pretrained(self.v_autoencoder, torch_dtype=torch.float16)
15
 
16
- # # Load the main pipeline
17
- # self.pipe = StableDiffusionXLPipeline.from_pretrained(
18
- # self.model_base,
19
- # torch_dtype=torch.float16,
20
- # vae=self.vae,
21
- # add_watermarker=False,
22
- # )
23
- # self.pipe.safety_checker = None
24
- # self.pipe.to("cuda")
25
 
26
- # # Load the refiner pipeline
27
- # self.pipe_refiner = DiffusionPipeline.from_pretrained(
28
- # self.model_refiner,
29
- # torch_dtype=torch.float16,
30
- # use_safetensors=True,
31
- # variant="fp16"
32
- # )
33
- # self.pipe_refiner.enable_model_cpu_offload()
34
 
35
  def __call__(self, inputs):
36
  print("data",inputs)
@@ -38,39 +38,40 @@ class EndpointHandler:
38
 
39
  prompt, prompt2, negative_prompt, negative_prompt2 = inputs['prompt'], inputs['prompt2'], inputs['negative_prompt'], inputs['negative_prompt2']
40
  print(prompt, prompt2, negative_prompt, negative_prompt2)
41
- # image_base_latent = self.pipe(
42
- # prompt=prompt,
43
- # prompt_2=prompt2,
44
- # negative_prompt=negative_prompt,
45
- # negative_prompt_2=negative_prompt2,
46
- # guidance_scale=7.0,
47
- # height=1024,
48
- # width=1024,
49
- # num_inference_steps=25,
50
- # output_type="latent",
51
- # denoising_end=0.8 # Cut the base denoising in 80%
52
- # ).images[0]
 
53
 
54
- # image_base_latent = image_base_latent.to("cuda")
55
 
56
- # # Refine the image
57
- # image_refiner = self.pipe_refiner(
58
- # prompt=prompt,
59
- # prompt_2=prompt2,
60
- # negative_prompt=negative_prompt,
61
- # negative_prompt_2=negative_prompt2,
62
- # image=image_base_latent,
63
- # num_inference_steps=25,
64
- # height=1024,
65
- # width=1024,
66
- # strength=0.3,
67
- # denoising_start=0.8
68
- # ).images[0]
69
 
70
- # # Convert the image to a format that can be easily outputted
71
- # buffer = BytesIO()
72
- # image_refiner.save(buffer, format="JPEG")
73
- # buffer.seek(0)
74
- # base64_encoded_result = base64.b64encode(buffer.read()).decode('utf-8')
75
 
76
  return {"image": "base64_encoded_result"}
 
11
  self.model_refiner = "stabilityai/stable-diffusion-xl-refiner-1.0"
12
 
13
  # # Load the VAE model
14
+ self.vae = AutoencoderKL.from_pretrained(self.v_autoencoder, torch_dtype=torch.float16)
15
 
16
+ # Load the main pipeline
17
+ self.pipe = StableDiffusionXLPipeline.from_pretrained(
18
+ self.model_base,
19
+ torch_dtype=torch.float16,
20
+ vae=self.vae,
21
+ add_watermarker=False,
22
+ )
23
+ self.pipe.safety_checker = None
24
+ self.pipe.to("cuda")
25
 
26
+ # Load the refiner pipeline
27
+ self.pipe_refiner = DiffusionPipeline.from_pretrained(
28
+ self.model_refiner,
29
+ torch_dtype=torch.float16,
30
+ use_safetensors=True,
31
+ variant="fp16"
32
+ )
33
+ self.pipe_refiner.enable_model_cpu_offload()
34
 
35
  def __call__(self, inputs):
36
  print("data",inputs)
 
38
 
39
  prompt, prompt2, negative_prompt, negative_prompt2 = inputs['prompt'], inputs['prompt2'], inputs['negative_prompt'], inputs['negative_prompt2']
40
  print(prompt, prompt2, negative_prompt, negative_prompt2)
41
+
42
+ image_base_latent = self.pipe(
43
+ prompt=prompt,
44
+ prompt_2=prompt2,
45
+ negative_prompt=negative_prompt,
46
+ negative_prompt_2=negative_prompt2,
47
+ guidance_scale=7.0,
48
+ height=1024,
49
+ width=1024,
50
+ num_inference_steps=25,
51
+ output_type="latent",
52
+ denoising_end=0.8 # Cut the base denoising in 80%
53
+ ).images[0]
54
 
55
+ image_base_latent = image_base_latent.to("cuda")
56
 
57
+ # Refine the image
58
+ image_refiner = self.pipe_refiner(
59
+ prompt=prompt,
60
+ prompt_2=prompt2,
61
+ negative_prompt=negative_prompt,
62
+ negative_prompt_2=negative_prompt2,
63
+ image=image_base_latent,
64
+ num_inference_steps=25,
65
+ height=1024,
66
+ width=1024,
67
+ strength=0.3,
68
+ denoising_start=0.8
69
+ ).images[0]
70
 
71
+ # Convert the image to a format that can be easily outputted
72
+ buffer = BytesIO()
73
+ image_refiner.save(buffer, format="JPEG")
74
+ buffer.seek(0)
75
+ base64_encoded_result = base64.b64encode(buffer.read()).decode('utf-8')
76
 
77
  return {"image": "base64_encoded_result"}