VoiceField / src /minimal_test.py
jostlebot's picture
Add debug info for API key access
b0a4b6b
raw
history blame
2.61 kB
import os
import streamlit as st
from anthropic import Anthropic
st.set_page_config(page_title="Claude Test", layout="wide")
st.title("Claude API Test")
# Debug: Show all environment variables (excluding the actual values)
st.write("Available environment variables:", [k for k in os.environ.keys()])
# Try multiple ways to get the API key
api_key = None
# Try direct environment variable
if 'ANTHROPIC_API_KEY' in os.environ:
api_key = os.environ['ANTHROPIC_API_KEY']
st.success("Found API key in os.environ['ANTHROPIC_API_KEY']")
# Try getenv
if not api_key:
api_key = os.getenv('ANTHROPIC_API_KEY')
if api_key:
st.success("Found API key using os.getenv()")
# Try Streamlit secrets
if not api_key and hasattr(st, 'secrets') and 'ANTHROPIC_API_KEY' in st.secrets:
api_key = st.secrets['ANTHROPIC_API_KEY']
st.success("Found API key in Streamlit secrets")
if not api_key:
st.error("""
⚠️ No API key found. Please make sure:
1. You've added the secret in Hugging Face Space settings
2. The secret is named exactly 'ANTHROPIC_API_KEY'
3. The Space has been rebuilt after adding the secret
""")
st.stop()
try:
# Initialize Claude client
client = Anthropic(api_key=api_key)
st.success("✅ Successfully initialized Anthropic client")
# Simple chat interface
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
# Chat input
if prompt := st.chat_input("Say something"):
# Display user message
with st.chat_message("user"):
st.write(prompt)
# Add user message to history
st.session_state.messages.append({"role": "user", "content": prompt})
# Get Claude's response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1000,
messages=[{"role": "user", "content": prompt}]
)
response_text = response.content[0].text
st.write(response_text)
# Add assistant response to history
st.session_state.messages.append({"role": "assistant", "content": response_text})
except Exception as e:
st.error(f"Error: {str(e)}")
st.write("Please check your API key configuration")