File size: 14,660 Bytes
2d55544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1036c6
2d55544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d309ddf
2d55544
 
 
 
 
 
 
 
 
d1036c6
 
 
 
 
 
2d55544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1036c6
2d55544
d1036c6
 
 
 
 
 
2d55544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d309ddf
 
 
 
 
 
2d55544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""intellite 500M SFT β€” RLHF data collector served as a Gradio HuggingFace Space.

Every assistant reply gets πŸ‘ / πŸ‘Ž buttons. When the user rates a reply,
the (system, prior messages, response, liked) tuple is appended to a
local JSONL file, and a CommitScheduler pushes that folder to a dataset
repo on the Hub every 5 minutes.

Weights are downloaded at startup from the `ProCreations/intellite-500m-sft`
model repo (the Space itself is capped at 1 GB LFS so we can't bundle them).

Environment variables:
    INTELLITE_MODEL_REPO   model repo id (default: ProCreations/intellite-500m-sft)
    HF_TOKEN               HF access token with *write* scope on the dataset
                           repo (REQUIRED β€” set as a Space secret)
    FEEDBACK_REPO          dataset repo id (default: ProCreations/Intellite-storage)
"""

import json
import os
import sys
import threading
import time
import traceback
import uuid
from pathlib import Path

import gradio as gr
import spaces        # HF ZeroGPU β€” provides @spaces.GPU decorator + module-level CUDA emulation
import tiktoken
import torch
from huggingface_hub import CommitScheduler, hf_hub_download
from safetensors.torch import load_file as load_safetensors

SPACE_DIR = Path(__file__).resolve().parent
sys.path.insert(0, str(SPACE_DIR))

from config import ModelConfig
from model import IntelliteGPT

# ------------------------------------------------------------------------
# Paths & constants

MODEL_REPO = os.environ.get("INTELLITE_MODEL_REPO", "ProCreations/intellite-500m-sft")
FEEDBACK_DIR = SPACE_DIR / "user_feedback"
FEEDBACK_DIR.mkdir(exist_ok=True)
# Unique filename per replica/restart so concurrent Spaces don't clobber.
FEEDBACK_FILE = FEEDBACK_DIR / f"data_{uuid.uuid4().hex}.jsonl"

FEEDBACK_REPO = os.environ.get("FEEDBACK_REPO", "ProCreations/Intellite-storage")
HF_TOKEN = os.environ.get("HF_TOKEN")

DEFAULT_SYSTEM = ""   # empty by default β€” adding a system prompt empirically hurts this checkpoint's quality
SYSTEM_TAG = "<|system|>\n"
USER_TAG = "<|user|>\n"
ASST_TAG = "<|assistant|>\n"
STOP_MARKERS = ("<|user|>", "<|system|>")


# ------------------------------------------------------------------------
# Model load (once, at startup)

# ZeroGPU: place the model on "cuda" unconditionally at module-level. PyTorch
# CUDA emulation handles this without a real GPU; the real H200 slice is only
# allocated inside @spaces.GPU functions. Outside ZeroGPU (local dev) this
# falls back to CPU.
DEVICE = "cuda" if (torch.cuda.is_available() or os.environ.get("SPACES_ZERO_GPU")) else "cpu"
print(f"[sys] device={DEVICE}  model_repo={MODEL_REPO}  zerogpu={bool(os.environ.get('SPACES_ZERO_GPU'))}")

# Pull architecture + weights from the Hub. First call downloads (~30 s for
# 1 GB on cold start); subsequent calls hit HF's local cache.
print(f"[hub] downloading config.json + model.safetensors from {MODEL_REPO}")
config_path = hf_hub_download(repo_id=MODEL_REPO, filename="config.json")
weights_path = hf_hub_download(repo_id=MODEL_REPO, filename="model.safetensors")
with open(config_path) as f:
    hub_cfg = json.load(f)

# Build our intellite ModelConfig from whatever fields HF's config.json carries
# that match. Anything missing falls back to dataclass defaults.
_fields = ModelConfig.__dataclass_fields__.keys()
MCFG = ModelConfig(**{k: v for k, v in hub_cfg.items() if k in _fields})

MODEL = IntelliteGPT(MCFG).to(DEVICE)

# Load weights as bf16 (matches what's in safetensors). Keep model in bf16 to
# halve memory and roughly double CPU inference speed vs fp32.
state = load_safetensors(weights_path, device=str(DEVICE))
_wdtype = next(iter(state.values())).dtype
if _wdtype != torch.float32:
    MODEL = MODEL.to(_wdtype)

# `lm_head.weight` was deduped from safetensors (tied to tok_emb.weight).
# IntelliteGPT.__init__ already ties them to the same tensor, so a single
# load of tok_emb.weight populates both β€” strict=False allows the missing key.
missing, unexpected = MODEL.load_state_dict(state, strict=False)
if unexpected:
    print(f"[load] unexpected keys (ignored): {unexpected}")
if missing and missing != ["lm_head.weight"]:
    print(f"[load] missing keys: {missing}")
MODEL.eval()

TOKENS_SEEN = 0   # not stored in the safetensors-only repo format
BEST_VAL = float("nan")

ENC = tiktoken.get_encoding("gpt2")
EOT = ENC.eot_token
N_PARAMS = MODEL.num_params()
print(f"[model] {N_PARAMS/1e6:.1f}M params  tokens_seen={TOKENS_SEEN:,}  best_val={BEST_VAL:.4f}")


# ------------------------------------------------------------------------
# Hub sync β€” CommitScheduler pushes FEEDBACK_DIR to the dataset every 5 min.

if HF_TOKEN:
    scheduler = CommitScheduler(
        repo_id=FEEDBACK_REPO,
        repo_type="dataset",
        folder_path=FEEDBACK_DIR,
        path_in_repo="data",
        every=5,
        token=HF_TOKEN,
    )
    print(f"[hub] scheduler active β†’ {FEEDBACK_REPO} (every 5 min)")
else:
    scheduler = None
    print("[hub] HF_TOKEN not set β€” feedback will stay local only")


# ------------------------------------------------------------------------
# Prompt templating + generation (mirrors chat.py)

def render_prompt_ids(system: str, prior_messages: list[dict], user_msg: str) -> list[int]:
    """Encode the SFT chat template exactly as sft_prepare.py did."""
    ids: list[int] = []
    if system:
        ids.extend(ENC.encode_ordinary(SYSTEM_TAG + system.strip() + "\n"))
    pending_user = None
    for m in prior_messages:
        role = m.get("role")
        content = (m.get("content") or "").strip()
        if role == "user":
            pending_user = content
        elif role == "assistant" and pending_user is not None:
            ids.extend(ENC.encode_ordinary(USER_TAG + pending_user + "\n"))
            ids.extend(ENC.encode_ordinary(ASST_TAG))
            ids.extend(ENC.encode_ordinary(content))
            ids.append(EOT)
            pending_user = None
    ids.extend(ENC.encode_ordinary(USER_TAG + user_msg.strip() + "\n"))
    ids.extend(ENC.encode_ordinary(ASST_TAG))
    return ids


@torch.no_grad()
def stream_reply(prompt_ids, max_new, temperature, top_k, top_p, rep_penalty):
    """Yield the partial assistant reply after each new token."""
    x = torch.tensor([prompt_ids], dtype=torch.long, device=DEVICE)
    ctx = MCFG.seq_len
    start = len(prompt_ids)
    reply = ""

    for _ in range(max_new):
        xc = x[:, -ctx:]
        if DEVICE == "cuda":
            with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
                logits, _ = MODEL(xc)
        else:
            logits, _ = MODEL(xc)
        logits = logits[0, -1, :].float()

        if rep_penalty and rep_penalty != 1.0:
            seen = torch.unique(x[0])
            prev = logits[seen]
            logits[seen] = torch.where(prev > 0, prev / rep_penalty, prev * rep_penalty)

        logits = logits / max(temperature, 1e-5)

        if top_k and top_k > 0:
            k = min(int(top_k), logits.numel())
            v, _ = torch.topk(logits, k)
            logits[logits < v[-1]] = -float("inf")

        if top_p and 0.0 < top_p < 1.0:
            sorted_logits, sorted_idx = torch.sort(logits, descending=True)
            cum = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)
            mask = cum > top_p
            mask[1:] = mask[:-1].clone()
            mask[0] = False
            logits[sorted_idx[mask]] = -float("inf")

        probs = torch.softmax(logits, dim=-1)
        next_tok = torch.multinomial(probs, num_samples=1)
        tok_id = int(next_tok.item())
        x = torch.cat([x, next_tok.unsqueeze(0)], dim=1)

        if tok_id == EOT:
            break

        reply = ENC.decode(x[0, start:].tolist())

        while reply.endswith("\ufffd"):
            reply = reply[:-1]

        hit_stop = False
        for marker in STOP_MARKERS:
            idx = reply.find(marker)
            if idx != -1:
                reply = reply[:idx]
                hit_stop = True
                break
        if hit_stop:
            break

        yield reply.strip()

    yield reply.strip()


# ------------------------------------------------------------------------
# Feedback store β€” JSONL, append-only, synced to Hub by CommitScheduler.

_local_lock = threading.Lock()
_local_count = {"total": 0, "liked": 0}


def _count_jsonl_lines(path: Path) -> tuple[int, int]:
    total, liked = 0, 0
    if not path.exists():
        return 0, 0
    with path.open() as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            total += 1
            try:
                if json.loads(line).get("liked"):
                    liked += 1
            except json.JSONDecodeError:
                pass
    return total, liked


t, l = _count_jsonl_lines(FEEDBACK_FILE)
_local_count["total"], _local_count["liked"] = t, l


def _stats_str() -> str:
    t = _local_count["total"]
    l = _local_count["liked"]
    repo_link = f"[`{FEEDBACK_REPO}`](https://huggingface.co/datasets/{FEEDBACK_REPO})"
    sync = "synced every 5 min" if scheduler else "**HF_TOKEN missing β€” not syncing**"
    return (
        f"**{t}** records this session Β· πŸ‘ {l} Β· πŸ‘Ž {t - l}  \n"
        f"Pushed to {repo_link} ({sync})"
    )


def save_feedback(evt: gr.LikeData, history: list, system: str) -> str:
    """Handle a thumbs-up / thumbs-down click on a chat message."""
    if evt.liked is None:
        return "rating cleared (nothing saved)"

    idx = evt.index[0] if isinstance(evt.index, (list, tuple)) else evt.index
    if not isinstance(idx, int) or idx < 0 or idx >= len(history):
        return f"bad index {evt.index!r}"

    msg = history[idx]
    if msg.get("role") != "assistant":
        return "skipped non-assistant message"

    record = {
        "ts": time.strftime("%Y-%m-%dT%H:%M:%S"),
        "system": (system or DEFAULT_SYSTEM).strip(),
        "prompt_messages": history[:idx],
        "response": msg.get("content", ""),
        "liked": bool(evt.liked),
    }

    # Write under the scheduler's lock (or our own) so the background push
    # never sees a half-written line.
    lock = scheduler.lock if scheduler else _local_lock
    with lock:
        with FEEDBACK_FILE.open("a") as f:
            f.write(json.dumps(record, ensure_ascii=False) + "\n")
        _local_count["total"] += 1
        if record["liked"]:
            _local_count["liked"] += 1

    verdict = "πŸ‘ good" if evt.liked else "πŸ‘Ž bad"
    return f"saved {verdict} Β· {_local_count['total']} this session"


# ------------------------------------------------------------------------
# Chat callback

@spaces.GPU(duration=60)
def chat(user_msg, history, system, max_new, temperature, top_k, top_p, rep_penalty):
    """Stream a reply; yield updated chatbot history after each token.

    Decorated with @spaces.GPU so ZeroGPU allocates a half-H200 slice for
    the duration of the generator. 500M dense at ~80 tok/s on H200 means
    a max-length 800-token reply finishes in ~10 s β€” well under the 60 s cap.
    """
    user_msg = (user_msg or "").strip()
    if not user_msg:
        yield history, ""
        return

    history = list(history) + [
        {"role": "user", "content": user_msg},
        {"role": "assistant", "content": ""},
    ]
    prior = history[:-2]

    ids = render_prompt_ids(system or DEFAULT_SYSTEM, prior, user_msg)
    room = MCFG.seq_len - int(max_new)
    if len(ids) > room > 0:
        ids = ids[-room:]

    try:
        for partial in stream_reply(ids, int(max_new), float(temperature),
                                    int(top_k), float(top_p), float(rep_penalty)):
            history[-1]["content"] = partial
            yield history, ""
    except Exception:
        history[-1]["content"] = f"[error] {traceback.format_exc()}"
        yield history, ""


# ------------------------------------------------------------------------
# UI

with gr.Blocks(
    title="intellite 500M SFT β€” RLHF collector",
    theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"),
) as demo:
    gr.Markdown(
        f"# intellite 500M SFT β€” RLHF data collector\n"
        f"{MCFG.d_model}d Γ— {MCFG.n_layers}L Γ— {MCFG.n_heads}h "
        f"({N_PARAMS/1e6:.1f}M params, bf16) Β· "
        f"weights from [`{MODEL_REPO}`](https://huggingface.co/{MODEL_REPO}) Β· "
        f"device `{DEVICE}`  \n"
        f"**Please rate every response with πŸ‘ or πŸ‘Ž.** Ratings auto-sync to "
        f"[`{FEEDBACK_REPO}`](https://huggingface.co/datasets/{FEEDBACK_REPO}) "
        f"every 5 minutes for RLHF training."
    )

    with gr.Row():
        with gr.Column(scale=3):
            chatbot = gr.Chatbot(
                type="messages",
                height=520,
                show_copy_button=True,
                avatar_images=(None, None),
            )
            msg = gr.Textbox(
                placeholder="Your message β€” Enter to send",
                lines=2,
                show_label=False,
                autofocus=True,
            )
            with gr.Row():
                send_btn = gr.Button("Send", variant="primary")
                clear_btn = gr.Button("Clear chat")
            feedback_status = gr.Markdown("_rate replies with πŸ‘ / πŸ‘Ž_")

        with gr.Column(scale=1):
            system = gr.Textbox(
                value=DEFAULT_SYSTEM,
                label="System prompt (optional β€” leave blank for best quality)",
                placeholder="(none β€” model behaves better without one)",
                lines=3,
            )
            max_new = gr.Slider(16, 800, value=400, step=16, label="max new tokens")
            temp = gr.Slider(0.1, 1.5, value=0.7, step=0.05, label="temperature")
            top_k = gr.Slider(0, 200, value=40, step=1, label="top-k (0 = off)")
            top_p = gr.Slider(0.1, 1.0, value=0.7, step=0.05, label="top-p")
            rep = gr.Slider(1.0, 1.5, value=1.1, step=0.01, label="repetition penalty")

            gr.Markdown("### RLHF data")
            stats_md = gr.Markdown(_stats_str())

    send_btn.click(
        chat,
        inputs=[msg, chatbot, system, max_new, temp, top_k, top_p, rep],
        outputs=[chatbot, msg],
    )
    msg.submit(
        chat,
        inputs=[msg, chatbot, system, max_new, temp, top_k, top_p, rep],
        outputs=[chatbot, msg],
    )
    clear_btn.click(lambda: [], None, chatbot, queue=False)

    chatbot.like(
        save_feedback,
        inputs=[chatbot, system],
        outputs=[feedback_status],
    ).then(lambda: _stats_str(), None, stats_md, queue=False)


demo.queue()

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