Improve mobile UI: Add clear scroll instructions and full-width button
Browse files- src/streamlit_app.py +88 -15
src/streamlit_app.py
CHANGED
@@ -9,16 +9,73 @@ st.set_page_config(page_title="Practice Difficult Conversations", page_icon="
|
|
9 |
|
10 |
# Initialize Anthropic client
|
11 |
try:
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
anthropic = Anthropic(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
except Exception as e:
|
15 |
-
st.error("""
|
16 |
-
⚠️
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
|
21 |
-
|
|
|
|
|
|
|
22 |
st.stop()
|
23 |
|
24 |
# Initialize session state variables
|
@@ -44,6 +101,8 @@ if not st.session_state.messages and not st.session_state.in_debrief:
|
|
44 |
- Build awareness of your patterns and responses
|
45 |
- Develop new communication strategies
|
46 |
- Process and integrate your experience with a reflective debrief
|
|
|
|
|
47 |
""")
|
48 |
|
49 |
# Conversation styles
|
@@ -69,7 +128,7 @@ if not st.session_state.messages and not st.session_state.in_debrief:
|
|
69 |
st.markdown(f"**{style}**: {details['description']}")
|
70 |
|
71 |
style = st.selectbox("Select a conversation style to practice with:", list(STYLES.keys()))
|
72 |
-
if st.button("Start Practice Session"):
|
73 |
system_message = STYLES[style]["system_prompt"]
|
74 |
st.session_state.messages = [{"role": "system", "content": system_message}]
|
75 |
st.rerun()
|
@@ -162,13 +221,27 @@ elif st.session_state.messages and not st.session_state.practice_complete:
|
|
162 |
# Get AI response
|
163 |
with st.chat_message("assistant"):
|
164 |
with st.spinner("Thinking..."):
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
# Add assistant response to chat history
|
174 |
st.session_state.messages.append({"role": "assistant", "content": response_content})
|
|
|
9 |
|
10 |
# Initialize Anthropic client
|
11 |
try:
|
12 |
+
# Try getting from environment variable first
|
13 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
14 |
+
if api_key:
|
15 |
+
st.write("Debug: Found key in environment variables")
|
16 |
+
st.write(f"Debug: Key length: {len(api_key)}")
|
17 |
+
st.write(f"Debug: Key starts with: {api_key[:15]}")
|
18 |
+
else:
|
19 |
+
# Fall back to Streamlit secrets
|
20 |
+
if "anthropic_key" in st.secrets:
|
21 |
+
api_key = st.secrets["anthropic_key"]
|
22 |
+
st.write("Debug: Found key in Streamlit secrets")
|
23 |
+
st.write(f"Debug: Key length: {len(api_key)}")
|
24 |
+
st.write(f"Debug: Key starts with: {api_key[:15]}")
|
25 |
+
else:
|
26 |
+
st.write("Debug: No key found in either location")
|
27 |
+
|
28 |
+
if not api_key:
|
29 |
+
st.error("""
|
30 |
+
⚠️ API key not found. Please ensure:
|
31 |
+
1. You have set the ANTHROPIC_API_KEY environment variable
|
32 |
+
2. The API key is in the correct format (starts with sk-ant-api03-)
|
33 |
+
3. You can get a new API key from https://console.anthropic.com/
|
34 |
+
""")
|
35 |
+
st.stop()
|
36 |
+
|
37 |
+
# Clean the API key
|
38 |
+
api_key = api_key.strip()
|
39 |
+
|
40 |
+
# Validate API key format
|
41 |
+
if not api_key.startswith("sk-ant-api03-"):
|
42 |
+
st.error(f"""
|
43 |
+
⚠️ Invalid API key format. Your key should:
|
44 |
+
1. Start with 'sk-ant-api03-'
|
45 |
+
2. Be from the Anthropic Console (https://console.anthropic.com/)
|
46 |
+
3. Be set as ANTHROPIC_API_KEY environment variable
|
47 |
+
|
48 |
+
Current key starts with: {api_key[:15]}
|
49 |
+
""")
|
50 |
+
st.stop()
|
51 |
+
|
52 |
+
# Create client with explicit API key
|
53 |
+
st.write("Debug: Attempting to create Anthropic client...")
|
54 |
anthropic = Anthropic(api_key=api_key)
|
55 |
+
st.write("Debug: Successfully created Anthropic client")
|
56 |
+
|
57 |
+
# Test the client
|
58 |
+
st.write("Debug: Testing API connection...")
|
59 |
+
test_response = anthropic.messages.create(
|
60 |
+
model="claude-3-opus-20240229",
|
61 |
+
max_tokens=10,
|
62 |
+
messages=[{"role": "user", "content": "test"}]
|
63 |
+
)
|
64 |
+
st.write("Debug: API test successful")
|
65 |
+
|
66 |
except Exception as e:
|
67 |
+
st.error(f"""
|
68 |
+
⚠️ Error initializing Anthropic client: {str(e)}
|
69 |
+
|
70 |
+
Please check:
|
71 |
+
1. Your API key is valid and in the correct format (starts with sk-ant-api03-)
|
72 |
+
2. You have set the ANTHROPIC_API_KEY environment variable
|
73 |
+
3. You can get a new API key from https://console.anthropic.com/
|
74 |
|
75 |
+
Debug info:
|
76 |
+
- Error type: {type(e).__name__}
|
77 |
+
- Error message: {str(e)}
|
78 |
+
""")
|
79 |
st.stop()
|
80 |
|
81 |
# Initialize session state variables
|
|
|
101 |
- Build awareness of your patterns and responses
|
102 |
- Develop new communication strategies
|
103 |
- Process and integrate your experience with a reflective debrief
|
104 |
+
|
105 |
+
👇 Scroll down to choose your practice scenario
|
106 |
""")
|
107 |
|
108 |
# Conversation styles
|
|
|
128 |
st.markdown(f"**{style}**: {details['description']}")
|
129 |
|
130 |
style = st.selectbox("Select a conversation style to practice with:", list(STYLES.keys()))
|
131 |
+
if st.button("Start Practice Session", use_container_width=True):
|
132 |
system_message = STYLES[style]["system_prompt"]
|
133 |
st.session_state.messages = [{"role": "system", "content": system_message}]
|
134 |
st.rerun()
|
|
|
221 |
# Get AI response
|
222 |
with st.chat_message("assistant"):
|
223 |
with st.spinner("Thinking..."):
|
224 |
+
try:
|
225 |
+
# Debug API key
|
226 |
+
st.write(f"Debug: Using API key starting with: {api_key[:10]}...")
|
227 |
+
|
228 |
+
response = anthropic.messages.create(
|
229 |
+
model="claude-3-opus-20240229",
|
230 |
+
max_tokens=1000,
|
231 |
+
messages=st.session_state.messages
|
232 |
+
)
|
233 |
+
response_content = response.content[0].text
|
234 |
+
st.markdown(response_content)
|
235 |
+
except Exception as e:
|
236 |
+
st.error(f"""
|
237 |
+
Error getting AI response: {str(e)}
|
238 |
+
|
239 |
+
Please check:
|
240 |
+
1. Your API key is valid and in the correct format (starts with sk-ant-api03-)
|
241 |
+
2. You have added it to Hugging Face space secrets as 'anthropic_key'
|
242 |
+
3. You can get a new API key from https://console.anthropic.com/
|
243 |
+
""")
|
244 |
+
st.stop()
|
245 |
|
246 |
# Add assistant response to chat history
|
247 |
st.session_state.messages.append({"role": "assistant", "content": response_content})
|