jostlebot commited on
Commit
e35a397
·
1 Parent(s): 23b7669

fix: Update Anthropic client initialization to fix proxies parameter error

Browse files
Files changed (3) hide show
  1. app.py +4 -1
  2. requirements.txt +1 -1
  3. 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(api_key=api_key)
 
 
 
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.7.8
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 debrief_sequence import DEBRIEF_SEQUENCE
6
 
7
- # Initialize session state for debrief
8
- if 'in_reflection' not in st.session_state:
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
- # ... existing code ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Add Reflect button at the top of chat
18
- if not st.session_state.in_reflection:
19
- if st.button("🤔 I'm ready to debrief", use_container_width=True):
20
- st.session_state.in_reflection = True
21
- st.session_state.debrief_stage = 0
22
- st.rerun()
 
 
 
 
 
23
 
24
- # Handle reflection mode
25
- if st.session_state.in_reflection:
26
- st.markdown("## 🪞 Final Debrief Sequence")
27
-
28
- # Display current stage of debrief
29
- current_debrief = DEBRIEF_SEQUENCE[st.session_state.debrief_stage]
30
-
31
- # Display the debrief content in a styled container
32
- st.markdown(f"""
33
- <div style="background-color: #f8f9fa; padding: 20px; border-radius: 10px; margin: 20px 0;">
34
- <div style="color: #666; font-size: 0.9em; margin-bottom: 10px;">
35
- {current_debrief["type"].replace("_", " ").title()}
36
- </div>
37
- <div style="white-space: pre-line;">
38
- {current_debrief["content"]}
39
- </div>
40
- </div>
41
- """, unsafe_allow_html=True)
42
-
43
- # Navigation buttons
44
- col1, col2, col3 = st.columns([1, 2, 1])
45
-
46
- with col1:
47
- if st.session_state.debrief_stage > 0:
48
- if st.button(" Previous", use_container_width=True):
49
- st.session_state.debrief_stage -= 1
50
- st.rerun()
 
 
 
 
 
 
 
51
 
52
- with col3:
53
- if st.session_state.debrief_stage < len(DEBRIEF_SEQUENCE) - 1:
54
- if st.button("Next ", use_container_width=True):
55
- st.session_state.debrief_stage += 1
56
- st.rerun()
57
- elif st.session_state.debrief_stage == len(DEBRIEF_SEQUENCE) - 1:
58
- if st.button("Complete Reflection", use_container_width=True):
59
- st.session_state.in_reflection = False
60
- st.session_state.debrief_stage = 0
61
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- # Optional note-taking area
64
- st.markdown("### Your Notes")
65
- st.text_area(
66
- "Use this space to write any thoughts, feelings, or insights that arise...",
67
- value=st.session_state.final_notes,
68
- key="reflection_notes",
69
- height=100,
70
- help="Your notes are private and will be cleared when you start a new session."
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})