File size: 24,226 Bytes
9830017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/usr/bin/env python3
"""
Interactive Embedding Inversion Demo Server - Multi-Model Support.
Runs on port 8080. Serves a TurboPuffer-style dark UI for real-time diffusion visualization.

Supports both Qwen3-Embedding and EmbeddingGemma models.
"""

import sys
import os
import pickle
import yaml
import json
import math
import random
import asyncio
import time
from pathlib import Path

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import AutoModel, AutoTokenizer
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse
from fastapi.responses import HTMLResponse, StreamingResponse, FileResponse
from pydantic import BaseModel
from typing import List, Optional

# ---------------------------------------------------------------------------
# Model architecture (matches checkpoint state_dict exactly)
# ---------------------------------------------------------------------------

class AdaLN(nn.Module):
    """Adaptive LayerNorm: norm(x) * (1+scale) + shift, conditioned on cond."""
    def __init__(self, hidden_dim, cond_dim):
        super().__init__()
        self.norm = nn.LayerNorm(hidden_dim, elementwise_affine=False)
        self.proj = nn.Linear(cond_dim, 2 * hidden_dim)

    def forward(self, x, cond):
        # cond: [B, cond_dim] -> [B, 2*hidden]
        params = self.proj(cond).unsqueeze(1)  # [B, 1, 2*hidden]
        scale, shift = params.chunk(2, dim=-1)
        return self.norm(x) * (1 + scale) + shift


class Block(nn.Module):
    """Transformer block with AdaLN conditioning."""
    def __init__(self, hidden_dim, num_heads, ff_dim, dropout=0.0):
        super().__init__()
        self.adaln1 = AdaLN(hidden_dim, hidden_dim)
        self.attn = nn.MultiheadAttention(hidden_dim, num_heads, dropout=dropout, batch_first=True)
        self.adaln2 = AdaLN(hidden_dim, hidden_dim)
        self.ff = nn.Sequential(
            nn.Linear(hidden_dim, ff_dim),
            nn.GELU(),
            nn.Dropout(dropout),
            nn.Linear(ff_dim, hidden_dim),
        )

    def forward(self, x, cond):
        # Self-attention with AdaLN
        normed = self.adaln1(x, cond)
        attn_out, _ = self.attn(normed, normed, normed, need_weights=False)
        x = x + attn_out
        # Feed-forward with AdaLN
        normed = self.adaln2(x, cond)
        x = x + self.ff(normed)
        return x


class ConditionalMDLM(nn.Module):
    """Conditional Masked Diffusion Language Model - matches checkpoint exactly."""
    def __init__(self, config):
        super().__init__()
        mc = config["model"]
        self.vocab_size = mc["vocab_size"]
        self.hidden_dim = mc["hidden_dim"]
        self.max_seq_len = mc["max_seq_len"]
        self.mask_token_id = mc["mask_token_id"]

        self.token_embed = nn.Embedding(self.vocab_size, self.hidden_dim)
        self.pos_embed = nn.Embedding(self.max_seq_len, self.hidden_dim)
        self.cond_proj = nn.Sequential(
            nn.Linear(mc["embedding_cond_dim"], self.hidden_dim),
            nn.GELU(),
            nn.Linear(self.hidden_dim, self.hidden_dim),
        )
        self.blocks = nn.ModuleList([
            Block(self.hidden_dim, mc["num_heads"], mc["ff_dim"], mc.get("dropout", 0.0))
            for _ in range(mc["num_layers"])
        ])
        self.final_norm = AdaLN(self.hidden_dim, self.hidden_dim)
        self.output_proj = nn.Linear(self.hidden_dim, self.vocab_size, bias=False)

    def forward(self, input_ids, cond_embedding, padding_mask=None):
        B, L = input_ids.shape
        positions = torch.arange(L, device=input_ids.device).unsqueeze(0)
        x = self.token_embed(input_ids) + self.pos_embed(positions)
        cond = self.cond_proj(cond_embedding)
        for block in self.blocks:
            x = block(x, cond)
        x = self.final_norm(x, cond)
        return self.output_proj(x)


# ---------------------------------------------------------------------------
# Encoder helper
# ---------------------------------------------------------------------------

def last_token_pool(hidden, attention_mask):
    """Pool using last non-padding token (Qwen3-Embedding style)."""
    left_padding = attention_mask[:, -1].sum() == attention_mask.shape[0]
    if left_padding:
        return hidden[:, -1]
    seq_lens = attention_mask.sum(dim=1) - 1
    return hidden[torch.arange(hidden.shape[0], device=hidden.device), seq_lens]


def mean_pool(hidden, attention_mask):
    m = attention_mask.unsqueeze(-1).expand(hidden.size()).float()
    return (hidden * m).sum(1) / m.sum(1).clamp(min=1e-9)

def get_pool_fn(model_name):
    if "qwen" in model_name.lower():
        return last_token_pool
    return mean_pool

# ---------------------------------------------------------------------------
# Globals - Multi-model support + Concurrency Control
# ---------------------------------------------------------------------------

app = FastAPI()

# CORS: only allow our domain
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://embedding-inversion-demo.jina.ai"],
    allow_methods=["GET", "POST"],
    allow_headers=["Content-Type"],
)

ALLOWED_ORIGINS = {
    "https://embedding-inversion-demo.jina.ai",
    "http://localhost:8080",
    "http://127.0.0.1:8080",
}

@app.middleware("http")
async def check_browser_request(request: Request, call_next):
    # Skip health/queue checks
    if request.url.path in ("/health", "/queue", "/", "/favicon.ico"):
        return await call_next(request)
    
    # Check origin or referer
    origin = request.headers.get("origin", "")
    referer = request.headers.get("referer", "")
    
    origin_ok = any(origin.startswith(o) for o in ALLOWED_ORIGINS) if origin else False
    referer_ok = any(referer.startswith(o) for o in ALLOWED_ORIGINS) if referer else False
    
    # Also allow requests with no origin/referer (same-origin page load)
    is_page_load = not origin and not referer and request.method == "GET"
    
    if not origin_ok and not referer_ok and not is_page_load:
        return JSONResponse(
            status_code=403,
            content={"error": "API access not allowed. Use the web interface."}
        )
    
    return await call_next(request)

# Each model has its own: MODEL, CONFIG, ENCODER_MODEL, ENCODER_TOK, DECODER_TOK
MODELS = {}  # model_key -> dict with model, config, encoder_model, encoder_tok, decoder_tok
DEVICE = None

# Concurrency control
ENCODE_SEM = asyncio.Semaphore(8)   # encode is fast, allow 4 concurrent
DECODE_SEM = asyncio.Semaphore(6)   # decode has 32 steps, allow 3 concurrent
ACTIVE_COUNT = 0
WAITING_COUNT = 0
count_lock = asyncio.Lock()

# Model configurations
MODEL_CONFIGS = {
    "qwen3": {
        "checkpoint_path": str(Path.home() / "checkpoints" / "qwen3_best.pt"),
        "config_path": "configs/v2_qwen3.yaml",
    },
    "gemma": {
        "checkpoint_path": str(Path.home() / "checkpoints" / "gemma_best.pt"),
        "config_path": "configs/v2_gemma.yaml",
    },
}

SAMPLE_SENTENCES_QWEN3_EASY = [
    "The coldest winter I ever spent was a summer in San Francisco, said Mark Twain",
    "Napoleon marched his Grand Army from Paris to Moscow in the winter of 1812",
    "The Great Wall of China stretches over 13,000 miles from Dandong to Lop Lake",
    "Albert Einstein developed the theory of relativity while working in Bern, Switzerland",
    "The Titanic sank in the North Atlantic Ocean after hitting an iceberg in April 1912",
    "Neil Armstrong landed on the Moon at the Sea of Tranquility on July 20, 1969",
    "The Berlin Wall fell on November 9, 1989, reuniting East and West Germany",
    "Amazon was founded by Jeff Bezos in his garage in Bellevue, Washington in 1994",
    "The Eiffel Tower in Paris was built by Gustave Eiffel for the 1889 World Fair",
    "Marco Polo traveled from Venice to China along the Silk Road in the 13th century",
    "The Panama Canal connects the Atlantic Ocean to the Pacific Ocean across Panama",
    "Steve Jobs unveiled the first iPhone at Moscone Center in San Francisco in 2007",
    "Mount Everest stands at 8,849 meters on the border between Nepal and Tibet",
    "The Treaty of Versailles was signed near Paris, France on June 28, 1919",
    "SpaceX launched its first Falcon 9 rocket from Cape Canaveral, Florida in 2010",
]

SAMPLE_SENTENCES_QWEN3_HARD = [
    "is this a pigeon? no it is a transformer model",
    "i asked chatgpt to write my resignation letter and it was too polite",
    "my embeddings are not aligned and neither is my sleep schedule",
    "sir this is a vector database not a therapy session",
    "instructions unclear, model started generating poetry",
    "me: i will go to bed early. also me at 3am: reading arxiv papers",
    "nobody: absolutely nobody: AI twitter: we need to talk about scaling laws",
    "OpenAI announces GPT-5 while researchers debate if benchmarks even matter",
    "NVIDIA stock hits new high as demand for H100 GPUs continues to outpace supply",
    "Google DeepMind achieves breakthrough in protein structure prediction",
    "the mitochondria is the powerhouse of the cell and I still remember that",
    "according to all known laws of aviation a bee should not be able to fly",
    "you miss 100 percent of the shots you do not take says Wayne Gretzky",
    "404 meaning of life not found try again after coffee",
    "rm -rf is not a valid debugging strategy no matter what stackoverflow says",
]

SAMPLE_SENTENCES_GEMMA_EASY = [
    "The coldest winter I ever spent was a summer in San Francisco, said Mark Twain",
    "Napoleon marched his Grand Army from Paris to Moscow in the winter of 1812",
    "The Great Wall of China stretches over 13,000 miles from Dandong to Lop Lake",
    "Albert Einstein developed the theory of relativity while working in Bern, Switzerland",
    "The Titanic sank in the North Atlantic Ocean after hitting an iceberg in April 1912",
    "Neil Armstrong landed on the Moon at the Sea of Tranquility on July 20, 1969",
    "Amazon was founded by Jeff Bezos in his garage in Bellevue, Washington in 1994",
    "The Eiffel Tower in Paris was built by Gustave Eiffel for the 1889 World Fair",
    "Marco Polo traveled from Venice to China along the Silk Road in the 13th century",
    "Mount Everest stands at 8,849 meters on the border between Nepal and Tibet",
]

SAMPLE_SENTENCES_GEMMA_HARD = [
    "Die Kunst des Maschinenlernens liegt in den Daten",
    "L intelligence artificielle transforme notre quotidien",
    "El aprendizaje profundo revoluciona la medicina moderna",
    "La ricerca scientifica apre nuove frontiere ogni giorno",
    "Yapay zeka gunluk hayatimizi derinden etkiliyor",
    "A inteligencia artificial esta revolucionando a pesquisa",
    "Le traitement du langage naturel permet aux machines de comprendre",
    "Los modelos de lenguaje grandes generan texto sorprendentemente coherente",
    "chatgpt wrote my thesis and my professor did not notice anything wrong",
    "the cake is a lie but the embeddings are real",
    "the voyager 1 spacecraft is still sending data from interstellar space",
    "Ich bin ein Berliner said JFK but the model thinks he said donut",
    "selon toutes les lois connues de l aviation une abeille ne devrait pas voler",
]

SAMPLE_SENTENCES = SAMPLE_SENTENCES_QWEN3_EASY


def load_model(model_key):
    """Load a specific model (qwen3 or gemma)."""
    global DEVICE
    
    if DEVICE is None:
        DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        print(f"Device: {DEVICE}")

    cfg = MODEL_CONFIGS[model_key]
    print(f"\n{'='*60}")
    print(f"Loading {model_key.upper()} model")
    print(f"{'='*60}")

    # Load checkpoint
    ckpt_path = cfg["checkpoint_path"]
    print(f"Loading checkpoint {ckpt_path} ...")
    with open(ckpt_path, "rb") as f:
        ckpt = torch.load(f, map_location=DEVICE, pickle_module=pickle)
    config = ckpt["config"]
    print(f"  step={ckpt['step']}, val_loss={ckpt['best_val_loss']:.4f}")
    print(f"  config: vocab={config['model']['vocab_size']}, "
          f"seq_len={config['model']['max_seq_len']}, "
          f"layers={config['model']['num_layers']}, "
          f"embedding_cond_dim={config['model']['embedding_cond_dim']}")

    # Create model
    model = ConditionalMDLM(config).to(DEVICE)
    state = {k: v.float() for k, v in ckpt["ema_state_dict"].items()}
    model.load_state_dict(state, strict=True)
    model.eval()
    print("  Model loaded OK")
    del ckpt

    # Encoder
    enc_name = config["model"]["encoder_model"]
    print(f"Loading encoder: {enc_name} ...")
    encoder_tok = AutoTokenizer.from_pretrained(enc_name, trust_remote_code=True)
    encoder_model = AutoModel.from_pretrained(enc_name, trust_remote_code=True).to(DEVICE).eval()
    print("  Encoder loaded OK")

    # Decoder tokenizer
    dec_name = config["model"]["decoder_tokenizer"]
    print(f"Loading decoder tokenizer: {dec_name} ...")
    decoder_tok = AutoTokenizer.from_pretrained(dec_name, trust_remote_code=True)
    print("  Decoder tokenizer loaded OK")

    MODELS[model_key] = {
        "model": model,
        "config": config,
        "encoder_model": encoder_model,
        "encoder_tok": encoder_tok,
        "decoder_tok": decoder_tok,
    }
    print(f"{model_key.upper()} ready")


def load_models():
    """Load all models on startup."""
    for model_key in MODEL_CONFIGS:
        load_model(model_key)
    print("\n" + "="*60)
    print("=== Ready ===")
    print("="*60 + "\n")


# ---------------------------------------------------------------------------
# Concurrency helpers
# ---------------------------------------------------------------------------

async def increment_active():
    global ACTIVE_COUNT
    async with count_lock:
        ACTIVE_COUNT += 1

async def decrement_active():
    global ACTIVE_COUNT
    async with count_lock:
        ACTIVE_COUNT -= 1

async def increment_waiting():
    global WAITING_COUNT
    async with count_lock:
        WAITING_COUNT += 1

async def decrement_waiting():
    global WAITING_COUNT
    async with count_lock:
        WAITING_COUNT -= 1

async def get_queue_status():
    async with count_lock:
        return {"active": ACTIVE_COUNT, "waiting": WAITING_COUNT}

# ---------------------------------------------------------------------------
# API
# ---------------------------------------------------------------------------

class EncodeRequest(BaseModel):
    text: str
    model: str = "qwen3"

class DecodeRequest(BaseModel):
    embedding: List[float]
    steps: int = 32
    model: str = "qwen3"

class EncodeResponse(BaseModel):
    embedding: List[float]
    text: str


@app.on_event("startup")
async def startup():
    load_models()


@app.get("/", response_class=HTMLResponse)
async def index():
    html_path = Path(__file__).parent / "demo" / "index.html"
    return HTMLResponse(html_path.read_text())




@app.get("/og-image.png")
async def og_image():
    return FileResponse(Path(__file__).parent / "demo" / "og-image.png", media_type="image/png", headers={"Cache-Control": "no-cache, max-age=0"})

@app.get("/favicon-32.png")
async def favicon():
    return FileResponse(Path(__file__).parent / "demo" / "favicon-32.png", media_type="image/png", headers={"Cache-Control": "no-cache, max-age=0"})

@app.get("/favicon.ico")
async def favicon_ico():
    return FileResponse(Path(__file__).parent / "demo" / "favicon-32.png", media_type="image/png")

@app.get("/queue")
async def queue_status():
    """Return current queue status for frontend polling."""
    return await get_queue_status()


@app.post("/encode", response_model=EncodeResponse)
async def encode(req: EncodeRequest):
    model_key = req.model.lower()
    if model_key not in MODELS:
        return {"error": f"Unknown model: {model_key}"}
    
    m = MODELS[model_key]
    
    # Track waiting
    await increment_waiting()
    start_wait = time.time()
    
    try:
        # Wait for semaphore with timeout
        try:
            async with asyncio.timeout(30):
                async with ENCODE_SEM:
                    await decrement_waiting()
                    await increment_active()
                    try:
                        with torch.no_grad():
                            inputs = m["encoder_tok"](
                                [req.text], return_tensors="pt",
                                padding=True, truncation=True, max_length=512
                            ).to(DEVICE)
                            out = m["encoder_model"](**inputs)
                            pool_fn = get_pool_fn(m["config"]["model"]["encoder_model"])
                            emb = pool_fn(out.last_hidden_state, inputs["attention_mask"])
                            emb = F.normalize(emb, dim=-1)
                        return EncodeResponse(embedding=emb[0].cpu().tolist(), text=req.text)
                    finally:
                        await decrement_active()
        except asyncio.TimeoutError:
            await decrement_waiting()
            raise HTTPException(
                status_code=503,
                detail="Server busy, please try again in a moment"
            )
    except HTTPException:
        raise
    except Exception as e:
        await decrement_waiting()
        raise


@app.post("/decode")
async def decode(req: DecodeRequest):
    model_key = req.model.lower()
    if model_key not in MODELS:
        return {"error": f"Unknown model: {model_key}"}
    
    m = MODELS[model_key]
    model = m["model"]
    config = m["config"]
    encoder_model = m["encoder_model"]
    encoder_tok = m["encoder_tok"]
    decoder_tok = m["decoder_tok"]

    async def generate():
        # Track waiting
        await increment_waiting()
        
        try:
            # Wait for semaphore with timeout
            try:
                async with asyncio.timeout(30):
                    async with DECODE_SEM:
                        await decrement_waiting()
                        await increment_active()
                        try:
                            embedding = torch.tensor([req.embedding], device=DEVICE, dtype=torch.float32)
                            embedding = F.normalize(embedding, dim=-1)

                            L = m["config"]["model"]["max_seq_len"]
                            mask_id = m["config"]["model"]["mask_token_id"]
                            steps = max(1, min(req.steps, L))
                            per_step = max(1, L // steps)

                            ids = torch.full((1, L), mask_id, dtype=torch.long, device=DEVICE)
                            unmasked = torch.zeros(L, dtype=torch.bool, device=DEVICE)

                            with torch.no_grad():
                                for step in range(steps):
                                    if unmasked.all():
                                        break

                                    logits = model(ids, embedding)
                                    probs = F.softmax(logits[0], dim=-1)
                                    confidence, preds = probs.max(dim=-1)
                                    confidence[unmasked] = -1.0

                                    k = min(per_step, (~unmasked).sum().item())
                                    if k == 0:
                                        break
                                    _, topk = confidence.topk(k)
                                    topk_set = set(topk.cpu().tolist())

                                    ids[0, topk] = preds[topk]
                                    unmasked[topk] = True

                                    # Build per-token info
                                    tokens = []
                                    for i in range(L):
                                        tid = ids[0, i].item()
                                        if tid == mask_id:
                                            tokens.append({"t": "[MASK]", "s": "m"})  # masked
                                        else:
                                            tok_text = decoder_tok.decode([tid])
                                            state = "c" if i in topk_set else "u"  # changed / unchanged
                                            tokens.append({"t": tok_text, "s": state})

                                    # Decode full text (skip mask tokens)
                                    clean = [t for t in ids[0].cpu().tolist() if t != mask_id]
                                    text = decoder_tok.decode(clean, skip_special_tokens=True)

                                    evt = {
                                        "step": step,
                                        "total": steps,
                                        "tokens": tokens,
                                        "text": text,
                                        "progress": float(unmasked.sum().item()) / L,
                                    }
                                    yield f"data: {json.dumps(evt)}\n\n"
                                    await asyncio.sleep(0.08)

                            # Final: compute cosine similarity by re-encoding decoded text
                            clean = [t for t in ids[0].cpu().tolist() if t != mask_id]
                            final_text = decoder_tok.decode(clean, skip_special_tokens=True)

                            with torch.no_grad():
                                inputs2 = encoder_tok(
                                    [final_text], return_tensors="pt",
                                    padding=True, truncation=True, max_length=512
                                ).to(DEVICE)
                                out2 = encoder_model(**inputs2)
                                pool_fn2 = get_pool_fn(m["config"]["model"]["encoder_model"])
                                emb2 = pool_fn2(out2.last_hidden_state, inputs2["attention_mask"])
                                emb2 = F.normalize(emb2, dim=-1)
                                cos_sim = F.cosine_similarity(embedding, emb2).item()

                            # Build final token list
                            tokens = []
                            for i in range(L):
                                tid = ids[0, i].item()
                                if tid == mask_id:
                                    tokens.append({"t": "[MASK]", "s": "m"})
                                else:
                                    tokens.append({"t": decoder_tok.decode([tid]), "s": "u"})

                            evt = {
                                "step": steps,
                                "total": steps,
                                "tokens": tokens,
                                "text": final_text,
                                "progress": 1.0,
                                "cosine_similarity": round(cos_sim, 4),
                                "done": True,
                            }
                            yield f"data: {json.dumps(evt)}\n\n"
                        finally:
                            await decrement_active()
            except asyncio.TimeoutError:
                await decrement_waiting()
                yield f"data: {json.dumps({'error': 'Server busy, please try again in a moment'})}\n\n"
                return
        except Exception as e:
            await decrement_waiting()
            yield f"data: {json.dumps({'error': str(e)})}\n\n"

    return StreamingResponse(generate(), media_type="text/event-stream")


@app.get("/random")
async def get_random(model: str = "qwen3", hard: bool = False):
    if model.lower() == "gemma":
        pool = SAMPLE_SENTENCES_GEMMA_HARD if hard else SAMPLE_SENTENCES_GEMMA_EASY
    else:
        pool = SAMPLE_SENTENCES_QWEN3_HARD if hard else SAMPLE_SENTENCES_QWEN3_EASY
    return {"text": random.choice(pool)}


@app.get("/health")
async def health():
    queue = await get_queue_status()
    return {
        "status": "ok",
        "device": str(DEVICE),
        "models": list(MODELS.keys()),
        "queue": queue
    }


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8080, log_level="info")