Spaces:
Sleeping
Sleeping
Improve API key handling to support both environment variables and Streamlit secrets
Browse files- src/streamlit_app.py +16 -1
src/streamlit_app.py
CHANGED
@@ -39,10 +39,25 @@ AGE_CONTEXT = {
|
|
39 |
|
40 |
# Handle API key setup
|
41 |
try:
|
|
|
|
|
|
|
|
|
42 |
api_key = os.getenv("ANTHROPIC_KEY")
|
|
|
|
|
|
|
|
|
|
|
43 |
if not api_key:
|
44 |
-
st.error("
|
|
|
|
|
|
|
|
|
|
|
45 |
st.stop()
|
|
|
46 |
c = Anthropic(api_key=api_key)
|
47 |
except Exception as e:
|
48 |
st.error(f"Error initializing Anthropic client: {str(e)}")
|
|
|
39 |
|
40 |
# Handle API key setup
|
41 |
try:
|
42 |
+
# Try multiple ways to get the API key
|
43 |
+
api_key = None
|
44 |
+
|
45 |
+
# Try environment variable
|
46 |
api_key = os.getenv("ANTHROPIC_KEY")
|
47 |
+
|
48 |
+
# Try Streamlit secrets if env var not found
|
49 |
+
if not api_key and hasattr(st, 'secrets'):
|
50 |
+
api_key = st.secrets.get("ANTHROPIC_KEY")
|
51 |
+
|
52 |
if not api_key:
|
53 |
+
st.error("""
|
54 |
+
⚠️ Anthropic API key not found. Please make sure:
|
55 |
+
1. You've added the secret in Hugging Face Space settings
|
56 |
+
2. The secret is named exactly 'ANTHROPIC_KEY'
|
57 |
+
3. The Space has been rebuilt after adding the secret
|
58 |
+
""")
|
59 |
st.stop()
|
60 |
+
|
61 |
c = Anthropic(api_key=api_key)
|
62 |
except Exception as e:
|
63 |
st.error(f"Error initializing Anthropic client: {str(e)}")
|