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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
"""

Dashboard - home page of the Distractor Annotation Tool.



Streamlit multipage: this file is the entry point.

All other pages live in pages/.

"""

import uuid
from collections import Counter
from datetime import datetime
import re

import pandas as pd
import streamlit as st

from constants import AVAILABLE_CHAT_MODELS, DEFAULT_CHAT_MODEL, KNOWN_DOMAINS
from dataset_io import (
    get_annotations_repo,
    get_hf_token,
    get_lmstudio_base_url,
    get_lmstudio_default_model,
    get_seed_data_path,
    load_annotations,
    load_seed_data,
    save_export_file,
    save_annotations,
)
from llm_client import chat_completion
from ui import render_sidebar

# -- Page config (must be first Streamlit call) -----------------------------
st.set_page_config(
    page_title="Distractor Annotation Tool",
    page_icon="🎯",
    layout="wide",
    initial_sidebar_state="expanded",
)

annotator = render_sidebar("Dashboard")


def reset_new_entry_wizard() -> None:
    st.session_state["new_entry_step"] = 0
    st.session_state["new_entry_draft"] = {}
    st.session_state["new_entry_chat_messages"] = []


def _slugify_for_filename(text: str) -> str:
    value = (text or "").strip().lower().replace(" ", "_")
    value = re.sub(r"[^a-z0-9_-]", "", value)
    return value or "unknown"


st.session_state.setdefault("new_entry_step", 0)
st.session_state.setdefault("new_entry_draft", {})
st.session_state.setdefault("new_entry_chat_messages", [])
st.session_state.setdefault("finished_annotations", [])

# -- Header -----------------------------------------------------------------
st.title("🎯 Distractor Annotation Tool")
st.markdown(
    "**NLP - Keeping LLMs on Track in Task-Oriented Dialogue**  \n"
    "Collaborative annotation workspace for your research group."
)
st.divider()

# -- Load annotations --------------------------------------------------------
with st.spinner("Loading shared annotations..."):
    try:
        annotations = load_annotations()
    except Exception as exc:
        st.error(f"Error loading annotations: {exc}")
        annotations = []

# -- Summary metrics ---------------------------------------------------------
c1, c2, c3, c4, c5 = st.columns(5)
c1.metric("πŸ“ Total Entries", len(annotations))
c2.metric("βœ… Approved", sum(1 for a in annotations if a.get("_review_status") == "approved"))
c3.metric("πŸ“‹ Draft", sum(1 for a in annotations if a.get("_review_status") == "draft"))
c4.metric(
    "🎭 Distractors",
    sum(len(a.get("distractors_multiturn", [])) for a in annotations),
)
c5.metric(
    "πŸ‘₯ Annotators",
    len({a.get("_annotator", "?") for a in annotations} - {"seed_data", "?"}),
)

st.divider()

# -- Configuration status ----------------------------------------------------
st.subheader("βš™οΈ Configuration")
hf_token = get_hf_token()
ann_repo = get_annotations_repo()

cfg1, cfg2 = st.columns(2)
with cfg1:
    if hf_token:
        st.success("βœ… `HF_TOKEN` is set")
    else:
        st.error("❌ `HF_TOKEN` not set - add it to your Space secrets")
with cfg2:
    if ann_repo:
        st.success(f"βœ… Annotations repo: `{ann_repo}`")
    else:
        st.error("❌ `ANNOTATIONS_REPO_ID` not set - add it to your Space secrets")

st.divider()

# -- New entry + LLM chat assistant -----------------------------------------
st.subheader("🧩 New Entry Assistant (LLM)")
st.caption("Create a new entry draft step-by-step, then open a model-backed chat window.")

overview_left, overview_right = st.columns([3, 1])
with overview_left:
    st.caption("Use the wizard to build entries, then export all finished annotations.")
with overview_right:
    st.metric("πŸ“¦ Ready To Export", len(st.session_state.get("finished_annotations", [])))
    if st.button("πŸ“€ Export", use_container_width=True):
        finished = st.session_state.get("finished_annotations", [])
        if not finished:
            st.warning("No finished annotations to export yet.")
        elif not annotator:
            st.error("Enter your name in the sidebar first.")
        else:
            domains = {entry.get("domain", "") for entry in finished if entry.get("domain")}
            domain_name = next(iter(domains)) if len(domains) == 1 else "mixed"

            now = datetime.now()
            date_part = now.strftime("%d%m")
            time_part = now.strftime("%H%M")
            filename = (
                f"distractors_{_slugify_for_filename(annotator)}_"
                f"{_slugify_for_filename(domain_name)}_{date_part}_{time_part}.json"
            )

            if save_export_file(filename, finished):
                st.success(
                    f"βœ… Exported {len(finished)} annotations to `{filename}` "
                    "in your configured HF dataset repo."
                )
                st.session_state["finished_annotations"] = []
                st.rerun()

act1, act2 = st.columns([2, 1])
with act1:
    if st.button("βž• Start New Entry", use_container_width=True, type="primary"):
        if not annotator:
            st.error("Enter your name in the sidebar first.")
        else:
            st.session_state["new_entry_draft"] = {
                "_annotator": annotator,
                "_created_at": datetime.now().isoformat(),
                "_updated_at": datetime.now().isoformat(),
                "_review_status": "draft",
                "_needs_human_review": True,
                "_llm_test_results": [],
            }
            st.session_state["new_entry_chat_messages"] = []
            st.session_state["new_entry_step"] = 1
            st.rerun()
with act2:
    if st.session_state.get("new_entry_step", 0) > 0:
        if st.button("β†Ί Reset Wizard", use_container_width=True):
            reset_new_entry_wizard()
            st.rerun()

step = st.session_state.get("new_entry_step", 0)
draft = st.session_state.get("new_entry_draft", {})

if step == 0:
    st.info("Click **Start New Entry** to begin.")

if step == 1:
    st.markdown("**Step 1/3 - Domain and Scenario**")
    with st.form("new_entry_domain_scenario_form"):
        chosen_domain = st.selectbox(
            "Select domain",
            options=KNOWN_DOMAINS,
            index=(
                KNOWN_DOMAINS.index(draft.get("domain"))
                if draft.get("domain") in KNOWN_DOMAINS
                else 0
            ),
            key="new_entry_domain",
        )
        scenario_text = st.text_area(
            "Scenario description",
            value=draft.get("scenario", ""),
            placeholder="Describe the user scenario you want to annotate...",
            height=140,
            key="new_entry_scenario",
        )
        confirm_step_1 = st.form_submit_button("Confirm Domain + Scenario")

    if confirm_step_1:
        scenario_text = (scenario_text or "").strip()
        if not scenario_text:
            st.error("Scenario description cannot be empty.")
        else:
            draft["domain"] = chosen_domain
            draft["scenario"] = scenario_text
            draft["_updated_at"] = datetime.now().isoformat()
            st.session_state["new_entry_draft"] = draft
            st.session_state["new_entry_step"] = 2
            st.rerun()

if step == 2:
    st.markdown("**Step 2/3 - Model + System Instruction**")
    with st.form("new_entry_model_instruction_form"):
        default_local_model = get_lmstudio_default_model() or DEFAULT_CHAT_MODEL
        candidate_models = AVAILABLE_CHAT_MODELS or [default_local_model]
        if default_local_model not in candidate_models:
            candidate_models = [default_local_model, *candidate_models]
        selected_model = st.selectbox(
            "Choose a chat model",
            options=candidate_models,
            index=(
                candidate_models.index(draft.get("model"))
                if draft.get("model") in candidate_models
                else (
                    candidate_models.index(default_local_model)
                    if default_local_model in candidate_models
                    else 0
                )
            ),
            key="new_entry_model_dropdown",
        )

        system_instruction = st.text_area(
            "System instruction",
            value=draft.get(
                "system_instruction",
                "",
            ),
            height=170,
            key="new_entry_system_instruction",
        )
        confirm_step_2 = st.form_submit_button("Open Chat Window")

    if confirm_step_2:
        system_instruction = (system_instruction or "").strip()
        if not system_instruction:
            st.error("System instruction cannot be empty.")
        else:
            draft["model"] = selected_model
            draft["system_instruction"] = system_instruction
            draft["_updated_at"] = datetime.now().isoformat()
            st.session_state["new_entry_draft"] = draft
            st.session_state["new_entry_step"] = 3
            st.rerun()

if step == 3:
    st.markdown("**Step 3/3 - Chat Window**")
    st.caption(
        f"Annotator: {draft.get('_annotator', '?')} | Domain: {draft.get('domain', '?')} | "
        f"Model: {draft.get('model', get_lmstudio_default_model() or DEFAULT_CHAT_MODEL)}"
    )
    st.caption(f"LM Studio endpoint: `{get_lmstudio_base_url()}`")
    st.caption(f"Finished annotations pending export: **{len(st.session_state.get('finished_annotations', []))}**")
    st.write(f"**Scenario:** {draft.get('scenario', '')}")
    st.write(f"**System instruction:** {draft.get('system_instruction', '')}")

    chat_messages = st.session_state.get("new_entry_chat_messages", [])
    for msg in chat_messages:
        with st.chat_message(msg.get("role", "assistant")):
            st.write(msg.get("content", ""))

    prompt = st.chat_input(
        "Send a message to the selected model",
        key="new_entry_chat_input",
    )
    if prompt:
        chat_messages.append({"role": "user", "content": prompt})
        response = chat_completion(
            messages=chat_messages,
            system_prompt=draft.get("system_instruction") or None,
            model=draft.get("model", get_lmstudio_default_model() or DEFAULT_CHAT_MODEL),
            stream=False,
        )
        chat_messages.append({"role": "assistant", "content": str(response)})
        st.session_state["new_entry_chat_messages"] = chat_messages
        st.rerun()

    row1_col1, row1_col2 = st.columns(2)
    row2_col1, row2_col2 = st.columns(2)

    with row1_col1:
        if st.button("βœ… Finish", use_container_width=True):
            if not annotator:
                st.error("Enter your name in the sidebar first.")
            elif not chat_messages:
                st.error("Current annotation is empty. Send at least one chat message first.")
            else:
                conversation = [
                    {
                        "role": msg.get("role", ""),
                        "content": msg.get("content", ""),
                    }
                    for msg in chat_messages
                    if msg.get("role") in {"user", "assistant"} and msg.get("content")
                ]
                finished_entry = {
                    "domain": draft.get("domain", ""),
                    "scenario": draft.get("scenario", ""),
                    "system_instructions": draft.get("system_instruction", ""),
                    "conversation": conversation,
                }
                st.session_state["finished_annotations"] = [
                    *st.session_state.get("finished_annotations", []),
                    finished_entry,
                ]
                reset_new_entry_wizard()
                st.success("Annotation finished and queued for export.")
                st.rerun()

    with row1_col2:
        if st.button("βœ… Finish + Export", use_container_width=True):
            if not annotator:
                st.error("Enter your name in the sidebar first.")
            elif not chat_messages:
                st.error("Current annotation is empty. Send at least one chat message first.")
            else:
                conversation = [
                    {
                        "role": msg.get("role", ""),
                        "content": msg.get("content", ""),
                    }
                    for msg in chat_messages
                    if msg.get("role") in {"user", "assistant"} and msg.get("content")
                ]
                finished_entry = {
                    "domain": draft.get("domain", ""),
                    "scenario": draft.get("scenario", ""),
                    "system_instructions": draft.get("system_instruction", ""),
                    "conversation": conversation,
                }
                finished = [
                    *st.session_state.get("finished_annotations", []),
                    finished_entry,
                ]

                domains = {entry.get("domain", "") for entry in finished if entry.get("domain")}
                domain_name = next(iter(domains)) if len(domains) == 1 else "mixed"

                now = datetime.now()
                date_part = now.strftime("%d%m")
                time_part = now.strftime("%H%M")
                filename = (
                    f"distractors_{_slugify_for_filename(annotator)}_"
                    f"{_slugify_for_filename(domain_name)}_{date_part}_{time_part}.json"
                )

                if save_export_file(filename, finished):
                    st.success(
                        f"βœ… Exported {len(finished)} annotations to `{filename}` "
                        "in your configured HF dataset repo."
                    )
                    st.session_state["finished_annotations"] = []
                    reset_new_entry_wizard()
                    st.rerun()

    with row2_col1:
        if st.button("πŸšͺ Exit", use_container_width=True):
            reset_new_entry_wizard()
            st.info("Current in-progress annotation was discarded.")
            st.rerun()

    with row2_col2:
        if st.button("πŸ›‘ Exit + Export", use_container_width=True):
            finished = st.session_state.get("finished_annotations", [])
            if not finished:
                st.warning("No finished annotations to export yet.")
            elif not annotator:
                st.error("Enter your name in the sidebar first.")
            else:
                domains = {entry.get("domain", "") for entry in finished if entry.get("domain")}
                domain_name = next(iter(domains)) if len(domains) == 1 else "mixed"

                now = datetime.now()
                date_part = now.strftime("%d%m")
                time_part = now.strftime("%H%M")
                filename = (
                    f"distractors_{_slugify_for_filename(annotator)}_"
                    f"{_slugify_for_filename(domain_name)}_{date_part}_{time_part}.json"
                )

                if save_export_file(filename, finished):
                    st.success(
                        f"βœ… Exported {len(finished)} annotations to `{filename}` "
                        "in your configured HF dataset repo."
                    )
                    st.session_state["finished_annotations"] = []
                    reset_new_entry_wizard()
                    st.rerun()

st.divider()

# -- data import --------------------------------------------------------
st.subheader("πŸ“¦ Import Annotation Data")
seed = load_seed_data()
if seed:
    st.write(
        f"**{len(seed)}** entries are bundled as seed data "
    )
    if len(annotations) == 0:
        st.info("The shared repo is empty. Import the seed data to get started.")

    col_imp, col_exp, col_info = st.columns([1, 1, 2])
    with col_imp:
        if st.button("🌱 Import Data", use_container_width=True, type="primary"):
            if not annotator:
                st.error("Enter your name in the sidebar first.")
            elif len(annotations) > 0:
                st.warning(
                    "Shared repo already has entries. "
                    "Clear all annotations on the **πŸ‘₯ Annotations** page first if you want to re-import."
                )
            else:
                tagged = []
                for entry in seed:
                    ec = dict(entry)
                    ec["_id"] = ec.get("_id") or str(uuid.uuid4())
                    ec.setdefault("_annotator", "seed_data")
                    ec.setdefault("_created_at", datetime.now().isoformat())
                    ec["_updated_at"] = datetime.now().isoformat()
                    ec.setdefault("_review_status", "draft")
                    ec.setdefault("_needs_human_review", True)
                    ec.setdefault("_llm_test_results", [])
                    tagged.append(ec)
                if save_annotations(tagged):
                    st.success(f"βœ… Imported {len(tagged)} seed entries into the shared repo!")
                    st.rerun()
    with col_exp:
        if st.button("πŸ“€ Export Imported Data", use_container_width=True):
            if not annotator:
                st.error("Enter your name in the sidebar first.")
            elif not annotations:
                st.warning("No imported data found to export yet.")
            else:
                now = datetime.now()
                date_part = now.strftime("%d%m")
                time_part = now.strftime("%H%M")
                filename = (
                    f"import_export_{_slugify_for_filename(annotator)}_"
                    f"{date_part}_{time_part}.json"
                )

                if save_export_file(filename, annotations):
                    st.success(
                        f"βœ… Exported {len(annotations)} imported entries to `{filename}` "
                        "in your configured HF dataset repo."
                    )
    with col_info:
        st.caption(
            "Seed data was generated with Gemma-4-E2B running locally via LM Studio, "
            "then reviewed and partially rewritten by your groupmate."
        )
else:
    expected_paths = [
        "seed_data/draft_distractors.json",
        "data/draft_distractors.json",
    ]
    st.warning(
        "Seed data file not found. Add `draft_distractors.json` to one of these locations: "
        + ", ".join(f"`{path}`" for path in expected_paths)
    )
    resolved_seed_path = get_seed_data_path()
    if resolved_seed_path is not None:
        st.caption(f"Detected seed data path: `{resolved_seed_path}`")

st.divider()

# -- Two-column section ------------------------------------------------------
col_left, col_right = st.columns(2)

with col_left:
    st.subheader("πŸ“Š Annotator Progress")
    if annotations:
        stats: dict[str, dict] = {}
        for a in annotations:
            ann = a.get("_annotator", "Unknown")
            stats.setdefault(ann, {"Entries": 0, "Distractors": 0, "Approved": 0})
            stats[ann]["Entries"] += 1
            stats[ann]["Distractors"] += len(a.get("distractors_multiturn", []))
            if a.get("_review_status") == "approved":
                stats[ann]["Approved"] += 1
        df = pd.DataFrame(
            [{"Annotator": k, **v} for k, v in stats.items()]
        ).sort_values("Entries", ascending=False)
        st.dataframe(df, use_container_width=True, hide_index=True)
    else:
        st.info("No annotations yet - import seed data below or go to **Annotate**.")

with col_right:
    st.subheader("πŸ“‹ Status & Domain Breakdown")
    if annotations:
        statuses = Counter(a.get("_review_status", "draft") for a in annotations)
        df_s = pd.DataFrame([{"Status": k, "Count": v} for k, v in statuses.most_common()])
        st.dataframe(df_s, use_container_width=True, hide_index=True)

        st.markdown("**By domain:**")
        domains = Counter(a.get("domain", "unknown") for a in annotations)
        for dom, cnt in domains.most_common():
            st.write(f"  - {dom}: **{cnt}**")
    else:
        st.info("No data yet.")

st.divider()

# -- Recent activity ---------------------------------------------------------
if annotations:
    st.subheader("πŸ•’ Recent Activity")
    recent = sorted(
        annotations,
        key=lambda x: x.get("_updated_at", ""),
        reverse=True,
    )[:8]
    for entry in recent:
        scenario_preview = entry.get("scenario", "Unknown")[:70]
        status_emoji = {"approved": "βœ…", "draft": "πŸ“‹", "failed": "❌"}.get(
            entry.get("_review_status", "draft"), "❓"
        )
        with st.expander(
            f"{status_emoji} [{entry.get('domain', '?')}] {scenario_preview}"
        ):
            rc1, rc2, rc3 = st.columns(3)
            rc1.write(f"**Annotator:** {entry.get('_annotator', '?')}")
            rc2.write(f"**Status:** {entry.get('_review_status', 'draft')}")
            rc3.write(f"**Distractors:** {len(entry.get('distractors_multiturn', []))}")
            updated = entry.get("_updated_at", "")[:16].replace("T", " ")
            st.caption(f"Last updated: {updated}")