camenduru commited on
Commit
7e192c7
1 Parent(s): 7de3203

Update worker_runpod.py

Browse files
Files changed (1) hide show
  1. worker_runpod.py +41 -25
worker_runpod.py CHANGED
@@ -17,10 +17,6 @@ from transport import Sampler, create_transport
17
  from diffusers.models import AutoencoderKL
18
  from transformers import AutoModel, AutoTokenizer
19
 
20
- discord_token = os.getenv('com_camenduru_discord_token')
21
- web_uri = os.getenv('com_camenduru_web_uri')
22
- web_token = os.getenv('com_camenduru_web_token')
23
-
24
  with torch.inference_mode():
25
  path_type = "Linear" # ["Linear", "GVP", "VP"]
26
  prediction = "velocity" # ["velocity", "score", "noise"]
@@ -197,39 +193,59 @@ def generate(input):
197
  print(traceback.format_exc())
198
 
199
  result = "/content/out.png"
200
- response = None
201
  try:
202
- source_id = values['source_id']
203
- del values['source_id']
204
- source_channel = values['source_channel']
205
- del values['source_channel']
 
 
 
 
 
 
 
 
 
 
 
 
206
  job_id = values['job_id']
207
  del values['job_id']
208
  default_filename = os.path.basename(result)
209
- files = {default_filename: open(result, "rb").read()}
210
- payload = {"content": f"{json.dumps(values)} <@{source_id}>"}
 
211
  response = requests.post(
212
- f"https://discord.com/api/v9/channels/{source_channel}/messages",
213
  data=payload,
214
- headers={"authorization": f"Bot {discord_token}"},
215
  files=files
216
  )
217
  response.raise_for_status()
 
 
 
 
 
 
 
 
 
 
218
  except Exception as e:
219
- print(f"An unexpected error occurred: {e}")
 
 
 
 
 
 
 
 
 
220
  finally:
221
  if os.path.exists(result):
222
  os.remove(result)
223
 
224
- if response and response.status_code == 200:
225
- try:
226
- payload = {"jobId": job_id, "result": response.json()['attachments'][0]['url']}
227
- requests.post(f"{web_uri}/api/notify", data=json.dumps(payload), headers={'Content-Type': 'application/json', "authorization": f"{web_token}"})
228
- except Exception as e:
229
- print(f"An unexpected error occurred: {e}")
230
- finally:
231
- return {"result": response.json()['attachments'][0]['url']}
232
- else:
233
- return {"result": "ERROR"}
234
-
235
  runpod.serverless.start({"handler": generate})
 
17
  from diffusers.models import AutoencoderKL
18
  from transformers import AutoModel, AutoTokenizer
19
 
 
 
 
 
20
  with torch.inference_mode():
21
  path_type = "Linear" # ["Linear", "GVP", "VP"]
22
  prediction = "velocity" # ["velocity", "score", "noise"]
 
193
  print(traceback.format_exc())
194
 
195
  result = "/content/out.png"
 
196
  try:
197
+ notify_uri = values['notify_uri']
198
+ del values['notify_uri']
199
+ notify_token = values['notify_token']
200
+ del values['notify_token']
201
+ discord_id = values['discord_id']
202
+ del values['discord_id']
203
+ if(discord_id == "discord_id"):
204
+ discord_id = os.getenv('com_camenduru_discord_id')
205
+ discord_channel = values['discord_channel']
206
+ del values['discord_channel']
207
+ if(discord_channel == "discord_channel"):
208
+ discord_channel = os.getenv('com_camenduru_discord_channel')
209
+ discord_token = values['discord_token']
210
+ del values['discord_token']
211
+ if(discord_token == "discord_token"):
212
+ discord_token = os.getenv('com_camenduru_discord_token')
213
  job_id = values['job_id']
214
  del values['job_id']
215
  default_filename = os.path.basename(result)
216
+ with open(result, "rb") as file:
217
+ files = {default_filename: file.read()}
218
+ payload = {"content": f"{json.dumps(values)} <@{discord_id}>"}
219
  response = requests.post(
220
+ f"https://discord.com/api/v9/channels/{discord_channel}/messages",
221
  data=payload,
222
+ headers={"Authorization": f"Bot {discord_token}"},
223
  files=files
224
  )
225
  response.raise_for_status()
226
+ result_url = response.json()['attachments'][0]['url']
227
+ notify_payload = {"jobId": job_id, "result": result_url, "status": "DONE"}
228
+ web_notify_uri = os.getenv('com_camenduru_web_notify_uri')
229
+ web_notify_token = os.getenv('com_camenduru_web_notify_token')
230
+ if(notify_uri == "notify_uri"):
231
+ requests.post(web_notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
232
+ else:
233
+ requests.post(web_notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
234
+ requests.post(notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": notify_token})
235
+ return {"jobId": job_id, "result": result_url, "status": "DONE"}
236
  except Exception as e:
237
+ error_payload = {"jobId": job_id, "status": "FAILED"}
238
+ try:
239
+ if(notify_uri == "notify_uri"):
240
+ requests.post(web_notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
241
+ else:
242
+ requests.post(web_notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
243
+ requests.post(notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": notify_token})
244
+ except:
245
+ pass
246
+ return {"jobId": job_id, "result": f"FAILED: {str(e)}", "status": "FAILED"}
247
  finally:
248
  if os.path.exists(result):
249
  os.remove(result)
250
 
 
 
 
 
 
 
 
 
 
 
 
251
  runpod.serverless.start({"handler": generate})