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
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
import streamlit as st
import time
import random
from datetime import datetime
from collections import Counter
import re

# ── Page config ────────────────────────────────────────────────────────────────
st.set_page_config(
    page_title="NanoChat Β· LLM Playground",
    page_icon="⚑",
    layout="wide",
    initial_sidebar_state="expanded",
)

# ── Custom CSS ─────────────────────────────────────────────────────────────────
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=Syne:wght@400;600;800&display=swap');

html, body, [class*="css"] {
    font-family: 'Syne', sans-serif;
}

/* Dark industrial background */
.stApp {
    background: #0d0d0f;
    color: #e8e4dc;
}

/* Sidebar */
section[data-testid="stSidebar"] {
    background: #111114 !important;
    border-right: 1px solid #2a2a30;
}

/* Headers */
h1, h2, h3 {
    font-family: 'Syne', sans-serif !important;
    font-weight: 800 !important;
    letter-spacing: -0.03em;
}

/* Chat messages */
.chat-msg {
    padding: 14px 18px;
    border-radius: 4px;
    margin: 8px 0;
    font-family: 'Space Mono', monospace;
    font-size: 0.85rem;
    line-height: 1.7;
    border-left: 3px solid transparent;
}
.chat-msg.user {
    background: #1a1a1f;
    border-left-color: #f0c040;
    color: #e8e4dc;
}
.chat-msg.assistant {
    background: #141418;
    border-left-color: #4af0a0;
    color: #c8f0dc;
}
.chat-msg .role-label {
    font-size: 0.65rem;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    opacity: 0.5;
    margin-bottom: 6px;
}

/* Metric cards */
.metric-card {
    background: #111114;
    border: 1px solid #2a2a30;
    border-radius: 4px;
    padding: 16px 20px;
    margin: 6px 0;
}
.metric-value {
    font-family: 'Space Mono', monospace;
    font-size: 2rem;
    font-weight: 700;
    color: #f0c040;
    line-height: 1;
}
.metric-label {
    font-size: 0.7rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    opacity: 0.45;
    margin-top: 4px;
}

/* Input box override */
.stTextInput > div > div > input, .stTextArea textarea {
    background: #111114 !important;
    color: #e8e4dc !important;
    border: 1px solid #2a2a30 !important;
    font-family: 'Space Mono', monospace !important;
    font-size: 0.85rem !important;
}
.stTextInput > div > div > input:focus, .stTextArea textarea:focus {
    border-color: #f0c040 !important;
    box-shadow: 0 0 0 2px rgba(240,192,64,0.15) !important;
}

/* Buttons */
.stButton > button {
    background: #f0c040 !important;
    color: #0d0d0f !important;
    font-family: 'Syne', sans-serif !important;
    font-weight: 700 !important;
    font-size: 0.8rem !important;
    letter-spacing: 0.08em !important;
    text-transform: uppercase !important;
    border: none !important;
    border-radius: 2px !important;
    padding: 10px 24px !important;
}
.stButton > button:hover {
    background: #ffd760 !important;
}

/* Selectbox */
.stSelectbox > div > div {
    background: #111114 !important;
    border: 1px solid #2a2a30 !important;
    color: #e8e4dc !important;
}

/* Tabs */
.stTabs [data-baseweb="tab-list"] {
    background: transparent;
    border-bottom: 1px solid #2a2a30;
    gap: 0;
}
.stTabs [data-baseweb="tab"] {
    font-family: 'Syne', sans-serif !important;
    font-weight: 600 !important;
    font-size: 0.78rem !important;
    letter-spacing: 0.1em !important;
    text-transform: uppercase !important;
    color: #888 !important;
    background: transparent !important;
    border: none !important;
    padding: 10px 24px !important;
}
.stTabs [aria-selected="true"] {
    color: #f0c040 !important;
    border-bottom: 2px solid #f0c040 !important;
}

/* Divider */
hr {
    border-color: #2a2a30 !important;
}

/* Spinner text */
.stSpinner > div {
    color: #4af0a0 !important;
}

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: #0d0d0f; }
::-webkit-scrollbar-thumb { background: #2a2a30; border-radius: 2px; }

/* Word freq bars */
.word-bar-container { margin: 4px 0; }
.word-bar-label {
    font-family: 'Space Mono', monospace;
    font-size: 0.72rem;
    color: #888;
    display: flex;
    justify-content: space-between;
    margin-bottom: 2px;
}
.word-bar {
    height: 6px;
    background: linear-gradient(90deg, #4af0a0, #f0c040);
    border-radius: 1px;
}
</style>
""", unsafe_allow_html=True)

# ── Session state ──────────────────────────────────────────────────────────────
if "messages" not in st.session_state:
    st.session_state.messages = []
if "model_loaded" not in st.session_state:
    st.session_state.model_loaded = False
if "pipeline" not in st.session_state:
    st.session_state.pipeline = None
if "total_tokens" not in st.session_state:
    st.session_state.total_tokens = 0
if "response_times" not in st.session_state:
    st.session_state.response_times = []
if "turn_count" not in st.session_state:
    st.session_state.turn_count = 0

# ── Model loader ───────────────────────────────────────────────────────────────
@st.cache_resource(show_spinner=False)
def load_model(model_id: str):
    from transformers import pipeline as hf_pipeline
    pipe = hf_pipeline(
        "text-generation",
        model=model_id,
        device_map="auto",
        trust_remote_code=True,
    )
    return pipe

# ── Helpers ────────────────────────────────────────────────────────────────────
MODEL_OPTIONS = {
    "SmolLM2-135M-Instruct (HF)": "HuggingFaceTB/SmolLM2-135M-Instruct",
    "SmolLM2-360M-Instruct (HF)": "HuggingFaceTB/SmolLM2-360M-Instruct",
    "TinyLlama-1.1B-Chat": "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
    "Qwen2.5-0.5B-Instruct": "Qwen/Qwen2.5-0.5B-Instruct",
}

def count_tokens_approx(text: str) -> int:
    return max(1, len(text.split()) * 4 // 3)

def get_word_freq(messages, top_n=10):
    all_text = " ".join(m["content"] for m in messages).lower()
    words = re.findall(r"\b[a-z]{4,}\b", all_text)
    stopwords = {"that","this","with","from","have","will","been","they",
                 "what","when","your","just","more","also","some","than",
                 "then","there","their","these","those","about","which","would"}
    words = [w for w in words if w not in stopwords]
    return Counter(words).most_common(top_n)

def format_chat_history(messages, model_id: str):
    """Build a prompt string compatible with most instruct models."""
    if "SmolLM2" in model_id or "Qwen" in model_id:
        # ChatML format
        prompt = ""
        for m in messages:
            role = m["role"]
            content = m["content"]
            prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
        prompt += "<|im_start|>assistant\n"
    else:
        # TinyLlama / Llama-2 chat format
        prompt = "<s>"
        for m in messages:
            if m["role"] == "user":
                prompt += f"[INST] {m['content']} [/INST]"
            else:
                prompt += f" {m['content']} </s><s>"
    return prompt

def generate_response(pipe, messages, model_id, max_new_tokens, temperature):
    prompt = format_chat_history(messages, model_id)
    t0 = time.time()
    out = pipe(
        prompt,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
        do_sample=temperature > 0,
        pad_token_id=pipe.tokenizer.eos_token_id,
        return_full_text=False,
    )
    elapsed = time.time() - t0
    text = out[0]["generated_text"].strip()
    # Strip any trailing special tokens
    for tok in ["<|im_end|>", "</s>", "[INST]"]:
        text = text.split(tok)[0].strip()
    return text, elapsed

# ── Sidebar ────────────────────────────────────────────────────────────────────
with st.sidebar:
    st.markdown("## ⚑ NanoChat")
    st.markdown("<p style='font-size:0.75rem;color:#666;font-family:Space Mono,monospace;margin-top:-8px'>Open-weight LLM Playground</p>", unsafe_allow_html=True)
    st.divider()

    selected_label = st.selectbox("Model", list(MODEL_OPTIONS.keys()))
    model_id = MODEL_OPTIONS[selected_label]

    max_new_tokens = st.slider("Max new tokens", 32, 512, 200, 16)
    temperature = st.slider("Temperature", 0.0, 1.5, 0.7, 0.05)

    st.divider()

    if st.button("⚑  Load / Reload Model"):
        with st.spinner(f"Loading {selected_label}…"):
            try:
                st.session_state.pipeline = load_model(model_id)
                st.session_state.model_loaded = True
                st.success("Model ready!")
            except Exception as e:
                st.error(f"Error: {e}")

    if st.button("πŸ—‘  Clear Chat"):
        st.session_state.messages = []
        st.session_state.total_tokens = 0
        st.session_state.response_times = []
        st.session_state.turn_count = 0
        st.rerun()

    st.divider()
    st.markdown(f"""
    <div style='font-family:Space Mono,monospace;font-size:0.68rem;color:#555;line-height:2'>
    Model ID<br>
    <span style='color:#f0c040'>{model_id.split("/")[-1]}</span><br><br>
    Status<br>
    <span style='color:{"#4af0a0" if st.session_state.model_loaded else "#f06060"}'>
    {"● Loaded" if st.session_state.model_loaded else "β—‹ Not loaded"}
    </span>
    </div>
    """, unsafe_allow_html=True)

# ── Main area ──────────────────────────────────────────────────────────────────
tab_chat, tab_viz = st.tabs(["πŸ’¬  Chat", "πŸ“Š  Analytics"])

# ─── Chat tab ──────────────────────────────────────────────────────────────────
with tab_chat:
    st.markdown("<h1 style='margin-bottom:2px'>Chat</h1>", unsafe_allow_html=True)
    st.markdown(f"<p style='font-size:0.78rem;color:#555;font-family:Space Mono,monospace;margin-bottom:24px'>{model_id}</p>", unsafe_allow_html=True)

    if not st.session_state.model_loaded:
        st.info("πŸ‘ˆ  Load a model from the sidebar to begin.")
    else:
        # Render history
        chat_container = st.container()
        with chat_container:
            for msg in st.session_state.messages:
                role_label = "YOU" if msg["role"] == "user" else "AI"
                css_class = "user" if msg["role"] == "user" else "assistant"
                st.markdown(f"""
                <div class='chat-msg {css_class}'>
                  <div class='role-label'>{role_label}</div>
                  {msg['content']}
                </div>
                """, unsafe_allow_html=True)

        # Input
        with st.form("chat_form", clear_on_submit=True):
            cols = st.columns([8, 1])
            with cols[0]:
                user_input = st.text_area("Message", height=80, label_visibility="collapsed",
                                          placeholder="Type a message and press Send…")
            with cols[1]:
                submitted = st.form_submit_button("Send", use_container_width=True)

        if submitted and user_input.strip():
            st.session_state.messages.append({"role": "user", "content": user_input.strip()})
            st.session_state.total_tokens += count_tokens_approx(user_input)

            with st.spinner("Thinking…"):
                try:
                    reply, elapsed = generate_response(
                        st.session_state.pipeline,
                        st.session_state.messages,
                        model_id,
                        max_new_tokens,
                        temperature,
                    )
                    st.session_state.messages.append({"role": "assistant", "content": reply})
                    st.session_state.total_tokens += count_tokens_approx(reply)
                    st.session_state.response_times.append(round(elapsed, 2))
                    st.session_state.turn_count += 1
                except Exception as e:
                    st.error(f"Generation error: {e}")
            st.rerun()

# ─── Analytics tab ─────────────────────────────────────────────────────────────
with tab_viz:
    st.markdown("<h1 style='margin-bottom:2px'>Analytics</h1>", unsafe_allow_html=True)
    st.markdown("<p style='font-size:0.78rem;color:#555;font-family:Space Mono,monospace;margin-bottom:24px'>Session insights</p>", unsafe_allow_html=True)

    msgs = st.session_state.messages
    rt   = st.session_state.response_times

    # ── Metrics row ────────────────────────────────────────────────────────────
    c1, c2, c3, c4 = st.columns(4)
    with c1:
        st.markdown(f"""
        <div class='metric-card'>
          <div class='metric-value'>{st.session_state.turn_count}</div>
          <div class='metric-label'>Turns</div>
        </div>""", unsafe_allow_html=True)
    with c2:
        st.markdown(f"""
        <div class='metric-card'>
          <div class='metric-value'>{st.session_state.total_tokens}</div>
          <div class='metric-label'>Est. Tokens</div>
        </div>""", unsafe_allow_html=True)
    with c3:
        avg_rt = round(sum(rt)/len(rt), 2) if rt else 0.0
        st.markdown(f"""
        <div class='metric-card'>
          <div class='metric-value'>{avg_rt}s</div>
          <div class='metric-label'>Avg Response</div>
        </div>""", unsafe_allow_html=True)
    with c4:
        user_msgs = [m for m in msgs if m["role"]=="user"]
        avg_len = round(sum(len(m["content"].split()) for m in user_msgs)/len(user_msgs)) if user_msgs else 0
        st.markdown(f"""
        <div class='metric-card'>
          <div class='metric-value'>{avg_len}</div>
          <div class='metric-label'>Avg User Words</div>
        </div>""", unsafe_allow_html=True)

    st.divider()

    col_left, col_right = st.columns(2)

    # ── Response time chart ────────────────────────────────────────────────────
    with col_left:
        st.markdown("#### Response times (s)")
        if rt:
            import pandas as pd
            df_rt = pd.DataFrame({"Turn": list(range(1, len(rt)+1)), "Seconds": rt})
            st.line_chart(df_rt.set_index("Turn"), color="#4af0a0", height=200)
        else:
            st.caption("No data yet β€” start chatting!")

    # ── Message length chart ───────────────────────────────────────────────────
    with col_right:
        st.markdown("#### Message lengths (words)")
        if msgs:
            import pandas as pd
            rows = []
            u_idx = a_idx = 1
            for m in msgs:
                wc = len(m["content"].split())
                if m["role"] == "user":
                    rows.append({"idx": u_idx, "role": "User", "words": wc})
                    u_idx += 1
                else:
                    rows.append({"idx": a_idx, "role": "AI", "words": wc})
                    a_idx += 1
            import pandas as pd
            df_ml = pd.DataFrame(rows)
            st.bar_chart(df_ml.pivot_table(index="idx", columns="role", values="words", aggfunc="sum").fillna(0),
                         color=["#f0c040", "#4af0a0"], height=200)
        else:
            st.caption("No data yet β€” start chatting!")

    st.divider()

    # ── Word frequency ──────────────────────────────────────────────────────────
    st.markdown("#### Top words across conversation")
    if msgs:
        freq = get_word_freq(msgs, top_n=12)
        if freq:
            max_count = freq[0][1]
            for word, count in freq:
                pct = int((count / max_count) * 100)
                st.markdown(f"""
                <div class='word-bar-container'>
                  <div class='word-bar-label'><span>{word}</span><span>{count}</span></div>
                  <div class='word-bar' style='width:{pct}%'></div>
                </div>""", unsafe_allow_html=True)
    else:
        st.caption("No data yet β€” start chatting!")

    st.divider()

    # ── Role distribution ────────────────────────────────────────────────────
    st.markdown("#### Message distribution")
    if msgs:
        u_count = sum(1 for m in msgs if m["role"]=="user")
        a_count = sum(1 for m in msgs if m["role"]=="assistant")
        total   = u_count + a_count
        u_pct   = int(u_count/total*100)
        a_pct   = 100 - u_pct
        st.markdown(f"""
        <div style='display:flex;gap:0;border-radius:3px;overflow:hidden;height:28px;margin:8px 0'>
          <div style='width:{u_pct}%;background:#f0c040;display:flex;align-items:center;
                      justify-content:center;font-family:Space Mono,monospace;
                      font-size:0.7rem;color:#0d0d0f;font-weight:700'>
            USER {u_pct}%
          </div>
          <div style='width:{a_pct}%;background:#4af0a0;display:flex;align-items:center;
                      justify-content:center;font-family:Space Mono,monospace;
                      font-size:0.7rem;color:#0d0d0f;font-weight:700'>
            AI {a_pct}%
          </div>
        </div>
        """, unsafe_allow_html=True)
    else:
        st.caption("No data yet.")