redkits commited on
Commit
9ab34a3
·
verified ·
1 Parent(s): bcaaeae

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. handler.py +42 -0
  2. requirements.txt +6 -0
handler.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+ import torch
4
+ from diffusers.pipelines.glm_image import GlmImagePipeline
5
+
6
+ class EndpointHandler:
7
+ def __init__(self, path=""):
8
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ self.dtype = torch.bfloat16
10
+ self.pipe = GlmImagePipeline.from_pretrained(
11
+ "zai-org/GLM-Image",
12
+ torch_dtype=self.dtype,
13
+ device_map="cuda",
14
+ enable_model_cpu_offload=True,
15
+ )
16
+
17
+ def __call__(self, data):
18
+ prompt = data.pop("inputs", "")
19
+ params = data.pop("parameters", {})
20
+
21
+ width = params.get("width", 1024)
22
+ height = params.get("height", 1024)
23
+ num_inference_steps = params.get("num_inference_steps", 50)
24
+ guidance_scale = params.get("guidance_scale", 1.5)
25
+
26
+ # GLM-Image requires dimensions divisible by 32
27
+ width = (width // 32) * 32
28
+ height = (height // 32) * 32
29
+
30
+ image = self.pipe(
31
+ prompt=prompt,
32
+ height=height,
33
+ width=width,
34
+ num_inference_steps=num_inference_steps,
35
+ guidance_scale=guidance_scale,
36
+ ).images[0]
37
+
38
+ buf = io.BytesIO()
39
+ image.save(buf, format="PNG")
40
+ img_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
41
+
42
+ return {"image": img_b64, "format": "png"}
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ git+https://github.com/huggingface/diffusers.git
2
+ transformers>=4.51.0
3
+ accelerate>=0.34.0
4
+ torch>=2.1.0
5
+ sentencepiece
6
+ protobuf