Improve API error handling and add setup instructions
Browse files- Add visible warning banner when API key not configured
- Better error messages for auth, rate limit, and connection errors
- Clear setup instructions for users
- Cleaner startup logging
- app.py +49 -13
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -6,14 +6,18 @@ import gradio as gr
|
|
| 6 |
import anthropic
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# ============ CONSTANTS ============
|
| 19 |
|
|
@@ -574,10 +578,7 @@ def call_claude(system_prompt, messages, context=""):
|
|
| 574 |
client = get_client()
|
| 575 |
|
| 576 |
if client is None:
|
| 577 |
-
#
|
| 578 |
-
checked = ["ANTHROPIC_API_KEY", "anthropic_key", "ANTHROPIC_KEY", "anthropic_api_key"]
|
| 579 |
-
available = [k for k in os.environ.keys() if 'key' in k.lower() or 'api' in k.lower() or 'anthrop' in k.lower()]
|
| 580 |
-
return f"API key not found. Checked: {checked}. Found env vars containing 'key'/'api'/'anthrop': {available}. Please add ANTHROPIC_API_KEY in Space Settings > Repository secrets, then restart the Space."
|
| 581 |
|
| 582 |
if context:
|
| 583 |
user_content = f"{context}\n\nUser message: {messages[-1]['content']}" if messages else context
|
|
@@ -595,6 +596,18 @@ def call_claude(system_prompt, messages, context=""):
|
|
| 595 |
|
| 596 |
# ============ STAGE PROCESSING ============
|
| 597 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
def process_stage(user_input, stage, session_data, somatic_enabled):
|
| 599 |
"""Process current stage and return response"""
|
| 600 |
|
|
@@ -610,8 +623,20 @@ def process_stage(user_input, stage, session_data, somatic_enabled):
|
|
| 610 |
[{"role": "user", "content": user_input}],
|
| 611 |
context
|
| 612 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
except Exception as e:
|
| 614 |
-
|
|
|
|
| 615 |
|
| 616 |
session_data = update_session_data(stage, user_input, session_data)
|
| 617 |
next_stage = stage + 1 if stage < 13 else 1
|
|
@@ -866,6 +891,17 @@ def create_app():
|
|
| 866 |
</div>
|
| 867 |
""")
|
| 868 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 869 |
gr.HTML("""
|
| 870 |
<div class="intro-text">
|
| 871 |
Write a raw message you want to send someone. This practice will guide you through
|
|
|
|
| 6 |
import anthropic
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# Check for API key at startup
|
| 10 |
+
def check_api_key():
|
| 11 |
+
"""Check if API key is configured"""
|
| 12 |
+
return bool(
|
| 13 |
+
os.environ.get("ANTHROPIC_API_KEY") or
|
| 14 |
+
os.environ.get("anthropic_key") or
|
| 15 |
+
os.environ.get("ANTHROPIC_KEY") or
|
| 16 |
+
os.environ.get("anthropic_api_key")
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
API_KEY_CONFIGURED = check_api_key()
|
| 20 |
+
print(f"=== STARTUP: API key configured: {API_KEY_CONFIGURED} ===")
|
| 21 |
|
| 22 |
# ============ CONSTANTS ============
|
| 23 |
|
|
|
|
| 578 |
client = get_client()
|
| 579 |
|
| 580 |
if client is None:
|
| 581 |
+
return None # Return None to indicate API not available
|
|
|
|
|
|
|
|
|
|
| 582 |
|
| 583 |
if context:
|
| 584 |
user_content = f"{context}\n\nUser message: {messages[-1]['content']}" if messages else context
|
|
|
|
| 596 |
|
| 597 |
# ============ STAGE PROCESSING ============
|
| 598 |
|
| 599 |
+
API_ERROR_MESSAGE = """**API Configuration Required**
|
| 600 |
+
|
| 601 |
+
To use this NVC practice tool, the Space owner needs to add an Anthropic API key:
|
| 602 |
+
|
| 603 |
+
1. Go to **Settings** (gear icon) in this Space
|
| 604 |
+
2. Click **Repository secrets**
|
| 605 |
+
3. Add a new secret named `ANTHROPIC_API_KEY`
|
| 606 |
+
4. Paste your API key from [console.anthropic.com](https://console.anthropic.com)
|
| 607 |
+
5. **Restart the Space** (Factory reboot)
|
| 608 |
+
|
| 609 |
+
If you're the Space owner and have already added the key, try restarting the Space."""
|
| 610 |
+
|
| 611 |
def process_stage(user_input, stage, session_data, somatic_enabled):
|
| 612 |
"""Process current stage and return response"""
|
| 613 |
|
|
|
|
| 623 |
[{"role": "user", "content": user_input}],
|
| 624 |
context
|
| 625 |
)
|
| 626 |
+
|
| 627 |
+
# Check if API key was not configured
|
| 628 |
+
if response is None:
|
| 629 |
+
return "", stage, session_data, API_ERROR_MESSAGE
|
| 630 |
+
|
| 631 |
+
except anthropic.AuthenticationError:
|
| 632 |
+
return "", stage, session_data, "**Invalid API Key** - The API key appears to be invalid. Please check that you've entered a valid Anthropic API key in the Space secrets."
|
| 633 |
+
except anthropic.RateLimitError:
|
| 634 |
+
return "", stage, session_data, "**Rate Limited** - Too many requests. Please wait a moment and try again."
|
| 635 |
+
except anthropic.APIConnectionError:
|
| 636 |
+
return "", stage, session_data, "**Connection Error** - Unable to connect to the Anthropic API. Please try again in a moment."
|
| 637 |
except Exception as e:
|
| 638 |
+
error_type = type(e).__name__
|
| 639 |
+
return "", stage, session_data, f"**Error** ({error_type}): {str(e)}"
|
| 640 |
|
| 641 |
session_data = update_session_data(stage, user_input, session_data)
|
| 642 |
next_stage = stage + 1 if stage < 13 else 1
|
|
|
|
| 891 |
</div>
|
| 892 |
""")
|
| 893 |
|
| 894 |
+
# Show warning if API key not configured
|
| 895 |
+
if not API_KEY_CONFIGURED:
|
| 896 |
+
gr.HTML("""
|
| 897 |
+
<div style="background: #FEF3C7; border: 2px solid #F59E0B; border-radius: 12px; padding: 1rem 1.5rem; margin-bottom: 1.5rem;">
|
| 898 |
+
<p style="color: #92400E; margin: 0; font-weight: 500;">
|
| 899 |
+
<strong>Setup Required:</strong> This Space needs an Anthropic API key to function.
|
| 900 |
+
If you're the owner, go to Settings > Repository secrets > Add <code>ANTHROPIC_API_KEY</code>, then restart the Space.
|
| 901 |
+
</p>
|
| 902 |
+
</div>
|
| 903 |
+
""")
|
| 904 |
+
|
| 905 |
gr.HTML("""
|
| 906 |
<div class="intro-text">
|
| 907 |
Write a raw message you want to send someone. This practice will guide you through
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
gradio
|
| 2 |
anthropic>=0.18.0
|
|
|
|
| 1 |
+
gradio>=5.12.0
|
| 2 |
anthropic>=0.18.0
|