replicate/replicate-python

Suggestion: Allow Passing API Token as a Parameter in Replicate Python Client

Closed this issue · 1 comments

The Replicate Python client currently requires the API token to be set as an environment variable (REPLICATE_API_TOKEN), which can be inconvenient in certain scenarios. It would be beneficial to have the option to pass the API token as a parameter when initializing the client, similar to the functionality provided in the Replicate JavaScript client.

Modify the Replicate Python client to accept the API token as a parameter when initializing the client, in addition to the existing environment variable option. This would provide more flexibility and make it easier to use the client in different environments or when the API token needs to be dynamically provided. The updated initialization could look like this:

from replicate import Replicate

replicate = Replicate(api_token="my_api_token")

This would allow users to either set the REPLICATE_API_TOKEN environment variable or pass the API token directly to the client constructor.

mattt commented

Hi @Mahad-lab. The default export of the Replicate package is a client that initializes with an API token populated from REPLICATE_API_TOKEN. But, like the JS client, you can import Client and create a Replicate client yourself:

import os
from replicate import Client

replicate = Client(api_token=os.getenv("SOME_OTHER_ENV_VARIABLE"))
replicate.run(
        "stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
        input={"prompt": "a 19th century portrait of a wombat gentleman"}
    )

Following The Twelve-Factor App guidelines, API tokens and other credentials should be set in the environment, not hard-coded in source code. So I'd recommend against setting it with a string literal like in your example code.