Connection with Bardapi does not work when deployed on Streamlit

Hello,
I have a simple code that works perfectly on my local machine but generates an error when deployed on Streamlit. I am using the bardapi from GitHub - dsdanielpark/Bard-API: The unofficial python package that returns response of Google Bard through cookie value. and using the latest version. in requirements.txt I am using bardapi==0.1.29

import streamlit as st
from dotenv.main import load_dotenv
import os
load_dotenv()
st.title("BardAPI for Python")

_BARD_API_KEY=os.environ['_BARD_API_KEY']
#import os
from bardapi import Bard

with st.container () :

    with st.spinner ( 'Wait till Bard gets the answers...' ) :
        input_text = "What is the largest state in the USA?"

        try :
            bard = Bard ( token = _BARD_API_KEY, timeout = 20 )
            response = (bard.get_answer ( input_text ) [ 'content' ])
            st.write ( response )
        except :
            st.error ( 'Please check if you have set the Cookie ' )

on the local machine I get:

BardAPI for Python

The largest state in the USA is Alaska. It has a total area of 586,412 square miles, which is more than two and a half times the size of Texas, the second largest state. Alaska is also the least densely populated state in the USA, with a population of only about 733,000 people.

Here is a list of the 10 largest states in the USA, ranked by total area:

  1. Alaska (586,412 square miles)
  2. Texas (268,596 square miles)
  3. California (163,695 square miles)
  4. Montana (147,046 square miles)
  5. New Mexico (121,590 square miles)
  6. Arizona (113,990 square miles)
  7. Nevada (110,572 square miles)
  8. Colorado (104,094 square miles)
  9. Oregon (98,379 square miles)
  10. Wyoming (97,813 square miles)

I hope this helps! Let me know if you have any other questions.

on Streamlit Hub I get:

BardAPI for Python

Response Error: b’)]}‘\n\n38\n[[“wrb.fr”,null,null,null,null,[9]]]\n56\n[[“di”,163],[“af.httprm”,163,“7269046257447756350”,0]]\n25\n[[“e”,4,null,null,131]]\n’. Unable to get response. Please double-check the cookie values and verify your network environment or google account.

I am using the same cookie; nothing changed.
Any help is appreciated.
Thank you
GR

Now I understand what you mean, load_dotenv() does not work in streamlit, I need to use ```
st.secrets

Thank you, I will test

Unfortunately, using st.secrets does not solve the problem.

I am encountering the same issue and found no fix for this yet. I suspect it has to do with the proxy.

I am having the same issue. I can run locally, and run locally on streamlit, but deploying to cloud does not work.

I am using st.secrets, but I get the same error. I also suspect it is some sort of traffic error.

Add your secret key in the app advanced settings. After remove the .streamlit folder form the root directory. In the time of adding your secret key to advanced settings, the format should be as follows:-

Name_of_the_key= xxxxxxxx(key value)

when you are calling this key, the format should be as follows:-

api_key=st.secrets['Name_of_the_key']