Trouble Setting Hugging Face Token in Streamlit Cloud

Hi,

I’m encountering an issue while trying to set up my Streamlit app in Streamlit Cloud. I’m using a Hugging Face token for authentication with the Hugging Face API, and I’m trying to set this token as an environment variable using Streamlit’s secrets feature.

Here’s a summary of the problem:
my code for hugging face token looks like this:

hf_token = os.getenv("HUGGINGFACE_TOKEN")
os.environ["HUGGINGFACEHUB_API_TOKEN"] = hf_token

However, despite verifying that the secret name matches and ensuring that the secret value is correctly set in Streamlit Cloud, I’m still encountering issues with authentication.

Here’s the relevant portion of my code:

and in advanced settings of deployed streamlit app, have added the following:

I’ve also double-checked the secret name and value, as well as ensured that the code works correctly locally.

Any suggestions or insights on what might be causing this issue or how I can troubleshoot it further would be greatly appreciated.

Thank you!
NOTE: I have added a proper hugging face token

Hi, instead of using os.getenv(), you can call st.secrets[“var_name”] and store the returned secret token inside any variable to use it in your code.

In your case, you have to call st.secrets[“token”] to be able to use it in your code.
For more information, you may refer: Secrets management - Streamlit Docs

1 Like

hii @Rishi-spec-art
thank you for your help.

I did solve the way you said, with just a bit of modification:

hf_token = st.secrets["HUGGINGFACE_TOKEN"]["token"]
os.environ["HUGGINGFACEHUB_API_TOKEN"] = hf_token
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.