makakwastaken commited on
Commit
80d356c
·
1 Parent(s): 186d8fb

Removed Inpaint

Browse files
Files changed (2) hide show
  1. app.py +109 -109
  2. requirements.txt +5 -3
app.py CHANGED
@@ -58,9 +58,9 @@ async def startup_event():
58
  model = Model(CONFIG['DEVICE'])
59
 
60
  # Initialize the stable-diffusion-inpainting model
61
- pipe = StableDiffusionInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-2-inpainting", safety_checker=None)
62
 
63
- pipe.to(CONFIG['DEVICE'])
64
 
65
  # Initialize the connection manager
66
  connectionManager = ConnectionManager()
@@ -69,7 +69,7 @@ async def startup_event():
69
  app.package = {
70
  "model": model,
71
  "connectionManager": connectionManager,
72
- "pipe": pipe
73
  }
74
 
75
  @app.get("/ping")
@@ -98,66 +98,66 @@ def show_about():
98
  "nvidia-smi": bash('nvidia-smi')
99
  }
100
 
101
- def resize_image(img, height=512, width=512):
102
- '''Resize image to `size`'''
103
 
104
- size = (width, height)
105
 
106
- img_resized = img.resize(size, Image.ANTIALIAS)
107
- return img_resized
108
 
109
- def crop_image(img, d=64):
110
- '''Make dimensions divisible by `d`'''
111
 
112
- new_size = (img.size[0] - img.size[0] % d,
113
- img.size[1] - img.size[1] % d)
114
 
115
- bbox = [
116
- int((img.size[0] - new_size[0])/2),
117
- int((img.size[1] - new_size[1])/2),
118
- int((img.size[0] + new_size[0])/2),
119
- int((img.size[1] + new_size[1])/2),
120
- ]
121
 
122
- img_cropped = img.crop(bbox)
123
- return img_cropped
124
 
125
- class InpaintBody(BaseModel):
126
- image: str
127
- mask: str
128
- prompt: str
129
 
130
- @app.post("/inpaint")
131
- async def do_inpaint(body: InpaintBody):
132
- """
133
- Perform inpainting on input data
134
- """
135
 
136
- logger.info('API inpaint called')
137
- image_data = body.image
138
- mask_data = body.mask
139
- prompt = body.prompt
140
 
141
- # Extract base64 from mask and convert to PIL.Image
142
- if (',' in image_data):
143
- image = Image.open(BytesIO(base64.b64decode(image_data.split(',')[1])))
144
- else:
145
- image = Image.open(BytesIO(base64.b64decode(image_data)))
146
-
147
- # Extract base64 from mask and convert to PIL.Image
148
- if (',' in mask_data):
149
- mask = Image.open(BytesIO(base64.b64decode(mask_data.split(',')[1])))
150
- else:
151
- mask = Image.open(BytesIO(base64.b64decode(mask_data)))
152
-
153
- # Resize image and mask to 512x512
154
- image = crop_image(resize_image(image, 512, 512))
155
- mask = crop_image(resize_image(image, 512, 512))
156
-
157
- pipe = app.package.get('pipe')
158
- result = pipe(prompt=prompt, image=image, mask_image=mask, num_inference_steps=10, num_images_per_prompt=1)
159
- images = result['images']
160
- return images
161
 
162
 
163
  class ImageBody(BaseModel):
@@ -188,65 +188,65 @@ async def do_predict(body: ImageBody):
188
  return {"ok": True, "status": "FINISHED", "result": img_str}
189
 
190
 
191
- @app.websocket("/ws-inpaint")
192
- async def inpaint_websocket_endpoint(websocket: WebSocket):
193
- connectionManager = app.package.get('connectionManager')
194
- await connectionManager.connect(websocket)
195
- await connectionManager.send_json({"ok": True, "status": "CONNECTED"}, websocket)
196
- while True:
197
- try:
198
- data: ImageBody = await connectionManager.receive_json(websocket)
199
- if (data is None):
200
- # Wait for data
201
- if not connectionManager.isConnected(websocket):
202
- break
203
- if connectionManager.shouldDisconnect(websocket):
204
- await websocket.close()
205
- connectionManager.disconnect(websocket)
206
- break
207
- continue
208
 
209
- image_data: str = data.get('image')
210
- mask_data: str = data.get('mask')
211
- prompt: str = data.get('prompt')
212
 
213
- await connectionManager.send_json({"ok": True, "status": "STARTED"}, websocket)
214
 
215
- # Extract base64 from mask and convert to PIL.Image
216
- if (',' in image_data):
217
- image = Image.open(BytesIO(base64.b64decode(image_data.split(',')[1])))
218
- else:
219
- image = Image.open(BytesIO(base64.b64decode(image_data)))
220
-
221
- # Extract base64 from mask and convert to PIL.Image
222
- if (',' in mask_data):
223
- mask = Image.open(BytesIO(base64.b64decode(mask_data.split(',')[1])))
224
- else:
225
- mask = Image.open(BytesIO(base64.b64decode(mask_data)))
226
-
227
- # Resize image and mask to 512x512
228
- image = crop_image(resize_image(image, 512, 512))
229
- mask = crop_image(resize_image(image, 512, 512))
230
-
231
- pipe = app.package.get('pipe')
232
- result = pipe(prompt=prompt, image=image, mask_image=mask, num_inference_steps=20, num_images_per_prompt=1)
233
- images = result['images']
234
-
235
- # Convert the result to base64 and send the json back
236
- result_array = []
237
- for image in images:
238
- buffered = BytesIO()
239
- image.save(buffered, format="JPEG")
240
- img_str = 'data:image/jpeg;base64,' + base64.b64encode(buffered.getvalue()).decode("utf-8")
241
- result_array.append(img_str)
242
-
243
- await connectionManager.send_json({"ok": True, "status": "FINISHED", "result": result_array}, websocket)
244
-
245
- await websocket.close()
246
- connectionManager.disconnect(websocket)
247
- except WebSocketDisconnect:
248
- connectionManager.disconnect(websocket)
249
- break
250
 
251
  @app.websocket("/ws")
252
  async def websocket_endpoint(websocket: WebSocket):
 
58
  model = Model(CONFIG['DEVICE'])
59
 
60
  # Initialize the stable-diffusion-inpainting model
61
+ # pipe = StableDiffusionInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-2-inpainting", safety_checker=None)
62
 
63
+ # pipe.to(CONFIG['DEVICE'])
64
 
65
  # Initialize the connection manager
66
  connectionManager = ConnectionManager()
 
69
  app.package = {
70
  "model": model,
71
  "connectionManager": connectionManager,
72
+ # "pipe": pipe
73
  }
74
 
75
  @app.get("/ping")
 
98
  "nvidia-smi": bash('nvidia-smi')
99
  }
100
 
101
+ # def resize_image(img, height=512, width=512):
102
+ # '''Resize image to `size`'''
103
 
104
+ # size = (width, height)
105
 
106
+ # img_resized = img.resize(size, Image.ANTIALIAS)
107
+ # return img_resized
108
 
109
+ # def crop_image(img, d=64):
110
+ # '''Make dimensions divisible by `d`'''
111
 
112
+ # new_size = (img.size[0] - img.size[0] % d,
113
+ # img.size[1] - img.size[1] % d)
114
 
115
+ # bbox = [
116
+ # int((img.size[0] - new_size[0])/2),
117
+ # int((img.size[1] - new_size[1])/2),
118
+ # int((img.size[0] + new_size[0])/2),
119
+ # int((img.size[1] + new_size[1])/2),
120
+ # ]
121
 
122
+ # img_cropped = img.crop(bbox)
123
+ # return img_cropped
124
 
125
+ # class InpaintBody(BaseModel):
126
+ # image: str
127
+ # mask: str
128
+ # prompt: str
129
 
130
+ # @app.post("/inpaint")
131
+ # async def do_inpaint(body: InpaintBody):
132
+ # """
133
+ # Perform inpainting on input data
134
+ # """
135
 
136
+ # logger.info('API inpaint called')
137
+ # image_data = body.image
138
+ # mask_data = body.mask
139
+ # prompt = body.prompt
140
 
141
+ # # Extract base64 from mask and convert to PIL.Image
142
+ # if (',' in image_data):
143
+ # image = Image.open(BytesIO(base64.b64decode(image_data.split(',')[1])))
144
+ # else:
145
+ # image = Image.open(BytesIO(base64.b64decode(image_data)))
146
+
147
+ # # Extract base64 from mask and convert to PIL.Image
148
+ # if (',' in mask_data):
149
+ # mask = Image.open(BytesIO(base64.b64decode(mask_data.split(',')[1])))
150
+ # else:
151
+ # mask = Image.open(BytesIO(base64.b64decode(mask_data)))
152
+
153
+ # # Resize image and mask to 512x512
154
+ # image = crop_image(resize_image(image, 512, 512))
155
+ # mask = crop_image(resize_image(image, 512, 512))
156
+
157
+ # pipe = app.package.get('pipe')
158
+ # result = pipe(prompt=prompt, image=image, mask_image=mask, num_inference_steps=10, num_images_per_prompt=1)
159
+ # images = result['images']
160
+ # return images
161
 
162
 
163
  class ImageBody(BaseModel):
 
188
  return {"ok": True, "status": "FINISHED", "result": img_str}
189
 
190
 
191
+ # @app.websocket("/ws-inpaint")
192
+ # async def inpaint_websocket_endpoint(websocket: WebSocket):
193
+ # connectionManager = app.package.get('connectionManager')
194
+ # await connectionManager.connect(websocket)
195
+ # await connectionManager.send_json({"ok": True, "status": "CONNECTED"}, websocket)
196
+ # while True:
197
+ # try:
198
+ # data: ImageBody = await connectionManager.receive_json(websocket)
199
+ # if (data is None):
200
+ # # Wait for data
201
+ # if not connectionManager.isConnected(websocket):
202
+ # break
203
+ # if connectionManager.shouldDisconnect(websocket):
204
+ # await websocket.close()
205
+ # connectionManager.disconnect(websocket)
206
+ # break
207
+ # continue
208
 
209
+ # image_data: str = data.get('image')
210
+ # mask_data: str = data.get('mask')
211
+ # prompt: str = data.get('prompt')
212
 
213
+ # await connectionManager.send_json({"ok": True, "status": "STARTED"}, websocket)
214
 
215
+ # # Extract base64 from mask and convert to PIL.Image
216
+ # if (',' in image_data):
217
+ # image = Image.open(BytesIO(base64.b64decode(image_data.split(',')[1])))
218
+ # else:
219
+ # image = Image.open(BytesIO(base64.b64decode(image_data)))
220
+
221
+ # # Extract base64 from mask and convert to PIL.Image
222
+ # if (',' in mask_data):
223
+ # mask = Image.open(BytesIO(base64.b64decode(mask_data.split(',')[1])))
224
+ # else:
225
+ # mask = Image.open(BytesIO(base64.b64decode(mask_data)))
226
+
227
+ # # Resize image and mask to 512x512
228
+ # image = crop_image(resize_image(image, 512, 512))
229
+ # mask = crop_image(resize_image(image, 512, 512))
230
+
231
+ # pipe = app.package.get('pipe')
232
+ # result = pipe(prompt=prompt, image=image, mask_image=mask, num_inference_steps=20, num_images_per_prompt=1)
233
+ # images = result['images']
234
+
235
+ # # Convert the result to base64 and send the json back
236
+ # result_array = []
237
+ # for image in images:
238
+ # buffered = BytesIO()
239
+ # image.save(buffered, format="JPEG")
240
+ # img_str = 'data:image/jpeg;base64,' + base64.b64encode(buffered.getvalue()).decode("utf-8")
241
+ # result_array.append(img_str)
242
+
243
+ # await connectionManager.send_json({"ok": True, "status": "FINISHED", "result": result_array}, websocket)
244
+
245
+ # await websocket.close()
246
+ # connectionManager.disconnect(websocket)
247
+ # except WebSocketDisconnect:
248
+ # connectionManager.disconnect(websocket)
249
+ # break
250
 
251
  @app.websocket("/ws")
252
  async def websocket_endpoint(websocket: WebSocket):
requirements.txt CHANGED
@@ -6,9 +6,11 @@ scikit-image==0.19.2
6
  torch==1.13.1
7
  torchvision==0.14.1
8
  tqdm==4.64.1
9
- diffusers==0.14.0
10
- transformers==4.27.1
11
- accelerate==0.17.1
 
 
12
 
13
  pycocotools==2.0.6
14
  fastapi==0.94.1
 
6
  torch==1.13.1
7
  torchvision==0.14.1
8
  tqdm==4.64.1
9
+
10
+ # Stable Diffusion Inpaint
11
+ # diffusers==0.14.0
12
+ # transformers==4.27.1
13
+ # accelerate==0.17.1
14
 
15
  pycocotools==2.0.6
16
  fastapi==0.94.1