how to add Replicate api key?
Asked Answered
M

5

7

i´m trying to use Replicate to run some neural networks in the cloud but it yields this error message:

ReplicateError: No API token provided. You need to set the REPLICATE_API_TOKEN environment variable or create a client with `replicate.Client(api_token=...)`.

You can find your API key on https://replicate.com

and python also can´t find it with the command

print(os.environ.get("REPLICATE_API_TOKEN"))

i already tried passing it using sysdm.cpl and it still doesnt work. How do I set it as an environmental variable?

Mispleading answered 10/10, 2022 at 20:50 Comment(1)
i already did exactly that and tried setting it in 2 different ways. stackoverflow is my last resort!!!Mispleading
V
9

You can set the API key using Python:

import os
import replicate

# Set the REPLICATE_API_TOKEN environment variable
os.environ["REPLICATE_API_TOKEN"] = "your_api_token_here"

However, this sets the variable value globally, making it accessible to other users of the same environment, and other Python modules. It's generally not a good practice to change environment variables from the code, but rather set them outside of the interpreter environment and use within.

Vlf answered 29/12, 2022 at 21:31 Comment(0)
S
8

I was facing the same issue when trying to run Llama. Here is my solution:

os.environ["REPLICATE_API_TOKEN"] = "TOKEN"
api = replicate.Client(api_token=os.environ["REPLICATE_API_TOKEN"])
output = api.run(
    "meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
        input={"prompt": prompt}
    )
for item in output:
    print(item, end="")
Santa answered 25/11, 2023 at 15:5 Comment(1)
Many thanks! This really saved the day for me!Figured
S
6

Was also getting an auth issue. This is what worked - replicate = replicate.Client(api_token='')

Scissel answered 15/10, 2023 at 0:52 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Insightful
A
2

This is what worked for me:

from replicate.client import Client

replicate = Client(api_token="<your_token>")

(Note the difference; it is replicate.client.Client)

Source: https://github.com/replicate/replicate-python/issues/58

Arvie answered 17/12, 2023 at 10:36 Comment(0)
I
0

You can also set environment variables via terminal. In powershell it would be:

$env:VARIABLE_NAME='VARIABLE_VALUE'
Impresario answered 10/10, 2023 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.