Gjm1234 commited on
Commit
01914b8
·
verified ·
1 Parent(s): c723b2a

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +10 -37
handler.py CHANGED
@@ -1,53 +1,26 @@
1
- import torch
2
  from diffusers import DiffusionPipeline
3
- from io import BytesIO
4
- import base64
5
 
6
  class EndpointHandler:
7
- def __init__(self, path="."):
8
- # Load model
9
  self.pipe = DiffusionPipeline.from_pretrained(
10
  path,
11
- torch_dtype=torch.float16,
12
- use_safetensors=True
13
  ).to("cuda")
14
 
15
- self.pipe.enable_model_cpu_offload()
16
- self.pipe.enable_vae_tiling()
17
-
18
- def img_to_b64(self, img):
19
- buffer = BytesIO()
20
- img.save(buffer, format="JPEG", quality=90)
21
- return base64.b64encode(buffer.getvalue()).decode("utf-8")
22
-
23
  def __call__(self, data):
24
- # HF ALWAYS sends "inputs"
25
  prompt = data.get("inputs", "")
26
-
27
  params = data.get("parameters", {})
28
- negative = params.get("negative_prompt", "")
29
  steps = params.get("num_inference_steps", 28)
30
  cfg = params.get("guidance_scale", 4.5)
31
- width = params.get("width", 1024)
32
- height = params.get("height", 1024)
33
- num_images = params.get("num_images", 1)
34
 
35
- # Clamp for safety
36
- if num_images > 10:
37
- num_images = 10
38
-
39
- # Run inference
40
  result = self.pipe(
41
- prompt=prompt,
42
- negative_prompt=negative,
43
  num_inference_steps=steps,
44
- guidance_scale=cfg,
45
- width=width,
46
- height=height,
47
- num_images_per_prompt=num_images
48
- ).images
49
-
50
- # Convert images → base64
51
- output = [self.img_to_b64(img) for img in result]
52
 
53
- return {"images": output}
 
 
 
1
  from diffusers import DiffusionPipeline
2
+ import torch
 
3
 
4
  class EndpointHandler:
5
+ def __init__(self, path=""):
6
+ print("Loading Juggernaut XL…")
7
  self.pipe = DiffusionPipeline.from_pretrained(
8
  path,
9
+ torch_dtype=torch.float16
 
10
  ).to("cuda")
11
 
 
 
 
 
 
 
 
 
12
  def __call__(self, data):
 
13
  prompt = data.get("inputs", "")
 
14
  params = data.get("parameters", {})
15
+
16
  steps = params.get("num_inference_steps", 28)
17
  cfg = params.get("guidance_scale", 4.5)
 
 
 
18
 
 
 
 
 
 
19
  result = self.pipe(
20
+ prompt,
 
21
  num_inference_steps=steps,
22
+ guidance_scale=cfg
23
+ )
 
 
 
 
 
 
24
 
25
+ # MUST return a PIL image
26
+ return result.images[0]