Update handler.py
Browse files- handler.py +16 -8
handler.py
CHANGED
@@ -1,19 +1,27 @@
|
|
|
|
1 |
import torch
|
|
|
2 |
from diffusers.utils import load_image
|
3 |
from diffusers import FluxControlNetModel
|
4 |
from diffusers.pipelines import FluxControlNetPipeline
|
5 |
from io import BytesIO
|
6 |
|
7 |
class EndpointHandler:
|
8 |
-
def __init__(self, model_dir="
|
|
|
|
|
|
|
|
|
|
|
9 |
# Load model and pipeline
|
10 |
-
self.controlnet = FluxControlNetModel.
|
11 |
-
model_dir, torch_dtype=torch.bfloat16
|
12 |
)
|
13 |
-
self.pipe = FluxControlNetPipeline.
|
14 |
"black-forest-labs/FLUX.1-dev",
|
15 |
controlnet=self.controlnet,
|
16 |
-
torch_dtype=torch.bfloat16
|
|
|
17 |
)
|
18 |
self.pipe.to("cuda")
|
19 |
|
@@ -27,7 +35,7 @@ class EndpointHandler:
|
|
27 |
# Upscale x4
|
28 |
return image.resize((w * 4, h * 4))
|
29 |
|
30 |
-
def
|
31 |
# Save output image to a file-like object
|
32 |
buffer = BytesIO()
|
33 |
output.save(buffer, format="PNG")
|
@@ -41,9 +49,9 @@ class EndpointHandler:
|
|
41 |
output_image = self.pipe(
|
42 |
prompt=data.get("prompt", ""),
|
43 |
control_image=control_image,
|
44 |
-
|
45 |
num_inference_steps=28,
|
46 |
-
height=control_image.
|
47 |
width=control_image.size[0],
|
48 |
).images[0]
|
49 |
# Postprocess output
|
|
|
1 |
+
import os
|
2 |
import torch
|
3 |
+
from PIL import Image
|
4 |
from diffusers.utils import load_image
|
5 |
from diffusers import FluxControlNetModel
|
6 |
from diffusers.pipelines import FluxControlNetPipeline
|
7 |
from io import BytesIO
|
8 |
|
9 |
class EndpointHandler:
|
10 |
+
def __init__(self, model_dir="huy Ai123/Flux.1dev-Image-Upscaler"):
|
11 |
+
# Access the environment variable
|
12 |
+
HUGGINGFACE_API_TOKEN = os.getenv('HUGGINGFACE_API_TOKEN')
|
13 |
+
if not HUGGINGFACE_API_TOKEN:
|
14 |
+
raise ValueError("HUGGINGFACE_API_TOKEN")
|
15 |
+
|
16 |
# Load model and pipeline
|
17 |
+
self.controlnet = FluxControlNetModel.From_Pretrained(
|
18 |
+
model_dir, torch_dtype=torch.bfloat16, use_auth_token=HUGGINGFACE_API_TOKEN
|
19 |
)
|
20 |
+
self.pipe = FluxControlNetPipeline.From_Pretrained(
|
21 |
"black-forest-labs/FLUX.1-dev",
|
22 |
controlnet=self.controlnet,
|
23 |
+
torch_dtype=torch.bfloat16,
|
24 |
+
use_auth_token=HUGGINGFACE_API_TOKEN
|
25 |
)
|
26 |
self.pipe.to("cuda")
|
27 |
|
|
|
35 |
# Upscale x4
|
36 |
return image.resize((w * 4, h * 4))
|
37 |
|
38 |
+
def post_process(self, output):
|
39 |
# Save output image to a file-like object
|
40 |
buffer = BytesIO()
|
41 |
output.save(buffer, format="PNG")
|
|
|
49 |
output_image = self.pipe(
|
50 |
prompt=data.get("prompt", ""),
|
51 |
control_image=control_image,
|
52 |
+
controlnet Conditioning_scale=0.6,
|
53 |
num_inference_steps=28,
|
54 |
+
height=control_image.Size[1],
|
55 |
width=control_image.size[0],
|
56 |
).images[0]
|
57 |
# Postprocess output
|