Search code examples
pythongithubweb-applicationsstreamlitopenai-api

How to to upload a Python code including an OpenAI API key to my GitHub Repository with out OpenAI recognizing it as a Security leak and disable API


I need to upload a Python script of a web application including an OpenAI API key to my GitHub Repository to deploy it in the Streamlit community cloud. But, when I deploy it, it works correctly only the first time. Since OpenAI recognizes it as a security breach. I get an email notification from OpenAI as below.

enter image description here

I get the following error when I Run the Web App in Streamlit, in the log.


    raise self.handle_error_response(

openai.error.AuthenticationError: <empty message>

2023-05-16 10:35:17.560 Uncaught app exception

Traceback (most recent call last): ```

Solution

  • The brief answer is you should never do such a thing as your API key will be exposed to the public.

    The correct way of doing this is specifying environmental variables in your deployment environment, which you'll reference in your code. As every cloud Steamlit has an established approach for this, see their docs here. Hope this helps!