Update handler.py
Browse files- handler.py +14 -1
handler.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import os
|
2 |
from diffusers import AutoPipelineForText2Image
|
3 |
import torch
|
|
|
|
|
|
|
4 |
|
5 |
class EndpointHandler:
|
6 |
def __init__(self, path: str = ""):
|
@@ -27,6 +30,16 @@ class EndpointHandler:
|
|
27 |
prompt = data.get("inputs", None)
|
28 |
if not prompt:
|
29 |
raise ValueError("No prompt provided in the input")
|
|
|
30 |
with torch.no_grad():
|
31 |
images = self.pipeline(prompt).images
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from diffusers import AutoPipelineForText2Image
|
3 |
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
import base64
|
7 |
|
8 |
class EndpointHandler:
|
9 |
def __init__(self, path: str = ""):
|
|
|
30 |
prompt = data.get("inputs", None)
|
31 |
if not prompt:
|
32 |
raise ValueError("No prompt provided in the input")
|
33 |
+
|
34 |
with torch.no_grad():
|
35 |
images = self.pipeline(prompt).images
|
36 |
+
|
37 |
+
# Get the first generated image
|
38 |
+
pil_image = images[0]
|
39 |
+
|
40 |
+
# Convert the image to bytes to return as the serialized format (for instance, base64)
|
41 |
+
buffered = BytesIO()
|
42 |
+
pil_image.save(buffered, format="PNG")
|
43 |
+
img_bytes = buffered.getvalue()
|
44 |
+
|
45 |
+
return img_bytes # Return the image bytes directly for Hugging Face to serialize
|