Summary (This is my first project.)
I am trying to set an API key as a secret environmental variable. I cannot get it to work with the secrets.toml file, either locally or online. The project worked before, when I just set the variable right in the code, but this exposed my API key when I uploaded it to GitHub.
Problem: It breaks on line 6:
#also do: pip install streamlit
import streamlit as st
#import secret API key through Streamlit secret variables
import os
openai.api_key = st.secrets[“API_KEY”] ### <------------BREAKS HERE
Local error:
Error parsing secrets file at C:\Users\cy\LearnPython\BookWriter.streamlit\secrets.toml
TomlDecodeError: invalid literal for int() with base 0: ‘sk-wqvhJxxxxxxxxxxxxxxxxxxxxxhIy3Kpc’ (line 1 column 1 char 0)
Traceback:
File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
exec(code, module.__dict__)File "C:\Users\cy\LearnPython\BookWriter\BookWriterLevel_6.py", line 6, in <module>
openai.api_key = st.secrets["API_KEY"]
~~~~~~~~~~^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\secrets.py", line 305, in __getitem__
value = self._parse(True)[key]
^^^^^^^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\secrets.py", line 204, in _parse
secrets.update(toml.loads(secrets_file_str))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\toml\decoder.py", line 514, in loads
raise TomlDecodeError(str(err), original, pos)
Online error:
TypeError: ‘Secrets’ object is not callable
Traceback:
File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
exec(code, module.__dict__)File "C:\Users\cy\LearnPython\BookWriter\BookWriterLevel_6.py", line 6, in <module>
openai.api_key = st.secrets("API_KEY")
^^^^^^^^^^^^^^^^^^^^^
Notes
When I originally developed the code, I had the API key right in the code at this point, though the tutorial I was copying noted that it was better to use environmental variables. The code worked just fine when the line read like this:
openai.api_key = “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”
(this api key is no longer valid; openai shut it down because I exposed it to GitHub, which is why I’m now trying to figure out how to set it secretly as an environmental variable)
I have created a secrets.toml file both locally and on the streamlit website in the Secrets section of the app. I know that the local file can be accessed locally because I tried printing the value. I just cannot get it to set this value as openai.api_key.
Links
-
Link to your deployed app:
https://cynthiaatonschooler-bookwriterondeskto-bookwriterlevel-6-cv83py.streamlit.app/ -
Link to your GitHub repo:
Also, it appears that secrets.toml has uploaded to GitHub…is this bad? Seems bad. Should I delete it?