PD03 commited on
Commit
eda7981
·
verified ·
1 Parent(s): 8f0f9a6

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +15 -10
src/streamlit_app.py CHANGED
@@ -112,24 +112,29 @@ st.markdown("""
112
  """, unsafe_allow_html=True)
113
 
114
  # Function to safely get OpenAI API key
 
115
  def get_openai_api_key():
116
  """Safely retrieve OpenAI API key from various sources"""
117
  api_key = None
118
 
119
- # Method 1: Try from Streamlit secrets
120
- try:
121
- if hasattr(st, 'secrets') and 'OPENAI_API_KEY' in st.secrets:
122
- api_key = st.secrets["OPENAI_API_KEY"]
123
- except Exception:
124
- pass
125
 
126
- # Method 2: Try from environment variables
127
  if not api_key:
128
- api_key = os.getenv('OPENAI_API_KEY')
129
 
130
- # Method 3: Try from Hugging Face Spaces environment
131
  if not api_key:
132
- api_key = os.getenv('OPENAI_API_TOKEN')
 
 
 
 
 
 
 
 
133
 
134
  return api_key
135
 
 
112
  """, unsafe_allow_html=True)
113
 
114
  # Function to safely get OpenAI API key
115
+ # Function to safely get OpenAI API key (UPDATED)
116
  def get_openai_api_key():
117
  """Safely retrieve OpenAI API key from various sources"""
118
  api_key = None
119
 
120
+ # Method 1: Try from environment variables first (for HF Spaces)
121
+ api_key = os.getenv('OPENAI_API_KEY')
 
 
 
 
122
 
123
+ # Method 2: Try alternative environment variable names
124
  if not api_key:
125
+ api_key = os.getenv('OPENAI_API_TOKEN')
126
 
127
+ # Method 3: Try from Streamlit secrets (only if secrets exist)
128
  if not api_key:
129
+ try:
130
+ # Check if secrets file exists before accessing
131
+ import streamlit as st
132
+ if hasattr(st, 'secrets') and st.secrets:
133
+ if 'OPENAI_API_KEY' in st.secrets:
134
+ api_key = st.secrets["OPENAI_API_KEY"]
135
+ except (FileNotFoundError, Exception):
136
+ # Silently continue if secrets file doesn't exist
137
+ pass
138
 
139
  return api_key
140