Spaces:
Sleeping
Sleeping
Add debug info to help find API key secret
Browse files
app.py
CHANGED
|
@@ -97,10 +97,12 @@ def analyze_prompt(prompt_text):
|
|
| 97 |
def generate_response(api_key, system_prompt, history, user_message):
|
| 98 |
"""Generate a response using Claude API."""
|
| 99 |
# Use provided key or fall back to environment variable
|
| 100 |
-
key_to_use = api_key.strip() if api_key else
|
|
|
|
|
|
|
| 101 |
|
| 102 |
if not key_to_use:
|
| 103 |
-
return "API key not found. Please enter your Anthropic API key above, or set ANTHROPIC_API_KEY in Space secrets."
|
| 104 |
if not system_prompt:
|
| 105 |
return "Please enter a system prompt first (use the Prompt Editor tab)."
|
| 106 |
if not user_message:
|
|
@@ -212,13 +214,37 @@ Generated: {timestamp}
|
|
| 212 |
# Get API key from environment if available
|
| 213 |
default_key = os.environ.get("ANTHROPIC_API_KEY", "")
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
def test_api_key(api_key_input):
|
| 217 |
"""Test if the API key works."""
|
| 218 |
-
key_to_use = api_key_input.strip() if api_key_input else
|
| 219 |
|
|
|
|
|
|
|
| 220 |
if not key_to_use:
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
try:
|
| 224 |
client = anthropic.Anthropic(api_key=key_to_use)
|
|
@@ -228,11 +254,11 @@ def test_api_key(api_key_input):
|
|
| 228 |
max_tokens=10,
|
| 229 |
messages=[{"role": "user", "content": "Say 'OK'"}]
|
| 230 |
)
|
| 231 |
-
return "API key
|
| 232 |
except anthropic.AuthenticationError:
|
| 233 |
-
return "Invalid API key. Please check your key."
|
| 234 |
except Exception as e:
|
| 235 |
-
return f"Error
|
| 236 |
|
| 237 |
|
| 238 |
# Build the interface
|
|
|
|
| 97 |
def generate_response(api_key, system_prompt, history, user_message):
|
| 98 |
"""Generate a response using Claude API."""
|
| 99 |
# Use provided key or fall back to environment variable
|
| 100 |
+
key_to_use = api_key.strip() if api_key else ""
|
| 101 |
+
if not key_to_use:
|
| 102 |
+
key_to_use, _ = get_api_key_from_env()
|
| 103 |
|
| 104 |
if not key_to_use:
|
| 105 |
+
return "API key not found. Please enter your Anthropic API key above, or set ANTHROPIC_API_KEY in Space Settings > Repository secrets."
|
| 106 |
if not system_prompt:
|
| 107 |
return "Please enter a system prompt first (use the Prompt Editor tab)."
|
| 108 |
if not user_message:
|
|
|
|
| 214 |
# Get API key from environment if available
|
| 215 |
default_key = os.environ.get("ANTHROPIC_API_KEY", "")
|
| 216 |
|
| 217 |
+
# Debug: Check for common secret name variations
|
| 218 |
+
def get_api_key_from_env():
|
| 219 |
+
"""Try multiple possible secret names."""
|
| 220 |
+
possible_names = [
|
| 221 |
+
"ANTHROPIC_API_KEY",
|
| 222 |
+
"anthropic_api_key",
|
| 223 |
+
"ANTHROPIC_KEY",
|
| 224 |
+
"API_KEY",
|
| 225 |
+
]
|
| 226 |
+
for name in possible_names:
|
| 227 |
+
key = os.environ.get(name, "")
|
| 228 |
+
if key:
|
| 229 |
+
return key, name
|
| 230 |
+
return "", None
|
| 231 |
+
|
| 232 |
|
| 233 |
def test_api_key(api_key_input):
|
| 234 |
"""Test if the API key works."""
|
| 235 |
+
key_to_use = api_key_input.strip() if api_key_input else ""
|
| 236 |
|
| 237 |
+
# If no key in textbox, try environment
|
| 238 |
+
source = "textbox"
|
| 239 |
if not key_to_use:
|
| 240 |
+
key_to_use, found_name = get_api_key_from_env()
|
| 241 |
+
source = f"env:{found_name}" if found_name else "none"
|
| 242 |
+
|
| 243 |
+
if not key_to_use:
|
| 244 |
+
# List what env vars exist (for debugging)
|
| 245 |
+
env_keys = [k for k in os.environ.keys() if 'KEY' in k.upper() or 'ANTHROPIC' in k.upper()]
|
| 246 |
+
hint = f" Found env vars with KEY/ANTHROPIC: {env_keys}" if env_keys else " No matching env vars found."
|
| 247 |
+
return f"No API key found.{hint} Add ANTHROPIC_API_KEY to Space Settings > Repository secrets."
|
| 248 |
|
| 249 |
try:
|
| 250 |
client = anthropic.Anthropic(api_key=key_to_use)
|
|
|
|
| 254 |
max_tokens=10,
|
| 255 |
messages=[{"role": "user", "content": "Say 'OK'"}]
|
| 256 |
)
|
| 257 |
+
return f"API key valid (source: {source})"
|
| 258 |
except anthropic.AuthenticationError:
|
| 259 |
+
return f"Invalid API key (source: {source}). Please check your key."
|
| 260 |
except Exception as e:
|
| 261 |
+
return f"Error: {str(e)}"
|
| 262 |
|
| 263 |
|
| 264 |
# Build the interface
|