No API token provided. You need to set the REPLICATE_API_TOKEN environment variable or create a client with `replicate.Client(api_token=...)`.
purerabbit opened this issue ยท 8 comments
Yes, I'm having a lot of trouble with this too. It's working in one case from the EXPORT and not in another case where I'm trying to use model = replicate.models.get("openai/whisper")
. I'd also prefer to do something like this, within the script:
# replicate api key
#load_dotenv()
#os.environ.get('REPLICATE_API_TOKEN')
#os.environ["'REPLICATE_API_TOKEN'"] = os.environ.get('REPLICATE_API_TOKEN')
But that doesn't seem to work and neither does using /etc/environment
on Ubuntu or putting an entry in the systemd service file (prefer not to do that, for obvious reasons).
It just seems to depend on where you use the parameters.
import webbrowser
import replicate
api = replicate.Client(api_token='TOKEN')
model = api.models.get("stability-ai/stable-diffusion")
output_url = model.predict(prompt="electric sheep, neon, synthwave")[0]
print(output_url)
webbrowser.open(output_url)
It just seems to depend on where you use the parameters.
import webbrowser
import replicate
api = replicate.Client(api_token='TOKEN') model = api.models.get("stability-ai/stable-diffusion") output_url = model.predict(prompt="electric sheep, neon, synthwave")[0] print(output_url) webbrowser.open(output_url)
Thanks I've combined your method with my approach and this works, for at least one of my problems:
load_dotenv()
...
api = replicate.Client(api_token=os.environ.get('REPLICATE_API_TOKEN'))
model = api.models.get("j-min/clip-caption-reward")
image_file = Path(file_path)
result = model.predict(image=image_file)
In fact, my problem is more complex in that I have a systemd service that calls scripts and subprocesses. In spite of the fact that the systemd service runs as a user with the token in .env, it doesn't seem to pick it up. That was part of the problem. I don't want the token in my scripts, so I'm just adding a tiny ad hoc routine to open and pick up from .env where necessary. A little uglier than I set out but will always work.
import replicate
import os;
os.environ["REPLICATE_API_TOKEN"] = "<token>"
model = replicate.models.get("cjwbw/anything-v3.0")
version = model.versions.get("f410ed4c6a0c3bf8b76747860b3a3c9e4c8b5a827a16eac9dd5ad9642edce9a2")
That's what worked for me using Windows. Setting the token in the windows environment variables didn't work.
import replicate import os; os.environ["REPLICATE_API_TOKEN"] = "<token>" model = replicate.models.get("cjwbw/anything-v3.0") version = model.versions.get("f410ed4c6a0c3bf8b76747860b3a3c9e4c8b5a827a16eac9dd5ad9642edce9a2")That's what worked for me using Windows. Setting the token in the windows environment variables didn't work.
I'm using Linux Mint. For the same user and script usage directly, I can pick up from the environment, no problem. If I use Python subprocess
(which I want) there's a problem. I've ended up with a little hidden file with my token so it stays out of github.
This is broken for me when using in a background worker as of March 2023.
Even when setting api token directly in the replicate.Client
class, and verifying that the api token is there, the error remains.
Instead, I am using requests
and calling the http endpoint directly.
Hi everyone. Sorry for any inconvenience caused by this behavior. This should be fixed in versions >= 0.15.0. If you're still having problems with Replicate's default client (used in top-level replicate.run
, et al.), you can create your own Client
and pass an API token explicitly:
from replicate.client import Client
replicate = Client(api_token="r8_.......")