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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
"""
pod_api.py β€” RunPod-side FastAPI server that delegates generation to local
trtllm-serve while keeping your existing api.py contract (job pattern,
Pydantic validation, normalizer routing, auto-save, error handling).

Architecture in this pod:

    Client  ─POST /v1/jobs──▢  pod_api.py (this file, port 5000)
                                    β”‚
                                    β”‚ enqueues job
                                    β–Ό
                              ThreadPoolExecutor
                                    β”‚
                                    β”‚ 1. normalize via Anthropic API
                                    β”‚ 2. POST to trtllm-serve
                                    β–Ό
                              trtllm-serve (port 8000, local) ──▢ model on GPU

Why this layout:
- Your reliability layer (job pattern, validation, GC, auto-save) stays.
- TRT-LLM does the actual generation β€” 2.85Γ— faster than transformers, and
  ready to add NGram speculative on top via the existing spec_config.yaml.
- Anthropic-based normalizer + dashboard routing keep working unchanged
  because we import your existing inference_edited_chat_opt module.

Setup:
    pip install fastapi "uvicorn[standard]" pydantic requests anthropic
    export ANTHROPIC_API_KEY=...

    # Make sure trtllm-serve is already running on :8000.
    # Then start this:
    uvicorn pod_api:app --host 0.0.0.0 --port 5000 --workers 1

Endpoints (same shape as your old api.py):
    GET  /v1/healthz
    GET  /v1/readyz
    POST /v1/jobs                  -> 202 {"job_id": ...}
    GET  /v1/jobs/{job_id}         -> status + html when done
    GET  /v1/jobs                  -> list recent jobs
    POST /v1/generate              -> synchronous variant
"""
from __future__ import annotations

import json
import logging
import os
import sys
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Literal, Optional

import requests
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field, field_validator

# Make /workspace importable so we can pull SYSTEM_PROMPT, normalizers,
# is_dashboard_prompt, and post_process from your existing module.
sys.path.insert(0, "/workspace")
import inference_edited_chat_opt as inf

# ──────────────────────────────────────────────────────────────────────────────
# Config
# ──────────────────────────────────────────────────────────────────────────────
TRTLLM_BASE_URL     = os.environ.get("TRTLLM_BASE_URL", "http://localhost:8000")
TRTLLM_MODEL        = os.environ.get("TRTLLM_MODEL", "final_model")
MAX_PROMPT_CHARS    = 8_000
MAX_CONCURRENT_JOBS = 16
JOB_TIMEOUT_S       = 60 * 25
SYNC_TIMEOUT_S      = 60 * 20
JOB_RETENTION_S     = 60 * 60
OUTPUT_DIR: Optional[Path] = Path(os.environ.get("API_OUTPUT_DIR", "/workspace/api_output"))
GENERATION_MAX_TOKENS = int(os.environ.get("GENERATION_MAX_TOKENS", "8192"))
GENERATION_TEMPERATURE = float(os.environ.get("GENERATION_TEMPERATURE", "0.0"))

logger = logging.getLogger("pod_api")
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)

# ──────────────────────────────────────────────────────────────────────────────
# Job state (same shape as your old api.py)
# ──────────────────────────────────────────────────────────────────────────────
JobStatus = Literal["queued", "running", "done", "error"]


@dataclass
class Job:
    id: str
    raw_prompt: str
    normalized_prompt: Optional[str] = None
    status: JobStatus = "queued"
    html: Optional[str] = None
    error: Optional[str] = None
    created_at: float = field(default_factory=time.time)
    started_at: Optional[float] = None
    finished_at: Optional[float] = None
    done_event: threading.Event = field(default_factory=threading.Event)

    def to_response(self) -> dict[str, Any]:
        body: dict[str, Any] = {
            "job_id": self.id,
            "status": self.status,
            "created_at": self.created_at,
        }
        if self.started_at is not None:
            body["started_at"] = self.started_at
        if self.finished_at is not None:
            body["finished_at"] = self.finished_at
            body["duration_seconds"] = round(
                self.finished_at - (self.started_at or self.created_at), 2
            )
        if self.normalized_prompt is not None:
            body["normalized_prompt"] = self.normalized_prompt
        if self.status == "done":
            body["html"] = self.html
        elif self.status == "error":
            body["error"] = self.error
        return body


_jobs: dict[str, Job] = {}
_jobs_lock = threading.Lock()
_executor: Optional[ThreadPoolExecutor] = None
_inflight = 0
_inflight_lock = threading.Lock()


def _store_job(job: Job) -> None:
    with _jobs_lock:
        _jobs[job.id] = job


def _get_job(job_id: str) -> Optional[Job]:
    with _jobs_lock:
        return _jobs.get(job_id)


def _gc_jobs() -> None:
    now = time.time()
    with _jobs_lock:
        stale = [
            jid for jid, j in _jobs.items()
            if j.finished_at is not None and (now - j.finished_at) > JOB_RETENTION_S
        ]
        for jid in stale:
            _jobs.pop(jid, None)


def _try_reserve_slot() -> bool:
    global _inflight
    with _inflight_lock:
        if _inflight >= MAX_CONCURRENT_JOBS:
            return False
        _inflight += 1
        return True


def _release_slot() -> None:
    global _inflight
    with _inflight_lock:
        _inflight = max(0, _inflight - 1)


def _inflight_count() -> int:
    with _inflight_lock:
        return _inflight


# ──────────────────────────────────────────────────────────────────────────────
# Generation β€” call into local trtllm-serve over HTTP
# ──────────────────────────────────────────────────────────────────────────────
def _trtllm_generate(prompt_text: str) -> str:
    """Send a chat-completion request to trtllm-serve and return the HTML."""
    body = {
        "model": TRTLLM_MODEL,
        "messages": [
            {"role": "system", "content": inf.SYSTEM_PROMPT},
            {"role": "user", "content": prompt_text},
        ],
        "max_tokens": GENERATION_MAX_TOKENS,
        "temperature": GENERATION_TEMPERATURE,
    }
    resp = requests.post(
        f"{TRTLLM_BASE_URL}/v1/chat/completions",
        headers={"Content-Type": "application/json"},
        json=body,
        timeout=JOB_TIMEOUT_S,
    )
    resp.raise_for_status()
    data = resp.json()
    text = data["choices"][0]["message"]["content"]
    if not isinstance(text, str) or not text.strip():
        raise RuntimeError("trtllm-serve returned empty content")
    return text


# ──────────────────────────────────────────────────────────────────────────────
# Job runner
# ──────────────────────────────────────────────────────────────────────────────
def _run_job(job: Job) -> None:
    job.started_at = time.time()
    job.status = "running"
    logger.info("job %s started", job.id)

    try:
        # Step 1 β€” normalize via Anthropic (uses your existing normalizers,
        # routed by is_dashboard_prompt for landing-page vs dashboard).
        try:
            normalized = inf.normalize_prompt(job.raw_prompt)
        except Exception as e:
            logger.warning(
                "normalize failed for job %s: %s β€” falling back to raw prompt",
                job.id, e,
            )
            normalized = job.raw_prompt
        if not isinstance(normalized, str) or not normalized.strip():
            normalized = job.raw_prompt
        job.normalized_prompt = normalized

        # Step 2 β€” generate via trtllm-serve (local HTTP, port 8000)
        raw_html = _trtllm_generate(job.normalized_prompt)

        # Step 3 β€” apply your existing post-processing
        html = inf.post_process(raw_html)
        if not html.strip():
            raise RuntimeError("post_process returned empty output")

        job.html = html
        job.status = "done"
        logger.info(
            "job %s done in %.1fs (%d chars)",
            job.id, time.time() - job.started_at, len(html),
        )

        # Auto-save to disk so results survive in-memory GC.
        if OUTPUT_DIR is not None:
            try:
                OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
                (OUTPUT_DIR / f"{job.id}.html").write_text(html, encoding="utf-8")
                (OUTPUT_DIR / f"{job.id}.json").write_text(
                    json.dumps({
                        "job_id": job.id,
                        "raw_prompt": job.raw_prompt,
                        "normalized_prompt": job.normalized_prompt,
                        "created_at": job.created_at,
                        "started_at": job.started_at,
                        "finished_at": time.time(),
                        "duration_seconds": round(time.time() - job.started_at, 2),
                    }, indent=2),
                    encoding="utf-8",
                )
                logger.info("job %s saved to %s", job.id, OUTPUT_DIR)
            except Exception as e:
                logger.warning("failed to persist job %s: %s", job.id, e)

    except requests.HTTPError as e:
        job.error = f"trtllm-serve returned {e.response.status_code}: {e.response.text[:500]}"
        job.status = "error"
        logger.exception("job %s β€” trtllm-serve HTTP error", job.id)

    except requests.RequestException as e:
        job.error = f"trtllm-serve unreachable: {e}"
        job.status = "error"
        logger.exception("job %s β€” trtllm-serve unreachable", job.id)

    except Exception as e:
        job.error = f"{type(e).__name__}: {e}"
        job.status = "error"
        logger.exception("job %s failed", job.id)

    finally:
        job.finished_at = time.time()
        job.done_event.set()
        _release_slot()
        _gc_jobs()


# ──────────────────────────────────────────────────────────────────────────────
# FastAPI app + lifespan
# ──────────────────────────────────────────────────────────────────────────────
@asynccontextmanager
async def lifespan(_: FastAPI):
    global _executor

    # Probe trtllm-serve once on startup so we fail fast if it's not running.
    try:
        r = requests.get(f"{TRTLLM_BASE_URL}/v1/models", timeout=10)
        r.raise_for_status()
        logger.info(
            "trtllm-serve OK at %s (%d models loaded)",
            TRTLLM_BASE_URL, len(r.json().get("data", [])),
        )
    except Exception as e:
        logger.error(
            "trtllm-serve not reachable at %s β€” %s. "
            "Start it before this API: trtllm-serve /workspace/final_model --host 0.0.0.0 --port 8000",
            TRTLLM_BASE_URL, e,
        )

    _executor = ThreadPoolExecutor(
        max_workers=MAX_CONCURRENT_JOBS,
        thread_name_prefix="job-runner",
    )
    logger.info(
        "executor started (max_workers=%d), output_dir=%s",
        MAX_CONCURRENT_JOBS, OUTPUT_DIR,
    )

    try:
        yield
    finally:
        if _executor is not None:
            _executor.shutdown(wait=False, cancel_futures=True)


app = FastAPI(title="HTML Generation API (TRT-LLM backed)", version="2.0.0", lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)


class GenerateRequest(BaseModel):
    prompt: str = Field(..., min_length=1, max_length=MAX_PROMPT_CHARS)

    @field_validator("prompt")
    @classmethod
    def _strip(cls, v: str) -> str:
        v = v.strip()
        if not v:
            raise ValueError("prompt is empty after stripping whitespace")
        return v


@app.exception_handler(Exception)
async def _unhandled(request, exc):
    logger.exception("unhandled exception in request: %s", exc)
    return JSONResponse(
        status_code=500,
        content={"error": "internal_server_error", "detail": str(exc)},
    )


# ──────────────────────────────────────────────────────────────────────────────
# Endpoints
# ──────────────────────────────────────────────────────────────────────────────
@app.get("/v1/healthz")
def healthz():
    return {"status": "ok"}


@app.get("/v1/readyz")
def readyz():
    if _executor is None:
        return JSONResponse(status_code=503, content={"status": "executor_not_ready"})
    try:
        r = requests.get(f"{TRTLLM_BASE_URL}/v1/models", timeout=5)
        if r.status_code != 200:
            return JSONResponse(
                status_code=503,
                content={"status": "trtllm_unhealthy", "trtllm_status": r.status_code},
            )
    except Exception as e:
        return JSONResponse(
            status_code=503,
            content={"status": "trtllm_unreachable", "detail": str(e)},
        )
    return {
        "status": "ready",
        "in_flight": _inflight_count(),
        "max_concurrent_jobs": MAX_CONCURRENT_JOBS,
        "trtllm_url": TRTLLM_BASE_URL,
    }


@app.post("/v1/jobs", status_code=202)
def create_job(req: GenerateRequest):
    if _executor is None:
        raise HTTPException(status_code=503, detail="server still warming up")
    if not _try_reserve_slot():
        raise HTTPException(
            status_code=503,
            detail=f"server at capacity ({MAX_CONCURRENT_JOBS} in-flight) β€” try again shortly",
        )
    job = Job(id=uuid.uuid4().hex, raw_prompt=req.prompt)
    _store_job(job)
    _executor.submit(_run_job, job)
    logger.info(
        "job %s queued (in_flight=%d, prompt_chars=%d)",
        job.id, _inflight_count(), len(req.prompt),
    )
    return {
        "job_id": job.id,
        "status": "queued",
        "in_flight": _inflight_count(),
    }


@app.get("/v1/jobs/{job_id}")
def get_job(job_id: str):
    job = _get_job(job_id)
    if job is not None:
        return job.to_response()
    # Fall back to disk if the job was GC'd from memory.
    if OUTPUT_DIR is not None:
        html_path = OUTPUT_DIR / f"{job_id}.html"
        meta_path = OUTPUT_DIR / f"{job_id}.json"
        if html_path.exists():
            try:
                meta = json.loads(meta_path.read_text(encoding="utf-8")) if meta_path.exists() else {}
                return {
                    "job_id": job_id,
                    "status": "done",
                    "html": html_path.read_text(encoding="utf-8"),
                    "source": "disk",
                    **meta,
                }
            except Exception as e:
                logger.warning("failed to read persisted job %s: %s", job_id, e)
    raise HTTPException(
        status_code=404,
        detail="job not found (not in memory and not persisted to disk)",
    )


@app.get("/v1/jobs")
def list_jobs(limit: int = 50):
    if limit < 1 or limit > 500:
        raise HTTPException(status_code=400, detail="limit must be between 1 and 500")
    with _jobs_lock:
        items = sorted(_jobs.values(), key=lambda j: j.created_at, reverse=True)[:limit]
    return {
        "count": len(items),
        "jobs": [
            {"job_id": j.id, "status": j.status, "created_at": j.created_at}
            for j in items
        ],
    }


@app.post("/v1/generate")
def generate_sync(req: GenerateRequest):
    if _executor is None:
        raise HTTPException(status_code=503, detail="server still warming up")
    if not _try_reserve_slot():
        raise HTTPException(
            status_code=503,
            detail=f"server at capacity ({MAX_CONCURRENT_JOBS} in-flight) β€” try again shortly",
        )
    job = Job(id=uuid.uuid4().hex, raw_prompt=req.prompt)
    _store_job(job)
    _executor.submit(_run_job, job)
    finished = job.done_event.wait(timeout=SYNC_TIMEOUT_S)
    if not finished:
        raise HTTPException(
            status_code=504,
            detail={
                "job_id": job.id,
                "error": "generation timed out β€” use GET /v1/jobs/{id} to retrieve",
            },
        )
    if job.status == "done":
        return {
            "job_id": job.id,
            "html": job.html,
            "normalized_prompt": job.normalized_prompt,
            "duration_seconds": round(
                (job.finished_at or 0) - (job.started_at or 0), 2
            ),
        }
    raise HTTPException(
        status_code=500,
        detail={"job_id": job.id, "error": job.error or "unknown error"},
    )