camenduru commited on
Commit
fac4ee8
·
verified ·
1 Parent(s): 8645dc4

Update worker_runpod.py

Browse files
Files changed (1) hide show
  1. worker_runpod.py +56 -36
worker_runpod.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import torch
2
  import numpy as np
3
  import rembg
@@ -20,12 +22,6 @@ from src.utils.train_util import instantiate_from_config
20
  from src.utils.camera_util import (FOV_to_intrinsics, get_zero123plus_input_cameras,get_circular_camera_poses,)
21
  from src.utils.mesh_util import save_obj, save_obj_with_mtl
22
 
23
- import os, json, requests, runpod
24
-
25
- discord_token = os.getenv('com_camenduru_discord_token')
26
- web_uri = os.getenv('com_camenduru_web_uri')
27
- web_token = os.getenv('com_camenduru_web_token')
28
-
29
  def preprocess(input_image, do_remove_background):
30
  rembg_session = rembg.new_session() if do_remove_background else None
31
  if do_remove_background:
@@ -178,47 +174,71 @@ def generate(input):
178
 
179
  output_video, output_model_obj = make3d(mv_images, model, device, IS_FLEXICUBES, infer_config, export_video, export_texmap)
180
  mesh_basename = os.path.splitext(output_model_obj)[0]
 
181
  result = [output_video, [output_model_obj, mesh_basename+'.mtl', mesh_basename+'.png']]
182
-
183
- response = None
184
  try:
185
- source_id = values['source_id']
186
- del values['source_id']
187
- source_channel = values['source_channel']
188
- del values['source_channel']
 
 
 
 
 
 
 
 
 
 
 
 
189
  job_id = values['job_id']
190
  del values['job_id']
191
-
192
- file_path = result[0]
193
- file_paths = result[1]
194
- default_filename = os.path.basename(file_path)
195
- files = { default_filename: open(file_path, "rb").read() }
196
- for path in file_paths:
197
  filename = os.path.basename(path)
198
  with open(path, "rb") as file:
199
  files[filename] = file.read()
200
-
201
- payload = {"content": f"{json.dumps(values)} <@{source_id}>"}
202
  response = requests.post(
203
- f"https://discord.com/api/v9/channels/{source_channel}/messages",
204
  data=payload,
205
- headers={"authorization": f"Bot {discord_token}"},
206
  files=files
207
  )
208
  response.raise_for_status()
 
 
 
 
 
 
 
 
 
 
209
  except Exception as e:
210
- print(f"An unexpected error occurred: {e}")
211
-
212
- if response and response.status_code == 200:
213
  try:
214
- urls = [attachment['url'] for attachment in response.json()['attachments']]
215
- payload = {"jobId": str(job_id), "result": str(urls)}
216
- requests.post(f"{web_uri}/api/notify", data=json.dumps(payload), headers={'Content-Type': 'application/json', "authorization": f"{web_token}"})
217
- except Exception as e:
218
- print(f"An unexpected error occurred: {e}")
219
- finally:
220
- return {"result": response.json()['attachments'][0]['url']}
221
- else:
222
- return {"result": "ERROR"}
223
-
224
- runpod.serverless.start({"handler": generate})
 
 
 
 
 
 
 
 
 
1
+ import os, json, requests, runpod
2
+
3
  import torch
4
  import numpy as np
5
  import rembg
 
22
  from src.utils.camera_util import (FOV_to_intrinsics, get_zero123plus_input_cameras,get_circular_camera_poses,)
23
  from src.utils.mesh_util import save_obj, save_obj_with_mtl
24
 
 
 
 
 
 
 
25
  def preprocess(input_image, do_remove_background):
26
  rembg_session = rembg.new_session() if do_remove_background else None
27
  if do_remove_background:
 
174
 
175
  output_video, output_model_obj = make3d(mv_images, model, device, IS_FLEXICUBES, infer_config, export_video, export_texmap)
176
  mesh_basename = os.path.splitext(output_model_obj)[0]
177
+
178
  result = [output_video, [output_model_obj, mesh_basename+'.mtl', mesh_basename+'.png']]
 
 
179
  try:
180
+ notify_uri = values['notify_uri']
181
+ del values['notify_uri']
182
+ notify_token = values['notify_token']
183
+ del values['notify_token']
184
+ discord_id = values['discord_id']
185
+ del values['discord_id']
186
+ if(discord_id == "discord_id"):
187
+ discord_id = os.getenv('com_camenduru_discord_id')
188
+ discord_channel = values['discord_channel']
189
+ del values['discord_channel']
190
+ if(discord_channel == "discord_channel"):
191
+ discord_channel = os.getenv('com_camenduru_discord_channel')
192
+ discord_token = values['discord_token']
193
+ del values['discord_token']
194
+ if(discord_token == "discord_token"):
195
+ discord_token = os.getenv('com_camenduru_discord_token')
196
  job_id = values['job_id']
197
  del values['job_id']
198
+ default_filename = os.path.basename(result[0])
199
+ with open(result[0], "rb") as file:
200
+ files = {default_filename: file.read()}
201
+ for path in result[1]:
 
 
202
  filename = os.path.basename(path)
203
  with open(path, "rb") as file:
204
  files[filename] = file.read()
205
+ payload = {"content": f"{json.dumps(values)} <@{discord_id}>"}
 
206
  response = requests.post(
207
+ f"https://discord.com/api/v9/channels/{discord_channel}/messages",
208
  data=payload,
209
+ headers={"Authorization": f"Bot {discord_token}"},
210
  files=files
211
  )
212
  response.raise_for_status()
213
+ result_urls = [attachment['url'] for attachment in response.json()['attachments']]
214
+ notify_payload = {"jobId": job_id, "result": str(result_urls), "status": "DONE"}
215
+ web_notify_uri = os.getenv('com_camenduru_web_notify_uri')
216
+ web_notify_token = os.getenv('com_camenduru_web_notify_token')
217
+ if(notify_uri == "notify_uri"):
218
+ requests.post(web_notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
219
+ else:
220
+ requests.post(web_notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
221
+ requests.post(notify_uri, data=json.dumps(notify_payload), headers={'Content-Type': 'application/json', "Authorization": notify_token})
222
+ return {"jobId": job_id, "result": str(result_urls), "status": "DONE"}
223
  except Exception as e:
224
+ error_payload = {"jobId": job_id, "status": "FAILED"}
 
 
225
  try:
226
+ if(notify_uri == "notify_uri"):
227
+ requests.post(web_notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
228
+ else:
229
+ requests.post(web_notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": web_notify_token})
230
+ requests.post(notify_uri, data=json.dumps(error_payload), headers={'Content-Type': 'application/json', "Authorization": notify_token})
231
+ except:
232
+ pass
233
+ return {"jobId": job_id, "result": f"FAILED: {str(e)}", "status": "FAILED"}
234
+ finally:
235
+ if os.path.exists(result[0]):
236
+ os.remove(result[0])
237
+ if os.path.exists(result[1][0]):
238
+ os.remove(result[1][0])
239
+ if os.path.exists(result[1][1]):
240
+ os.remove(result[1][1])
241
+ if os.path.exists(result[1][2]):
242
+ os.remove(result[1][2])
243
+
244
+ runpod.serverless.start({"handler": generate})