Views
No views yet
Stable Diffusion is a latent text-to-image diffusion model capable of generating photo-realistic images given any text input. For more information about how Stable Diffusion functions, please have a look at 🤗's Stable Diffusion with 🧨Diffusers blog.
handler task for text-guided-to-image-inpainting for 🤗 Inference Endpoints. The code for the customized pipeline is in the handler.py.handler.py
1{
2 "inputs": "A prompt used for image generation",
3 "image" : "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAAAABGdBTUEAALGPC",
4 "mask_image": "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAAAABGdBTUEAALGPC",
5}requests.1import json
2from typing import List
3import requests as r
4import base64
5from PIL import Image
6from io import BytesIO
7
8ENDPOINT_URL = ""
9HF_TOKEN = ""
10
11# helper image utils
12def encode_image(image_path):
13 with open(image_path, "rb") as i:
14 b64 = base64.b64encode(i.read())
15 return b64.decode("utf-8")
16
17
18def predict(prompt, image, mask_image):
19 image = encode_image(image)
20 mask_image = encode_image(mask_image)
21
22 # prepare sample payload
23 request = {"inputs": prompt, "image": image, "mask_image": mask_image}
24 # headers
25 headers = {
26 "Authorization": f"Bearer {HF_TOKEN}",
27 "Content-Type": "application/json",
28 "Accept": "image/png" # important to get an image back
29 }
30
31 response = r.post(ENDPOINT_URL, headers=headers, json=payload)
32 img = Image.open(BytesIO(response.content))
33 return img
34
35prediction = predict(
36 prompt="Face of a bengal cat, high resolution, sitting on a park bench",
37 image="dog.png",
38 mask_image="mask_dog.png"
39)