Update handler.py
Browse files- 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 |
-
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 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"}
|