Datasets:

Languages:
English
ArXiv:
License:
mattdeitke commited on
Commit
3e167e7
1 Parent(s): 11fa1d5

use fsspec to download objects

Browse files
Files changed (2) hide show
  1. objaverse_xl/__init__.py +41 -24
  2. requirements.txt +2 -1
objaverse_xl/__init__.py CHANGED
@@ -5,6 +5,7 @@ from functools import partial
5
  from multiprocessing import Pool
6
  from typing import Dict, List, Optional
7
 
 
8
  import pandas as pd
9
  import requests
10
  from loguru import logger
@@ -31,7 +32,8 @@ def load_smithsonian_metadata(
31
 
32
  Args:
33
  download_dir (str, optional): Directory to download the parquet metadata file.
34
- Defaults to "~/.objaverse-xl".
 
35
 
36
  Returns:
37
  pd.DataFrame: Smithsonian Object Metadata dataset as a Pandas DataFrame with
@@ -39,16 +41,22 @@ def load_smithsonian_metadata(
39
  "license". The quality is always Medium and the file_type is always glb.
40
  """
41
  dirname = os.path.expanduser(os.path.join(download_dir, "smithsonian"))
42
- os.makedirs(dirname, exist_ok=True)
43
  filename = os.path.join(dirname, "object-metadata.parquet")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- url = "https://huggingface.co/datasets/allenai/objaverse-xl/resolve/main/smithsonian/object-metadata.parquet"
46
- response = requests.get(url)
47
- response.raise_for_status()
48
- with open(filename, "wb") as file:
49
- file.write(response.content)
50
-
51
- df = pd.read_parquet(filename)
52
  df["uid"] = df["url"].apply(get_uid_from_str)
53
  df["license"] = "CC0"
54
  return df
@@ -60,7 +68,8 @@ def download_smithsonian_object(url: str, download_dir: str = "~/.objaverse-xl")
60
  Args:
61
  url (str): URL to download the Smithsonian Object from.
62
  download_dir (str, optional): Directory to download the Smithsonian Object to.
63
- Defaults to "~/.objaverse-xl".
 
64
 
65
  Returns:
66
  str: Path to the downloaded Smithsonian Object.
@@ -68,21 +77,27 @@ def download_smithsonian_object(url: str, download_dir: str = "~/.objaverse-xl")
68
  uid = get_uid_from_str(url)
69
 
70
  dirname = os.path.expanduser(os.path.join(download_dir, "smithsonian", "objects"))
71
- os.makedirs(dirname, exist_ok=True)
72
  filename = os.path.join(dirname, f"{uid}.glb")
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- if os.path.exists(filename):
75
- return filename
 
 
76
 
77
- tmp_path = os.path.join(dirname, f"{uid}.glb.tmp")
78
- response = requests.get(url)
79
- if response.status_code == 404:
80
- logger.warning(f"404 for {url}")
81
- return None
82
- with open(tmp_path, "wb") as file:
83
- for chunk in response.iter_content(chunk_size=8192):
84
- file.write(chunk)
85
- os.rename(tmp_path, filename)
86
 
87
  return filename
88
 
@@ -99,9 +114,11 @@ def download_smithsonian_objects(
99
  from. If None, all Smithsonian Objects will be downloaded. Defaults to None.
100
  processes (Optional[int], optional): Number of processes to use for downloading
101
  the Smithsonian Objects. If None, the number of processes will be set to the
102
- number of CPUs on the machine (multiprocessing.cpu_count()). Defaults to None.
 
103
  download_dir (str, optional): Directory to download the Smithsonian Objects to.
104
- Defaults to "~/.objaverse-xl".
 
105
 
106
  Returns:
107
  List[Dict[str, str]]: List of dictionaries with keys "download_path" and "url"
 
5
  from multiprocessing import Pool
6
  from typing import Dict, List, Optional
7
 
8
+ import fsspec
9
  import pandas as pd
10
  import requests
11
  from loguru import logger
 
32
 
33
  Args:
34
  download_dir (str, optional): Directory to download the parquet metadata file.
35
+ Supports all file systems supported by fsspec. Defaults to
36
+ "~/.objaverse-xl".
37
 
38
  Returns:
39
  pd.DataFrame: Smithsonian Object Metadata dataset as a Pandas DataFrame with
 
41
  "license". The quality is always Medium and the file_type is always glb.
42
  """
43
  dirname = os.path.expanduser(os.path.join(download_dir, "smithsonian"))
 
44
  filename = os.path.join(dirname, "object-metadata.parquet")
45
+ fs, path = fsspec.core.url_to_fs(filename)
46
+ if fs.protocol == "file":
47
+ os.makedirs(dirname, exist_ok=True)
48
+
49
+ if fs.exists(filename):
50
+ df = pd.read_parquet(filename)
51
+ return df
52
+ else:
53
+ url = "https://huggingface.co/datasets/allenai/objaverse-xl/resolve/main/smithsonian/object-metadata.parquet"
54
+ response = requests.get(url)
55
+ response.raise_for_status()
56
+ with fs.open(filename, "wb") as file:
57
+ file.write(response.content)
58
+ df = pd.read_parquet(filename)
59
 
 
 
 
 
 
 
 
60
  df["uid"] = df["url"].apply(get_uid_from_str)
61
  df["license"] = "CC0"
62
  return df
 
68
  Args:
69
  url (str): URL to download the Smithsonian Object from.
70
  download_dir (str, optional): Directory to download the Smithsonian Object to.
71
+ Supports all file systems supported by fsspec. Defaults to
72
+ "~/.objaverse-xl".
73
 
74
  Returns:
75
  str: Path to the downloaded Smithsonian Object.
 
77
  uid = get_uid_from_str(url)
78
 
79
  dirname = os.path.expanduser(os.path.join(download_dir, "smithsonian", "objects"))
 
80
  filename = os.path.join(dirname, f"{uid}.glb")
81
+ fs, path = fsspec.core.url_to_fs(filename)
82
+ if fs.protocol == "file":
83
+ os.makedirs(dirname, exist_ok=True)
84
+
85
+ if not fs.exists(filename):
86
+ tmp_path = os.path.join(dirname, f"{uid}.glb.tmp")
87
+ response = requests.get(url)
88
+
89
+ # check if the path is valid
90
+ if response.status_code == 404:
91
+ logger.warning(f"404 for {url}")
92
+ return None
93
 
94
+ # write to tmp path
95
+ with fs.open(tmp_path, "wb") as file:
96
+ for chunk in response.iter_content(chunk_size=8192):
97
+ file.write(chunk)
98
 
99
+ # rename to final path
100
+ fs.rename(tmp_path, filename)
 
 
 
 
 
 
 
101
 
102
  return filename
103
 
 
114
  from. If None, all Smithsonian Objects will be downloaded. Defaults to None.
115
  processes (Optional[int], optional): Number of processes to use for downloading
116
  the Smithsonian Objects. If None, the number of processes will be set to the
117
+ number of CPUs on the machine (multiprocessing.cpu_count()). Defaults to
118
+ None.
119
  download_dir (str, optional): Directory to download the Smithsonian Objects to.
120
+ Supports all file systems supported by fsspec. Defaults to
121
+ "~/.objaverse-xl".
122
 
123
  Returns:
124
  List[Dict[str, str]]: List of dictionaries with keys "download_path" and "url"
requirements.txt CHANGED
@@ -2,4 +2,5 @@ requests
2
  pandas
3
  pyarrow
4
  tqdm
5
- loguru
 
 
2
  pandas
3
  pyarrow
4
  tqdm
5
+ loguru
6
+ fsspec==2022.11.0