Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file download URL as provided by the API #370

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/sparsezoo/api/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

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
14 changes: 5 additions & 9 deletions src/sparsezoo/model/utils.py
Original file line number Diff line number Diff line change
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"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this used now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, never mind, see it was replacing the previous key

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get_file_download_url function is only called from this file, I can get rid of that method and move the logic here if you like. That would look something like:

def include_file_download_url(files: List[Dict]):
    for file in files:
        download_url = file["download_url"]
        del file["download_url"]

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

        file["url"] = download_url

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need, looks good thanks

del file["download_url"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required, just trying to maintain the existing file dict interface of sorts. I'd think having both a url and download_url property would confuse anyone looking at the dict and trying to make sense of things



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 "?"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

download_url += f"{delimiter}increment_download=False"

return download_url
Loading