camenduru commited on
Commit
cfcd7cb
1 Parent(s): fc33311

Update worker_runpod.py

Browse files
Files changed (1) hide show
  1. worker_runpod.py +41 -25
worker_runpod.py CHANGED
@@ -1,9 +1,5 @@
1
  import os, json, requests, runpod
2
 
3
- discord_token = os.getenv('com_camenduru_discord_token')
4
- web_uri = os.getenv('com_camenduru_web_uri')
5
- web_token = os.getenv('com_camenduru_web_token')
6
-
7
  import random, time
8
  import torch
9
  import numpy as np
@@ -112,39 +108,59 @@ def generate(input):
112
  Image.fromarray(np.array(decoded*255, dtype=np.uint8)[0]).save("/content/onevision_flux.png")
113
 
114
  result = "/content/onevision_flux.png"
115
- response = None
116
  try:
117
- source_id = values['source_id']
118
- del values['source_id']
119
- source_channel = values['source_channel']
120
- del values['source_channel']
 
 
 
 
 
 
 
 
 
 
 
 
121
  job_id = values['job_id']
122
  del values['job_id']
123
  default_filename = os.path.basename(result)
124
- files = {default_filename: open(result, "rb").read()}
125
- payload = {"content": f"{json.dumps(values)} <@{source_id}>"}
 
126
  response = requests.post(
127
- f"https://discord.com/api/v9/channels/{source_channel}/messages",
128
  data=payload,
129
- headers={"authorization": f"Bot {discord_token}"},
130
  files=files
131
  )
132
  response.raise_for_status()
 
 
 
 
 
 
 
 
 
 
133
  except Exception as e:
134
- print(f"An unexpected error occurred: {e}")
 
 
 
 
 
 
 
 
 
135
  finally:
136
  if os.path.exists(result):
137
  os.remove(result)
138
 
139
- if response and response.status_code == 200:
140
- try:
141
- payload = {"jobId": job_id, "result": response.json()['attachments'][0]['url']}
142
- requests.post(f"{web_uri}/api/notify", data=json.dumps(payload), headers={'Content-Type': 'application/json', "authorization": f"{web_token}"})
143
- except Exception as e:
144
- print(f"An unexpected error occurred: {e}")
145
- finally:
146
- return {"result": response.json()['attachments'][0]['url']}
147
- else:
148
- return {"result": "ERROR"}
149
-
150
  runpod.serverless.start({"handler": generate})
 
1
  import os, json, requests, runpod
2
 
 
 
 
 
3
  import random, time
4
  import torch
5
  import numpy as np
 
108
  Image.fromarray(np.array(decoded*255, dtype=np.uint8)[0]).save("/content/onevision_flux.png")
109
 
110
  result = "/content/onevision_flux.png"
 
111
  try:
112
+ notify_uri = values['notify_uri']
113
+ del values['notify_uri']
114
+ notify_token = values['notify_token']
115
+ del values['notify_token']
116
+ discord_id = values['discord_id']
117
+ del values['discord_id']
118
+ if(discord_id == "discord_id"):
119
+ discord_id = os.getenv('com_camenduru_discord_id')
120
+ discord_channel = values['discord_channel']
121
+ del values['discord_channel']
122
+ if(discord_channel == "discord_channel"):
123
+ discord_channel = os.getenv('com_camenduru_discord_channel')
124
+ discord_token = values['discord_token']
125
+ del values['discord_token']
126
+ if(discord_token == "discord_token"):
127
+ discord_token = os.getenv('com_camenduru_discord_token')
128
  job_id = values['job_id']
129
  del values['job_id']
130
  default_filename = os.path.basename(result)
131
+ with open(result, "rb") as file:
132
+ files = {default_filename: file.read()}
133
+ payload = {"content": f"{json.dumps(values)} <@{discord_id}>"}
134
  response = requests.post(
135
+ f"https://discord.com/api/v9/channels/{discord_channel}/messages",
136
  data=payload,
137
+ headers={"Authorization": f"Bot {discord_token}"},
138
  files=files
139
  )
140
  response.raise_for_status()
141
+ result_url = response.json()['attachments'][0]['url']
142
+ notify_payload = {"jobId": job_id, "result": result_url, "status": "DONE"}
143
+ web_notify_uri = os.getenv('com_camenduru_web_notify_uri')
144
+ web_notify_token = os.getenv('com_camenduru_web_notify_token')
145
+ if(notify_uri == "notify_uri"):
146
+ requests.post(web_notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
147
+ else:
148
+ requests.post(web_notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
149
+ requests.post(notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": notify_token})
150
+ return {"jobId": job_id, "result": result_url, "status": "DONE"}
151
  except Exception as e:
152
+ error_payload = {"jobId": job_id, "status": "FAILED"}
153
+ try:
154
+ if(notify_uri == "notify_uri"):
155
+ requests.post(web_notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
156
+ else:
157
+ requests.post(web_notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
158
+ requests.post(notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": notify_token})
159
+ except:
160
+ pass
161
+ return {"jobId": job_id, "result": f"FAILED: {str(e)}", "status": "FAILED"}
162
  finally:
163
  if os.path.exists(result):
164
  os.remove(result)
165
 
 
 
 
 
 
 
 
 
 
 
 
166
  runpod.serverless.start({"handler": generate})