Commit
•
f076f26
0
Parent(s):
Duplicate from pimcore/IEP__image-to-image-instruct
Browse filesCo-authored-by: Christian Fasching <fashxp@users.noreply.huggingface.co>
- .gitattributes +35 -0
- .gitignore +3 -0
- README.md +30 -0
- handler.py +35 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# PhpStorm / IDEA
|
2 |
+
.idea
|
3 |
+
|
README.md
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- vision
|
4 |
+
- image-to-image
|
5 |
+
- endpoints-template
|
6 |
+
inference: false
|
7 |
+
pipeline_tag: image-to-image
|
8 |
+
base_model: timbrooks/instruct-pix2pix
|
9 |
+
library_name: generic
|
10 |
+
---
|
11 |
+
|
12 |
+
# Fork of [timbrooks/instruct-pix2pix](https://huggingface.co/timbrooks/instruct-pix2pix) for an `image-to-image` Inference endpoint.
|
13 |
+
|
14 |
+
This repository implements a `custom` task for `image-to-image` with instructions for 🤗 Inference Endpoints. The code for the customized
|
15 |
+
pipeline is in the handler.py.
|
16 |
+
|
17 |
+
To use deploy this model an Inference Endpoint you have to select `Custom` as task to use the `handler.py` file.
|
18 |
+
|
19 |
+
### expected Request payload
|
20 |
+
|
21 |
+
```json
|
22 |
+
{
|
23 |
+
"image": encoded_image,
|
24 |
+
"parameters": {
|
25 |
+
"candidate_labels": "green, yellow, blue, white, silver"
|
26 |
+
}
|
27 |
+
}
|
28 |
+
```
|
29 |
+
|
30 |
+
`encoded_image` is a base64 encoded image.
|
handler.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
import torch
|
5 |
+
import base64
|
6 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler
|
7 |
+
|
8 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
9 |
+
|
10 |
+
class EndpointHandler():
|
11 |
+
def __init__(self, path=""):
|
12 |
+
model_id = "timbrooks/instruct-pix2pix"
|
13 |
+
self.pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, safety_checker=None)
|
14 |
+
self.pipe.to(device)
|
15 |
+
self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config)
|
16 |
+
|
17 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
18 |
+
"""
|
19 |
+
data args:
|
20 |
+
inputs (:obj:`string`)
|
21 |
+
parameters (:obj:)
|
22 |
+
Return:
|
23 |
+
A :obj:`string`:. Base64 encoded image string
|
24 |
+
"""
|
25 |
+
|
26 |
+
|
27 |
+
image_data = data.pop("inputs", data)
|
28 |
+
# decode base64 image to PIL
|
29 |
+
image = Image.open(BytesIO(base64.b64decode(image_data)))
|
30 |
+
|
31 |
+
parameters = data.pop("parameters", data)
|
32 |
+
prompt = parameters['prompt']
|
33 |
+
|
34 |
+
images = self.pipe(prompt, image=image, num_inference_steps=10, image_guidance_scale=1).images
|
35 |
+
return images[0]
|