Quoc Bao Bui
commited on
Commit
·
b1e2b1f
1
Parent(s):
80ac413
Optimize handler, update README
Browse files- README.md +26 -43
- handler.py +10 -14
README.md
CHANGED
@@ -4,47 +4,30 @@ tags:
|
|
4 |
- stable-diffusion
|
5 |
- stable-diffusion-diffusers
|
6 |
- endpoints-template
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
---
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
below is an example on how to run a request using Python and `requests`.
|
25 |
-
## Run Request
|
26 |
-
```python
|
27 |
-
import json
|
28 |
-
from typing import List
|
29 |
-
import requests as r
|
30 |
-
import base64
|
31 |
-
from PIL import Image
|
32 |
-
from io import BytesIO
|
33 |
-
ENDPOINT_URL = ""
|
34 |
-
HF_TOKEN = ""
|
35 |
-
# helper decoder
|
36 |
-
def decode_base64_image(image_string):
|
37 |
-
base64_image = base64.b64decode(image_string)
|
38 |
-
buffer = BytesIO(base64_image)
|
39 |
-
return Image.open(buffer)
|
40 |
-
def predict(prompt:str=None):
|
41 |
-
payload = {"inputs": code_snippet,"parameters": parameters}
|
42 |
-
response = r.post(
|
43 |
-
ENDPOINT_URL, headers={"Authorization": f"Bearer {HF_TOKEN}"}, json={"inputs": prompt}
|
44 |
-
)
|
45 |
-
resp = response.json()
|
46 |
-
return decode_base64_image(resp["image"])
|
47 |
-
prediction = predict(
|
48 |
-
prompt="the first animal on the mars"
|
49 |
-
)
|
50 |
-
```
|
|
|
4 |
- stable-diffusion
|
5 |
- stable-diffusion-diffusers
|
6 |
- endpoints-template
|
7 |
+
|
8 |
+
extra_gated_prompt: |-
|
9 |
+
This model is open access and available to all, with a CreativeML OpenRAIL-M license further specifying rights and usage.
|
10 |
+
The CreativeML OpenRAIL License specifies:
|
11 |
+
|
12 |
+
1. You can't use the model to deliberately produce nor share illegal or harmful outputs or content
|
13 |
+
2. CompVis claims no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in the license
|
14 |
+
3. You may re-distribute the weights and use the model commercially and/or as a service. If you do, please be aware you have to include the same use restrictions as the ones in the license and share a copy of the CreativeML OpenRAIL-M to all your users (please read the license entirely and carefully)
|
15 |
+
Please read the full license carefully here: https://huggingface.co/spaces/CompVis/stable-diffusion-license
|
16 |
+
|
17 |
+
extra_gated_heading: Please read the LICENSE to access this model
|
18 |
---
|
19 |
+
# Stable Diffusion v1-5 Custom Inference
|
20 |
+
This repo is for running diffusion custom inference endpoints with `prompts` and an optional `image` as inputs (Unlike normal text-to-image inference). To
|
21 |
+
achieve this goal, this repo implements a `handler.py` script. For more information regarding custom inference, please visit
|
22 |
+
this [link](https://huggingface.co/docs/inference-endpoints/guides/custom_handler).
|
23 |
+
For more information about the model, license and limitations please check the original [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5)
|
24 |
+
or diffusion [documentation](https://huggingface.co/docs/diffusers/index).
|
25 |
+
|
26 |
+
### Local test custom handler
|
27 |
+
To test custom inference locally, please run the following command:
|
28 |
+
```commandline
|
29 |
+
python local_request.py --prompts="whale in the universe" --image="test_image.jpg"
|
30 |
+
```
|
31 |
+
**Note**: `--image` parameter is optional.
|
32 |
+
|
33 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handler.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import base64
|
2 |
from io import BytesIO
|
3 |
-
from typing import Dict,
|
4 |
-
import os
|
5 |
|
6 |
import torch
|
7 |
from PIL import Image
|
@@ -17,17 +16,15 @@ def decode_base64_image(image_string):
|
|
17 |
|
18 |
class EndpointHandler:
|
19 |
def __init__(self, path=""):
|
20 |
-
|
21 |
-
|
22 |
self.pipe = self.pipe.to("cuda")
|
23 |
|
24 |
-
def __call__(self, data: Any) ->
|
25 |
"""
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
Return:
|
30 |
-
A :obj:`dict`:. base64 encoded image
|
31 |
"""
|
32 |
prompts = data.pop("inputs", None)
|
33 |
encoded_image = data.pop("image", None)
|
@@ -35,11 +32,10 @@ class EndpointHandler:
|
|
35 |
if encoded_image:
|
36 |
init_image = decode_base64_image(encoded_image)
|
37 |
init_image.thumbnail((768, 768))
|
38 |
-
image = self.pipe(prompts, init_image=init_image).images[0]
|
39 |
|
40 |
-
|
41 |
buffered = BytesIO()
|
42 |
image.save(buffered, format="png")
|
|
|
43 |
|
44 |
-
|
45 |
-
return {"image": buffered.getvalue()}
|
|
|
1 |
import base64
|
2 |
from io import BytesIO
|
3 |
+
from typing import Dict, Any
|
|
|
4 |
|
5 |
import torch
|
6 |
from PIL import Image
|
|
|
16 |
|
17 |
class EndpointHandler:
|
18 |
def __init__(self, path=""):
|
19 |
+
self.pipe = StableDiffusionPipeline.from_pretrained("/repository/stable-diffusion-v1-5",
|
20 |
+
torch_dtype=torch.float16, revision="fp16")
|
21 |
self.pipe = self.pipe.to("cuda")
|
22 |
|
23 |
+
def __call__(self, data: Any) -> Dict[str, str]:
|
24 |
"""
|
25 |
+
Return predict value.
|
26 |
+
:param data: A dictionary contains `inputs` and optional `image` field.
|
27 |
+
:return: A dictionary with `image` field contains image in base64.
|
|
|
|
|
28 |
"""
|
29 |
prompts = data.pop("inputs", None)
|
30 |
encoded_image = data.pop("image", None)
|
|
|
32 |
if encoded_image:
|
33 |
init_image = decode_base64_image(encoded_image)
|
34 |
init_image.thumbnail((768, 768))
|
|
|
35 |
|
36 |
+
image = self.pipe(prompts, init_image=init_image).images[0]
|
37 |
buffered = BytesIO()
|
38 |
image.save(buffered, format="png")
|
39 |
+
img_str = base64.b64encode(buffered.getvalue())
|
40 |
|
41 |
+
return {"image": img_str.decode()}
|
|