rdune71 commited on
Commit
89a4312
Β·
1 Parent(s): e228957

Enhance debug panel with API call tracking and add session state fields for tracking Ollama/HF calls

Browse files
Files changed (1) hide show
  1. app.py +54 -1
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import time
3
  import os
4
  import sys
 
5
  from pathlib import Path
6
  sys.path.append(str(Path(__file__).parent))
7
 
@@ -17,6 +18,18 @@ if "messages" not in st.session_state:
17
  st.session_state.messages = []
18
  if "last_error" not in st.session_state:
19
  st.session_state.last_error = ""
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Sidebar
22
  with st.sidebar:
@@ -61,6 +74,28 @@ with st.sidebar:
61
  st.write(f"**HF Token Set**: {'βœ… Yes' if config.hf_token else '❌ No'}")
62
  if st.session_state.last_error:
63
  st.warning(f"Last Error: {st.session_state.last_error}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  # Main chat interface
66
  st.title("🧠 AI Life Coach")
@@ -118,16 +153,34 @@ if send_button and user_input.strip():
118
  st.session_state.selected_model
119
  )
120
  backend_used = "Ollama"
 
 
 
 
 
121
  except Exception as e:
122
  error_msg = f"Ollama error: {str(e)}"
 
 
 
 
123
 
124
  # Fallback to Hugging Face
125
  if not ai_response and config.hf_token:
126
  try:
127
  ai_response = send_to_hf(user_input, conversation_history)
128
  backend_used = "Hugging Face"
 
 
 
 
 
129
  except Exception as e:
130
  error_msg = f"Hugging Face error: {str(e)}"
 
 
 
 
131
 
132
  if ai_response:
133
  st.markdown(f"{ai_response}")
@@ -136,7 +189,7 @@ if send_button and user_input.strip():
136
  conversation.append({"role": "user", "content": user_input})
137
  conversation.append({"role": "assistant", "content": ai_response})
138
 
139
- # Update session
140
  user_session["conversation"] = conversation
141
  session_manager.update_session("default_user", user_session)
142
 
 
2
  import time
3
  import os
4
  import sys
5
+ from datetime import datetime
6
  from pathlib import Path
7
  sys.path.append(str(Path(__file__).parent))
8
 
 
18
  st.session_state.messages = []
19
  if "last_error" not in st.session_state:
20
  st.session_state.last_error = ""
21
+ if "last_ollama_call_success" not in st.session_state:
22
+ st.session_state.last_ollama_call_success = None
23
+ if "last_ollama_call_time" not in st.session_state:
24
+ st.session_state.last_ollama_call_time = ""
25
+ if "last_ollama_response_preview" not in st.session_state:
26
+ st.session_state.last_ollama_response_preview = ""
27
+ if "last_hf_call_success" not in st.session_state:
28
+ st.session_state.last_hf_call_success = None
29
+ if "last_hf_call_time" not in st.session_state:
30
+ st.session_state.last_hf_call_time = ""
31
+ if "last_hf_response_preview" not in st.session_state:
32
+ st.session_state.last_hf_response_preview = ""
33
 
34
  # Sidebar
35
  with st.sidebar:
 
74
  st.write(f"**HF Token Set**: {'βœ… Yes' if config.hf_token else '❌ No'}")
75
  if st.session_state.last_error:
76
  st.warning(f"Last Error: {st.session_state.last_error}")
77
+
78
+ # Ollama API Call Tracking
79
+ if st.session_state.last_ollama_call_success is not None:
80
+ status_icon = "βœ… Success" if st.session_state.last_ollama_call_success else "❌ Failed"
81
+ st.write(f"**Last Ollama Call**: {status_icon}")
82
+ st.write(f"**At**: {st.session_state.last_ollama_call_time}")
83
+ if st.session_state.last_ollama_response_preview:
84
+ st.code(st.session_state.last_ollama_response_preview[:200] + ("..." if len(st.session_state.last_ollama_response_preview) > 200 else ""), language="text")
85
+
86
+ # Hugging Face API Call Tracking
87
+ if st.session_state.last_hf_call_success is not None:
88
+ status_icon = "βœ… Success" if st.session_state.last_hf_call_success else "❌ Failed"
89
+ st.write(f"**Last HF Call**: {status_icon}")
90
+ st.write(f"**At**: {st.session_state.last_hf_call_time}")
91
+ if st.session_state.last_hf_response_preview:
92
+ st.code(st.session_state.last_hf_response_preview[:200] + ("..." if len(st.session_state.last_hf_response_preview) > 200 else ""), language="text")
93
+
94
+ # Manual Refresh Button
95
+ if st.button("πŸ”„ Refresh Ollama Status"):
96
+ from services.ollama_monitor import check_ollama_status
97
+ status = check_ollama_status()
98
+ st.toast(f"Ollama Status: {'Running' if status['running'] else 'Unavailable'}", icon="πŸ“‘")
99
 
100
  # Main chat interface
101
  st.title("🧠 AI Life Coach")
 
153
  st.session_state.selected_model
154
  )
155
  backend_used = "Ollama"
156
+
157
+ # Capture success metadata
158
+ st.session_state.last_ollama_call_success = True
159
+ st.session_state.last_ollama_call_time = str(datetime.utcnow())
160
+ st.session_state.last_ollama_response_preview = ai_response[:200] if ai_response else ""
161
  except Exception as e:
162
  error_msg = f"Ollama error: {str(e)}"
163
+ # Capture failure metadata
164
+ st.session_state.last_ollama_call_success = False
165
+ st.session_state.last_ollama_call_time = str(datetime.utcnow())
166
+ st.session_state.last_ollama_response_preview = str(e)[:200]
167
 
168
  # Fallback to Hugging Face
169
  if not ai_response and config.hf_token:
170
  try:
171
  ai_response = send_to_hf(user_input, conversation_history)
172
  backend_used = "Hugging Face"
173
+
174
+ # Capture success metadata
175
+ st.session_state.last_hf_call_success = True
176
+ st.session_state.last_hf_call_time = str(datetime.utcnow())
177
+ st.session_state.last_hf_response_preview = ai_response[:200] if ai_response else ""
178
  except Exception as e:
179
  error_msg = f"Hugging Face error: {str(e)}"
180
+ # Capture failure metadata
181
+ st.session_state.last_hf_call_success = False
182
+ st.session_state.last_hf_call_time = str(datetime.utcnow())
183
+ st.session_state.last_hf_response_preview = str(e)[:200]
184
 
185
  if ai_response:
186
  st.markdown(f"{ai_response}")
 
189
  conversation.append({"role": "user", "content": user_input})
190
  conversation.append({"role": "assistant", "content": ai_response})
191
 
192
+ # Update session using the correct method
193
  user_session["conversation"] = conversation
194
  session_manager.update_session("default_user", user_session)
195