Kerikim commited on
Commit
702933a
ยท
1 Parent(s): 7979c8a

elkay: api.py, chatbot, welcome

Browse files
Files changed (2) hide show
  1. phase/Student_view/chatbot.py +18 -3
  2. phase/welcome.py +3 -3
phase/Student_view/chatbot.py CHANGED
@@ -1,5 +1,6 @@
1
  # phase/Student_view/chatbot.py
2
  import os
 
3
  import datetime
4
  import traceback
5
  import streamlit as st
@@ -17,6 +18,15 @@ except ModuleNotFoundError:
17
 
18
  TUTOR_WELCOME = "Hi! I'm your AI Financial Tutor. What would you like to learn today?"
19
 
 
 
 
 
 
 
 
 
 
20
  # -------------------------------
21
  # History helpers
22
  # -------------------------------
@@ -73,18 +83,23 @@ def _history_for_backend():
73
  # Reply via backend (/chat)
74
  # -------------------------------
75
  def _reply_via_backend(user_text: str) -> str:
76
- # Defaults: use selected lesson/level if present
77
  lesson_id = st.session_state.get("current_lesson_id") or 0
78
  level_slug = (st.session_state.get("user", {}).get("level") or "beginner").strip().lower()
79
 
 
 
 
 
 
 
80
  try:
81
  answer = backend.chat_ai(
82
  query=user_text,
83
  lesson_id=lesson_id,
84
  level_slug=level_slug,
85
- history=_history_for_backend(),
86
  )
87
- return (answer or "").strip()
88
  except Exception as e:
89
  err_text = "".join(traceback.format_exception_only(type(e), e)).strip()
90
  return f"โš ๏ธ Chat failed: {err_text}"
 
1
  # phase/Student_view/chatbot.py
2
  import os
3
+ import re
4
  import datetime
5
  import traceback
6
  import streamlit as st
 
18
 
19
  TUTOR_WELCOME = "Hi! I'm your AI Financial Tutor. What would you like to learn today?"
20
 
21
+ def _clean_bot_text(t: str) -> str:
22
+ # strip xml-ish tags like <user>...</user>, <assistant>...</assistant>
23
+ t = re.sub(r"</?(user|assistant|system)\b[^>]*>", "", t, flags=re.I)
24
+ # strip leading speaker labels (User:, Assistant:, System:)
25
+ t = re.sub(r"(?im)^(user|assistant|system)\s*:\s*", "", t)
26
+ # collapse extra newlines
27
+ t = re.sub(r"\n{3,}", "\n\n", t)
28
+ return t.strip()
29
+
30
  # -------------------------------
31
  # History helpers
32
  # -------------------------------
 
83
  # Reply via backend (/chat)
84
  # -------------------------------
85
  def _reply_via_backend(user_text: str) -> str:
 
86
  lesson_id = st.session_state.get("current_lesson_id") or 0
87
  level_slug = (st.session_state.get("user", {}).get("level") or "beginner").strip().lower()
88
 
89
+ # Build history and remove duplicate of the message we are sending as `query`
90
+ hist = _history_for_backend()
91
+ if hist and hist[-1].get("role") == "user" and hist[-1].get("content", "").strip() == (user_text or "").strip():
92
+ hist = hist[:-1]
93
+ hist = hist[-4:]
94
+
95
  try:
96
  answer = backend.chat_ai(
97
  query=user_text,
98
  lesson_id=lesson_id,
99
  level_slug=level_slug,
100
+ history=hist,
101
  )
102
+ return _clean_bot_text((answer or "").strip())
103
  except Exception as e:
104
  err_text = "".join(traceback.format_exception_only(type(e), e)).strip()
105
  return f"โš ๏ธ Chat failed: {err_text}"
phase/welcome.py CHANGED
@@ -128,8 +128,8 @@ def welcomeui():
128
  col1, col2, col3, col4 = st.columns(4)
129
 
130
  features = [
131
- ("๐Ÿ“–", "Interactive Lessons", "Engaging content that makes financial literacy fun and accessible for all ages!"),
132
- ("๐ŸŽฎ", "Educational Games", "Learn through play with our collection of money management games and challenges!"),
133
  ("๐Ÿ†", "Progress Tracking", "Monitor learning progress with quizzes, achievements, and detailed analytics!"),
134
  ("๐Ÿค–", "AI Assistant", "Get instant help and personalized guidance from our friendly chatbot!")
135
  ]
@@ -165,7 +165,7 @@ def welcomeui():
165
  <div class="card" style="background: linear-gradient(to right, #30E8BF, #FF8235); color: #fff;">
166
  <div class="card-icon">๐Ÿ‘ฉโ€๐Ÿซ</div>
167
  <h3>For Teachers</h3>
168
- <p>Manage your classroom, track progress, and engage your students with financial education!</p>
169
  </div>
170
  """, unsafe_allow_html=True)
171
  if st.button("๐Ÿ“Š Teacher Dashboard"):
 
128
  col1, col2, col3, col4 = st.columns(4)
129
 
130
  features = [
131
+ ("๐Ÿ“–", "Lessons", "Engaging content that makes financial literacy fun and accessible for all ages!"),
132
+ ("๐ŸŽฎ", "Games", "Learn through play with our collection of money management games and challenges!"),
133
  ("๐Ÿ†", "Progress Tracking", "Monitor learning progress with quizzes, achievements, and detailed analytics!"),
134
  ("๐Ÿค–", "AI Assistant", "Get instant help and personalized guidance from our friendly chatbot!")
135
  ]
 
165
  <div class="card" style="background: linear-gradient(to right, #30E8BF, #FF8235); color: #fff;">
166
  <div class="card-icon">๐Ÿ‘ฉโ€๐Ÿซ</div>
167
  <h3>For Teachers</h3>
168
+ <p>Manage your classroom, track progress, and engage your students!</p>
169
  </div>
170
  """, unsafe_allow_html=True)
171
  if st.button("๐Ÿ“Š Teacher Dashboard"):