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
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
#!/usr/bin/env python3
"""
UnimeType Transliteration 2.0 (`v2.6`) Live Playground Server.

Serves the Editorial Design System UI on `:7860` immediately while warming up
all three official `v2.6` GGUF engines (`0.8B`, `4B`, `9B` `Q4_K_M`) on local
`llama-server` daemons inside Hugging Face's 16 GB RAM Docker container.
"""
from __future__ import annotations

import asyncio
import collections
import json
import math
import os
import shutil
import subprocess
import threading
import time
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any, AsyncIterator

import httpx
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
from huggingface_hub import hf_hub_download

APP_DIR = Path(__file__).resolve().parent
MODEL_DIR = Path(os.environ.get("MODEL_DIR", str(APP_DIR / "models")))
LLAMA_BIN_DIR = Path(os.environ.get("LLAMA_BIN_DIR", "/opt/llama"))
LLAMA_SERVER_BIN = os.environ.get("LLAMA_SERVER_BIN", str(LLAMA_BIN_DIR / "llama-server"))

TIER_SPECS: dict[str, dict[str, Any]] = {
    "08b": {
        "tier": "08b",
        "label": "0.8B",
        "repo_id": "UnimeType/Transliteration-2.0-0.8B-GGUF",
        "filename": "Transliteration-2.0-0.8B-Convert-Q4_K_M.gguf",
        "port": 8081,
        "size_mb": 533,
    },
    "4b": {
        "tier": "4b",
        "label": "4B",
        "repo_id": "UnimeType/Transliteration-2.0-4B-GGUF",
        "filename": "Transliteration-2.0-4B-Convert-Q4_K_M.gguf",
        "port": 8082,
        "size_mb": 2740,
    },
    "9b": {
        "tier": "9b",
        "label": "9B",
        "repo_id": "UnimeType/Transliteration-2.0-9B-GGUF",
        "filename": "Transliteration-2.0-9B-Convert-Q4_K_M.gguf",
        "port": 8083,
        "size_mb": 5680,
    },
}

ENABLED_TIERS = [
    t.strip()
    for t in os.environ.get("PLAYGROUND_TIERS", "08b,4b,9b").split(",")
    if t.strip() in TIER_SPECS
]


def detect_cgroup_threads() -> int:
    """Detect real cgroup CPU quota so llama-server never over-spawns threads on 2-vCPU pods."""
    override = os.environ.get("LLAMA_THREADS")
    if override and override.isdigit():
        return max(1, int(override))
    # cgroup v2
    try:
        raw = Path("/sys/fs/cgroup/cpu.max").read_text(encoding="utf-8").strip().split()
        if len(raw) == 2 and raw[0] != "max":
            quota, period = int(raw[0]), int(raw[1])
            if quota > 0 and period > 0:
                return max(1, min(16, math.ceil(quota / period)))
    except Exception:
        pass
    # cgroup v1
    try:
        quota = int(Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").read_text(encoding="utf-8").strip())
        period = int(Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us").read_text(encoding="utf-8").strip())
        if quota > 0 and period > 0:
            return max(1, min(16, math.ceil(quota / period)))
    except Exception:
        pass
    return max(1, min(4, os.cpu_count() or 2))


CPU_THREADS = detect_cgroup_threads()

TIER_STATE: dict[str, dict[str, Any]] = {
    t: {
        "tier": t,
        "label": TIER_SPECS[t]["label"],
        "repo_id": TIER_SPECS[t]["repo_id"],
        "quant": "Q4_K_M",
        "state": "queued" if t in ENABLED_TIERS else "disabled",
        "detail": "Queued for warmup" if t in ENABLED_TIERS else "Disabled",
        "ready": False,
    }
    for t in TIER_SPECS
}

PROCS: dict[str, subprocess.Popen[str]] = {}
STOP_EVENT = threading.Event()
INFER_SEMAPHORE = asyncio.Semaphore(1)

# Simple sliding-window per-IP rate limiter (36 requests / 60s)
RATE_BUCKETS: dict[str, collections.deque[float]] = {}
RATE_LIMIT_MAX = 36
RATE_LIMIT_WINDOW = 60.0


def check_rate_limit(client_ip: str) -> None:
    now = time.monotonic()
    dq = RATE_BUCKETS.setdefault(client_ip, collections.deque())
    while dq and now - dq[0] > RATE_LIMIT_WINDOW:
        dq.popleft()
    if len(dq) >= RATE_LIMIT_MAX:
        raise HTTPException(
            status_code=429,
            detail="Rate limit reached (36 conversions/min). Wait a few seconds or download UnimeType for unlimited local use.",
        )
    dq.append(now)


def cleanup_temp_caches() -> None:
    for env_key in ("HF_XET_CACHE",):
        val = os.environ.get(env_key)
        if val:
            shutil.rmtree(val, ignore_errors=True)
    shutil.rmtree(MODEL_DIR / ".cache", ignore_errors=True)


def start_llama_daemon(tier: str, gguf_path: Path) -> subprocess.Popen[str]:
    spec = TIER_SPECS[tier]
    env = os.environ.copy()
    existing_ld = env.get("LD_LIBRARY_PATH", "")
    env["LD_LIBRARY_PATH"] = f"{LLAMA_BIN_DIR}:{existing_ld}" if existing_ld else str(LLAMA_BIN_DIR)

    cmd = [
        LLAMA_SERVER_BIN,
        "-m",
        str(gguf_path),
        "--host",
        "127.0.0.1",
        "--port",
        str(spec["port"]),
        "-c",
        "512",
        "-np",
        "1",
        "-t",
        str(CPU_THREADS),
        "-tb",
        str(CPU_THREADS),
        "--no-webui",
        "--reasoning",
        "off",
    ]
    return subprocess.Popen(
        cmd,
        env=env,
        stdout=subprocess.DEVNULL,
        stderr=subprocess.STDOUT,
        text=True,
    )


def wait_daemon_healthy(port: int, proc: subprocess.Popen[str], timeout_s: float = 90.0) -> bool:
    deadline = time.monotonic() + timeout_s
    url = f"http://127.0.0.1:{port}/health"
    while time.monotonic() < deadline and not STOP_EVENT.is_set():
        if proc.poll() is not None:
            return False
        try:
            r = httpx.get(url, timeout=1.5)
            if r.status_code == 200 and r.json().get("status") == "ok":
                return True
        except Exception:
            pass
        time.sleep(0.4)
    return False


def warmup_worker() -> None:
    MODEL_DIR.mkdir(parents=True, exist_ok=True)
    for tier in ENABLED_TIERS:
        if STOP_EVENT.is_set():
            return
        spec = TIER_SPECS[tier]
        target_file = MODEL_DIR / spec["filename"]
        try:
            if not target_file.exists():
                TIER_STATE[tier]["state"] = "downloading"
                TIER_STATE[tier]["detail"] = f"Pulling {spec['label']} Q4_K_M ({spec['size_mb']} MB)..."
                downloaded = hf_hub_download(
                    repo_id=spec["repo_id"],
                    filename=spec["filename"],
                    revision="v2.6",
                    local_dir=str(MODEL_DIR),
                )
                target_file = Path(downloaded)
                cleanup_temp_caches()

            TIER_STATE[tier]["state"] = "starting"
            TIER_STATE[tier]["detail"] = f"Loading {spec['label']} into memory..."
            proc = start_llama_daemon(tier, target_file)
            PROCS[tier] = proc
            if wait_daemon_healthy(spec["port"], proc):
                TIER_STATE[tier]["state"] = "ready"
                TIER_STATE[tier]["ready"] = True
                TIER_STATE[tier]["detail"] = f"Live · {spec['label']} Q4_K_M ({CPU_THREADS} vCPU)"
            else:
                TIER_STATE[tier]["state"] = "error"
                TIER_STATE[tier]["detail"] = f"{spec['label']} failed healthcheck"
        except Exception as exc:
            TIER_STATE[tier]["state"] = "error"
            TIER_STATE[tier]["detail"] = f"{spec['label']} warmup error: {exc}"

    # Keep watching daemons and auto-restart if any exits unexpectedly
    while not STOP_EVENT.is_set():
        for tier in ENABLED_TIERS:
            proc = PROCS.get(tier)
            spec = TIER_SPECS[tier]
            target_file = MODEL_DIR / spec["filename"]
            if proc is not None and proc.poll() is not None and target_file.exists():
                TIER_STATE[tier]["ready"] = False
                TIER_STATE[tier]["state"] = "starting"
                TIER_STATE[tier]["detail"] = f"Restarting {spec['label']}..."
                new_proc = start_llama_daemon(tier, target_file)
                PROCS[tier] = new_proc
                if wait_daemon_healthy(spec["port"], new_proc):
                    TIER_STATE[tier]["ready"] = True
                    TIER_STATE[tier]["state"] = "ready"
                    TIER_STATE[tier]["detail"] = f"Live · {spec['label']} Q4_K_M ({CPU_THREADS} vCPU)"
        STOP_EVENT.wait(3.0)


@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
    worker = threading.Thread(target=warmup_worker, daemon=True, name="v26-warmup")
    worker.start()
    try:
        yield
    finally:
        STOP_EVENT.set()
        for proc in PROCS.values():
            if proc.poll() is None:
                proc.terminate()


app = FastAPI(title="UnimeType Transliteration 2.0 (v2.6) Playground", lifespan=lifespan)


def normalize_tier(raw: str) -> str:
    s = (raw or "").strip().lower()
    if s in ("9b", "unimetype/transliteration-2.0-9b", "unimetype/transliteration-2.0-9b-gguf"):
        return "9b"
    if s in ("4b", "unimetype/transliteration-2.0-4b", "unimetype/transliteration-2.0-4b-gguf"):
        return "4b"
    if s in ("08b", "0.8b", "unimetype/transliteration-2.0-0.8b", "unimetype/transliteration-2.0-0.8b-gguf"):
        return "08b"
    if "9b" in s:
        return "9b"
    if "4b" in s:
        return "4b"
    if "0.8b" in s or "08b" in s:
        return "08b"
    return "9b"


def build_v26_prompt(payload_Or_str: dict[str, Any] | str) -> str:
    if isinstance(payload_Or_str, str):
        user_json = payload_Or_str.strip()
    else:
        allowed_keys = (
            "operation",
            "input",
            "enabled_languages",
            "primary_language",
            "japanese_output_style",
            "rejected_outputs",
            "lexicon",
        )
        clean: dict[str, Any] = {}
        for k in allowed_keys:
            if k in payload_Or_str and payload_Or_str[k] is not None:
                clean[k] = payload_Or_str[k]
        inp = str(clean.get("input", "")).strip()
        if not inp:
            raise HTTPException(status_code=400, detail="Input cannot be empty.")
        if len(inp) > 600:
            raise HTTPException(status_code=400, detail="Input exceeds 600 characters.")
        clean["input"] = inp
        user_json = json.dumps(clean, ensure_ascii=False, separators=(",", ":"))
    if len(user_json) > 1600:
        raise HTTPException(status_code=400, detail="Wire payload too large.")
    return f"<|im_start|>user\n{user_json}<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"


async def wait_until_tier_ready(tier: str, max_wait_s: float = 45.0) -> None:
    if tier not in ENABLED_TIERS:
        raise HTTPException(status_code=404, detail=f"Model tier '{tier}' is not enabled on this server.")
    deadline = time.monotonic() + max_wait_s
    while time.monotonic() < deadline:
        if TIER_STATE[tier]["ready"]:
            return
        if TIER_STATE[tier]["state"] == "error":
            raise HTTPException(status_code=503, detail=TIER_STATE[tier]["detail"])
        await asyncio.sleep(0.35)
    raise HTTPException(status_code=503, detail=f"{TIER_SPECS[tier]['label']} is still warming up — try again in a few seconds.")


def strip_stop_tokens(text: str) -> str:
    for tok in ("<|im_end|>", "<|endoftext|>"):
        idx = text.find(tok)
        if idx != -1:
            text = text[:idx]
    return text


@app.get("/")
async def serve_index() -> FileResponse:
    return FileResponse(APP_DIR / "index.html", media_type="text/html; charset=utf-8")


@app.get("/style.css")
async def serve_css() -> FileResponse:
    return FileResponse(APP_DIR / "style.css", media_type="text/css; charset=utf-8")


@app.get("/api/status")
async def api_status() -> JSONResponse:
    return JSONResponse(
        {
            "ok": True,
            "release": "v2.6",
            "cpu_threads": CPU_THREADS,
            "tiers": TIER_STATE,
        }
    )


@app.post("/api/convert")
async def api_convert(request: Request) -> Any:
    forwarded = request.headers.get("x-forwarded-for", "")
    client_ip = forwarded.split(",")[0].strip() if forwarded else (request.client.host if request.client else "local")
    check_rate_limit(client_ip)

    body = await request.json()
    tier = normalize_tier(str(body.get("tier", "9b")))
    payload = body.get("payload")
    if not isinstance(payload, dict):
        raise HTTPException(status_code=400, detail="'payload' must be a JSON object.")
    stream = bool(body.get("stream", True))
    prompt = build_v26_prompt(payload)

    await wait_until_tier_ready(tier)
    port = TIER_SPECS[tier]["port"]
    llama_req = {
        "prompt": prompt,
        "n_predict": 224,
        "temperature": 0.0,
        "top_k": 1,
        "stop": ["<|im_end|>", "<|endoftext|>"],
        "cache_prompt": True,
        "stream": stream,
    }

    if not stream:
        async with INFER_SEMAPHORE:
            t0 = time.perf_counter()
            async with httpx.AsyncClient(timeout=60.0) as client:
                r = await client.post(f"http://127.0.0.1:{port}/completion", json=llama_req)
                r.raise_for_status()
                data = r.json()
            elapsed_ms = round((time.perf_counter() - t0) * 1000)
            out = strip_stop_tokens(data.get("content", "")).strip()
            return JSONResponse(
                {
                    "tier": tier,
                    "label": TIER_SPECS[tier]["label"],
                    "output": out,
                    "elapsed_ms": elapsed_ms,
                    "tokens_predicted": data.get("tokens_predicted", 0),
                }
            )

    async def event_gen() -> AsyncIterator[bytes]:
        # Emit immediate status event if waiting in semaphore queue
        yield f"data: {json.dumps({'queued': True, 'tier': tier})}\n\n".encode("utf-8")
        async with INFER_SEMAPHORE:
            t0 = time.perf_counter()
            acc: list[str] = []
            tokens_pred = 0
            try:
                async with httpx.AsyncClient(timeout=75.0) as client:
                    async with client.stream("POST", f"http://127.0.0.1:{port}/completion", json=llama_req) as resp:
                        resp.raise_for_status()
                        async for line in resp.aiter_lines():
                            if not line.startswith("data: "):
                                continue
                            chunk = json.loads(line[6:])
                            piece = chunk.get("content", "")
                            if piece:
                                acc.append(piece)
                                clean_so_far = strip_stop_tokens("".join(acc))
                                yield f"data: {json.dumps({'delta': piece, 'text': clean_so_far, 'tier': tier}, ensure_ascii=False)}\n\n".encode("utf-8")
                            if chunk.get("tokens_predicted"):
                                tokens_pred = int(chunk["tokens_predicted"])
                elapsed_ms = round((time.perf_counter() - t0) * 1000)
                final_text = strip_stop_tokens("".join(acc)).strip()
                yield f"data: {json.dumps({'done': True, 'output': final_text, 'elapsed_ms': elapsed_ms, 'tokens_predicted': tokens_pred, 'tier': tier}, ensure_ascii=False)}\n\n".encode("utf-8")
            except Exception as exc:
                yield f"data: {json.dumps({'error': str(exc), 'tier': tier}, ensure_ascii=False)}\n\n".encode("utf-8")

    return StreamingResponse(
        event_gen(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "X-Accel-Buffering": "no",
        },
    )


@app.post("/v1/chat/completions")
async def openai_chat_completions(request: Request) -> JSONResponse:
    """OpenAI-compatible endpoint so developers can run the Playground cURL snippet live."""
    forwarded = request.headers.get("x-forwarded-for", "")
    client_ip = forwarded.split(",")[0].strip() if forwarded else (request.client.host if request.client else "local")
    check_rate_limit(client_ip)

    body = await request.json()
    tier = normalize_tier(str(body.get("model", "9b")))
    messages = body.get("messages") or []
    user_msgs = [m.get("content", "") for m in messages if isinstance(m, dict) and m.get("role") == "user"]
    if not user_msgs:
        raise HTTPException(status_code=400, detail="At least one user message is required.")
    prompt = build_v26_prompt(str(user_msgs[-1]))

    await wait_until_tier_ready(tier)
    port = TIER_SPECS[tier]["port"]
    max_tokens = max(1, min(224, int(body.get("max_tokens", 224))))

    async with INFER_SEMAPHORE:
        t0 = time.perf_counter()
        async with httpx.AsyncClient(timeout=60.0) as client:
            r = await client.post(
                f"http://127.0.0.1:{port}/completion",
                json={
                    "prompt": prompt,
                    "n_predict": max_tokens,
                    "temperature": 0.0,
                    "top_k": 1,
                    "stop": ["<|im_end|>", "<|endoftext|>"],
                    "cache_prompt": True,
                    "stream": False,
                },
            )
            r.raise_for_status()
            data = r.json()
        elapsed_ms = round((time.perf_counter() - t0) * 1000)
        out = strip_stop_tokens(data.get("content", "")).strip()

    return JSONResponse(
        {
            "id": f"chatcmpl-v26-{tier}-{int(time.time())}",
            "object": "chat.completion",
            "created": int(time.time()),
            "model": TIER_SPECS[tier]["repo_id"],
            "choices": [
                {
                    "index": 0,
                    "message": {"role": "assistant", "content": out},
                    "finish_reason": "stop",
                }
            ],
            "usage": {
                "prompt_tokens": data.get("tokens_evaluated", 0),
                "completion_tokens": data.get("tokens_predicted", 0),
                "total_tokens": data.get("tokens_evaluated", 0) + data.get("tokens_predicted", 0),
            },
            "unimetype_elapsed_ms": elapsed_ms,
        }
    )