replicate/replicate-python

Import replicate error

xss1de opened this issue · 4 comments

xss1de commented

I wrote the code, checked it on my computer and everything worked, transferred it to a dedicated server and the following error appears after trying to import module.

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    import replicate
  File "/usr/local/lib/python3.8/dist-packages/replicate/__init__.py", line 3, in <module>
    default_client = Client()
  File "/usr/local/lib/python3.8/dist-packages/replicate/client.py", line 30, in __init__
    read_retries = Retry(
TypeError: __init__() got an unexpected keyword argument 'allowed_methods'

I fixed it by making the folllowing changes in the /usr/local/lib/python3.8/dist-packages/replicate/client.py file:
Replaced the method_whitelist variable with allowed_methods as mentioned in Retry parameters.

class Client:
    def __init__(self, api_token=None) -> None:
        super().__init__()
        # Client is instantiated at import time, so do as little as possible.
        # This includes resolving environment variables -- they might be set programmatically.
        self.api_token = api_token
        self.base_url = os.environ.get(
            "REPLICATE_API_BASE_URL", "https://api.replicate.com"
        )
        self.poll_interval = float(os.environ.get("REPLICATE_POLL_INTERVAL", "0.5"))

        # TODO: make thread safe
        self.read_session = requests.Session()
        read_retries = Retry(
            total=5,
            backoff_factor=2,
            # Only retry 500s on GET so we don't unintionally mutute data
            allowed_methods=["GET"],
            # https://support.cloudflare.com/hc/en-us/articles/115003011431-Troubleshooting-Cloudflare-5XX-errors
            status_forcelist=[
                429,
                500,
                502,
                503,
                504,
                520,
                521,
                522,
                523,
                524,
                526,
                527,
            ],
        )
        self.read_session.mount("http://", HTTPAdapter(max_retries=read_retries))
        self.read_session.mount("https://", HTTPAdapter(max_retries=read_retries))

        self.write_session = requests.Session()
        write_retries = Retry(
            total=5,
            backoff_factor=2,
            allowed_methods=["POST", "PUT"],
            # Only retry POST/PUT requests on rate limits, so we don't unintionally mutute data
            status_forcelist=[429],
        )
        self.write_session.mount("http://", HTTPAdapter(max_retries=write_retries))
        self.write_session.mount("https://", HTTPAdapter(max_retries=write_retries))
mattt commented

@xss1de Apologies for not responding sooner. I believe the root issue stems from urllib3/urllib3#2092.

Thanks to @johnjhr for proposing a workaround involving patching the library. A more direct option would be to pin and upgrade to the latest version of urllib.

issue still persists. Should we PR replicate or just update urllib?

mattt commented

@xss1de @johnjhr @elvinagam Thank you all for your patience. I just merged #147, which replaces requests with httpx, which doesn't have urllib3 as a dependency. This issue should be resolved if you upgrade to the latest release, 0.14.0.