Commit
·
1c67eec
1
Parent(s):
f0cb941
support downloader of all sources
Browse files
objaverse_xl/objaverse_xl_downloader.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from objaverse_xl.abstract import ObjaverseSource
|
3 |
+
from objaverse_xl.github import GitHubDownloader
|
4 |
+
from objaverse_xl.thingiverse import ThingiverseDownloader
|
5 |
+
from objaverse_xl.smithsonian import SmithsonianDownloader
|
6 |
+
from objaverse_xl.objaverse_v1 import SketchfabDownloader
|
7 |
+
from typing import Optional, Callable, Dict
|
8 |
+
|
9 |
+
|
10 |
+
class ObjaverseXLDownloader(ObjaverseSource):
|
11 |
+
def __init__(self):
|
12 |
+
super().__init__()
|
13 |
+
|
14 |
+
self.downloaders = {
|
15 |
+
"github": GitHubDownloader(),
|
16 |
+
"thingiverse": ThingiverseDownloader(),
|
17 |
+
"smithsonian": SmithsonianDownloader(),
|
18 |
+
"sketchfab": SketchfabDownloader(),
|
19 |
+
}
|
20 |
+
|
21 |
+
def get_annotations(self, download_dir: str = "~/.objaverse") -> pd.DataFrame:
|
22 |
+
"""Loads the 3D object metadata as a Pandas DataFrame.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
download_dir (str, optional): Directory to download the parquet metadata
|
26 |
+
file. Supports all file systems supported by fsspec. Defaults to
|
27 |
+
"~/.objaverse".
|
28 |
+
|
29 |
+
Returns:
|
30 |
+
pd.DataFrame: Metadata of the 3D objects as a Pandas DataFrame with columns
|
31 |
+
for the object "fileIdentifier", "license", "source", "fileType",
|
32 |
+
"sha256", and "metadata".
|
33 |
+
"""
|
34 |
+
annotations = [
|
35 |
+
downloader.get_annotations(download_dir)
|
36 |
+
for downloader in self.downloaders.values()
|
37 |
+
]
|
38 |
+
return pd.concat(annotations, ignore_index=True)
|
39 |
+
|
40 |
+
def download_objects(
|
41 |
+
self,
|
42 |
+
objects: pd.DataFrame,
|
43 |
+
download_dir: str = "~/.objaverse",
|
44 |
+
processes: Optional[int] = None,
|
45 |
+
handle_found_object: Optional[Callable] = None,
|
46 |
+
handle_modified_object: Optional[Callable] = None,
|
47 |
+
handle_missing_object: Optional[Callable] = None,
|
48 |
+
**kwargs,
|
49 |
+
) -> Dict[str, str]:
|
50 |
+
"""Downloads all objects from the source.
|
51 |
+
|
52 |
+
Args:
|
53 |
+
objects (pd.DataFrame): Objects to download. Must have columns for
|
54 |
+
the object "fileIdentifier" and "sha256". Use the `get_annotations`
|
55 |
+
function to get the metadata.
|
56 |
+
processes (Optional[int], optional): Number of processes to use for
|
57 |
+
downloading. If None, will use the number of CPUs on the machine.
|
58 |
+
Defaults to None.
|
59 |
+
download_dir (str, optional): Directory to download the objects to.
|
60 |
+
Supports all file systems supported by fsspec. Defaults to
|
61 |
+
"~/.objaverse".
|
62 |
+
save_repo_format (Optional[Literal["zip", "tar", "tar.gz", "files"]],
|
63 |
+
optional): Format to save the repository. If None, the repository will
|
64 |
+
not be saved. If "files" is specified, each file will be saved
|
65 |
+
individually. Otherwise, the repository can be saved as a "zip", "tar",
|
66 |
+
or "tar.gz" file. Defaults to None.
|
67 |
+
handle_found_object (Optional[Callable], optional): Called when an object is
|
68 |
+
successfully found and downloaded. Here, the object has the same sha256
|
69 |
+
as the one that was downloaded with Objaverse-XL. If None, the object
|
70 |
+
will be downloaded, but nothing will be done with it. Args for the
|
71 |
+
function include:
|
72 |
+
- local_path (str): Local path to the downloaded 3D object.
|
73 |
+
- file_identifier (str): File identifier of the 3D object.
|
74 |
+
- sha256 (str): SHA256 of the contents of the 3D object.
|
75 |
+
- metadata (Dict[Hashable, Any]): Metadata about the 3D object,
|
76 |
+
including the GitHub organization and repo names.
|
77 |
+
Return is not used. Defaults to None.
|
78 |
+
handle_modified_object (Optional[Callable], optional): Called when a
|
79 |
+
modified object is found and downloaded. Here, the object is
|
80 |
+
successfully downloaded, but it has a different sha256 than the one that
|
81 |
+
was downloaded with Objaverse-XL. This is not expected to happen very
|
82 |
+
often, because the same commit hash is used for each repo. If None, the
|
83 |
+
object will be downloaded, but nothing will be done with it. Args for
|
84 |
+
the function include:
|
85 |
+
- local_path (str): Local path to the downloaded 3D object.
|
86 |
+
- file_identifier (str): File identifier of the 3D object.
|
87 |
+
- new_sha256 (str): SHA256 of the contents of the newly downloaded 3D
|
88 |
+
object.
|
89 |
+
- old_sha256 (str): Expected SHA256 of the contents of the 3D object as
|
90 |
+
it was when it was downloaded with Objaverse-XL.
|
91 |
+
- metadata (Dict[Hashable, Any]): Metadata about the 3D object, which is
|
92 |
+
particular to the souce.
|
93 |
+
Return is not used. Defaults to None.
|
94 |
+
handle_missing_object (Optional[Callable], optional): Called when an object
|
95 |
+
that is in Objaverse-XL is not found. Here, it is likely that the
|
96 |
+
repository was deleted or renamed. If None, nothing will be done with
|
97 |
+
the missing object.
|
98 |
+
Args for the function include:
|
99 |
+
- file_identifier (str): File identifier of the 3D object.
|
100 |
+
- sha256 (str): SHA256 of the contents of the original 3D object.
|
101 |
+
- metadata (Dict[Hashable, Any]): Metadata about the 3D object, which is
|
102 |
+
particular to the source.
|
103 |
+
Return is not used. Defaults to None.
|
104 |
+
|
105 |
+
Returns:
|
106 |
+
Dict[str, str]: Mapping of file identifiers to local paths of the downloaded
|
107 |
+
3D objects.
|
108 |
+
"""
|
109 |
+
sources = set(objects["source"].unique().tolist())
|
110 |
+
all_sources = {"github", "thingiverse", "smithsonian", "sketchfab"}
|
111 |
+
|
112 |
+
if not sources.issubset(all_sources):
|
113 |
+
raise ValueError(
|
114 |
+
f"Invalid sources: {sources}. Must be a subset of {all_sources}."
|
115 |
+
)
|
116 |
+
|
117 |
+
downloaded_objects = {}
|
118 |
+
for source in sources:
|
119 |
+
source_downloads = self.downloaders[source].download_objects(
|
120 |
+
objects[objects["source"] == source],
|
121 |
+
download_dir,
|
122 |
+
processes,
|
123 |
+
handle_found_object,
|
124 |
+
handle_modified_object,
|
125 |
+
handle_missing_object,
|
126 |
+
**kwargs,
|
127 |
+
)
|
128 |
+
downloaded_objects.update(source_downloads)
|
129 |
+
|
130 |
+
return downloaded_objects
|