Skip to content

Commit

Permalink
Use file download URL as provided by the API (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny Guinther committed Sep 26, 2023
1 parent 038a113 commit 32c2793
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/sparsezoo/api/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

DEFAULT_MODELS_FIELDS = ["modelId", "stub"]

DEFAULT_FILES_FIELDS = ["displayName", "fileSize", "modelId", "fileType"]
DEFAULT_FILES_FIELDS = ["displayName", "downloadUrl", "fileSize", "fileType", "modelId"]

DEFAULT_TRAINING_RESULTS_FIELDS = [
"datasetName",
Expand Down
16 changes: 6 additions & 10 deletions src/sparsezoo/model/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
ValidationResult,
)
from sparsezoo.objects import Directory, File, NumpyDirectory, OnnxGz, Recipes
from sparsezoo.utils import BASE_API_URL, convert_to_bool, save_numpy
from sparsezoo.utils import convert_to_bool, save_numpy


__all__ = [
Expand Down Expand Up @@ -599,9 +599,8 @@ def _copy_and_overwrite(from_path, to_path, func):

def include_file_download_url(files: List[Dict]):
for file in files:
file["url"] = get_file_download_url(
model_id=file["model_id"], file_name=file["display_name"]
)
file["url"] = get_file_download_url(file["download_url"])
del file["download_url"]


def get_model_metadata_from_stub(stub: str) -> Dict[str, str]:
Expand Down Expand Up @@ -637,15 +636,12 @@ def is_stub(candidate: str) -> bool:


def get_file_download_url(
model_id: str,
file_name: str,
base_url: str = BASE_API_URL,
download_url: str,
):
"""Url to download a file"""
download_url = f"{base_url}/v2/models/{model_id}/files/{file_name}"

# important, do not remove
if convert_to_bool(os.getenv("SPARSEZOO_TEST_MODE")):
download_url += "?increment_download=False"
delimiter = "&" if "?" in download_url else "?"
download_url += f"{delimiter}increment_download=False"

return download_url
2 changes: 1 addition & 1 deletion tests/sparsezoo/api/test_query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"fields": (
"modelId displayName benchmarkResults "
"{ batchSize deviceInfo numCores recordedUnits recordedValue } "
"files { displayName fileSize modelId fileType } "
"files { displayName downloadUrl fileSize fileType modelId } "
),
},
),
Expand Down

0 comments on commit 32c2793

Please sign in to comment.