Add handler.py for inference
Browse files- handler.py +17 -0
handler.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import DiffusionPipeline
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
class EndpointHandler:
|
| 5 |
+
def __init__(self, path=""):
|
| 6 |
+
print("Loading pipeline...")
|
| 7 |
+
self.pipe = DiffusionPipeline.from_pretrained(
|
| 8 |
+
path,
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
revision="fp16",
|
| 11 |
+
use_safetensors=True
|
| 12 |
+
).to("cuda")
|
| 13 |
+
|
| 14 |
+
def __call__(self, data):
|
| 15 |
+
inputs = data.pop("inputs", data)
|
| 16 |
+
prompt = inputs if isinstance(inputs, str) else inputs[0]
|
| 17 |
+
return self.pipe(prompt).images[0]
|