fashxp commited on
Commit
2d4a49c
·
1 Parent(s): 2c800a8

initial commit

Browse files
Files changed (3) hide show
  1. .gitignore +3 -0
  2. README.md +28 -1
  3. handler.py +34 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # PhpStorm / IDEA
2
+ .idea
3
+
README.md CHANGED
@@ -1,3 +1,30 @@
1
  ---
2
- license: mit
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ image_data = data.pop("inputs", data)
27
+ # decode base64 image to PIL
28
+ image = Image.open(BytesIO(base64.b64decode(image_data)))
29
+
30
+ parameters = data.pop("parameters", data)
31
+ prompt = parameters['prompt']
32
+
33
+ images = pipe(prompt, image=image, num_inference_steps=10, image_guidance_scale=1).images
34
+ return images[0]