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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
share_trace.py — Run a live PaperProf session and push the agent trace to HF Hub.
|
| 3 |
+
|
| 4 |
+
Records each LLM step (question generation, answer evaluation, MCQ generation)
|
| 5 |
+
as a structured dataset so the community can see how PaperProf works end-to-end.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python share_trace.py
|
| 9 |
+
|
| 10 |
+
Output:
|
| 11 |
+
Dataset pushed to build-small-hackathon/PaperProf-traces
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import json
|
| 15 |
+
import time
|
| 16 |
+
import uuid
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
from datetime import datetime, timezone
|
| 20 |
+
|
| 21 |
+
sys.path.insert(0, os.path.dirname(__file__))
|
| 22 |
+
|
| 23 |
+
TRACE_REPO = "build-small-hackathon/PaperProf-traces"
|
| 24 |
+
|
| 25 |
+
# Three chunks from different domains — covers the full diversity of PaperProf use cases
|
| 26 |
+
DEMO_CHUNKS = [
|
| 27 |
+
{
|
| 28 |
+
"topic": "Operating Systems — Virtual Memory",
|
| 29 |
+
"chunk": (
|
| 30 |
+
"Virtual memory is a memory management technique that gives each process the "
|
| 31 |
+
"illusion of having access to a large, contiguous block of memory. The OS maps "
|
| 32 |
+
"virtual addresses used by programs to physical addresses in RAM using a page table. "
|
| 33 |
+
"When a process accesses a page not currently in RAM, a page fault occurs and the OS "
|
| 34 |
+
"loads the required page from disk (swap space). This allows systems to run programs "
|
| 35 |
+
"larger than physical RAM and provides memory isolation between processes."
|
| 36 |
+
),
|
| 37 |
+
"student_answers": {
|
| 38 |
+
"open": "Virtual memory allows programs to use more memory than physically available by mapping virtual addresses to physical ones using a page table.",
|
| 39 |
+
"wrong": "Virtual memory is just another name for RAM, it speeds up the CPU cache.",
|
| 40 |
+
},
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"topic": "Machine Learning — Gradient Descent",
|
| 44 |
+
"chunk": (
|
| 45 |
+
"Gradient descent is an iterative optimization algorithm used to minimize a loss "
|
| 46 |
+
"function by updating model parameters in the direction opposite to the gradient. "
|
| 47 |
+
"In each iteration, the gradient of the loss with respect to the parameters is "
|
| 48 |
+
"computed, and the parameters are updated as θ = θ − α∇L(θ), where α is the "
|
| 49 |
+
"learning rate. Too large a learning rate causes divergence; too small slows "
|
| 50 |
+
"convergence. Stochastic gradient descent (SGD) approximates the true gradient "
|
| 51 |
+
"using a random mini-batch at each step, making it scalable to large datasets."
|
| 52 |
+
),
|
| 53 |
+
"student_answers": {
|
| 54 |
+
"open": "Gradient descent minimizes the loss by repeatedly moving parameters opposite to the gradient, scaled by the learning rate.",
|
| 55 |
+
"wrong": "Gradient descent always finds the global minimum of any function.",
|
| 56 |
+
},
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"topic": "Networking — TCP Three-Way Handshake",
|
| 60 |
+
"chunk": (
|
| 61 |
+
"The TCP three-way handshake establishes a reliable connection between a client "
|
| 62 |
+
"and server before data transfer begins. The client sends a SYN segment, the server "
|
| 63 |
+
"responds with SYN-ACK, and the client completes the handshake with an ACK. Each "
|
| 64 |
+
"side advertises its initial sequence number during this exchange, which is used to "
|
| 65 |
+
"order and acknowledge packets throughout the connection. This ensures both parties "
|
| 66 |
+
"are ready to send and receive before any application data flows."
|
| 67 |
+
),
|
| 68 |
+
"student_answers": {
|
| 69 |
+
"open": "The TCP handshake uses SYN, SYN-ACK, and ACK to synchronize sequence numbers and confirm both sides are ready to communicate.",
|
| 70 |
+
"wrong": "TCP uses a two-way handshake: SYN from client and ACK from server.",
|
| 71 |
+
},
|
| 72 |
+
},
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def timed(fn, *args, **kwargs):
|
| 77 |
+
t0 = time.time()
|
| 78 |
+
result = fn(*args, **kwargs)
|
| 79 |
+
return result, round(time.time() - t0, 2)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def run_session(chunk_info: dict, session_id: str, model_id: str) -> list[dict]:
|
| 83 |
+
from core.questioner import generate_question, generate_mcq
|
| 84 |
+
from core.evaluator import evaluate_answer
|
| 85 |
+
|
| 86 |
+
steps = []
|
| 87 |
+
chunk = chunk_info["chunk"]
|
| 88 |
+
topic = chunk_info["topic"]
|
| 89 |
+
answers = chunk_info["student_answers"]
|
| 90 |
+
|
| 91 |
+
print(f"\n{'='*60}")
|
| 92 |
+
print(f"Topic: {topic}")
|
| 93 |
+
print(f"{'='*60}")
|
| 94 |
+
|
| 95 |
+
# Step 1 — Open question generation
|
| 96 |
+
print("[1/4] Generating open question…")
|
| 97 |
+
question, dur = timed(generate_question, chunk, language="English", difficulty="Normal")
|
| 98 |
+
print(f" Q: {question} ({dur}s)")
|
| 99 |
+
steps.append({
|
| 100 |
+
"session_id": session_id,
|
| 101 |
+
"step": 1,
|
| 102 |
+
"type": "question_generation",
|
| 103 |
+
"topic": topic,
|
| 104 |
+
"input": {"chunk": chunk, "difficulty": "Normal", "language": "English"},
|
| 105 |
+
"output": {"question": question},
|
| 106 |
+
"duration_s": dur,
|
| 107 |
+
"model": model_id,
|
| 108 |
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 109 |
+
})
|
| 110 |
+
|
| 111 |
+
# Step 2 — Evaluate a correct answer
|
| 112 |
+
print("[2/4] Evaluating correct answer…")
|
| 113 |
+
feedback_ok, dur = timed(evaluate_answer, question, chunk, answers["open"], language="English")
|
| 114 |
+
print(f" Feedback (correct): {feedback_ok[:80]}… ({dur}s)")
|
| 115 |
+
steps.append({
|
| 116 |
+
"session_id": session_id,
|
| 117 |
+
"step": 2,
|
| 118 |
+
"type": "answer_evaluation",
|
| 119 |
+
"topic": topic,
|
| 120 |
+
"input": {
|
| 121 |
+
"chunk": chunk,
|
| 122 |
+
"question": question,
|
| 123 |
+
"student_answer": answers["open"],
|
| 124 |
+
"expected_quality": "correct",
|
| 125 |
+
},
|
| 126 |
+
"output": {"feedback": feedback_ok},
|
| 127 |
+
"duration_s": dur,
|
| 128 |
+
"model": model_id,
|
| 129 |
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 130 |
+
})
|
| 131 |
+
|
| 132 |
+
# Step 3 — Evaluate a wrong answer
|
| 133 |
+
print("[3/4] Evaluating incorrect answer…")
|
| 134 |
+
feedback_bad, dur = timed(evaluate_answer, question, chunk, answers["wrong"], language="English")
|
| 135 |
+
print(f" Feedback (wrong): {feedback_bad[:80]}… ({dur}s)")
|
| 136 |
+
steps.append({
|
| 137 |
+
"session_id": session_id,
|
| 138 |
+
"step": 3,
|
| 139 |
+
"type": "answer_evaluation",
|
| 140 |
+
"topic": topic,
|
| 141 |
+
"input": {
|
| 142 |
+
"chunk": chunk,
|
| 143 |
+
"question": question,
|
| 144 |
+
"student_answer": answers["wrong"],
|
| 145 |
+
"expected_quality": "incorrect",
|
| 146 |
+
},
|
| 147 |
+
"output": {"feedback": feedback_bad},
|
| 148 |
+
"duration_s": dur,
|
| 149 |
+
"model": model_id,
|
| 150 |
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 151 |
+
})
|
| 152 |
+
|
| 153 |
+
# Step 4 — MCQ generation
|
| 154 |
+
print("[4/4] Generating MCQ…")
|
| 155 |
+
mcq, dur = timed(generate_mcq, chunk, language="English")
|
| 156 |
+
print(f" MCQ question: {str(mcq.get('question',''))[:80]} ({dur}s)")
|
| 157 |
+
steps.append({
|
| 158 |
+
"session_id": session_id,
|
| 159 |
+
"step": 4,
|
| 160 |
+
"type": "mcq_generation",
|
| 161 |
+
"topic": topic,
|
| 162 |
+
"input": {"chunk": chunk, "language": "English"},
|
| 163 |
+
"output": {"mcq": mcq},
|
| 164 |
+
"duration_s": dur,
|
| 165 |
+
"model": model_id,
|
| 166 |
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 167 |
+
})
|
| 168 |
+
|
| 169 |
+
return steps
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def push_trace(all_steps: list[dict], model_id: str):
|
| 173 |
+
from huggingface_hub import HfApi
|
| 174 |
+
|
| 175 |
+
token = os.environ.get("HF_TOKEN")
|
| 176 |
+
api = HfApi(token=token)
|
| 177 |
+
|
| 178 |
+
api.create_repo(TRACE_REPO, repo_type="dataset", exist_ok=True, private=False)
|
| 179 |
+
|
| 180 |
+
# JSONL trace file
|
| 181 |
+
jsonl = "\n".join(json.dumps(s, ensure_ascii=False) for s in all_steps)
|
| 182 |
+
trace_bytes = jsonl.encode()
|
| 183 |
+
|
| 184 |
+
api.upload_file(
|
| 185 |
+
path_or_fileobj=trace_bytes,
|
| 186 |
+
path_in_repo="paperprof_trace.jsonl",
|
| 187 |
+
repo_id=TRACE_REPO,
|
| 188 |
+
repo_type="dataset",
|
| 189 |
+
commit_message="chore: upload PaperProf agent trace",
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
readme = f"""---
|
| 193 |
+
license: apache-2.0
|
| 194 |
+
task_categories:
|
| 195 |
+
- question-answering
|
| 196 |
+
- text-generation
|
| 197 |
+
language:
|
| 198 |
+
- en
|
| 199 |
+
tags:
|
| 200 |
+
- agent-trace
|
| 201 |
+
- education
|
| 202 |
+
- paperprof
|
| 203 |
+
- build-small-hackathon
|
| 204 |
+
---
|
| 205 |
+
|
| 206 |
+
# PaperProf Agent Trace
|
| 207 |
+
|
| 208 |
+
Step-by-step trace of [PaperProf](https://huggingface.co/spaces/build-small-hackathon/PaperProf),
|
| 209 |
+
an AI study buddy that turns course PDFs into interactive quiz sessions.
|
| 210 |
+
|
| 211 |
+
## What's in this dataset
|
| 212 |
+
|
| 213 |
+
Each row in `paperprof_trace.jsonl` is one LLM call. Fields:
|
| 214 |
+
|
| 215 |
+
| Field | Description |
|
| 216 |
+
|---|---|
|
| 217 |
+
| `session_id` | Groups steps from the same session |
|
| 218 |
+
| `step` | Step index within the session (1–4) |
|
| 219 |
+
| `type` | `question_generation` / `answer_evaluation` / `mcq_generation` |
|
| 220 |
+
| `topic` | Domain of the source chunk |
|
| 221 |
+
| `input` | Exact input sent to the model (chunk, question, student answer…) |
|
| 222 |
+
| `output` | Raw model output |
|
| 223 |
+
| `duration_s` | Wall-clock inference time |
|
| 224 |
+
| `model` | Model ID used |
|
| 225 |
+
|
| 226 |
+
## Session structure
|
| 227 |
+
|
| 228 |
+
Each session runs 4 steps on one text chunk:
|
| 229 |
+
1. **Open question generation** — the model writes a focused exam question
|
| 230 |
+
2. **Correct answer evaluation** — structured tutor feedback on a good answer
|
| 231 |
+
3. **Wrong answer evaluation** — structured tutor feedback on a bad answer
|
| 232 |
+
4. **MCQ generation** — 4-option question with per-option explanations
|
| 233 |
+
|
| 234 |
+
Three sessions are included, covering: Operating Systems, Machine Learning, and Networking.
|
| 235 |
+
|
| 236 |
+
## Model
|
| 237 |
+
|
| 238 |
+
`{model_id}`
|
| 239 |
+
|
| 240 |
+
Built for the Build Small Hackathon, June 2026, by Team PaperProf (EPITA).
|
| 241 |
+
"""
|
| 242 |
+
api.upload_file(
|
| 243 |
+
path_or_fileobj=readme.encode(),
|
| 244 |
+
path_in_repo="README.md",
|
| 245 |
+
repo_id=TRACE_REPO,
|
| 246 |
+
repo_type="dataset",
|
| 247 |
+
commit_message="chore: add dataset card",
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
print(f"\n✅ Trace pushed → https://huggingface.co/datasets/{TRACE_REPO}")
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def main():
|
| 254 |
+
from model.llm import get_llm, DEFAULT_MODEL_ID
|
| 255 |
+
|
| 256 |
+
print("Loading model (first call may take 60–90s locally)…")
|
| 257 |
+
get_llm() # warm up
|
| 258 |
+
model_id = os.environ.get("PAPERPROF_MODEL", DEFAULT_MODEL_ID)
|
| 259 |
+
|
| 260 |
+
all_steps = []
|
| 261 |
+
for chunk_info in DEMO_CHUNKS:
|
| 262 |
+
session_id = str(uuid.uuid4())[:8]
|
| 263 |
+
steps = run_session(chunk_info, session_id, model_id)
|
| 264 |
+
all_steps.extend(steps)
|
| 265 |
+
|
| 266 |
+
print(f"\n[push] {len(all_steps)} steps captured across {len(DEMO_CHUNKS)} sessions…")
|
| 267 |
+
push_trace(all_steps, model_id)
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
if __name__ == "__main__":
|
| 271 |
+
main()
|