fix: Update Anthropic client initialization to fix proxies parameter error
Browse files- app.py +4 -1
- requirements.txt +1 -1
- src/streamlit_app.py +167 -61
app.py
CHANGED
@@ -46,7 +46,10 @@ try:
|
|
46 |
st.stop()
|
47 |
|
48 |
# Initialize client with API key from environment
|
49 |
-
client = Anthropic(
|
|
|
|
|
|
|
50 |
st.write("Debug: Successfully created Anthropic client")
|
51 |
|
52 |
except Exception as e:
|
|
|
46 |
st.stop()
|
47 |
|
48 |
# Initialize client with API key from environment
|
49 |
+
client = Anthropic(
|
50 |
+
api_key=api_key,
|
51 |
+
base_url="https://api.anthropic.com"
|
52 |
+
)
|
53 |
st.write("Debug: Successfully created Anthropic client")
|
54 |
|
55 |
except Exception as e:
|
requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
streamlit>=1.32.0
|
2 |
-
anthropic==0.
|
3 |
python-dotenv>=1.0.0
|
4 |
# openai>=1.0.0 # Replaced with anthropic
|
5 |
# pyttsx3==2.98 # No longer needed
|
|
|
1 |
streamlit>=1.32.0
|
2 |
+
anthropic==0.8.0
|
3 |
python-dotenv>=1.0.0
|
4 |
# openai>=1.0.0 # Replaced with anthropic
|
5 |
# pyttsx3==2.98 # No longer needed
|
src/streamlit_app.py
CHANGED
@@ -2,70 +2,176 @@ import os
|
|
2 |
import streamlit as st
|
3 |
from anthropic import Anthropic
|
4 |
from datetime import datetime
|
5 |
-
from
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
st.session_state.in_reflection = False
|
10 |
-
if 'debrief_stage' not in st.session_state:
|
11 |
-
st.session_state.debrief_stage = 0
|
12 |
-
if 'final_notes' not in st.session_state:
|
13 |
-
st.session_state.final_notes = ""
|
14 |
|
15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
#
|
18 |
-
if not st.session_state
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
with
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
#
|
64 |
-
st.
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import streamlit as st
|
3 |
from anthropic import Anthropic
|
4 |
from datetime import datetime
|
5 |
+
from debrief_ai import DEBRIEF_SYSTEM_PROMPT, analyze_conversation
|
6 |
|
7 |
+
# Page config
|
8 |
+
st.set_page_config(page_title="Practice Difficult Conversations", page_icon="💭")
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Initialize Anthropic client
|
11 |
+
try:
|
12 |
+
api_key = st.secrets["anthropic_key"]
|
13 |
+
anthropic = Anthropic(
|
14 |
+
api_key=api_key,
|
15 |
+
base_url="https://api.anthropic.com"
|
16 |
+
)
|
17 |
+
except Exception as e:
|
18 |
+
st.error("""
|
19 |
+
⚠️ API key error. This could be because:
|
20 |
+
1. The API key is in the old format (starts with sk-ant-api03-)
|
21 |
+
2. You need to get a new API key from https://console.anthropic.com/
|
22 |
+
3. Add it to your Hugging Face space secrets as 'anthropic_key'
|
23 |
+
|
24 |
+
Error details: """ + str(e))
|
25 |
+
st.stop()
|
26 |
|
27 |
+
# Initialize session state variables
|
28 |
+
if 'messages' not in st.session_state:
|
29 |
+
st.session_state.messages = []
|
30 |
+
if 'in_debrief' not in st.session_state:
|
31 |
+
st.session_state.in_debrief = False
|
32 |
+
if 'debrief_messages' not in st.session_state:
|
33 |
+
st.session_state.debrief_messages = []
|
34 |
+
if 'practice_complete' not in st.session_state:
|
35 |
+
st.session_state.practice_complete = False
|
36 |
+
if 'conversation_analysis' not in st.session_state:
|
37 |
+
st.session_state.conversation_analysis = None
|
38 |
|
39 |
+
# App header
|
40 |
+
st.title("Practice Difficult Conversations 💭")
|
41 |
+
|
42 |
+
# Only show welcome message if no conversation started
|
43 |
+
if not st.session_state.messages and not st.session_state.in_debrief:
|
44 |
+
st.markdown("""
|
45 |
+
Welcome to your safe space for practicing challenging interactions. Here you can:
|
46 |
+
- Practice responding to different conversation styles
|
47 |
+
- Build awareness of your patterns and responses
|
48 |
+
- Develop new communication strategies
|
49 |
+
- Process and integrate your experience with a reflective debrief
|
50 |
+
""")
|
51 |
+
|
52 |
+
# Conversation styles
|
53 |
+
STYLES = {
|
54 |
+
"The Ghost": {
|
55 |
+
"description": "Someone who is emotionally unavailable and subtly dismissive",
|
56 |
+
"system_prompt": "You are roleplaying as someone who is emotionally unavailable and subtly dismissive in conversation. Your responses should be brief and slightly evasive, showing emotional distance while maintaining plausible deniability. Key traits:\n- Deflect personal questions\n- Give non-committal responses\n- Minimize others' emotional experiences\n- Change the subject when things get too personal\n\nRespond in character while tracking the conversation for later analysis."
|
57 |
+
},
|
58 |
+
"The Sycophant": {
|
59 |
+
"description": "Someone who struggles with boundaries and excessive people-pleasing",
|
60 |
+
"system_prompt": "You are roleplaying as someone who struggles with maintaining healthy boundaries and tends toward excessive people-pleasing. Key traits:\n- Agree with everything, even when contradictory\n- Apologize frequently, even unnecessarily\n- Sacrifice your own needs for others\n- Have difficulty saying no\n- Express anxiety about potential disapproval\n\nRespond in character while tracking the conversation for later analysis."
|
61 |
+
},
|
62 |
+
"The Narcissist": {
|
63 |
+
"description": "Someone who shows self-focused behavior and limited empathy",
|
64 |
+
"system_prompt": "You are roleplaying as someone with narcissistic tendencies in conversation. Your responses should demonstrate self-importance while maintaining plausible deniability. Key traits:\n- Turn conversations back to yourself\n- Subtly dismiss others' experiences\n- Seek admiration and validation\n- Show limited empathy\n- Use subtle manipulation tactics\n\nRespond in character while tracking the conversation for later analysis."
|
65 |
+
}
|
66 |
+
}
|
67 |
+
|
68 |
+
# Style selection at start
|
69 |
+
if not st.session_state.messages and not st.session_state.in_debrief:
|
70 |
+
st.markdown("### Choose Your Practice Scenario")
|
71 |
+
for style, details in STYLES.items():
|
72 |
+
st.markdown(f"**{style}**: {details['description']}")
|
73 |
|
74 |
+
style = st.selectbox("Select a conversation style to practice with:", list(STYLES.keys()))
|
75 |
+
if st.button("Start Practice Session"):
|
76 |
+
system_message = STYLES[style]["system_prompt"]
|
77 |
+
st.session_state.messages = [{"role": "system", "content": system_message}]
|
78 |
+
st.rerun()
|
79 |
+
|
80 |
+
# Add Complete Practice button if conversation has started
|
81 |
+
if st.session_state.messages and not st.session_state.in_debrief and not st.session_state.practice_complete:
|
82 |
+
if st.button("✓ Complete Practice & Begin Debrief", use_container_width=True):
|
83 |
+
# Analyze conversation
|
84 |
+
st.session_state.conversation_analysis = analyze_conversation(st.session_state.messages)
|
85 |
+
|
86 |
+
# Initialize debrief chat with system message
|
87 |
+
st.session_state.debrief_messages = [
|
88 |
+
{"role": "system", "content": DEBRIEF_SYSTEM_PROMPT},
|
89 |
+
{"role": "user", "content": f"Please help me process my conversation. Here's the full transcript: {str(st.session_state.messages)}"}
|
90 |
+
]
|
91 |
+
|
92 |
+
# Get initial debrief response
|
93 |
+
response = anthropic.messages.create(
|
94 |
+
model="claude-3-opus-20240229",
|
95 |
+
max_tokens=1000,
|
96 |
+
messages=st.session_state.debrief_messages
|
97 |
+
)
|
98 |
+
st.session_state.debrief_messages.append(
|
99 |
+
{"role": "assistant", "content": response.content[0].text}
|
100 |
+
)
|
101 |
+
|
102 |
+
st.session_state.in_debrief = True
|
103 |
+
st.session_state.practice_complete = True
|
104 |
+
st.rerun()
|
105 |
+
|
106 |
+
# Handle debrief mode
|
107 |
+
if st.session_state.in_debrief:
|
108 |
+
st.markdown("## 🤝 Debrief & Integration")
|
109 |
|
110 |
+
# Display debrief conversation
|
111 |
+
for message in st.session_state.debrief_messages[1:]: # Skip system message
|
112 |
+
with st.chat_message(message["role"]):
|
113 |
+
st.markdown(message["content"])
|
114 |
+
|
115 |
+
# Chat input for debrief
|
116 |
+
if prompt := st.chat_input("Share your thoughts or ask a question..."):
|
117 |
+
# Add user message
|
118 |
+
st.session_state.debrief_messages.append({"role": "user", "content": prompt})
|
119 |
+
|
120 |
+
# Display user message
|
121 |
+
with st.chat_message("user"):
|
122 |
+
st.markdown(prompt)
|
123 |
+
|
124 |
+
# Get AI response
|
125 |
+
with st.chat_message("assistant"):
|
126 |
+
with st.spinner("Reflecting..."):
|
127 |
+
response = anthropic.messages.create(
|
128 |
+
model="claude-3-opus-20240229",
|
129 |
+
max_tokens=1000,
|
130 |
+
messages=st.session_state.debrief_messages
|
131 |
+
)
|
132 |
+
response_content = response.content[0].text
|
133 |
+
st.markdown(response_content)
|
134 |
+
|
135 |
+
# Add assistant response to chat history
|
136 |
+
st.session_state.debrief_messages.append(
|
137 |
+
{"role": "assistant", "content": response_content}
|
138 |
+
)
|
139 |
+
|
140 |
+
# Add button to start new practice session
|
141 |
+
if st.button("Start New Practice Session", use_container_width=True):
|
142 |
+
st.session_state.messages = []
|
143 |
+
st.session_state.debrief_messages = []
|
144 |
+
st.session_state.in_debrief = False
|
145 |
+
st.session_state.practice_complete = False
|
146 |
+
st.session_state.conversation_analysis = None
|
147 |
+
st.rerun()
|
148 |
+
|
149 |
+
# Display practice chat interface if not in debrief mode
|
150 |
+
elif st.session_state.messages and not st.session_state.practice_complete:
|
151 |
+
# Display conversation history
|
152 |
+
for message in st.session_state.messages[1:]: # Skip system message
|
153 |
+
with st.chat_message(message["role"]):
|
154 |
+
st.markdown(message["content"])
|
155 |
+
|
156 |
+
# Chat input
|
157 |
+
if prompt := st.chat_input("Type your message here..."):
|
158 |
+
# Add user message to chat history
|
159 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
160 |
+
|
161 |
+
# Display user message
|
162 |
+
with st.chat_message("user"):
|
163 |
+
st.markdown(prompt)
|
164 |
+
|
165 |
+
# Get AI response
|
166 |
+
with st.chat_message("assistant"):
|
167 |
+
with st.spinner("Thinking..."):
|
168 |
+
response = anthropic.messages.create(
|
169 |
+
model="claude-3-opus-20240229",
|
170 |
+
max_tokens=1000,
|
171 |
+
messages=st.session_state.messages
|
172 |
+
)
|
173 |
+
response_content = response.content[0].text
|
174 |
+
st.markdown(response_content)
|
175 |
+
|
176 |
+
# Add assistant response to chat history
|
177 |
+
st.session_state.messages.append({"role": "assistant", "content": response_content})
|