rdune71 commited on
Commit
f750678
Β·
1 Parent(s): 80344dc

Fixed expander nesting issue in HF Expert analysis section

Browse files
Files changed (1) hide show
  1. app.py +64 -73
app.py CHANGED
@@ -48,12 +48,6 @@ if "tts_enabled" not in st.session_state:
48
  if "voice_input" not in st.session_state:
49
  st.session_state.voice_input = ""
50
 
51
- # DEBUG: Validate session messages (fixed syntax)
52
- for idx, msg in enumerate(st.session_state.messages):
53
- if not isinstance(msg, dict) or "role" not in msg or "content" not in msg:
54
- st.error(f"🚨 Malformed message at index {idx}: {msg}")
55
- st.stop()
56
-
57
  # Sidebar layout redesign
58
  with st.sidebar:
59
  st.title("🧠 AI Life Coach")
@@ -146,22 +140,19 @@ with st.sidebar:
146
  st.divider()
147
 
148
  st.subheader("πŸ€– HF Expert Analysis")
149
- col1, col2 = st.columns([3, 1])
150
- with col1:
151
- st.markdown("""
152
- **HF Expert Features:**
153
- - Analyzes entire conversation history
154
- - Performs web research when needed
155
- - Provides deep insights and recommendations
156
- - Acts as expert consultant in your conversation
157
- """)
158
- with col2:
159
- if st.button("🧠 Activate HF Expert",
160
- key="activate_hf_expert_sidebar",
161
- help="Send conversation to HF endpoint for deep analysis",
162
- use_container_width=True,
163
- disabled=st.session_state.is_processing):
164
- st.session_state.hf_expert_requested = True
165
 
166
  # Main interface
167
  st.title("🧠 AI Life Coach")
@@ -212,58 +203,58 @@ if st.session_state.messages and len(st.session_state.messages) > 0:
212
  use_container_width=True,
213
  disabled=st.session_state.is_processing):
214
  st.session_state.hf_expert_requested = True
215
-
216
- # Show HF expert analysis when requested
217
- if st.session_state.get("hf_expert_requested", False):
218
- with st.spinner("🧠 HF Expert analyzing conversation..."):
219
- try:
220
- # Get conversation history
221
- user_session = session_manager.get_session("default_user")
222
- conversation_history = user_session.get("conversation", [])
223
-
224
- # Show what HF expert sees
225
- with st.expander("πŸ“‹ HF Expert Input", expanded=False):
226
- st.markdown("**Conversation History Sent to HF Expert:**")
227
- for i, msg in enumerate(conversation_history[-10:]): # Last 10 messages
228
- st.markdown(f"**{msg['role'].capitalize()}:** {msg['content'][:100]}{'...' if len(msg['content']) > 100 else ''}")
229
-
230
- # Request HF analysis
231
- hf_analysis = coordinator.manual_hf_analysis(
232
- "default_user",
233
- conversation_history
234
- )
235
-
236
- if hf_analysis:
237
- # Display HF expert response with clear indication
238
- with st.chat_message("assistant"):
239
- st.markdown("### πŸ€– HF Expert Analysis")
240
- st.markdown(hf_analysis)
241
-
242
- # Add research/web search decisions
243
- research_needs = coordinator.determine_web_search_needs(conversation_history)
244
- if research_needs["needs_search"]:
245
- st.info(f"πŸ” **Research Needed:** {research_needs['reasoning']}")
246
- if st.button("πŸ”Ž Perform Web Research", key="web_research_button"):
247
- # Perform web search
248
- with st.spinner("πŸ”Ž Searching for current information..."):
249
- # Add web search logic here
250
- st.success("βœ… Web research completed!")
251
-
252
- # Add to message history with HF expert tag
253
- st.session_state.messages.append({
254
- "role": "assistant",
255
- "content": hf_analysis,
256
- "timestamp": datetime.now().strftime("%H:%M:%S"),
257
- "source": "hf_expert",
258
- "research_needs": research_needs
259
- })
260
-
261
- st.session_state.hf_expert_requested = False
262
 
263
- except Exception as e:
264
- user_msg = translate_error(e)
265
- st.error(f"❌ HF Expert analysis failed: {user_msg}")
266
- st.session_state.hf_expert_requested = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
 
268
  # Chat input - FIXED VERSION (moved outside of tabs)
269
  user_input = st.chat_input("Type your message here...", disabled=st.session_state.is_processing)
 
48
  if "voice_input" not in st.session_state:
49
  st.session_state.voice_input = ""
50
 
 
 
 
 
 
 
51
  # Sidebar layout redesign
52
  with st.sidebar:
53
  st.title("🧠 AI Life Coach")
 
140
  st.divider()
141
 
142
  st.subheader("πŸ€– HF Expert Analysis")
143
+ st.markdown("""
144
+ **HF Expert Features:**
145
+ - Analyzes entire conversation history
146
+ - Performs web research when needed
147
+ - Provides deep insights and recommendations
148
+ - Acts as expert consultant in your conversation
149
+ """)
150
+ if st.button("🧠 Activate HF Expert",
151
+ key="activate_hf_expert_sidebar",
152
+ help="Send conversation to HF endpoint for deep analysis",
153
+ use_container_width=True,
154
+ disabled=st.session_state.is_processing):
155
+ st.session_state.hf_expert_requested = True
 
 
 
156
 
157
  # Main interface
158
  st.title("🧠 AI Life Coach")
 
203
  use_container_width=True,
204
  disabled=st.session_state.is_processing):
205
  st.session_state.hf_expert_requested = True
206
+
207
+ # Show HF expert analysis when requested (outside of the expander)
208
+ if st.session_state.get("hf_expert_requested", False):
209
+ with st.spinner("🧠 HF Expert analyzing conversation..."):
210
+ try:
211
+ # Get conversation history
212
+ user_session = session_manager.get_session("default_user")
213
+ conversation_history = user_session.get("conversation", [])
214
+
215
+ # Show what HF expert sees in a separate expander
216
+ with st.expander("πŸ“‹ HF Expert Input", expanded=False):
217
+ st.markdown("**Conversation History Sent to HF Expert:**")
218
+ for i, msg in enumerate(conversation_history[-10:]): # Last 10 messages
219
+ st.markdown(f"**{msg['role'].capitalize()}:** {msg['content'][:100]}{'...' if len(msg['content']) > 100 else ''}")
220
+
221
+ # Request HF analysis
222
+ hf_analysis = coordinator.manual_hf_analysis(
223
+ "default_user",
224
+ conversation_history
225
+ )
226
+
227
+ if hf_analysis:
228
+ # Display HF expert response with clear indication
229
+ with st.chat_message("assistant"):
230
+ st.markdown("### πŸ€– HF Expert Analysis")
231
+ st.markdown(hf_analysis)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
233
+ # Add research/web search decisions
234
+ research_needs = coordinator.determine_web_search_needs(conversation_history)
235
+ if research_needs["needs_search"]:
236
+ st.info(f"πŸ” **Research Needed:** {research_needs['reasoning']}")
237
+ if st.button("πŸ”Ž Perform Web Research", key="web_research_button"):
238
+ # Perform web search
239
+ with st.spinner("πŸ”Ž Searching for current information..."):
240
+ # Add web search logic here
241
+ st.success("βœ… Web research completed!")
242
+
243
+ # Add to message history with HF expert tag
244
+ st.session_state.messages.append({
245
+ "role": "assistant",
246
+ "content": hf_analysis,
247
+ "timestamp": datetime.now().strftime("%H:%M:%S"),
248
+ "source": "hf_expert",
249
+ "research_needs": research_needs
250
+ })
251
+
252
+ st.session_state.hf_expert_requested = False
253
+
254
+ except Exception as e:
255
+ user_msg = translate_error(e)
256
+ st.error(f"❌ HF Expert analysis failed: {user_msg}")
257
+ st.session_state.hf_expert_requested = False
258
 
259
  # Chat input - FIXED VERSION (moved outside of tabs)
260
  user_input = st.chat_input("Type your message here...", disabled=st.session_state.is_processing)