Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- 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
|
| 120 |
-
|
| 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
|
| 127 |
if not api_key:
|
| 128 |
-
api_key = os.getenv('
|
| 129 |
|
| 130 |
-
# Method 3: Try from
|
| 131 |
if not api_key:
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|