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

import packaging to be compatible with setuptools==70.0.0 #450

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
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
47 changes: 46 additions & 1 deletion clip/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import urllib
import warnings
from typing import Any, Union, List
from pkg_resources import packaging
import pkg_resources

import torch
from PIL import Image
Expand All @@ -20,6 +20,30 @@
BICUBIC = Image.BICUBIC


import pkg_resources

# Solves import issue with changes in setuptools 70.0.0 (https://setuptools.pypa.io/en/stable/history.html#v70-0-0)
def import_packaging():
try:
# Get the version of setuptools
setuptools_version = pkg_resources.get_distribution("setuptools").version
# Print setuptools version for debugging
print(f"setuptools version: {setuptools_version}")

# Parse the version string to compare versions
from packaging import version
if version.parse(setuptools_version) >= version.parse("70.0.0"):
# For setuptools version 70.0.0 and above
import packaging
else:
# For setuptools versions below 70.0.0
from pkg_resources import packaging
return packaging
except Exception as e:
raise ImportError(f"Failed to import 'packaging' module: {e}")

packaging = import_packaging()

if packaging.version.parse(torch.__version__) < packaging.version.parse("1.7.1"):
warnings.warn("PyTorch version 1.7.1 or higher is recommended")

Expand All @@ -39,6 +63,27 @@
"ViT-L/14@336px": "https://openaipublic.azureedge.net/clip/models/3035c92b350959924f9f00213499208652fc7ea050643e8b385c2dac08641f02/ViT-L-14-336px.pt",
}

def import_packaging():
try:
# Get the version of setuptools
setuptools_version = pkg_resources.get_distribution("setuptools").version
# Print setuptools version for debugging
print(f"setuptools version: {setuptools_version}")

# Parse the version string to compare versions
from packaging import version
if version.parse(setuptools_version) >= version.parse("70.0.0"):
# For setuptools version 70.0.0 and above
import packaging
else:
# For setuptools versions below 70.0.0
from pkg_resources import packaging
return packaging
except Exception as e:
raise ImportError(f"Failed to import 'packaging' module: {e}")

packaging = import_packaging()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the contribution -- looks like the function was defined twice unintentionally, could you remove the second part here?

Copy link
Contributor Author

@JamieMartin JamieMartin Jun 3, 2024

Choose a reason for hiding this comment

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

Nice catch thanks. I've now removed the second part, which is duplicated.


def _download(url: str, root: str):
os.makedirs(root, exist_ok=True)
Expand Down