Test diffuser
Browse files- handler.py +43 -0
- requirements.txt +3 -0
handler.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
import torch
|
| 3 |
+
from torch import autocast
|
| 4 |
+
from diffusers import StableDiffusionPipeline
|
| 5 |
+
import base64
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# set device
|
| 10 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 11 |
+
|
| 12 |
+
#if device.type != 'cuda':
|
| 13 |
+
# raise ValueError("need to run on GPU")
|
| 14 |
+
|
| 15 |
+
class EndpointHandler():
|
| 16 |
+
def __init__(self, path=""):
|
| 17 |
+
# load the optimized model
|
| 18 |
+
path = "YaYaB/sd-onepiece-diffusers-test"
|
| 19 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
|
| 20 |
+
self.pipe = self.pipe.to(device)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
| 24 |
+
"""
|
| 25 |
+
Args:
|
| 26 |
+
data (:obj:):
|
| 27 |
+
includes the input data and the parameters for the inference.
|
| 28 |
+
Return:
|
| 29 |
+
A :obj:`dict`:. base64 encoded image
|
| 30 |
+
"""
|
| 31 |
+
inputs = data.pop("inputs", data)
|
| 32 |
+
|
| 33 |
+
# run inference pipeline
|
| 34 |
+
with autocast(device.type):
|
| 35 |
+
image = self.pipe(inputs, guidance_scale=7.5)["sample"][0]
|
| 36 |
+
|
| 37 |
+
# encode image as base 64
|
| 38 |
+
buffered = BytesIO()
|
| 39 |
+
image.save(buffered, format="JPEG")
|
| 40 |
+
img_str = base64.b64encode(buffered.getvalue())
|
| 41 |
+
|
| 42 |
+
# postprocess the prediction
|
| 43 |
+
return {"image": img_str.decode()}
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
diffusers
|
| 3 |
+
torch
|