connersdavis commited on
Commit
17e6337
1 Parent(s): bea2110

handler.py

Browse files
Files changed (2) hide show
  1. handler.py +36 -0
  2. requirements.txt +3 -0
handler.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from typing import Dict, List, Any
3
+ import torch
4
+ from torch import autocast
5
+ from diffusers import StableDiffusionPipeline
6
+ import base64
7
+ from io import BytesIO
8
+ # set device
9
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
+
11
+ if device.type != 'cuda':
12
+ raise ValueError("need to run on GPU")
13
+
14
+ class EndpointHandler():
15
+ def __init__(self, path=""):
16
+ # load the optimized model
17
+ self.pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
18
+ self.pipe = self.pipe.to(device)
19
+
20
+
21
+ def __call__(self, data: Any) -> "PIL.Image":
22
+ """
23
+ Args:
24
+ data (:obj:):
25
+ includes the input data and the parameters for the inference.
26
+ Return:
27
+ A :obj:`dict`:. base64 encoded image
28
+ """
29
+ inputs = data.pop("inputs", data)
30
+
31
+ # run inference pipeline
32
+ with autocast(device.type):
33
+ image = self.pipe(inputs, guidance_scale=7.5)["sample"][0]
34
+
35
+ # encoding image as base 64 is done by the default toolkit
36
+ return image
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ diffusers
3
+ typing