philschmid HF staff commited on
Commit
33a1591
1 Parent(s): 384a620
create_handler.ipynb ADDED
@@ -0,0 +1,452 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "## Create Custom Handler for Inference Endpoints\n"
8
+ ]
9
+ },
10
+ {
11
+ "cell_type": "code",
12
+ "execution_count": 7,
13
+ "metadata": {},
14
+ "outputs": [
15
+ {
16
+ "name": "stdout",
17
+ "output_type": "stream",
18
+ "text": [
19
+ "Requirement already satisfied: diffusers in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (0.9.0)\n",
20
+ "Collecting diffusers\n",
21
+ " Using cached diffusers-0.10.2-py3-none-any.whl (503 kB)\n",
22
+ "Requirement already satisfied: importlib-metadata in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from diffusers) (4.11.4)\n",
23
+ "Requirement already satisfied: filelock in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from diffusers) (3.8.0)\n",
24
+ "Requirement already satisfied: numpy in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from diffusers) (1.22.4)\n",
25
+ "Requirement already satisfied: Pillow in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from diffusers) (9.2.0)\n",
26
+ "Requirement already satisfied: huggingface-hub>=0.10.0 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from diffusers) (0.11.1)\n",
27
+ "Requirement already satisfied: requests in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from diffusers) (2.28.1)\n",
28
+ "Requirement already satisfied: regex!=2019.12.17 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from diffusers) (2022.7.25)\n",
29
+ "Requirement already satisfied: packaging>=20.9 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from huggingface-hub>=0.10.0->diffusers) (21.3)\n",
30
+ "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from huggingface-hub>=0.10.0->diffusers) (4.3.0)\n",
31
+ "Requirement already satisfied: pyyaml>=5.1 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from huggingface-hub>=0.10.0->diffusers) (6.0)\n",
32
+ "Requirement already satisfied: tqdm in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from huggingface-hub>=0.10.0->diffusers) (4.64.0)\n",
33
+ "Requirement already satisfied: zipp>=0.5 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from importlib-metadata->diffusers) (3.8.1)\n",
34
+ "Requirement already satisfied: idna<4,>=2.5 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from requests->diffusers) (3.3)\n",
35
+ "Requirement already satisfied: charset-normalizer<3,>=2 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from requests->diffusers) (2.1.0)\n",
36
+ "Requirement already satisfied: certifi>=2017.4.17 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from requests->diffusers) (2022.6.15)\n",
37
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from requests->diffusers) (1.26.11)\n",
38
+ "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages (from packaging>=20.9->huggingface-hub>=0.10.0->diffusers) (3.0.9)\n",
39
+ "Installing collected packages: diffusers\n",
40
+ " Attempting uninstall: diffusers\n",
41
+ " Found existing installation: diffusers 0.9.0\n",
42
+ " Uninstalling diffusers-0.9.0:\n",
43
+ " Successfully uninstalled diffusers-0.9.0\n",
44
+ "Successfully installed diffusers-0.10.2\n"
45
+ ]
46
+ }
47
+ ],
48
+ "source": [
49
+ "!pip install diffusers --upgrade"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": 4,
55
+ "metadata": {},
56
+ "outputs": [],
57
+ "source": [
58
+ "import torch\n",
59
+ "\n",
60
+ "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
61
+ "\n",
62
+ "\n",
63
+ "if device.type != 'cuda':\n",
64
+ " raise ValueError(\"need to run on GPU\")"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "code",
69
+ "execution_count": 3,
70
+ "metadata": {},
71
+ "outputs": [
72
+ {
73
+ "name": "stdout",
74
+ "output_type": "stream",
75
+ "text": [
76
+ "Overwriting handler.py\n"
77
+ ]
78
+ }
79
+ ],
80
+ "source": [
81
+ "%%writefile handler.py\n",
82
+ "from typing import Dict, List, Any\n",
83
+ "import torch\n",
84
+ "from diffusers import DPMSolverMultistepScheduler, StableDiffusionInpaintPipeline\n",
85
+ "from PIL import Image\n",
86
+ "import base64\n",
87
+ "from io import BytesIO\n",
88
+ "\n",
89
+ "\n",
90
+ "# set device\n",
91
+ "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
92
+ "\n",
93
+ "if device.type != 'cuda':\n",
94
+ " raise ValueError(\"need to run on GPU\")\n",
95
+ "\n",
96
+ "class EndpointHandler():\n",
97
+ " def __init__(self, path=\"\"):\n",
98
+ " # load StableDiffusionInpaintPipeline pipeline\n",
99
+ " self.pipe = StableDiffusionInpaintPipeline.from_pretrained(path, torch_dtype=torch.float16)\n",
100
+ " # use DPMSolverMultistepScheduler\n",
101
+ " self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(self.pipe.scheduler.config)\n",
102
+ " # move to device \n",
103
+ " self.pipe = self.pipe.to(device)\n",
104
+ "\n",
105
+ "\n",
106
+ " def __call__(self, data: Any) -> List[List[Dict[str, float]]]:\n",
107
+ " \"\"\"\n",
108
+ " :param data: A dictionary contains `inputs` and optional `image` field.\n",
109
+ " :return: A dictionary with `image` field contains image in base64.\n",
110
+ " \"\"\"\n",
111
+ " inputs = data.pop(\"inputs\", data)\n",
112
+ " encoded_image = data.pop(\"image\", None)\n",
113
+ " encoded_mask_image = data.pop(\"mask_image\", None)\n",
114
+ " \n",
115
+ " # hyperparamters\n",
116
+ " num_inference_steps = data.pop(\"num_inference_steps\", 25)\n",
117
+ " guidance_scale = data.pop(\"guidance_scale\", 7.5)\n",
118
+ " negative_prompt = data.pop(\"negative_prompt\", None)\n",
119
+ " height = data.pop(\"height\", None)\n",
120
+ " width = data.pop(\"width\", None)\n",
121
+ " \n",
122
+ " # process image\n",
123
+ " if encoded_image is not None and encoded_mask_image is not None:\n",
124
+ " image = self.decode_base64_image(encoded_image)\n",
125
+ " mask_image = self.decode_base64_image(encoded_mask_image)\n",
126
+ " else:\n",
127
+ " image = None\n",
128
+ " mask_image = None \n",
129
+ " \n",
130
+ " # run inference pipeline\n",
131
+ " out = self.pipe(inputs, \n",
132
+ " image=image, \n",
133
+ " mask_image=mask_image, \n",
134
+ " num_inference_steps=num_inference_steps,\n",
135
+ " guidance_scale=guidance_scale,\n",
136
+ " num_images_per_prompt=1,\n",
137
+ " negative_prompt=negative_prompt,\n",
138
+ " height=height,\n",
139
+ " width=width\n",
140
+ " )\n",
141
+ " \n",
142
+ " # return first generate PIL image\n",
143
+ " return out.images[0]\n",
144
+ " \n",
145
+ " # helper to decode input image\n",
146
+ " def decode_base64_image(self, image_string):\n",
147
+ " base64_image = base64.b64decode(image_string)\n",
148
+ " buffer = BytesIO(base64_image)\n",
149
+ " image = Image.open(buffer)\n",
150
+ " return image.convert(\"RGB\").thumbnail((768, 768))"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": null,
156
+ "metadata": {},
157
+ "outputs": [],
158
+ "source": []
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "execution_count": 1,
163
+ "metadata": {},
164
+ "outputs": [
165
+ {
166
+ "name": "stderr",
167
+ "output_type": "stream",
168
+ "text": [
169
+ "The config attributes {'upcast_attention': False} were passed to UNet2DConditionModel, but are not expected and will be ignored. Please verify your config.json configuration file.\n"
170
+ ]
171
+ }
172
+ ],
173
+ "source": [
174
+ "from handler import EndpointHandler\n",
175
+ "\n",
176
+ "# init handler\n",
177
+ "my_handler = EndpointHandler(path=\".\")"
178
+ ]
179
+ },
180
+ {
181
+ "cell_type": "code",
182
+ "execution_count": 2,
183
+ "metadata": {},
184
+ "outputs": [
185
+ {
186
+ "data": {
187
+ "text/html": [
188
+ "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000; text-decoration-color: #800000\">╭─────────────────────────────── </span><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">Traceback </span><span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f; font-weight: bold\">(most recent call last)</span><span style=\"color: #800000; text-decoration-color: #800000\"> ────────────────────────────────╮</span>\n",
189
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/tmp/ipykernel_12933/</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">3313959780.py</span>:<span style=\"color: #0000ff; text-decoration-color: #0000ff\">26</span> in <span style=\"color: #00ff00; text-decoration-color: #00ff00\">&lt;cell line: 26&gt;</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
190
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
191
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000; font-style: italic\">[Errno 2] No such file or directory: '/tmp/ipykernel_12933/3313959780.py'</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
192
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
193
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/home/ubuntu/endpoints/stable-diffusion-2-inpainting-endpoint/</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">handler.py</span>:<span style=\"color: #0000ff; text-decoration-color: #0000ff\">50</span> in <span style=\"color: #00ff00; text-decoration-color: #00ff00\">__call__</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
194
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
195
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">47 │ │ │ </span>mask_image = <span style=\"color: #0000ff; text-decoration-color: #0000ff\">None</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
196
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">48 │ │ </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
197
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">49 │ │ # run inference pipeline</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
198
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">❱ </span>50 <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">│ │ </span>out = <span style=\"color: #00ffff; text-decoration-color: #00ffff\">self</span>.pipe(inputs, <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
199
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">51 │ │ │ │ │ │ </span>image=image, <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
200
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">52 │ │ │ │ │ │ </span>mask_image=mask_image, <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
201
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">53 │ │ │ │ │ │ </span>num_inference_steps=num_inference_steps, <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
202
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
203
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages/torch/autograd/</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">grad_mode.py</span>:<span style=\"color: #0000ff; text-decoration-color: #0000ff\">27</span> in <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
204
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #00ff00; text-decoration-color: #00ff00\">decorate_context</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
205
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
206
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 24 │ │ </span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">@functools</span>.wraps(func) <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
207
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 25 │ │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">def</span> <span style=\"color: #00ff00; text-decoration-color: #00ff00\">decorate_context</span>(*args, **kwargs): <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
208
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 26 │ │ │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">with</span> <span style=\"color: #00ffff; text-decoration-color: #00ffff\">self</span>.clone(): <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
209
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">❱ </span> 27 <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">│ │ │ │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">return</span> func(*args, **kwargs) <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
210
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 28 │ │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">return</span> cast(F, decorate_context) <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
211
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 29 │ </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
212
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 30 │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">def</span> <span style=\"color: #00ff00; text-decoration-color: #00ff00\">_wrap_generator</span>(<span style=\"color: #00ffff; text-decoration-color: #00ffff\">self</span>, func): <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
213
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
214
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages/diffusers/pipelines/stable_diffusion</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
215
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">pipeline_stable_diffusion_inpaint.py</span>:<span style=\"color: #0000ff; text-decoration-color: #0000ff\">676</span> in <span style=\"color: #00ff00; text-decoration-color: #00ff00\">__call__</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
216
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
217
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">673 │ │ </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
218
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">674 │ │ # 7. Prepare mask latent variables</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
219
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">675 │ │ </span>mask, masked_image_latents = <span style=\"color: #00ffff; text-decoration-color: #00ffff\">self</span>.prepare_mask_latents( <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
220
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">❱ </span>676 <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">│ │ │ </span>mask, <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
221
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">677 │ │ │ </span>masked_image, <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
222
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">678 │ │ │ </span>batch_size * num_images_per_prompt, <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
223
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">679 │ │ │ </span>height, <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
224
+ "<span style=\"color: #800000; text-decoration-color: #800000\">╰──────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
225
+ "<span style=\"color: #ff0000; text-decoration-color: #ff0000; font-weight: bold\">UnboundLocalError: </span>local variable <span style=\"color: #008000; text-decoration-color: #008000\">'mask'</span> referenced before assignment\n",
226
+ "</pre>\n"
227
+ ],
228
+ "text/plain": [
229
+ "\u001b[31m╭─\u001b[0m\u001b[31m──────────────────────────────\u001b[0m\u001b[31m \u001b[0m\u001b[1;31mTraceback \u001b[0m\u001b[1;2;31m(most recent call last)\u001b[0m\u001b[31m \u001b[0m\u001b[31m───────────────────────────────\u001b[0m\u001b[31m─╮\u001b[0m\n",
230
+ "\u001b[31m│\u001b[0m \u001b[2;33m/tmp/ipykernel_12933/\u001b[0m\u001b[1;33m3313959780.py\u001b[0m:\u001b[94m26\u001b[0m in \u001b[92m<cell line: 26>\u001b[0m \u001b[31m│\u001b[0m\n",
231
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
232
+ "\u001b[31m│\u001b[0m \u001b[3;31m[Errno 2] No such file or directory: '/tmp/ipykernel_12933/3313959780.py'\u001b[0m \u001b[31m│\u001b[0m\n",
233
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
234
+ "\u001b[31m│\u001b[0m \u001b[2;33m/home/ubuntu/endpoints/stable-diffusion-2-inpainting-endpoint/\u001b[0m\u001b[1;33mhandler.py\u001b[0m:\u001b[94m50\u001b[0m in \u001b[92m__call__\u001b[0m \u001b[31m│\u001b[0m\n",
235
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
236
+ "\u001b[31m│\u001b[0m \u001b[2m47 \u001b[0m\u001b[2m│ │ │ \u001b[0mmask_image = \u001b[94mNone\u001b[0m \u001b[31m│\u001b[0m\n",
237
+ "\u001b[31m│\u001b[0m \u001b[2m48 \u001b[0m\u001b[2m│ │ \u001b[0m \u001b[31m│\u001b[0m\n",
238
+ "\u001b[31m│\u001b[0m \u001b[2m49 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[2m# run inference pipeline\u001b[0m \u001b[31m│\u001b[0m\n",
239
+ "\u001b[31m│\u001b[0m \u001b[31m❱ \u001b[0m50 \u001b[2m│ │ \u001b[0mout = \u001b[96mself\u001b[0m.pipe(inputs, \u001b[31m│\u001b[0m\n",
240
+ "\u001b[31m│\u001b[0m \u001b[2m51 \u001b[0m\u001b[2m│ │ │ │ │ │ \u001b[0mimage=image, \u001b[31m│\u001b[0m\n",
241
+ "\u001b[31m│\u001b[0m \u001b[2m52 \u001b[0m\u001b[2m�� │ │ │ │ │ \u001b[0mmask_image=mask_image, \u001b[31m│\u001b[0m\n",
242
+ "\u001b[31m│\u001b[0m \u001b[2m53 \u001b[0m\u001b[2m│ │ │ │ │ │ \u001b[0mnum_inference_steps=num_inference_steps, \u001b[31m│\u001b[0m\n",
243
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
244
+ "\u001b[31m│\u001b[0m \u001b[2;33m/home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages/torch/autograd/\u001b[0m\u001b[1;33mgrad_mode.py\u001b[0m:\u001b[94m27\u001b[0m in \u001b[31m│\u001b[0m\n",
245
+ "\u001b[31m│\u001b[0m \u001b[92mdecorate_context\u001b[0m \u001b[31m│\u001b[0m\n",
246
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
247
+ "\u001b[31m│\u001b[0m \u001b[2m 24 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[1;95m@functools\u001b[0m.wraps(func) \u001b[31m│\u001b[0m\n",
248
+ "\u001b[31m│\u001b[0m \u001b[2m 25 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[94mdef\u001b[0m \u001b[92mdecorate_context\u001b[0m(*args, **kwargs): \u001b[31m│\u001b[0m\n",
249
+ "\u001b[31m│\u001b[0m \u001b[2m 26 \u001b[0m\u001b[2m│ │ │ \u001b[0m\u001b[94mwith\u001b[0m \u001b[96mself\u001b[0m.clone(): \u001b[31m│\u001b[0m\n",
250
+ "\u001b[31m│\u001b[0m \u001b[31m❱ \u001b[0m 27 \u001b[2m│ │ │ │ \u001b[0m\u001b[94mreturn\u001b[0m func(*args, **kwargs) \u001b[31m│\u001b[0m\n",
251
+ "\u001b[31m│\u001b[0m \u001b[2m 28 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[94mreturn\u001b[0m cast(F, decorate_context) \u001b[31m│\u001b[0m\n",
252
+ "\u001b[31m│\u001b[0m \u001b[2m 29 \u001b[0m\u001b[2m│ \u001b[0m \u001b[31m│\u001b[0m\n",
253
+ "\u001b[31m│\u001b[0m \u001b[2m 30 \u001b[0m\u001b[2m│ \u001b[0m\u001b[94mdef\u001b[0m \u001b[92m_wrap_generator\u001b[0m(\u001b[96mself\u001b[0m, func): \u001b[31m│\u001b[0m\n",
254
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
255
+ "\u001b[31m│\u001b[0m \u001b[2;33m/home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages/diffusers/pipelines/stable_diffusion\u001b[0m \u001b[31m│\u001b[0m\n",
256
+ "\u001b[31m│\u001b[0m \u001b[2;33m/\u001b[0m\u001b[1;33mpipeline_stable_diffusion_inpaint.py\u001b[0m:\u001b[94m676\u001b[0m in \u001b[92m__call__\u001b[0m \u001b[31m│\u001b[0m\n",
257
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
258
+ "\u001b[31m│\u001b[0m \u001b[2m673 \u001b[0m\u001b[2m│ │ \u001b[0m \u001b[31m│\u001b[0m\n",
259
+ "\u001b[31m│\u001b[0m \u001b[2m674 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[2m# 7. Prepare mask latent variables\u001b[0m \u001b[31m│\u001b[0m\n",
260
+ "\u001b[31m│\u001b[0m \u001b[2m675 \u001b[0m\u001b[2m│ │ \u001b[0mmask, masked_image_latents = \u001b[96mself\u001b[0m.prepare_mask_latents( \u001b[31m│\u001b[0m\n",
261
+ "\u001b[31m│\u001b[0m \u001b[31m❱ \u001b[0m676 \u001b[2m│ │ │ \u001b[0mmask, \u001b[31m│\u001b[0m\n",
262
+ "\u001b[31m│\u001b[0m \u001b[2m677 \u001b[0m\u001b[2m│ │ │ \u001b[0mmasked_image, \u001b[31m│\u001b[0m\n",
263
+ "\u001b[31m│\u001b[0m \u001b[2m678 \u001b[0m\u001b[2m│ │ │ \u001b[0mbatch_size * num_images_per_prompt, \u001b[31m│\u001b[0m\n",
264
+ "\u001b[31m│\u001b[0m \u001b[2m679 \u001b[0m\u001b[2m│ │ │ \u001b[0mheight, \u001b[31m│\u001b[0m\n",
265
+ "\u001b[31m╰──────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n",
266
+ "\u001b[1;91mUnboundLocalError: \u001b[0mlocal variable \u001b[32m'mask'\u001b[0m referenced before assignment\n"
267
+ ]
268
+ },
269
+ "metadata": {},
270
+ "output_type": "display_data"
271
+ }
272
+ ],
273
+ "source": [
274
+ "import base64\n",
275
+ "from PIL import Image\n",
276
+ "from io import BytesIO\n",
277
+ "\n",
278
+ "# helper image utils\n",
279
+ "def encode_image(image_path):\n",
280
+ " with open(image_path, \"rb\") as i:\n",
281
+ " b64 = base64.b64encode(i.read())\n",
282
+ " return b64.decode(\"utf-8\")\n",
283
+ " \n",
284
+ "def decode_image(image_string):\n",
285
+ " base64_image = base64.b64decode(image_string)\n",
286
+ " buffer = BytesIO(base64_image)\n",
287
+ " image = Image.open(buffer)\n",
288
+ " return image\n",
289
+ "\n",
290
+ "prompt = \"a high resulotion image of a seberian cat\"\n",
291
+ "image = encode_image(\"dog.png\")\n",
292
+ "mask_image = encode_image(\"mask_dog.png\")\n",
293
+ "\n",
294
+ "# prepare sample payload\n",
295
+ "request = {\"inputs\": prompt, \"image\": image, \"mask_image\": mask_image}\n",
296
+ "\n",
297
+ "# test the handler\n",
298
+ "pred = my_handler(request)"
299
+ ]
300
+ },
301
+ {
302
+ "cell_type": "code",
303
+ "execution_count": 4,
304
+ "metadata": {},
305
+ "outputs": [],
306
+ "source": [
307
+ "decode_image(pred[\"image\"])"
308
+ ]
309
+ },
310
+ {
311
+ "cell_type": "markdown",
312
+ "metadata": {},
313
+ "source": [
314
+ "![test](sample.jpg)"
315
+ ]
316
+ },
317
+ {
318
+ "cell_type": "code",
319
+ "execution_count": 8,
320
+ "metadata": {},
321
+ "outputs": [
322
+ {
323
+ "name": "stderr",
324
+ "output_type": "stream",
325
+ "text": [
326
+ "The config attributes {'upcast_attention': False} were passed to UNet2DConditionModel, but are not expected and will be ignored. Please verify your config.json configuration file.\n"
327
+ ]
328
+ },
329
+ {
330
+ "data": {
331
+ "text/html": [
332
+ "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000; text-decoration-color: #800000\">╭─────────────────────────────── </span><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">Traceback </span><span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f; font-weight: bold\">(most recent call last)</span><span style=\"color: #800000; text-decoration-color: #800000\"> ────────────────────────────────╮</span>\n",
333
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/tmp/ipykernel_12933/</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">1159614552.py</span>:<span style=\"color: #0000ff; text-decoration-color: #0000ff\">16</span> in <span style=\"color: #00ff00; text-decoration-color: #00ff00\">&lt;cell line: 16&gt;</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
334
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
335
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000; font-style: italic\">[Errno 2] No such file or directory: '/tmp/ipykernel_12933/1159614552.py'</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
336
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
337
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages/torch/autograd/</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">grad_mode.py</span>:<span style=\"color: #0000ff; text-decoration-color: #0000ff\">27</span> in <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
338
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #00ff00; text-decoration-color: #00ff00\">decorate_context</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
339
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
340
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 24 │ │ </span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">@functools</span>.wraps(func) <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
341
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 25 │ │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">def</span> <span style=\"color: #00ff00; text-decoration-color: #00ff00\">decorate_context</span>(*args, **kwargs): <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
342
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 26 │ │ │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">with</span> <span style=\"color: #00ffff; text-decoration-color: #00ffff\">self</span>.clone(): <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
343
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">❱ </span> 27 <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">│ │ │ │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">return</span> func(*args, **kwargs) <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
344
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 28 │ │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">return</span> cast(F, decorate_context) <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
345
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 29 │ </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
346
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> 30 │ </span><span style=\"color: #0000ff; text-decoration-color: #0000ff\">def</span> <span style=\"color: #00ff00; text-decoration-color: #00ff00\">_wrap_generator</span>(<span style=\"color: #00ffff; text-decoration-color: #00ffff\">self</span>, func): <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
347
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
348
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages/diffusers/pipelines/stable_diffusion</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
349
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">pipeline_stable_diffusion_inpaint.py</span>:<span style=\"color: #0000ff; text-decoration-color: #0000ff\">676</span> in <span style=\"color: #00ff00; text-decoration-color: #00ff00\">__call__</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
350
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
351
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">673 │ │ </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
352
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">674 │ │ # 11. Post-processing</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
353
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">675 │ │ </span>image = <span style=\"color: #00ffff; text-decoration-color: #00ffff\">self</span>.decode_latents(latents) <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
354
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">❱ </span>676 <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">│ │ </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
355
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">677 │ │ # 12. Run safety checker</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
356
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">678 │ │ </span>image, has_nsfw_concept = <span style=\"color: #00ffff; text-decoration-color: #00ffff\">self</span>.run_safety_checker(image, device, text_embeddings <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
357
+ "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">679 </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
358
+ "<span style=\"color: #800000; text-decoration-color: #800000\">╰──────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
359
+ "<span style=\"color: #ff0000; text-decoration-color: #ff0000; font-weight: bold\">UnboundLocalError: </span>local variable <span style=\"color: #008000; text-decoration-color: #008000\">'mask'</span> referenced before assignment\n",
360
+ "</pre>\n"
361
+ ],
362
+ "text/plain": [
363
+ "\u001b[31m╭─\u001b[0m\u001b[31m──────────────────────────────\u001b[0m\u001b[31m \u001b[0m\u001b[1;31mTraceback \u001b[0m\u001b[1;2;31m(most recent call last)\u001b[0m\u001b[31m \u001b[0m\u001b[31m───────────────────────────────\u001b[0m\u001b[31m─╮\u001b[0m\n",
364
+ "\u001b[31m│\u001b[0m \u001b[2;33m/tmp/ipykernel_12933/\u001b[0m\u001b[1;33m1159614552.py\u001b[0m:\u001b[94m16\u001b[0m in \u001b[92m<cell line: 16>\u001b[0m \u001b[31m│\u001b[0m\n",
365
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
366
+ "\u001b[31m│\u001b[0m \u001b[3;31m[Errno 2] No such file or directory: '/tmp/ipykernel_12933/1159614552.py'\u001b[0m \u001b[31m│\u001b[0m\n",
367
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
368
+ "\u001b[31m│\u001b[0m \u001b[2;33m/home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages/torch/autograd/\u001b[0m\u001b[1;33mgrad_mode.py\u001b[0m:\u001b[94m27\u001b[0m in \u001b[31m│\u001b[0m\n",
369
+ "\u001b[31m│\u001b[0m \u001b[92mdecorate_context\u001b[0m \u001b[31m│\u001b[0m\n",
370
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
371
+ "\u001b[31m│\u001b[0m \u001b[2m 24 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[1;95m@functools\u001b[0m.wraps(func) \u001b[31m│\u001b[0m\n",
372
+ "\u001b[31m│\u001b[0m \u001b[2m 25 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[94mdef\u001b[0m \u001b[92mdecorate_context\u001b[0m(*args, **kwargs): \u001b[31m│\u001b[0m\n",
373
+ "\u001b[31m│\u001b[0m \u001b[2m 26 \u001b[0m\u001b[2m│ │ │ \u001b[0m\u001b[94mwith\u001b[0m \u001b[96mself\u001b[0m.clone(): \u001b[31m│\u001b[0m\n",
374
+ "\u001b[31m│\u001b[0m \u001b[31m❱ \u001b[0m 27 \u001b[2m│ │ │ │ \u001b[0m\u001b[94mreturn\u001b[0m func(*args, **kwargs) \u001b[31m│\u001b[0m\n",
375
+ "\u001b[31m│\u001b[0m \u001b[2m 28 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[94mreturn\u001b[0m cast(F, decorate_context) \u001b[31m│\u001b[0m\n",
376
+ "\u001b[31m│\u001b[0m \u001b[2m 29 \u001b[0m\u001b[2m│ \u001b[0m \u001b[31m│\u001b[0m\n",
377
+ "\u001b[31m│\u001b[0m \u001b[2m 30 \u001b[0m\u001b[2m│ \u001b[0m\u001b[94mdef\u001b[0m \u001b[92m_wrap_generator\u001b[0m(\u001b[96mself\u001b[0m, func): \u001b[31m│\u001b[0m\n",
378
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
379
+ "\u001b[31m│\u001b[0m \u001b[2;33m/home/ubuntu/miniconda/envs/dev/lib/python3.9/site-packages/diffusers/pipelines/stable_diffusion\u001b[0m \u001b[31m│\u001b[0m\n",
380
+ "\u001b[31m│\u001b[0m \u001b[2;33m/\u001b[0m\u001b[1;33mpipeline_stable_diffusion_inpaint.py\u001b[0m:\u001b[94m676\u001b[0m in \u001b[92m__call__\u001b[0m \u001b[31m│\u001b[0m\n",
381
+ "\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m\n",
382
+ "\u001b[31m│\u001b[0m \u001b[2m673 \u001b[0m\u001b[2m│ │ \u001b[0m \u001b[31m│\u001b[0m\n",
383
+ "\u001b[31m│\u001b[0m \u001b[2m674 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[2m# 11. Post-processing\u001b[0m \u001b[31m│\u001b[0m\n",
384
+ "\u001b[31m│\u001b[0m \u001b[2m675 \u001b[0m\u001b[2m│ │ \u001b[0mimage = \u001b[96mself\u001b[0m.decode_latents(latents) \u001b[31m│\u001b[0m\n",
385
+ "\u001b[31m│\u001b[0m \u001b[31m❱ \u001b[0m676 \u001b[2m│ │ \u001b[0m \u001b[31m│\u001b[0m\n",
386
+ "\u001b[31m│\u001b[0m \u001b[2m677 \u001b[0m\u001b[2m│ │ \u001b[0m\u001b[2m# 12. Run safety checker\u001b[0m \u001b[31m│\u001b[0m\n",
387
+ "\u001b[31m│\u001b[0m \u001b[2m678 \u001b[0m\u001b[2m│ │ \u001b[0mimage, has_nsfw_concept = \u001b[96mself\u001b[0m.run_safety_checker(image, device, text_embeddings \u001b[31m│\u001b[0m\n",
388
+ "\u001b[31m│\u001b[0m \u001b[2m679 \u001b[0m \u001b[31m│\u001b[0m\n",
389
+ "\u001b[31m╰──────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n",
390
+ "\u001b[1;91mUnboundLocalError: \u001b[0mlocal variable \u001b[32m'mask'\u001b[0m referenced before assignment\n"
391
+ ]
392
+ },
393
+ "metadata": {},
394
+ "output_type": "display_data"
395
+ }
396
+ ],
397
+ "source": [
398
+ "from diffusers import StableDiffusionInpaintPipeline\n",
399
+ "import torch\n",
400
+ "from PIL import Image\n",
401
+ "\n",
402
+ "pipe = StableDiffusionInpaintPipeline.from_pretrained(\n",
403
+ "\".\" , torch_dtype=torch.float16)\n",
404
+ "pipe.to(\"cuda\")\n",
405
+ "\n",
406
+ "\n",
407
+ "image = Image.open(\"dog.png\").convert(\"RGB\").thumbnail((768, 768))\n",
408
+ "mask = Image.open(\"mask_dog.png\").convert(\"RGB\").thumbnail((768, 768))\n",
409
+ "\n",
410
+ "prompt = \"Face of a yellow cat, high resolution, sitting on a park bench\"\n",
411
+ "#image and mask_image should be PIL images.\n",
412
+ "#The mask structure is white for inpainting and black for keeping as is\n",
413
+ "image = pipe(prompt=prompt, image=image, mask_image=mask).images[0]\n",
414
+ "image.save(\"./yellow_cat_on_park_bench.png\")"
415
+ ]
416
+ },
417
+ {
418
+ "cell_type": "code",
419
+ "execution_count": null,
420
+ "metadata": {},
421
+ "outputs": [],
422
+ "source": []
423
+ }
424
+ ],
425
+ "metadata": {
426
+ "kernelspec": {
427
+ "display_name": "Python 3.9.13 ('dev': conda)",
428
+ "language": "python",
429
+ "name": "python3"
430
+ },
431
+ "language_info": {
432
+ "codemirror_mode": {
433
+ "name": "ipython",
434
+ "version": 3
435
+ },
436
+ "file_extension": ".py",
437
+ "mimetype": "text/x-python",
438
+ "name": "python",
439
+ "nbconvert_exporter": "python",
440
+ "pygments_lexer": "ipython3",
441
+ "version": "3.9.13"
442
+ },
443
+ "orig_nbformat": 4,
444
+ "vscode": {
445
+ "interpreter": {
446
+ "hash": "f6dd96c16031089903d5a31ec148b80aeb0d39c32affb1a1080393235fbfa2fc"
447
+ }
448
+ }
449
+ },
450
+ "nbformat": 4,
451
+ "nbformat_minor": 2
452
+ }
dog.png ADDED
handler.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ import torch
3
+ from diffusers import DPMSolverMultistepScheduler, StableDiffusionInpaintPipeline
4
+ from PIL import Image
5
+ import base64
6
+ from io import BytesIO
7
+
8
+
9
+ # set device
10
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11
+
12
+ if device.type != 'cuda':
13
+ raise ValueError("need to run on GPU")
14
+
15
+ class EndpointHandler():
16
+ def __init__(self, path=""):
17
+ # load StableDiffusionInpaintPipeline pipeline
18
+ self.pipe = StableDiffusionInpaintPipeline.from_pretrained(path, torch_dtype=torch.float16)
19
+ # use DPMSolverMultistepScheduler
20
+ self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(self.pipe.scheduler.config)
21
+ # move to device
22
+ self.pipe = self.pipe.to(device)
23
+
24
+
25
+ def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
26
+ """
27
+ :param data: A dictionary contains `inputs` and optional `image` field.
28
+ :return: A dictionary with `image` field contains image in base64.
29
+ """
30
+ inputs = data.pop("inputs", data)
31
+ encoded_image = data.pop("image", None)
32
+ encoded_mask_image = data.pop("mask_image", None)
33
+
34
+ # hyperparamters
35
+ num_inference_steps = data.pop("num_inference_steps", 25)
36
+ guidance_scale = data.pop("guidance_scale", 7.5)
37
+ negative_prompt = data.pop("negative_prompt", None)
38
+ height = data.pop("height", None)
39
+ width = data.pop("width", None)
40
+
41
+ # process image
42
+ if encoded_image is not None and encoded_mask_image is not None:
43
+ image = self.decode_base64_image(encoded_image)
44
+ mask_image = self.decode_base64_image(encoded_mask_image)
45
+ else:
46
+ image = None
47
+ mask_image = None
48
+
49
+ # run inference pipeline
50
+ out = self.pipe(inputs,
51
+ image=image,
52
+ mask_image=mask_image,
53
+ num_inference_steps=num_inference_steps,
54
+ guidance_scale=guidance_scale,
55
+ num_images_per_prompt=1,
56
+ negative_prompt=negative_prompt,
57
+ height=height,
58
+ width=width
59
+ )
60
+
61
+ # return first generate PIL image
62
+ return out.images[0]
63
+
64
+ # helper to decode input image
65
+ def decode_base64_image(self, image_string):
66
+ base64_image = base64.b64decode(image_string)
67
+ buffer = BytesIO(base64_image)
68
+ image = Image.open(buffer)
69
+ return image.convert("RGB").thumbnail((768, 768))
mask_dog.png ADDED
model_index.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "StableDiffusionInpaintPipeline",
3
+ "_diffusers_version": "0.10.2",
4
+ "feature_extractor": [
5
+ null,
6
+ null
7
+ ],
8
+ "requires_safety_checker": false,
9
+ "safety_checker": [
10
+ null,
11
+ null
12
+ ],
13
+ "scheduler": [
14
+ "diffusers",
15
+ "PNDMScheduler"
16
+ ],
17
+ "text_encoder": [
18
+ "transformers",
19
+ "CLIPTextModel"
20
+ ],
21
+ "tokenizer": [
22
+ "transformers",
23
+ "CLIPTokenizer"
24
+ ],
25
+ "unet": [
26
+ "diffusers",
27
+ "UNet2DConditionModel"
28
+ ],
29
+ "vae": [
30
+ "diffusers",
31
+ "AutoencoderKL"
32
+ ]
33
+ }
requirements.txt ADDED
@@ -0,0 +1 @@
 
1
+ diffusers==0.10.2
scheduler/scheduler_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "PNDMScheduler",
3
+ "_diffusers_version": "0.10.2",
4
+ "beta_end": 0.012,
5
+ "beta_schedule": "scaled_linear",
6
+ "beta_start": 0.00085,
7
+ "clip_sample": false,
8
+ "num_train_timesteps": 1000,
9
+ "prediction_type": "epsilon",
10
+ "set_alpha_to_one": false,
11
+ "skip_prk_steps": true,
12
+ "steps_offset": 1,
13
+ "trained_betas": null
14
+ }
text_encoder/config.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/home/ubuntu/.cache/huggingface/diffusers/models--stabilityai--stable-diffusion-2-inpainting/snapshots/76b00d76134aca7fc5e7137a469498627ad6b4bf/text_encoder",
3
+ "architectures": [
4
+ "CLIPTextModel"
5
+ ],
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 0,
8
+ "dropout": 0.0,
9
+ "eos_token_id": 2,
10
+ "hidden_act": "gelu",
11
+ "hidden_size": 1024,
12
+ "initializer_factor": 1.0,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 4096,
15
+ "layer_norm_eps": 1e-05,
16
+ "max_position_embeddings": 77,
17
+ "model_type": "clip_text_model",
18
+ "num_attention_heads": 16,
19
+ "num_hidden_layers": 23,
20
+ "pad_token_id": 1,
21
+ "projection_dim": 512,
22
+ "torch_dtype": "float16",
23
+ "transformers_version": "4.24.0",
24
+ "vocab_size": 49408
25
+ }
text_encoder/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1f1fce5bf3a7d2f31cddebc1f67ec9b34c1786c5b5804fc9513a4231e8d1bf10
3
+ size 680896215
tokenizer/merges.txt ADDED
The diff for this file is too large to render. See raw diff
tokenizer/special_tokens_map.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<|startoftext|>",
4
+ "lstrip": false,
5
+ "normalized": true,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "<|endoftext|>",
11
+ "lstrip": false,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": "!",
17
+ "unk_token": {
18
+ "content": "<|endoftext|>",
19
+ "lstrip": false,
20
+ "normalized": true,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ }
24
+ }
tokenizer/tokenizer_config.json ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "bos_token": {
4
+ "__type": "AddedToken",
5
+ "content": "<|startoftext|>",
6
+ "lstrip": false,
7
+ "normalized": true,
8
+ "rstrip": false,
9
+ "single_word": false
10
+ },
11
+ "do_lower_case": true,
12
+ "eos_token": {
13
+ "__type": "AddedToken",
14
+ "content": "<|endoftext|>",
15
+ "lstrip": false,
16
+ "normalized": true,
17
+ "rstrip": false,
18
+ "single_word": false
19
+ },
20
+ "errors": "replace",
21
+ "model_max_length": 77,
22
+ "name_or_path": "/home/ubuntu/.cache/huggingface/diffusers/models--stabilityai--stable-diffusion-2-inpainting/snapshots/76b00d76134aca7fc5e7137a469498627ad6b4bf/tokenizer",
23
+ "pad_token": "<|endoftext|>",
24
+ "special_tokens_map_file": "./special_tokens_map.json",
25
+ "tokenizer_class": "CLIPTokenizer",
26
+ "unk_token": {
27
+ "__type": "AddedToken",
28
+ "content": "<|endoftext|>",
29
+ "lstrip": false,
30
+ "normalized": true,
31
+ "rstrip": false,
32
+ "single_word": false
33
+ }
34
+ }
tokenizer/vocab.json ADDED
The diff for this file is too large to render. See raw diff
unet/config.json ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "UNet2DConditionModel",
3
+ "_diffusers_version": "0.10.2",
4
+ "_name_or_path": "/home/ubuntu/.cache/huggingface/diffusers/models--stabilityai--stable-diffusion-2-inpainting/snapshots/76b00d76134aca7fc5e7137a469498627ad6b4bf/unet",
5
+ "act_fn": "silu",
6
+ "attention_head_dim": [
7
+ 5,
8
+ 10,
9
+ 20,
10
+ 20
11
+ ],
12
+ "block_out_channels": [
13
+ 320,
14
+ 640,
15
+ 1280,
16
+ 1280
17
+ ],
18
+ "center_input_sample": false,
19
+ "cross_attention_dim": 1024,
20
+ "down_block_types": [
21
+ "CrossAttnDownBlock2D",
22
+ "CrossAttnDownBlock2D",
23
+ "CrossAttnDownBlock2D",
24
+ "DownBlock2D"
25
+ ],
26
+ "downsample_padding": 1,
27
+ "dual_cross_attention": false,
28
+ "flip_sin_to_cos": true,
29
+ "freq_shift": 0,
30
+ "in_channels": 9,
31
+ "layers_per_block": 2,
32
+ "mid_block_scale_factor": 1,
33
+ "norm_eps": 1e-05,
34
+ "norm_num_groups": 32,
35
+ "num_class_embeds": null,
36
+ "only_cross_attention": false,
37
+ "out_channels": 4,
38
+ "sample_size": 64,
39
+ "up_block_types": [
40
+ "UpBlock2D",
41
+ "CrossAttnUpBlock2D",
42
+ "CrossAttnUpBlock2D",
43
+ "CrossAttnUpBlock2D"
44
+ ],
45
+ "upcast_attention": false,
46
+ "use_linear_projection": true
47
+ }
unet/diffusion_pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:20722649ed12ff926183129d7f2f7957388d03edc79888be6da8e3346ef9e873
3
+ size 1732120805
vae/config.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "AutoencoderKL",
3
+ "_diffusers_version": "0.10.2",
4
+ "_name_or_path": "/home/ubuntu/.cache/huggingface/diffusers/models--stabilityai--stable-diffusion-2-inpainting/snapshots/76b00d76134aca7fc5e7137a469498627ad6b4bf/vae",
5
+ "act_fn": "silu",
6
+ "block_out_channels": [
7
+ 128,
8
+ 256,
9
+ 512,
10
+ 512
11
+ ],
12
+ "down_block_types": [
13
+ "DownEncoderBlock2D",
14
+ "DownEncoderBlock2D",
15
+ "DownEncoderBlock2D",
16
+ "DownEncoderBlock2D"
17
+ ],
18
+ "in_channels": 3,
19
+ "latent_channels": 4,
20
+ "layers_per_block": 2,
21
+ "norm_num_groups": 32,
22
+ "out_channels": 3,
23
+ "sample_size": 512,
24
+ "up_block_types": [
25
+ "UpDecoderBlock2D",
26
+ "UpDecoderBlock2D",
27
+ "UpDecoderBlock2D",
28
+ "UpDecoderBlock2D"
29
+ ]
30
+ }
vae/diffusion_pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3b0f1843b01fbf820827e257eec40db099cde863f07314ae6ab641298e11fc98
3
+ size 167399505