Files changed (1) hide show
  1. worker_runpod.py +25 -41
worker_runpod.py CHANGED
@@ -7,6 +7,10 @@ from PIL import Image
7
  from comfy.sd import load_checkpoint_guess_config
8
  import nodes
9
 
 
 
 
 
10
  with torch.inference_mode():
11
  model_patcher, clip, vae, clipvision = load_checkpoint_guess_config("/content/ComfyUI/models/checkpoints/model.safetensors", output_vae=True, output_clip=True, embedding_directory=None)
12
 
@@ -48,59 +52,39 @@ def generate(input):
48
  Image.fromarray(np.array(decoded*255, dtype=np.uint8)[0]).save("/content/output_image.png")
49
 
50
  result = "/content/output_image.png"
 
51
  try:
52
- notify_uri = values['notify_uri']
53
- del values['notify_uri']
54
- notify_token = values['notify_token']
55
- del values['notify_token']
56
- discord_id = values['discord_id']
57
- del values['discord_id']
58
- if(discord_id == "discord_id"):
59
- discord_id = os.getenv('com_camenduru_discord_id')
60
- discord_channel = values['discord_channel']
61
- del values['discord_channel']
62
- if(discord_channel == "discord_channel"):
63
- discord_channel = os.getenv('com_camenduru_discord_channel')
64
- discord_token = values['discord_token']
65
- del values['discord_token']
66
- if(discord_token == "discord_token"):
67
- discord_token = os.getenv('com_camenduru_discord_token')
68
  job_id = values['job_id']
69
  del values['job_id']
70
  default_filename = os.path.basename(result)
71
- with open(result, "rb") as file:
72
- files = {default_filename: file.read()}
73
- payload = {"content": f"{json.dumps(values)} <@{discord_id}>"}
74
  response = requests.post(
75
- f"https://discord.com/api/v9/channels/{discord_channel}/messages",
76
  data=payload,
77
- headers={"Authorization": f"Bot {discord_token}"},
78
  files=files
79
  )
80
  response.raise_for_status()
81
- result_url = response.json()['attachments'][0]['url']
82
- notify_payload = {"jobId": job_id, "result": result_url, "status": "DONE"}
83
- web_notify_uri = os.getenv('com_camenduru_web_notify_uri')
84
- web_notify_token = os.getenv('com_camenduru_web_notify_token')
85
- if(notify_uri == "notify_uri"):
86
- requests.post(web_notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
87
- else:
88
- requests.post(web_notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
89
- requests.post(notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": notify_token})
90
- return {"jobId": job_id, "result": result_url, "status": "DONE"}
91
  except Exception as e:
92
- error_payload = {"jobId": job_id, "status": "FAILED"}
93
- try:
94
- if(notify_uri == "notify_uri"):
95
- requests.post(web_notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
96
- else:
97
- requests.post(web_notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
98
- requests.post(notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": notify_token})
99
- except:
100
- pass
101
- return {"jobId": job_id, "result": f"FAILED: {str(e)}", "status": "FAILED"}
102
  finally:
103
  if os.path.exists(result):
104
  os.remove(result)
105
 
 
 
 
 
 
 
 
 
 
 
 
106
  runpod.serverless.start({"handler": generate})
 
7
  from comfy.sd import load_checkpoint_guess_config
8
  import nodes
9
 
10
+ discord_token = os.getenv('com_camenduru_discord_token')
11
+ web_uri = os.getenv('com_camenduru_web_uri')
12
+ web_token = os.getenv('com_camenduru_web_token')
13
+
14
  with torch.inference_mode():
15
  model_patcher, clip, vae, clipvision = load_checkpoint_guess_config("/content/ComfyUI/models/checkpoints/model.safetensors", output_vae=True, output_clip=True, embedding_directory=None)
16
 
 
52
  Image.fromarray(np.array(decoded*255, dtype=np.uint8)[0]).save("/content/output_image.png")
53
 
54
  result = "/content/output_image.png"
55
+ response = None
56
  try:
57
+ source_id = values['source_id']
58
+ del values['source_id']
59
+ source_channel = values['source_channel']
60
+ del values['source_channel']
 
 
 
 
 
 
 
 
 
 
 
 
61
  job_id = values['job_id']
62
  del values['job_id']
63
  default_filename = os.path.basename(result)
64
+ files = {default_filename: open(result, "rb").read()}
65
+ payload = {"content": f"{json.dumps(values)} <@{source_id}>"}
 
66
  response = requests.post(
67
+ f"https://discord.com/api/v9/channels/{source_channel}/messages",
68
  data=payload,
69
+ headers={"authorization": f"Bot {discord_token}"},
70
  files=files
71
  )
72
  response.raise_for_status()
 
 
 
 
 
 
 
 
 
 
73
  except Exception as e:
74
+ print(f"An unexpected error occurred: {e}")
 
 
 
 
 
 
 
 
 
75
  finally:
76
  if os.path.exists(result):
77
  os.remove(result)
78
 
79
+ if response and response.status_code == 200:
80
+ try:
81
+ payload = {"jobId": job_id, "result": response.json()['attachments'][0]['url']}
82
+ requests.post(f"{web_uri}/api/notify", data=json.dumps(payload), headers={'Content-Type': 'application/json', "authorization": f"{web_token}"})
83
+ except Exception as e:
84
+ print(f"An unexpected error occurred: {e}")
85
+ finally:
86
+ return {"result": response.json()['attachments'][0]['url']}
87
+ else:
88
+ return {"result": "ERROR"}
89
+
90
  runpod.serverless.start({"handler": generate})