File size: 12,633 Bytes
d4a01e1 47a20a8 d4a01e1 0656632 d4a01e1 47a20a8 9ce3c16 47a20a8 d4a01e1 ef396ec d4a01e1 47a20a8 d4a01e1 47a20a8 d4a01e1 9ce3c16 d4a01e1 9ce3c16 d4a01e1 0656632 d4a01e1 47a20a8 d4a01e1 9ce3c16 d4a01e1 9ce3c16 d4a01e1 47a20a8 d4a01e1 9ce3c16 d4a01e1 ef396ec d4a01e1 47a20a8 d4a01e1 9ce3c16 d4a01e1 47a20a8 9ce3c16 d4a01e1 9ce3c16 d4a01e1 9ce3c16 47a20a8 9ce3c16 47a20a8 9ce3c16 47a20a8 ef396ec d4a01e1 9ce3c16 d4a01e1 47a20a8 d4a01e1 47a20a8 d4a01e1 47a20a8 d4a01e1 9ce3c16 d4a01e1 9ce3c16 47a20a8 9ce3c16 d4a01e1 ef396ec d4a01e1 9ce3c16 ef396ec d4a01e1 9ce3c16 d4a01e1 9ce3c16 d4a01e1 9ce3c16 d4a01e1 9ce3c16 d4a01e1 9ce3c16 d4a01e1 9ce3c16 d4a01e1 47a20a8 d4a01e1 9ce3c16 d4a01e1 47a20a8 9ce3c16 d4a01e1 9ce3c16 d4a01e1 9ce3c16 0656632 47a20a8 0656632 d4a01e1 e95a1cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
import os
import csv
import uuid
from datetime import datetime
from typing import List, Tuple
import torch
import gradio as gr
from filelock import FileLock
from huggingface_hub import HfApi
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
pipeline,
StoppingCriteria,
StoppingCriteriaList,
)
from peft import PeftModel
import tempfile
import pandas as pd
from datasets import load_dataset
# =========================
# ⚙️ Config
# =========================
MAX_HISTORY_TURNS = 10
MAX_PROMPT_TOKENS = 1024
MAX_NEW_TOKENS = 60
LOG_DIR = "logs"
os.makedirs(LOG_DIR, exist_ok=True)
LOCK_PATH = os.path.join(LOG_DIR, ".lock")
HF_TOKEN = os.environ.get("HF_TOKEN")
SPACE_ID = os.environ.get("SPACE_ID")
MODEL_ID = "hparten/prob1_qlora_math_student"
# =========================
# 🔠 Model + Tokenizer
# =========================
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
tokenizer.pad_token = tokenizer.eos_token
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
dtype=torch.float16,
device_map="auto",
)
# =========================
# 🧩 Strategy Explanations
# =========================
strategy_explanations = {
"friendly": "You add on from 41 until you get to 84, usually by counting by 10s, 20s, or 40, then ones.",
"differencing": "You difference the ones or tens separately during any part of your answer.",
"subtraction": "You turn the problem into a subtraction: 84 minus 41 equals blank to find the missing addend.",
}
# =========================
# 🧠 Build System Prompt
# =========================
def build_system_block(problem_prefix, strategy):
problem_text = "41 plus blank equals 84"
strat_key = strategy.lower()
strat_expl = strategy_explanations.get(strat_key, "Use the named strategy to explain your steps clearly.")
strategy_tag = f"<strategy_{strat_key}>"
problem_tag = f"<{problem_prefix.lower()}>"
system_text = (
f"<system>\n"
f"You are the student in a math dialogue.\n"
f"Solving the PROBLEM: {problem_tag} - {problem_text}\n"
f"Using the STRATEGY: {strategy_tag} — {strat_expl}\n"
f"Return EXACTLY one sentence inside <student> ... </student>."
f"Do NOT ask questions or include teacher text.\n"
f"Mention the strategy implicity only if natural.\n"
f"</system>\n"
)
return system_text.strip()
# =========================
# 🧾 Logging
# =========================
#CSV_HEADERS = ["timestamp", "session_id", "username", "strategy", "teacher", "student"]
#
#def _append_csv(path, row):
# with FileLock(LOCK_PATH):
# file_exists = os.path.exists(path)
# with open(path, "a", newline="", encoding="utf-8") as f:
# w = csv.writer(f)
# if not file_exists:
# w.writerow(CSV_HEADERS)
# w.writerow(row)
#
#def log_turn(session_id, username, strategy, teacher_msg, student_msg):
# row = [datetime.now().isoformat(timespec="seconds"), session_id, username, strategy, #teacher_msg, student_msg]
# per_session = os.path.join(LOG_DIR, f"chat_{session_id}.csv")
# _append_csv(per_session, row)
# =========================
# 🧩 Prompt builder
# =========================
def build_prompt(strategy, history, teacher_question, tokenizer, problem_prefix="Problem_1"):
base_system_prompt = build_system_block(problem_prefix, strategy)
turns = []
for tq, sa in history[-MAX_HISTORY_TURNS:]:
turns.append(f"<teacher> {tq} </teacher> <student> {sa} </student>")
full_prompt = base_system_prompt + "\n" + " ".join(turns)
full_prompt += f"<teacher> {teacher_question} </teacher>\n<student>"
while len(tokenizer.encode(full_prompt, add_special_tokens=False)) > MAX_PROMPT_TOKENS and len(turns) > 0:
turns.pop(0)
convo_block = " ".join(turns)
full_prompt = base_system_prompt + convo_block + f"<teacher> {teacher_question} </teacher>"
return full_prompt.strip()
# =========================================================
# ❌ Banned Tokens (prevents teacher drift)
# =========================================================
def make_bad_words_ids(tokenizer, words: List[str]) -> List[List[int]]:
"""Safely constructs bad_words_ids for special and normal tokens."""
out = []
for w in words:
if w in tokenizer.all_special_tokens:
tid = tokenizer.convert_tokens_to_ids(w)
if tid != tokenizer.unk_token_id:
out.append([tid])
else:
toks = tokenizer.encode(w, add_special_tokens=False)
if toks:
out.append(toks)
return out
bad_words_ids = make_bad_words_ids(
tokenizer,
["<teacher>", "</teacher>", "<system>", "</system>", "Teacher:", "teacher:"]
)
eos_id = tokenizer.convert_tokens_to_ids("</student>")
# =========================
# ☁️ In-Memory Logging + HF Upload
# =========================
HF_DATASET_REPO = "hparten/math_chatbot_logs"
api = HfApi()
session_logs = {} # session_id -> list of turns
last_activity = {} # session_id -> timestamp
def add_turn_to_memory(session_id, username, strategy, teacher_msg, student_msg):
"""Store one turn in memory."""
from datetime import datetime
row = {
"timestamp": datetime.now().isoformat(timespec="seconds"),
"session_id": session_id,
"username": username,
"strategy": strategy,
"teacher": teacher_msg,
"student": student_msg,
}
session_logs.setdefault(session_id, []).append(row)
update_activity(session_id)
def update_activity(session_id):
import time
last_activity[session_id] = time.time()
def flush_session_to_hub(session_id):
"""Upload session logs to Hugging Face dataset as a single Parquet file."""
if session_id not in session_logs or not session_logs[session_id]:
print(f"[flush] No logs found for session {session_id}")
return
df = pd.DataFrame(session_logs[session_id])
del session_logs[session_id]
try:
ds = load_dataset(HF_DATASET_REPO, split="train", token=HF_TOKEN)
existing = ds.to_pandas()
combined = pd.concat([existing, df], ignore_index=True)
except Exception:
combined = df
with tempfile.NamedTemporaryFile("wb", delete=False, suffix=".parquet") as tmp:
combined.to_parquet(tmp.name, index=False)
tmp_path = tmp.name
api.upload_file(
path_or_fileobj=tmp_path,
path_in_repo="chat_logs.parquet",
repo_id=HF_DATASET_REPO,
repo_type="dataset",
token=HF_TOKEN,
)
os.remove(tmp_path)
print(f"[flush] Uploaded session {session_id} to HF dataset.")
# =========================
# 🤖 Generation
# =========================
def generate_response(teacher_question, username, history, session_id, strategy):
prompt = build_prompt(strategy, history, teacher_question, tokenizer)
out = pipe(
prompt,
max_new_tokens=MAX_NEW_TOKENS,
do_sample=True,
temperature=0.4,
top_p=0.9,
repetition_penalty=1.05,
no_repeat_ngram_size=6,
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
eos_token_id=eos_id,
bad_words_ids=bad_words_ids,
return_full_text=False,
)
out_text = out[0]["generated_text"]
if "<student>" in out_text and "</student>" in out_text:
student_reply = out_text.split("<student>", 1)[1].split("</student>", 1)[0].strip()
else:
student_reply = out_text.strip()
# Force single-sentence cleanup
student_reply = student_reply.split(".")[0].strip() + "."
history.append((teacher_question, student_reply))
add_turn_to_memory(session_id, username, strategy, teacher_question, student_reply)
return student_reply, history
# =========================
# ☁️ Flush session logs to Hugging Face Hub
# =========================
#
#def flush_session_to_hub(session_id):
# """Append this session to one Parquet file in the private HF dataset."""
# if session_id not in session_logs or not session_logs[session_id]:
# print(f"[flush] No logs found for session {session_id}")
# return
#
# df = pd.DataFrame(session_logs[session_id])
# del session_logs[session_id]
#
# try:
# ds = load_dataset(HF_DATASET_REPO, split="train", token=HF_TOKEN)
# existing = ds.to_pandas()
# combined = pd.concat([existing, df], ignore_index=True)
# except Exception:
# combined = df
#
# with tempfile.NamedTemporaryFile("wb", delete=False, suffix=".parquet") as tmp:
# combined.to_parquet(tmp.name, index=False)
# tmp_path = tmp.name
#
# api.upload_file(
# path_or_fileobj=tmp_path,
# path_in_repo="chat_logs.parquet",
# repo_id=HF_DATASET_REPO,
# repo_type="dataset",
# token=HF_TOKEN,
# )
#
# os.remove(tmp_path)
# print(f"[flush] Uploaded session {session_id} to HF dataset.")
# =========================
# Inactivity flush
# =========================
import threading, time
INACTIVITY_LIMIT = 600 # 10 minutes
def check_inactivity_loop():
while True:
now = time.time()
inactive = [sid for sid, ts in last_activity.items() if now - ts > INACTIVITY_LIMIT]
for sid in inactive:
try:
flush_session_to_hub(sid)
del last_activity[sid]
except Exception as e:
print(f"[auto-flush-error] {sid}: {e}")
time.sleep(60)
threading.Thread(target=check_inactivity_loop, daemon=True).start()
# =========================
# 🖥 Gradio UI
# =========================
def on_send(teacher_question, username, strategy_choice, history, session_id):
if not session_id:
session_id = uuid.uuid4().hex[:12]
if history is None:
history = []
if not username.strip():
gr.Warning("Please enter your name before starting the chat.")
return history, history, "", session_id
if not teacher_question.strip():
gr.Warning("Please type a question for the student before sending.")
return history, history, "", session_id
student_reply, history = generate_response(
teacher_question.strip(),
username.strip(),
history,
session_id,
strategy_choice.lower()
)
msgs = []
for t, s in history[-MAX_HISTORY_TURNS:]:
msgs.append({"role": "user", "content": t})
msgs.append({"role": "assistant", "content": s})
return msgs, history, "", session_id
def on_reset(chat, history, teacher_q, session_id):
"""Flush the current session before resetting."""
if session_id:
try:
flush_session_to_hub(session_id)
print(f"[manual flush] Uploaded session {session_id} to HF dataset.")
except Exception as e:
print(f"[manual flush error] {session_id}: {e}")
return [], [], "", uuid.uuid4().hex[:12]
# =========================
# 🚀 Gradio App
# =========================
with gr.Blocks(title="Elementary Math Student Chatbot") as demo:
gr.Markdown("## 🧮 Practice Eliciting Student Thinking (Prototype)")
gr.Markdown(
"You are an elementary math teacher exploring a student's reasoning for **41 + ___ = 84**."
"\nAsk questions and see how the student explains their thinking."
)
with gr.Row():
username = gr.Textbox(label="👤 Your Name (first last)", placeholder="Enter your name...")
strategy_choice = gr.Dropdown(
["friendly", "differencing", "subtraction"],
value="Choose one",
label="🧩 Student Strategy"
)
reset_btn = gr.Button("🔄 Start Over", variant="secondary")
teacher_q = gr.Textbox(label="👩🏫 Teacher Question", placeholder="Ask the student a question…")
chat = gr.Chatbot(label="💬 Chat", type="messages")
state_history = gr.State([])
state_session = gr.State("")
send = gr.Button("Send", variant="primary")
send.click(
on_send,
inputs=[teacher_q, username, strategy_choice, state_history, state_session],
outputs=[chat, state_history, teacher_q, state_session],
)
reset_btn.click(
on_reset,
inputs=[chat, state_history, teacher_q, state_session],
outputs=[chat, state_history, teacher_q, state_session],
)
if __name__ == "__main__":
demo.queue()
demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False) |