camenduru commited on
Commit
5d2f629
Β·
verified Β·
1 Parent(s): 5f254cd

Update worker.py

Browse files
Files changed (1) hide show
  1. worker.py +46 -61
worker.py CHANGED
@@ -1,4 +1,4 @@
1
- import os
2
  import math
3
  import numpy as np
4
  import torch
@@ -8,6 +8,7 @@ from PIL import Image
8
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
9
  from diffusers import AutoencoderKL, UNet2DConditionModel, DDIMScheduler, EulerAncestralDiscreteScheduler, DPMSolverMultistepScheduler
10
  from diffusers.models.attention_processor import AttnProcessor2_0
 
11
  from transformers import CLIPTextModel, CLIPTokenizer
12
  from briarmbg import BriaRMBG
13
  from enum import Enum
@@ -333,9 +334,6 @@ def process(input_fg, prompt, image_width, image_height, num_samples, seed, step
333
 
334
  return pytorch2numpy(pixels)
335
 
336
- import json
337
- from diffusers.utils import load_image
338
-
339
  def closestNumber(n, m):
340
  q = int(n / m)
341
  n1 = m * q
@@ -347,67 +345,54 @@ def closestNumber(n, m):
347
  return n1
348
  return n2
349
 
350
- def is_parsable_json(command):
351
- try:
352
- json.loads(command)
353
- return True
354
- except json.JSONDecodeError:
355
- return False
356
-
357
- def save_and_upload_images(images, job_id):
358
- os.makedirs(f"/content/{job_id}", exist_ok=True)
359
- image_urls = []
360
- for index, image_array in enumerate(images):
361
- image = Image.fromarray(image_array)
362
- image_path = os.path.join(f"/content/{job_id}", f"{index}.png")
363
- image.save(image_path)
364
- image_url = rp_upload.upload_image(job_id, image_path)
365
- image_urls.append(image_url)
366
- rp_cleanup.clean([f"/content/{job_id}"])
367
- return image_urls
368
-
369
  @torch.inference_mode()
370
  def generate(input):
371
- print(input)
372
  command = input["input"]
373
  command = json.dumps(command)
374
- print(command)
375
- if is_parsable_json(command):
376
- values = json.loads(command)
377
- input_fg = values['input_fg']
378
- input_fg = load_image(input_fg)
379
- input_fg = np.asarray(input_fg)
380
- prompt = values['prompt']
381
- print("prompt")
382
- print(prompt)
383
- width =closestNumber(values['width'], 8)
384
- height = closestNumber(values['height'], 8)
385
- seed = values['seed']
386
- steps = values['steps']
387
- a_prompt = values['a_prompt']
388
- n_prompt = values['n_prompt']
389
- cfg = values['cfg']
390
- highres_scale = values['highres_scale']
391
- highres_denoise = values['highres_denoise']
392
- lowres_denoise = values['lowres_denoise']
393
- bg_source = values['bg_source']
394
- input_fg, matting = run_rmbg(input_fg)
395
- images = process(input_fg, prompt, width, height, 1, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, lowres_denoise, bg_source)
396
- image_urls = save_and_upload_images(images, input['id'])
397
- image = Image.fromarray(images[0])
398
- image.save('/content/image.jpg')
399
- print("image_urls")
400
- print(image_urls)
401
- return {"image_url": image_urls[0]} if len(image_urls) == 1 else {"images": image_urls}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
  else:
403
- input_fg = load_image("https://hips.hearstapps.com/hmg-prod/images/scarlett-johansson-attends-the-premiere-of-illuminations-news-photo-1639390369.jpg?crop=1.00xw:0.836xh;0,0&resize=640:*")
404
- input_fg = np.asarray(input_fg)
405
- width = closestNumber(512, 8)
406
- height = closestNumber(512, 8)
407
- images = process(input_fg, command, width, height, 1, 1, 25, 'best quality', 'lowres, bad anatomy, bad hands, cropped, worst quality', 2, 1.5, 0.5, 0.9, 'RIGHT')
408
- image_urls = save_and_upload_images(images, input['id'])
409
- image = Image.fromarray(images[0])
410
- image.save('/content/image.jpg')
411
- return {"image_url": image_urls[0]} if len(image_urls) == 1 else {"images": image_urls}
412
 
413
  runpod.serverless.start({"handler": generate})
 
1
+ import json, os, requests
2
  import math
3
  import numpy as np
4
  import torch
 
8
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
9
  from diffusers import AutoencoderKL, UNet2DConditionModel, DDIMScheduler, EulerAncestralDiscreteScheduler, DPMSolverMultistepScheduler
10
  from diffusers.models.attention_processor import AttnProcessor2_0
11
+ from diffusers.utils import load_image
12
  from transformers import CLIPTextModel, CLIPTokenizer
13
  from briarmbg import BriaRMBG
14
  from enum import Enum
 
334
 
335
  return pytorch2numpy(pixels)
336
 
 
 
 
337
  def closestNumber(n, m):
338
  q = int(n / m)
339
  n1 = m * q
 
345
  return n1
346
  return n2
347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
  @torch.inference_mode()
349
  def generate(input):
 
350
  command = input["input"]
351
  command = json.dumps(command)
352
+ values = json.loads(command)
353
+ input_fg = values['input_fg']
354
+ input_fg = load_image(input_fg)
355
+ input_fg = np.asarray(input_fg)
356
+ prompt = values['prompt']
357
+ width =closestNumber(values['width'], 8)
358
+ height = closestNumber(values['height'], 8)
359
+ seed = values['seed']
360
+ steps = values['steps']
361
+ a_prompt = values['a_prompt']
362
+ n_prompt = values['n_prompt']
363
+ cfg = values['cfg']
364
+ highres_scale = values['highres_scale']
365
+ highres_denoise = values['highres_denoise']
366
+ lowres_denoise = values['lowres_denoise']
367
+ bg_source = values['bg_source']
368
+ input_fg, matting = run_rmbg(input_fg)
369
+ images = process(input_fg, prompt, width, height, 1, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, lowres_denoise, bg_source)
370
+ image = Image.fromarray(images[0])
371
+ result = f"/content/{input['id']}.png"
372
+ image.save(result)
373
+
374
+ response = None
375
+ try:
376
+ source_id = values['source_id']
377
+ source_channel = values['source_channel']
378
+ files = {f"image.png": open(result, "rb").read()}
379
+ payload = {"content": f"{command} <@{source_id}>"}
380
+ response = requests.post(
381
+ f"https://discord.com/api/v9/channels/{source_channel}/messages",
382
+ data=payload,
383
+ headers={"authorization": f"Bot {discord_token}"},
384
+ files=files
385
+ )
386
+ response.raise_for_status()
387
+ except Exception as e:
388
+ print(f"An unexpected error occurred: {e}")
389
+ finally:
390
+ if os.path.exists(result):
391
+ os.remove(result)
392
+
393
+ if response and response.status_code == 200:
394
+ return {"result": response.json()['attachments'][0]['url']}
395
  else:
396
+ return {"result": "ERROR"}
 
 
 
 
 
 
 
 
397
 
398
  runpod.serverless.start({"handler": generate})