elkay: api.py, chatbot, welcome
Browse files- phase/Student_view/chatbot.py +18 -3
- 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=
|
| 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 |
-
("๐", "
|
| 132 |
-
("๐ฎ", "
|
| 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
|
| 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"):
|