File size: 13,275 Bytes
3df4c39
 
 
 
 
bdf1f9b
 
 
3df4c39
bdf1f9b
3df4c39
 
bdf1f9b
3df4c39
 
bdf1f9b
3df4c39
b8f4d94
 
3df4c39
6ba2d75
 
bdf1f9b
2d99da4
3df4c39
 
 
 
b8f4d94
87582fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f4d94
2d99da4
07198f4
 
3df4c39
 
 
87582fc
 
 
 
3df4c39
bdf1f9b
3df4c39
 
fda6a62
 
 
 
 
 
3df4c39
 
 
bdf1f9b
 
 
 
 
 
 
 
 
3df4c39
 
87582fc
 
 
 
 
 
 
 
 
3df4c39
2d99da4
 
b8f4d94
3df4c39
2d99da4
3df4c39
 
 
2d99da4
b8f4d94
3df4c39
87582fc
 
3df4c39
b8f4d94
 
3df4c39
bdf1f9b
 
 
fda6a62
bdf1f9b
b8f4d94
 
 
 
3df4c39
b8f4d94
bdf1f9b
8adbcfe
87582fc
 
fda6a62
b8f4d94
3df4c39
 
b8f4d94
 
3df4c39
 
fda6a62
 
3df4c39
fda6a62
3df4c39
 
bdf1f9b
 
 
 
 
3df4c39
bdf1f9b
3df4c39
 
 
 
 
bdf1f9b
 
3df4c39
 
bdf1f9b
 
 
 
 
 
fda6a62
 
 
3df4c39
bdf1f9b
 
 
 
 
 
fda6a62
3df4c39
bdf1f9b
fda6a62
3df4c39
bdf1f9b
 
 
 
 
 
 
 
 
 
 
 
 
3df4c39
bdf1f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df4c39
b8f4d94
3df4c39
 
b8f4d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df4c39
 
 
 
 
bdf1f9b
 
3df4c39
 
 
 
 
 
 
 
 
 
bdf1f9b
3df4c39
bdf1f9b
3df4c39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fda6a62
bdf1f9b
3df4c39
 
 
 
bdf1f9b
3df4c39
bdf1f9b
3df4c39
 
 
 
b8f4d94
bdf1f9b
 
3df4c39
 
 
 
2d99da4
3df4c39
 
 
bdf1f9b
 
2d99da4
 
 
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
import os
import random
import time
import logging
import asyncio
import threading
import uuid
import json

from datetime import datetime, timezone
from collections import defaultdict, Counter

from huggingface_hub import InferenceClient, HfApi, CommitOperationAdd, hf_hub_download, list_repo_files
from datasets import Dataset, load_dataset, concatenate_datasets
import gradio as gr
import pandas as pd

from model import ModelWrapper, get_model


HF_TOKEN = os.environ.get("HF_TOKEN")
HF_API = HfApi(token=HF_TOKEN)
REPO_ID = "aracape/LA-Arena"

MAX_REQUESTS_PER_HOUR = 20
MAX_TOKENS = 512
TEMPERATURE = 0.5
DEFAULT_SYSTEM = "You are a helpful learning assistant who supports students and helps them learn."
EXTENDED_SYSTEM = """
## Core Principles

**Guide, Don't Tell**: Your primary role is to facilitate learning through thoughtful questioning and scaffolded hints. Avoid giving direct answers unless absolutely necessary for the student's learning progression.

**Socratic Method with Flexibility**: Use questions to guide students toward insights, but remain adaptive. If a student is completely stuck on a prerequisite concept or needs a direct factual clarification to move forward, provide it concisely, then return to guided questioning.

**Build Understanding Incrementally**: Break complex problems into manageable steps. Start with what the student knows, then build toward the solution progressively.

## Your Approach

### 1. **Start with Diagnosis**
- Ask questions to understand what the student already knows
- Identify specific points of confusion
- Assess their current level of understanding

Example questions:
- "What have you tried so far?"
- "Which part of the problem feels most challenging?"
- "Can you explain what you understand about [concept] in your own words?"

### 2. **Provide Scaffolded Hints**
When offering hints, follow this progression:
- **First hint**: Point to a relevant concept or approach without revealing the solution
- **Second hint**: Break down the problem into smaller sub-problems
- **Third hint**: Provide a similar worked example or analogy
- **Only if needed**: Give more direct guidance while still leaving the final step to the student

### 3. **Ask Thoughtful, Purposeful Questions**
Your questions should:
- Direct attention to relevant concepts or relationships
- Prompt specific analytical thinking
- Help students recognize patterns or connections
- Encourage self-correction

Avoid vague questions like "Does that make sense?" Instead use:
- "What happens if you apply [concept] to this part?"
- "How does this connect to [related idea] we discussed?"
- "What do you notice about [specific element]?"

### 4. **Ensure Accuracy**
- Provide factually correct information in all hints and guidance
- If you're pointing toward a concept, ensure your description is precise
- Verify that your hints lead toward the correct solution path

### 5. **Adapt Your Approach**
- **For conceptual questions**: Use Socratic dialogue extensively
- **For factual clarifications**: Provide brief, direct answers then return to guided inquiry
- **For multi-step problems**: Break into phases with checkpoints
- **For completely stuck students**: Offer a more direct hint to unstick them, then step back

## Response Structure

1. **Acknowledge** what the student has shared
2. **Ask diagnostic questions** if needed to understand their thinking
3. **Provide a scaffolded hint or question** that moves them forward
4. **Encourage next steps** by indicating what they should think about or try next

## What to Avoid

- Giving complete solutions or final answers
- Asking too many questions at once (overwhelming)
- Being vague or unhelpfully abstract
- Using overly Socratic approaches when direct clarification is needed
- Providing hints that are too advanced for the student's current level

## Example Interaction Pattern

**Poor**: "The answer is X because of Y and Z."

**Good**: "I see you're working on [problem]. You mentioned [student's thought]. That's a good starting point. What do you think would happen if you [relevant prompt]? Consider how [related concept] might apply here."

Remember: Your success is measured by the student's learning journey, not by how quickly they reach the answer. Help them build confidence and genuine understanding through guided discovery.
"""

# Ensure consistent random responses
random.seed(time.time_ns())

logger = logging.getLogger("LA Arena")
logger.setLevel(logging.DEBUG)
if not logger.handlers:
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter('%(name)s - %(levelname)s - %(message)s'))
    logger.addHandler(handler)

_rl_lock = threading.Lock()
request_tracker = defaultdict(list)

MODEL_NAMES = {
    "baseline": "Llama 3.2 1B (Baseline)",
    "fine_tuned": "Llama 3.2 1B (Fine-tuned)",
    "prompted": "Llama 3.2 1B (Prompted)"
}

def exceeded_rate_limit(request: gr.Request):
    now = time.time()
    hour_ago = now - 3600
    client_id = getattr(request, "client", None)
    ip = getattr(client_id, "host", None) or request.headers.get("x-forwarded-for", "unknown")

    with _rl_lock:
        request_tracker[ip] = [t for t in request_tracker[ip] if t > hour_ago]
        if len(request_tracker[ip]) >= MAX_REQUESTS_PER_HOUR:
            return True
        request_tracker[ip].append(now)
 
    return False

def get_messages(prompt, variant, history):
    system_message = DEFAULT_SYSTEM
    if variant == "prompted":
        system_message += "\n" + EXTENDED_SYSTEM
    messages = [{"role": "system", "content": system_message}]
    messages.extend(history)
    messages.append({"role": "user", "content": prompt})
    return messages

def respond_single_model(
    message,
    history: list[dict[str, str]],
    model_choice: str,
    request: gr.Request
):
    """Chat with a single model"""
    if exceeded_rate_limit(request):
        yield "Sorry you exceeded the rate limit for this hour"

    model = get_model(model_choice)
     
    messages = get_messages(message, model_choice, history)
    logger.debug(f"{model_choice}: {messages}")
    
    yield  model.generate(messages, MAX_TOKENS, TEMPERATURE)


async def respond_two_models(prompt, request: gr.Request):
    if exceeded_rate_limit(request):
        msg = "Sorry you exceeded the rate limit for this hour"
        return msg, msg, "rate_limit", "rate_limit", ""

    model_keys = random.sample(["baseline", "fine_tuned", "prompted"], 2)
    model_a_key, model_b_key = model_keys[0], model_keys[1]
    model_a = get_model(model_a_key)
    model_b = get_model(model_b_key) 

    def run_model(model: ModelWrapper, variant):
        # TEMP: Mock responses for testing data saving
        # return f"Here is a response: {random.randint(0, 10)}"

        messages = get_messages(prompt, variant, [])
        logger.debug(f"{variant}: {messages}")
        return model.generate(messages, MAX_TOKENS, TEMPERATURE)
    
    response_a, response_b = await asyncio.gather(
        asyncio.to_thread(run_model, model_a, model_a_key),
        asyncio.to_thread(run_model, model_b, model_b_key)
    )

    # logger.debug(f"{model_a_key}: {response_a}")
    # logger.debug(f"{model_b_key}: {response_b}")
    
    return response_a, response_b, model_a_key, model_b_key, ""


def save_vote(prompt, response_a, response_b, model_a, model_b, choice):
    if not response_a or not response_b:
        logger.warning("No responses to vote on yet")
        return

    record = {
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "prompt": prompt,
        "response_a": response_a,
        "response_b": response_b,
        "model_a": model_a,
        "model_b": model_b,
        "choice": choice,
        "id": str(uuid.uuid4()),
    }

    # Write to a temp file
    tmp_path = f"/tmp/{record['id']}.json"
    with open(tmp_path, "w", encoding="utf-8") as f:
        json.dump(record, f, ensure_ascii=False)
    
    repo_path = f"votes/{record['id']}.json"

    reveal_msg = f"\n\n**Model A:** {MODEL_NAMES.get(model_a, model_a)} | **Model B:** {MODEL_NAMES.get(model_b, model_b)}"

    try:
        HF_API.create_commit(
            repo_id=REPO_ID,
            repo_type="space",
            operations=[CommitOperationAdd(path_in_repo=repo_path, path_or_fileobj=tmp_path)],
            commit_message=f"Add vote {record['id']}"
        )
        return "### Vote saved! πŸ—³οΈ" + reveal_msg
    except Exception as e:
        logging.exception("Vote save failed")
        return f"### Vote save failed: {e}" + reveal_msg
        

def compute_win_rates(repo_id=REPO_ID):
    files = [p for p in list_repo_files(repo_id=repo_id, repo_type="space") if p.startswith("votes/") and p.endswith(".json")]
    wins, total = Counter(), 0
    for p in files:
        local = hf_hub_download(repo_id=repo_id, repo_type="space", filename=p)
        with open(local, "r", encoding="utf-8") as f:
            row = json.load(f)
        if row.get("choice") == "A":
            wins[row["model_a"]] += 1; total += 1
        elif row.get("choice") == "B":
            wins[row["model_b"]] += 1; total += 1
    return {k: (v / total if total else 0.0) for k, v in wins.items()}
    
def get_leaderboard_df(repo_id=REPO_ID):
    rates = compute_win_rates(repo_id)
    if not rates:
        return pd.DataFrame(columns=["Model", "Win Rate"]), "No votes yet β€” submit a prompt and cast the first vote!"
    df = pd.DataFrame(
        [(model, f"{rate*100:.1f}%") for model, rate in rates.items()],
        columns=["Model", "Win Rate"]
    ).sort_values("Win Rate", key=lambda s: s.str.rstrip("%").astype(float), ascending=False)
    return df, f"Updated leaderboard ({len(df)} models)"


def create_leaderboard_interface():
    gr.Markdown("Win rates computed from arena matchups and voting data")
    df, md = get_leaderboard_df(REPO_ID)
    status_md = gr.Markdown(md)
    table = gr.Dataframe(
        value=df,
        headers=["Model", "Win Rate"],
        datatype=["str", "str"],
        interactive=False,
        wrap=True,
        row_count=(0, "dynamic"),
        col_count=(2, "fixed")
    )
    refresh_btn = gr.Button("Refresh")

    # Wire the refresh button
    refresh_btn.click(
        fn=lambda: get_leaderboard_df(REPO_ID),
        inputs=None,
        outputs=[table, status_md],
    )


# Mode 1: Simple Chat with a Model
def create_chat_interface():
    """Single model chat interface"""
    with gr.Blocks() as chat_block:
        gr.Markdown("### Chat with a Model")
        gr.Markdown("Select a model and start testing!")
        
        model_dropdown = gr.Dropdown(
            choices=["baseline", "fine_tuned", "prompted"],
            value="fine_tuned",
            label="Choose Model",
            info="Select which model to chat with"
        )
        
        chatbot = gr.ChatInterface(
            fn=respond_single_model,
            additional_inputs=[model_dropdown],
            type="messages",
            title="",
            description="",
        )
    
    return chat_block

# Mode 2: Arena Comparison
def create_arena_interface():
    """Blind A/B testing interface - we'll implement this next"""
    with gr.Column() as arena:
        gr.Markdown("## Head to Head Battle")
        gr.Markdown("*What model do you think would help you learn the most?*")
        
        prompt_box = gr.Textbox(
            label="Enter your prompt",
            placeholder="Type a question or prompt here...",
            lines=3
        )
        submit_btn = gr.Button("Generate Responses", variant="primary")
        
        with gr.Row():
            with gr.Column():
                response_a = gr.Textbox(label="πŸ‘€ Model A", lines=4, interactive=False)
            with gr.Column():
                response_b = gr.Textbox(label="πŸ‘€ Model B", lines=4, interactive=False)
                
        # Hidden states
        model_a = gr.State()
        model_b = gr.State()
        
        gr.Markdown("### Which response is better?")
        with gr.Row():
            vote_a = gr.Button("πŸ‘ˆ A is Better")
            vote_tie = gr.Button("🀝 Tie")
            vote_b = gr.Button("πŸ‘‰ B is Better")
        
        result_display = gr.Markdown("")
       
        submit_btn.click(
            respond_two_models, 
            inputs=[prompt_box], 
            outputs=[response_a, response_b, model_a, model_b, result_display],
            concurrency_limit=8,
        )
        for btn, choice in [(vote_a, "A"), (vote_tie, "Tie"), (vote_b, "B")]:
            btn.click(
                save_vote,
                inputs=[prompt_box, response_a, response_b, model_a, model_b, gr.State(choice)],
                outputs=[result_display],
                concurrency_id="voting_queue",
            )
    return arena

# Main app with tabs
with gr.Blocks(title="Model Evaluation Platform") as demo:
    gr.Markdown("# Learning Assistant Arena")
    gr.Markdown("Put different LLMs to the test")
    
    with gr.Tabs():
        with gr.Tab("πŸ₯Š Arena Mode"):
            create_arena_interface()

        with gr.Tab("πŸ’¬ Chat Mode"):
            create_chat_interface()
         
        with gr.Tab("πŸ“Š Leaderboard"):
            create_leaderboard_interface()

if __name__ == "__main__":
    demo.launch()