Spaces:
Sleeping
Sleeping
Improve API key handling and add test file
Browse files- README.md +24 -9
- src/streamlit_app.py +31 -15
- src/test_api.py +29 -0
README.md
CHANGED
|
@@ -20,22 +20,37 @@ A Streamlit application that helps users practice difficult conversations based
|
|
| 20 |
- Customizable conversation scenarios
|
| 21 |
- AI-powered dialogue using Claude
|
| 22 |
|
| 23 |
-
## Setup
|
| 24 |
-
1. Install dependencies:
|
| 25 |
-
```bash
|
| 26 |
-
pip install -r requirements.txt
|
| 27 |
-
```
|
| 28 |
|
| 29 |
-
|
| 30 |
-
Create a `.env` file
|
|
|
|
| 31 |
```
|
| 32 |
ANTHROPIC_API_KEY=your_api_key_here
|
| 33 |
```
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
```bash
|
| 37 |
streamlit run src/streamlit_app.py
|
| 38 |
```
|
| 39 |
|
| 40 |
## Deployment
|
| 41 |
-
The app is configured for deployment on Hugging Face Spaces. Make sure to set the `ANTHROPIC_API_KEY` in the Space's secrets.
|
|
|
|
| 20 |
- Customizable conversation scenarios
|
| 21 |
- AI-powered dialogue using Claude
|
| 22 |
|
| 23 |
+
## API Key Setup
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
### Local Development
|
| 26 |
+
1. Create a `.env` file in the root directory
|
| 27 |
+
2. Add your Anthropic API key:
|
| 28 |
```
|
| 29 |
ANTHROPIC_API_KEY=your_api_key_here
|
| 30 |
```
|
| 31 |
|
| 32 |
+
### Hugging Face Spaces
|
| 33 |
+
1. Go to your Space's Settings
|
| 34 |
+
2. Under "Repository secrets", add:
|
| 35 |
+
- Name: `ANTHROPIC_API_KEY`
|
| 36 |
+
- Value: Your Anthropic API key
|
| 37 |
+
|
| 38 |
+
### Testing API Key
|
| 39 |
+
To verify your API key is working:
|
| 40 |
+
1. Run the test app: `streamlit run src/test_api.py`
|
| 41 |
+
2. If successful, you'll see "✅ API key works!"
|
| 42 |
+
3. If there's an error, check your API key configuration
|
| 43 |
+
|
| 44 |
+
## Running the App
|
| 45 |
+
1. Install dependencies:
|
| 46 |
+
```bash
|
| 47 |
+
pip install -r requirements.txt
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
2. Run the main app:
|
| 51 |
```bash
|
| 52 |
streamlit run src/streamlit_app.py
|
| 53 |
```
|
| 54 |
|
| 55 |
## Deployment
|
| 56 |
+
The app is configured for deployment on Hugging Face Spaces. Make sure to set the `ANTHROPIC_API_KEY` in the Space's secrets before deploying.
|
src/streamlit_app.py
CHANGED
|
@@ -3,27 +3,43 @@ import streamlit as st
|
|
| 3 |
from anthropic import Anthropic
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
if os.path.exists(".env"):
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
-
# Get API key from environment variables (works for both local and Hugging Face Spaces)
|
| 11 |
-
api_key = os.getenv('ANTHROPIC_API_KEY')
|
| 12 |
-
if not api_key:
|
| 13 |
-
st.error("Please set your ANTHROPIC_API_KEY in the environment variables or .env file")
|
| 14 |
-
st.stop()
|
| 15 |
-
|
| 16 |
-
# Configure page
|
| 17 |
st.set_page_config(
|
| 18 |
page_title="Attachment Style Roleplay Simulator",
|
| 19 |
page_icon="🗣️",
|
| 20 |
layout="wide"
|
| 21 |
)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Initialize session state variables
|
| 29 |
if 'messages' not in st.session_state:
|
|
|
|
| 3 |
from anthropic import Anthropic
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
# Initialize page configuration first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="Attachment Style Roleplay Simulator",
|
| 9 |
page_icon="🗣️",
|
| 10 |
layout="wide"
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Handle API key
|
| 14 |
+
try:
|
| 15 |
+
# Load from .env file for local development
|
| 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 |
+
|
| 31 |
+
# Initialize Anthropic client
|
| 32 |
+
c = Anthropic(api_key=api_key)
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"""
|
| 36 |
+
⚠️ Error initializing Anthropic client: {str(e)}
|
| 37 |
+
|
| 38 |
+
Please check:
|
| 39 |
+
1. Your ANTHROPIC_API_KEY is set correctly
|
| 40 |
+
2. You're using a valid API key from Anthropic
|
| 41 |
+
""")
|
| 42 |
+
st.stop()
|
| 43 |
|
| 44 |
# Initialize session state variables
|
| 45 |
if 'messages' not in st.session_state:
|
src/test_api.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from anthropic import Anthropic
|
| 4 |
+
|
| 5 |
+
st.title("Anthropic API Key Test")
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
# Get API key from environment
|
| 9 |
+
api_key = os.getenv('ANTHROPIC_API_KEY')
|
| 10 |
+
if not api_key:
|
| 11 |
+
st.error("❌ No API key found in environment variables")
|
| 12 |
+
st.stop()
|
| 13 |
+
|
| 14 |
+
# Try to initialize client
|
| 15 |
+
client = Anthropic(api_key=api_key)
|
| 16 |
+
|
| 17 |
+
# Try a simple API call
|
| 18 |
+
response = client.messages.create(
|
| 19 |
+
model="claude-3-opus-20240229",
|
| 20 |
+
max_tokens=10,
|
| 21 |
+
messages=[{"role": "user", "content": "Say hello"}]
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
st.success("✅ API key works! Got response from Claude")
|
| 25 |
+
st.write("Response:", response.content[0].text)
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
st.error(f"❌ Error: {str(e)}")
|
| 29 |
+
st.write("Please check your API key configuration")
|