Krika commited on
Commit
0617fab
·
1 Parent(s): 5878215

chatbot sherika

Browse files
Files changed (1) hide show
  1. phase/Student_view/chatbot.py +22 -5
phase/Student_view/chatbot.py CHANGED
@@ -60,11 +60,12 @@ def _normalize_messages():
60
  st.session_state.messages = normed
61
 
62
  def _history_for_backend():
63
- """Convert our local history into [{role, content}] for the backend."""
 
64
  hist = []
65
  for m in st.session_state.get("messages", []):
66
  text = (m.get("text") or "").strip()
67
- if not text:
68
  continue
69
  role = "assistant" if (m.get("sender") == "assistant") else "user"
70
  hist.append({"role": role, "content": text})
@@ -97,6 +98,7 @@ def show_page():
97
  st.title("🤖 AI Financial Tutor")
98
  st.caption("Get personalized help with your financial questions")
99
 
 
100
  if "messages" not in st.session_state:
101
  st.session_state.messages = [{
102
  "id": "1",
@@ -106,9 +108,12 @@ def show_page():
106
  }]
107
  if "is_typing" not in st.session_state:
108
  st.session_state.is_typing = False
 
 
109
 
110
  _normalize_messages()
111
 
 
112
  chat_container = st.container()
113
  with chat_container:
114
  for msg in st.session_state.messages:
@@ -130,7 +135,16 @@ def show_page():
130
  if st.session_state.is_typing:
131
  st.markdown("🤖 _FinanceBot is typing..._")
132
 
133
- # Quick suggestions when only the welcome is present
 
 
 
 
 
 
 
 
 
134
  if len(st.session_state.messages) == 1:
135
  st.markdown("Try asking about:")
136
  cols = st.columns(2)
@@ -146,19 +160,22 @@ def show_page():
146
  st.session_state.is_typing = True
147
  st.rerun()
148
 
 
149
  user_input = st.chat_input("Ask me anything about personal finance...")
150
  if user_input:
151
  add_message(user_input, "user")
152
  st.session_state.is_typing = True
153
  st.rerun()
154
 
 
155
  if st.session_state.is_typing:
156
  with st.spinner("FinanceBot is thinking..."):
157
- bot_reply = _reply_via_backend(st.session_state.messages[-1]["text"])
 
158
  add_message(bot_reply, "assistant")
159
  st.session_state.is_typing = False
160
  st.rerun()
161
 
162
  if st.button("Back to Dashboard", key="ai_tutor_back_btn"):
163
  st.session_state.current_page = "Student Dashboard"
164
- st.rerun()
 
60
  st.session_state.messages = normed
61
 
62
  def _history_for_backend():
63
+ """Convert our local history into [{role, content}] for the backend,
64
+ excluding the static welcome message."""
65
  hist = []
66
  for m in st.session_state.get("messages", []):
67
  text = (m.get("text") or "").strip()
68
+ if not text or text == TUTOR_WELCOME:
69
  continue
70
  role = "assistant" if (m.get("sender") == "assistant") else "user"
71
  hist.append({"role": role, "content": text})
 
98
  st.title("🤖 AI Financial Tutor")
99
  st.caption("Get personalized help with your financial questions")
100
 
101
+ # --- session state init ---
102
  if "messages" not in st.session_state:
103
  st.session_state.messages = [{
104
  "id": "1",
 
108
  }]
109
  if "is_typing" not in st.session_state:
110
  st.session_state.is_typing = False
111
+ if "chatbot_prefill_sent" not in st.session_state:
112
+ st.session_state.chatbot_prefill_sent = False
113
 
114
  _normalize_messages()
115
 
116
+ # --- render chat bubbles ---
117
  chat_container = st.container()
118
  with chat_container:
119
  for msg in st.session_state.messages:
 
135
  if st.session_state.is_typing:
136
  st.markdown("🤖 _FinanceBot is typing..._")
137
 
138
+ # --- quiz handoff auto-prompt (only once) ---
139
+ prefill = st.session_state.get("chatbot_prefill")
140
+ if prefill and not st.session_state.chatbot_prefill_sent:
141
+ add_message(prefill, "user")
142
+ st.session_state.is_typing = True
143
+ st.session_state.chatbot_prefill_sent = True
144
+ st.session_state.chatbot_prefill = None
145
+ st.rerun()
146
+
147
+ # --- quick suggestions when fresh ---
148
  if len(st.session_state.messages) == 1:
149
  st.markdown("Try asking about:")
150
  cols = st.columns(2)
 
160
  st.session_state.is_typing = True
161
  st.rerun()
162
 
163
+ # --- user input ---
164
  user_input = st.chat_input("Ask me anything about personal finance...")
165
  if user_input:
166
  add_message(user_input, "user")
167
  st.session_state.is_typing = True
168
  st.rerun()
169
 
170
+ # --- handle pending bot reply ---
171
  if st.session_state.is_typing:
172
  with st.spinner("FinanceBot is thinking..."):
173
+ last_user_msg = next((m["text"] for m in reversed(st.session_state.messages) if m["sender"] == "user"), "")
174
+ bot_reply = _reply_via_backend(last_user_msg)
175
  add_message(bot_reply, "assistant")
176
  st.session_state.is_typing = False
177
  st.rerun()
178
 
179
  if st.button("Back to Dashboard", key="ai_tutor_back_btn"):
180
  st.session_state.current_page = "Student Dashboard"
181
+ st.rerun()