Texttra commited on
Commit
110a12d
·
verified ·
1 Parent(s): 7e29046

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +16 -48
handler.py CHANGED
@@ -1,55 +1,23 @@
1
- from typing import Dict
2
- import torch
3
- from diffusers import DiffusionPipeline
4
- from compel import Compel
5
- from io import BytesIO
6
- import base64
7
- import os
8
 
9
- class EndpointHandler:
10
- def __init__(self, path: str = ""):
11
- print(f"Initializing model from: {path}")
12
- self.pipe = DiffusionPipeline.from_pretrained(
13
- "black-forest-labs/FLUX.1-dev",
14
- torch_dtype=torch.float16,
15
- use_auth_token=True
16
- )
17
- lora_path = os.path.join(path, "c1t3_v1.safetensors")
18
- print(f"Loading LoRA weights from: {lora_path}")
19
- self.pipe.load_lora_weights(lora_path)
20
 
21
- if torch.cuda.is_available():
22
- self.pipe.to("cuda")
23
- else:
24
- self.pipe.to("cpu")
25
 
26
- self.pipe.enable_model_cpu_offload()
 
27
 
28
- self.compel = Compel(
29
- tokenizer=self.pipe.tokenizer,
30
- text_encoder=self.pipe.text_encoder
31
- )
32
- print("Model initialized.")
33
 
34
- def __call__(self, data: Dict) -> Dict:
35
- print("Received data:", data)
36
 
37
- inputs = data.get("inputs", {})
38
- prompt = inputs.get("prompt", "")
39
- print("Extracted prompt:", prompt)
40
 
41
- if not prompt:
42
- return {"error": "No prompt provided."}
43
-
44
- conditioning = self.compel(prompt)
45
- print("Conditioning complete.")
46
-
47
- image = self.pipe(prompt_embeds=conditioning).images[0]
48
- print("Image generated.")
49
-
50
- buffer = BytesIO()
51
- image.save(buffer, format="PNG")
52
- base64_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
53
- print("Returning image.")
54
-
55
- return {"image": base64_image}
 
1
+ def __call__(self, data: Dict) -> Dict:
2
+ print("Received data:", data)
 
 
 
 
 
3
 
4
+ inputs = data.get("inputs", {})
5
+ prompt = inputs.get("prompt", "")
 
 
 
 
 
 
 
 
 
6
 
7
+ print("Extracted prompt:", prompt)
 
 
 
8
 
9
+ if not prompt:
10
+ return {"error": "No prompt provided."}
11
 
12
+ conditioning = self.compel(prompt)
13
+ print("Conditioning complete.")
 
 
 
14
 
15
+ image = self.pipe(prompt_embeds=conditioning).images[0]
16
+ print("Image generated.")
17
 
18
+ buffer = BytesIO()
19
+ image.save(buffer, format="PNG")
20
+ base64_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
21
 
22
+ print("Returning image.")
23
+ return {"image": base64_image}