3DEval / Data /Rodin /1.py
slongle's picture
Upload folder using huggingface_hub
e6c583e verified
Raw History Blame Contribute Delete
3.5 kB
import os
import time
import json
import requests
from tqdm import tqdm
from glob import glob
# Constants
BASE_URL = "https://hyperhuman.deemos.com/api/v2"
API_KEY = "1Luy3x8gNiCpbueAWehgpCd8nI2YZcmV1oePuQsVUM75Cts24KtKPciUMCkBmsa4"
def download_results(task_uuid):
url = f"{BASE_URL}/download"
data = {"task_uuid": task_uuid}
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
response = requests.post(url, headers=headers, json=data)
return response.json()
def check_status(subscription_key):
url = f"{BASE_URL}/status"
data = {"subscription_key": subscription_key}
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
response = requests.post(url, headers=headers, json=data)
return response.json()
def submit_task(image_path=None, text_prompt=None, seed=None, highpack=None):
assert (
image_path is not None or text_prompt is not None
), "Either image_path or text_prompt must be provided."
url = f"{BASE_URL}/rodin"
files = dict()
if image_path is not None:
with open(image_path, "rb") as image_file:
image_data = image_file.read()
files = {"images": (os.path.basename(image_path), image_data, "image/jpeg")}
data = {
key: value
for key, value in {"prompt": text_prompt, "seed": seed}.items()
if value is not None
}
if highpack:
data["addons"] = "HighPack"
data["tier"] = "Regular"
headers = {
"Authorization": f"Bearer {API_KEY}",
}
# Note that we are not sending the data as JSON, but as form data.
# This is because we are sending a file as well.
response = requests.post(url, files=files, data=data, headers=headers)
return response.json()
def generate(result_path, image_path=None, text_prompt=None, seed=None, highpack=None):
task_response = submit_task(
image_path=image_path, text_prompt=text_prompt, seed=seed, highpack=highpack
)
task_uuid = task_response["uuid"]
subscription_key = task_response["jobs"]["subscription_key"]
status = []
while len(status) == 0 or not all(s["status"] in ["Done", "Failed"] for s in status):
time.sleep(5)
status_response = check_status(subscription_key)
status = status_response["jobs"]
for s in status:
print(f"job {s['uuid']}: {s['status']}")
download_response = download_results(task_uuid)
download_items = download_response["list"]
for item in download_items:
print(f"File Name: {item['name']}, URL: {item['url']}")
dest_fname = os.path.join(result_path, item["name"])
os.makedirs(os.path.dirname(dest_fname), exist_ok=True)
with open(dest_fname, "wb") as f:
response = requests.get(item["url"])
f.write(response.content)
print(f"Downloaded {dest_fname}")
def main():
result_root = "results-lowpack"
prompt_filename = "/ibex/user/dengj/3DEval/Data/text/prompts.json"
seed = 42
highpack = False
prompts = json.load(open(prompt_filename))
for i, prompt in enumerate(tqdm(prompts)):
result_path = os.path.join(result_root, f"{i}")
glbs = glob(os.path.join(result_path, "*.glb"))
if len(glbs) > 0:
print(f"Skipping {result_path}")
continue
generate(result_path, text_prompt=prompt, seed=42, highpack=highpack)
if __name__ == "__main__":
main()