jostlebot commited on
Commit
7eee57b
·
1 Parent(s): b0a4b6b

Update main app with improved API key handling

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. src/streamlit_app.py +19 -7
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: blue
5
  colorTo: purple
6
  sdk: streamlit
7
  sdk_version: 1.31.1
8
- app_file: src/minimal_test.py
9
  pinned: false
10
  license: mit
11
  ---
 
5
  colorTo: purple
6
  sdk: streamlit
7
  sdk_version: 1.31.1
8
+ app_file: src/streamlit_app.py
9
  pinned: false
10
  license: mit
11
  ---
src/streamlit_app.py CHANGED
@@ -16,15 +16,27 @@ try:
16
  if os.path.exists(".env"):
17
  load_dotenv()
18
 
19
- # Get API key from environment
20
- api_key = os.getenv('ANTHROPIC_API_KEY')
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  if not api_key:
22
  st.error("""
23
- ⚠️ Anthropic API Key not found!
24
-
25
- Please make sure you have set the ANTHROPIC_API_KEY:
26
- - For local development: in your .env file
27
- - For Hugging Face Spaces: in your Space's secrets
28
  """)
29
  st.stop()
30
 
 
16
  if os.path.exists(".env"):
17
  load_dotenv()
18
 
19
+ # Try multiple ways to get the API key
20
+ api_key = None
21
+
22
+ # Try direct environment variable
23
+ if 'ANTHROPIC_API_KEY' in os.environ:
24
+ api_key = os.environ['ANTHROPIC_API_KEY']
25
+
26
+ # Try getenv
27
+ if not api_key:
28
+ api_key = os.getenv('ANTHROPIC_API_KEY')
29
+
30
+ # Try Streamlit secrets
31
+ if not api_key and hasattr(st, 'secrets') and 'ANTHROPIC_API_KEY' in st.secrets:
32
+ api_key = st.secrets['ANTHROPIC_API_KEY']
33
+
34
  if not api_key:
35
  st.error("""
36
+ ⚠️ No API key found. Please make sure:
37
+ 1. You've added the secret in Hugging Face Space settings
38
+ 2. The secret is named exactly 'ANTHROPIC_API_KEY'
39
+ 3. The Space has been rebuilt after adding the secret
 
40
  """)
41
  st.stop()
42