huyai123 commited on
Commit
b636aa5
·
verified ·
1 Parent(s): 59d59d3

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +52 -52
handler.py CHANGED
@@ -1,52 +1,52 @@
1
- import torch
2
- from diffusers.utils import load_image
3
- from diffusers import FluxControlNetModel
4
- from diffusers.pipelines import FluxControlNetPipeline
5
- from PIL import Image
6
- import io
7
-
8
- class CustomHandler:
9
- def __init__(self, model_dir):
10
- # Load model and pipeline
11
- self.controlnet = FluxControlNetModel.from_pretrained(
12
- model_dir, torch_dtype=torch.bfloat16
13
- )
14
- self.pipe = FluxControlNetPipeline.from_pretrained(
15
- "black-forest-labs/FLUX.1-dev",
16
- controlnet=self.controlnet,
17
- torch_dtype=torch.bfloat16
18
- )
19
- self.pipe.to("cuda")
20
-
21
- def preprocess(self, data):
22
- # Load image from file
23
- image_file = data.get("control_image", None)
24
- if not image_file:
25
- raise ValueError("Missing control_image in input.")
26
- image = Image.open(image_file)
27
- w, h = image.size
28
- # Upscale x4
29
- return image.resize((w * 4, h * 4))
30
-
31
- def postprocess(self, output):
32
- # Save output image to a file-like object
33
- buffer = io.BytesIO()
34
- output.save(buffer, format="PNG")
35
- buffer.seek(0) # Reset buffer pointer
36
- return buffer
37
-
38
- def inference(self, data):
39
- # Preprocess input
40
- control_image = self.preprocess(data)
41
- # Generate output
42
- output_image = self.pipe(
43
- prompt=data.get("prompt", ""),
44
- control_image=control_image,
45
- controlnet_conditioning_scale=0.6,
46
- num_inference_steps=28,
47
- guidance_scale=3.5,
48
- height=control_image.size[1],
49
- width=control_image.size[0],
50
- ).images[0]
51
- # Postprocess output
52
- return self.postprocess(output_image)
 
1
+ import torch
2
+ from diffusers.utils import load_image
3
+ from diffusers import FluxControlNetModel
4
+ from diffusers.pipelines import FluxControlNetPipeline
5
+ from PIL import Image
6
+ import io
7
+
8
+ class CustomHandler:
9
+ def __init__(self, model_dir="huyai123/Flux.1-dev-Image-Upscaler"):
10
+ # Load model and pipeline
11
+ self.controlnet = FluxControlNetModel.from_pretrained(
12
+ model_dir, torch_dtype=torch.bfloat16
13
+ )
14
+ self.pipe = FluxControlNetPipeline.from_pretrained(
15
+ "black-forest-labs/FLUX.1-dev",
16
+ controlnet=self.controlnet,
17
+ torch_dtype=torch.bfloat16
18
+ )
19
+ self.pipe.to("cuda")
20
+
21
+ def preprocess(self, data):
22
+ # Load image from file
23
+ image_file = data.get("control_image", None)
24
+ if not image_file:
25
+ raise ValueError("Missing control_image in input.")
26
+ image = Image.open(image_file)
27
+ w, h = image.size
28
+ # Upscale x4
29
+ return image.resize((w * 4, h * 4))
30
+
31
+ def postprocess(self, output):
32
+ # Save output image to a file-like object
33
+ buffer = io.BytesIO()
34
+ output.save(buffer, format="PNG")
35
+ buffer.seek(0) # Reset buffer pointer
36
+ return buffer
37
+
38
+ def inference(self, data):
39
+ # Preprocess input
40
+ control_image = self.preprocess(data)
41
+ # Generate output
42
+ output_image = self.pipe(
43
+ prompt=data.get("prompt", ""),
44
+ control_image=control_image,
45
+ controlnet_conditioning_scale=0.6,
46
+ num_inference_steps=28,
47
+ guidance_scale=3.5,
48
+ height=control_image.size[1],
49
+ width=control_image.size[0],
50
+ ).images[0]
51
+ # Postprocess output
52
+ return self.postprocess(output_image)