adorabook commited on
Commit
5592f9d
·
verified ·
1 Parent(s): d3cc1ea

Update handler.py

Browse files
Files changed (1) hide show
  1. 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
- # Initialize the model and tokenizer
12
- class ModelHandler:
13
- def __init__(self):
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, input_data):
22
  # Extracts image and parameters from the input data
23
- id_image = input_data[0]
24
- supp_images = input_data[1:4]
25
- prompt = input_data[4]
26
- neg_prompt = input_data[5]
27
- scale = input_data[6]
28
- seed = int(input_data[7])
29
- steps = int(input_data[8])
30
- H = int(input_data[9])
31
- W = int(input_data[10])
32
- id_scale = input_data[11]
33
- num_zero = int(input_data[12])
34
- ortho = input_data[13]
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, input_data):
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(input_data)
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 np.array(img), str(seed), self.pipeline.debug_img_list
75
-
76
-
77
- # Instantiate the model handler
78
- handler = ModelHandler()
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
+ }