Update handler.py
Browse files- handler.py +23 -28
handler.py
CHANGED
@@ -8,30 +8,29 @@ from pulid import attention_processor as attention
|
|
8 |
|
9 |
torch.set_grad_enabled(False)
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
# Set default model parameters
|
15 |
self.pipeline = PuLIDPipeline(sdxl_repo='RunDiffusion/Juggernaut-XL-v9', sampler='dpmpp_sde')
|
16 |
self.default_cfg = 7.0
|
17 |
self.default_steps = 25
|
18 |
self.attention = attention
|
19 |
self.pipeline.debug_img_list = []
|
20 |
|
21 |
-
def preprocess(self,
|
22 |
# Extracts image and parameters from the input data
|
23 |
-
id_image =
|
24 |
-
supp_images =
|
25 |
-
prompt =
|
26 |
-
neg_prompt =
|
27 |
-
scale =
|
28 |
-
seed = int(
|
29 |
-
steps = int(
|
30 |
-
H = int(
|
31 |
-
W = int(
|
32 |
-
id_scale =
|
33 |
-
num_zero = int(
|
34 |
-
ortho =
|
35 |
|
36 |
# Set seed if needed
|
37 |
if seed == -1:
|
@@ -62,21 +61,17 @@ class ModelHandler:
|
|
62 |
|
63 |
return (prompt, neg_prompt, scale, seed, steps, H, W, id_scale, num_zero, uncond_id_embedding, id_embedding)
|
64 |
|
65 |
-
def predict(self,
|
66 |
# Preprocess the input data
|
67 |
-
(prompt, neg_prompt, scale, seed, steps, H, W, id_scale, num_zero, uncond_id_embedding, id_embedding) = self.preprocess(
|
68 |
|
69 |
# Run the inference pipeline
|
70 |
img = self.pipeline.inference(
|
71 |
prompt, (1, H, W), neg_prompt, id_embedding, uncond_id_embedding, id_scale, scale, steps, seed
|
72 |
)[0]
|
73 |
|
74 |
-
return
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
def handler_function(input_data):
|
81 |
-
# Predict using the handler
|
82 |
-
return handler.predict(input_data)
|
|
|
8 |
|
9 |
torch.set_grad_enabled(False)
|
10 |
|
11 |
+
class EndpointHandler:
|
12 |
+
def __init__(self, model_dir=None):
|
13 |
+
# Initialize the model and tokenizer
|
|
|
14 |
self.pipeline = PuLIDPipeline(sdxl_repo='RunDiffusion/Juggernaut-XL-v9', sampler='dpmpp_sde')
|
15 |
self.default_cfg = 7.0
|
16 |
self.default_steps = 25
|
17 |
self.attention = attention
|
18 |
self.pipeline.debug_img_list = []
|
19 |
|
20 |
+
def preprocess(self, inputs):
|
21 |
# Extracts image and parameters from the input data
|
22 |
+
id_image = inputs[0]
|
23 |
+
supp_images = inputs[1:4]
|
24 |
+
prompt = inputs[4]
|
25 |
+
neg_prompt = inputs[5]
|
26 |
+
scale = inputs[6]
|
27 |
+
seed = int(inputs[7])
|
28 |
+
steps = int(inputs[8])
|
29 |
+
H = int(inputs[9])
|
30 |
+
W = int(inputs[10])
|
31 |
+
id_scale = inputs[11]
|
32 |
+
num_zero = int(inputs[12])
|
33 |
+
ortho = inputs[13]
|
34 |
|
35 |
# Set seed if needed
|
36 |
if seed == -1:
|
|
|
61 |
|
62 |
return (prompt, neg_prompt, scale, seed, steps, H, W, id_scale, num_zero, uncond_id_embedding, id_embedding)
|
63 |
|
64 |
+
def predict(self, inputs):
|
65 |
# Preprocess the input data
|
66 |
+
(prompt, neg_prompt, scale, seed, steps, H, W, id_scale, num_zero, uncond_id_embedding, id_embedding) = self.preprocess(inputs)
|
67 |
|
68 |
# Run the inference pipeline
|
69 |
img = self.pipeline.inference(
|
70 |
prompt, (1, H, W), neg_prompt, id_embedding, uncond_id_embedding, id_scale, scale, steps, seed
|
71 |
)[0]
|
72 |
|
73 |
+
return {
|
74 |
+
"image": np.array(img).tolist(),
|
75 |
+
"seed": str(seed),
|
76 |
+
"debug_images": [np.array(debug_img).tolist() for debug_img in self.pipeline.debug_img_list],
|
77 |
+
}
|
|
|
|
|
|
|
|