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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f092803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53e24ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Local decision playground with one resident model and serialized GPU requests."""

import argparse
import asyncio
import json
import logging
import math
import subprocess
import time
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager
from pathlib import Path
from tempfile import TemporaryDirectory
from threading import Event

from anyio import CancelScope
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from PIL import Image, ImageOps, UnidentifiedImageError
from starlette.datastructures import UploadFile
from starlette.middleware.trustedhost import TrustedHostMiddleware

from .core import DecisionEngine, Independent, State, parse_question

MAX_UPLOAD_BYTES = 200 * 1024 * 1024
STATIC = Path(__file__).parent / "static"
LOGGER = logging.getLogger(__name__)


def normalize_question(raw: dict) -> dict:
    question = dict(raw)
    kind = question.get("type")
    criteria = question.get("criteria")
    if kind in {"choice", "independent"}:
        if isinstance(criteria, list):
            if not all(isinstance(name, str) and name.strip() for name in criteria):
                raise ValueError("Choice names must be nonempty strings")
            if len(set(criteria)) != len(criteria):
                raise ValueError("Choice names must be unique")
            criteria = dict.fromkeys(criteria, "")
        if isinstance(criteria, dict):
            question["criteria"] = {
                name: name
                if description is None
                or description == ""
                or isinstance(description, str)
                and not description.strip()
                else description
                for name, description in criteria.items()
            }
    elif kind == "score":
        if criteria is None:
            count = question.pop("levels", 5)
            if type(count) is not int or not 2 <= count <= 10:
                raise ValueError("Choose between 2 and 10 grade levels")
            criteria = [""] * count
        if isinstance(criteria, list):
            question["criteria"] = [
                f"Grade {index} on a scale from 0 (lowest) to {len(criteria) - 1} (highest)"
                if description is None or isinstance(description, str) and not description.strip()
                else str(description)
                if type(description) in (int, float)
                else description
                for index, description in enumerate(criteria)
            ]
    return question


def read_spec(encoded: str) -> tuple[dict, dict]:
    def unique_object(pairs):
        result = {}
        for key, value in pairs:
            if key in result:
                raise ValueError(f"Duplicate JSON key: {key}")
            result[key] = value
        return result

    spec = json.loads(encoded, object_pairs_hook=unique_object)
    if not isinstance(spec, dict) or set(spec) - {"text", "instructions", "questions", "media"}:
        raise ValueError("Request accepts text, instructions, questions, and uploaded media")
    if not isinstance(spec.get("text", ""), str) or not isinstance(
        spec.get("instructions", ""), str
    ):
        raise ValueError("Text and shared instructions must be strings")
    raw_questions = spec.get("questions")
    if not isinstance(raw_questions, dict) or not 1 <= len(raw_questions) <= 32:
        raise ValueError("Define between 1 and 32 output fields")
    questions = {}
    for name, raw in raw_questions.items():
        if not isinstance(name, str) or not name.strip() or len(name) > 100:
            raise ValueError("Each field needs a nonempty name of at most 100 characters")
        if not isinstance(raw, dict):
            raise ValueError(f"Field {name} must be a question object")
        raw = normalize_question(raw)
        spec["questions"][name] = raw
        question = parse_question(raw)
        if spec.get("instructions", "").strip():
            question = parse_question(
                {
                    **raw,
                    "instructions": f"{spec['instructions']}\n\n{question.instructions}",
                }
            )
        questions[name] = question
    field_count = sum(
        len(q.criteria) if isinstance(q, Independent) else 1 for q in questions.values()
    )
    if field_count > 128:
        raise ValueError("At most 128 individual fields or independent labels can run together")
    media = spec.get("media", [])
    if not isinstance(media, list) or len(media) > 10:
        raise ValueError("Attach at most 8 images, one audio clip, and one video")
    counts = {"image": 0, "audio": 0, "video": 0}
    for item in media:
        if not isinstance(item, dict) or set(item) != {"kind", "name"}:
            raise ValueError("Each attachment needs kind and name")
        if item["kind"] not in counts or not isinstance(item["name"], str):
            raise ValueError("Unknown media type")
        counts[item["kind"]] += 1
    if counts["image"] > 8 or counts["audio"] > 1 or counts["video"] > 1:
        raise ValueError("Attach at most 8 images, one audio clip, and one video")
    return spec, questions


async def read_upload(request: Request, directory: str):
    async with request.form(max_files=10, max_fields=1, max_part_size=1024 * 1024) as form:
        if set(form) - {"spec", "media"} or not isinstance(form.get("spec"), str):
            raise ValueError("Submit a JSON spec and optional media attachments")
        spec, questions = read_spec(form["spec"])
        files = form.getlist("media")
        if len(files) != len(spec.get("media", [])) or not all(
            isinstance(file, UploadFile) for file in files
        ):
            raise ValueError("Attachment files do not match the request")
        paths, total = [], 0
        for index, file in enumerate(files):
            suffix = Path(file.filename or "upload").suffix.lower()
            if len(suffix) > 10 or not suffix.replace(".", "").isalnum():
                suffix = ".bin"
            path = Path(directory) / f"attachment-{index}{suffix}"
            with path.open("wb") as output:
                while chunk := await file.read(1024 * 1024):
                    total += len(chunk)
                    if total > MAX_UPLOAD_BYTES:
                        raise ValueError("Attachments exceed the 200 MB total upload limit")
                    output.write(chunk)
            paths.append(path)
        return spec, questions, paths


def inspect_media(path: Path) -> dict:
    try:
        process = subprocess.run(
            ["ffprobe", "-v", "error", "-show_format", "-show_streams", "-of", "json", str(path)],
            capture_output=True,
            check=True,
            timeout=20,
        )
        return json.loads(process.stdout)
    except FileNotFoundError as exc:
        raise ValueError("Install ffmpeg and ffprobe to use audio or video") from exc
    except (subprocess.SubprocessError, json.JSONDecodeError) as exc:
        raise ValueError(
            "Could not read this audio/video file. Try WAV, MP3, MP4, MOV, or WebM."
        ) from exc


def prepare_media(paths: list[Path], entries: list[dict]) -> tuple[dict, list[dict]]:
    state_paths = {"images": [], "audio": [], "videos": []}
    metadata = []
    for path, entry in zip(paths, entries, strict=True):
        kind = entry["kind"]
        item = {"name": entry["name"], "kind": kind, "bytes": path.stat().st_size}
        if kind == "image":
            try:
                with Image.open(path) as image:
                    frames = getattr(image, "n_frames", 1)
                    if frames > 1 and image.format not in {"JPEG", "MPO"}:
                        raise ValueError(
                            "This file is an animation. Use a video clip to evaluate its motion."
                        )
                    item.update(width=image.width, height=image.height)
                    if image.format in {"JPEG", "MPO"} and frames > 1:
                        # Phone HDR/depth JPEGs can contain multiple pictures;
                        # this does not make the primary photograph animated.
                        item.update(
                            source_format=image.format, embedded_images=frames, used_frame=0
                        )
                        image.seek(0)
                        photograph = ImageOps.exif_transpose(image).convert("RGB")
                        path = path.with_name(path.stem + "-primary.png")
                        photograph.save(path)
                    else:
                        image.verify()
            except (UnidentifiedImageError, OSError, Image.DecompressionBombError) as exc:
                raise ValueError("Could not read this image. Try PNG, JPEG, or WebP.") from exc
            state_paths["images"].append(str(path))
        else:
            info = inspect_media(path)
            types = {stream.get("codec_type") for stream in info.get("streams", [])}
            # Browsers often label an audio-only .webm upload as video/webm.
            # Use its actual streams so an exported recording still works.
            if kind == "video" and "video" not in types and "audio" in types:
                kind = "audio"
                item["kind"] = "audio"
            seconds = float(info.get("format", {}).get("duration", "nan"))
            limit = 30 if kind == "audio" or "audio" in types else 60
            if kind == "video" and (not math.isfinite(seconds) or not 0 < seconds <= limit):
                raise ValueError(f"Video must have a known duration of at most {limit} seconds")
            if kind == "audio" and math.isfinite(seconds) and not 0 < seconds <= 30:
                raise ValueError("Audio must be at most 30 seconds")
            if kind == "audio":
                if "audio" not in types or "video" in types:
                    raise ValueError(
                        "Attach video files as video, and audio-only files as speech/audio"
                    )
                converted = path.with_name(path.stem + "-audio.wav")
                try:
                    subprocess.run(
                        [
                            "ffmpeg",
                            "-v",
                            "error",
                            "-i",
                            str(path),
                            "-vn",
                            "-ac",
                            "1",
                            "-ar",
                            "16000",
                            str(converted),
                        ],
                        capture_output=True,
                        check=True,
                        timeout=45,
                    )
                except (OSError, subprocess.SubprocessError) as exc:
                    raise ValueError("Could not decode this audio clip") from exc
                # MediaRecorder WebM often has no duration metadata. Decode the
                # complete clip, then enforce the limit on the resulting WAV.
                seconds = float(inspect_media(converted).get("format", {}).get("duration", "nan"))
                if not math.isfinite(seconds) or not 0 < seconds <= 30:
                    raise ValueError(
                        "Audio must be at most 30 seconds; the recording was not trimmed"
                    )
                state_paths["audio"].append(str(converted))
            else:
                if "video" not in types:
                    raise ValueError("This attachment does not contain a video stream")
                state_paths["videos"].append(str(path))
            item.update(
                duration_seconds=seconds, has_soundtrack=kind == "video" and "audio" in types
            )
        metadata.append(item)
    if state_paths["audio"] and any(item.get("has_soundtrack") for item in metadata):
        raise ValueError(
            "The video already has a soundtrack. Remove the separate audio clip to use it."
        )
    return {key: tuple(values) for key, values in state_paths.items()}, metadata


class Runtime:
    def __init__(self, model: str, work_dir: Path, backend_factory=None):
        self.model = model
        self.work_dir = work_dir
        self.factory = backend_factory
        self.pool = ThreadPoolExecutor(max_workers=1, thread_name_prefix="decision-model")
        self.lock = asyncio.Lock()
        self.backend = None
        self.normal_backend = None
        self.error = None
        self.load_seconds = None

    async def load(self):
        def initialize():
            started = time.perf_counter()
            if self.factory is None:
                from .json_backend import JSONMLXBackend

                backend = JSONMLXBackend(self.model, branch_batch_size=8)
            else:
                backend = self.factory()
            from .comparison import generation_backend

            self.normal_backend = generation_backend(backend)
            self.backend = backend
            self.load_seconds = time.perf_counter() - started

        try:
            await asyncio.get_running_loop().run_in_executor(self.pool, initialize)
        except Exception:
            LOGGER.exception("Model initialization failed")
            self.error = "The model could not load. Check the model path and server log."

    def evaluate(
        self, spec: dict, questions: dict, paths: list[Path], comparison: bool = False, emit=None
    ) -> dict:
        started = time.perf_counter()
        media, metadata = prepare_media(paths, spec.get("media", []))
        normalized = time.perf_counter()
        state = State(text=spec.get("text", ""), **media)
        details = None
        if comparison:
            from .comparison import compare

            result, details = (
                compare(
                    self.backend,
                    state,
                    questions,
                    normalized - started,
                    emit=emit,
                    concurrent=True,
                    normal_backend=self.normal_backend,
                )
                if emit
                else compare(self.backend, state, questions, normalized - started)
            )
        else:
            result = DecisionEngine(self.backend).system_one(state, questions)
        finished = time.perf_counter()
        return {
            **result,
            "execution": result.get("execution", dict(self.backend.last_stats)),
            "media": metadata,
            "media_prepare_seconds": normalized - started,
            "decision_seconds": result.get("inference_seconds", finished - normalized),
            "total_seconds": finished - started,
            "model": "Gemma 4 E2B · frozen scorer",
            "load_seconds": self.load_seconds,
            **({"comparison": details} if details else {}),
        }


def create_app(model: str, work_dir: Path, backend_factory=None) -> FastAPI:
    runtime = Runtime(model, work_dir, backend_factory)

    @asynccontextmanager
    async def lifespan(app):
        work_dir.mkdir(parents=True, exist_ok=True)
        loading = asyncio.create_task(runtime.load())
        yield
        await loading
        runtime.pool.shutdown(wait=True)

    app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None)
    app.state.runtime = runtime
    app.add_middleware(TrustedHostMiddleware, allowed_hosts=["127.0.0.1", "localhost"])

    @app.middleware("http")
    async def local_requests(request: Request, call_next):
        if request.method == "POST":
            origin = request.headers.get("origin")
            if origin and origin != f"http://{request.headers.get('host')}":
                return JSONResponse(
                    {"error": "Open the playground directly on localhost to run a request"},
                    status_code=403,
                )
            try:
                if int(request.headers.get("content-length", "0")) > MAX_UPLOAD_BYTES + 1024 * 1024:
                    return JSONResponse(
                        {"error": "Attachments exceed the 200 MB total upload limit"},
                        status_code=413,
                    )
            except ValueError:
                return JSONResponse({"error": "Invalid content length"}, status_code=400)
        response = await call_next(request)
        response.headers["X-Content-Type-Options"] = "nosniff"
        response.headers["Cache-Control"] = "no-store"
        response.headers["Content-Security-Policy"] = (
            "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' blob: data:; media-src 'self' blob:; connect-src 'self'; object-src 'none'; frame-ancestors 'none'"
        )
        return response

    @app.get("/")
    async def index():
        return FileResponse(STATIC / "index.html")

    @app.get("/demo")
    async def demo():
        return FileResponse(STATIC / "demo.html")

    @app.post("/api/compare-stream")
    async def stream(request: Request):
        if runtime.backend is None or runtime.error:
            return JSONResponse(
                {"error": runtime.error or "The model is still loading."}, status_code=503
            )
        if runtime.lock.locked():
            return JSONResponse(
                {"error": "Another evaluation is running. Wait for it to finish."}, status_code=429
            )

        await runtime.lock.acquire()
        try:
            directory = TemporaryDirectory(prefix="stream-", dir=work_dir)
        except BaseException:
            runtime.lock.release()
            raise
        try:
            spec, questions, paths = await read_upload(request, directory.name)
        except BaseException as exc:
            directory.cleanup()
            runtime.lock.release()
            if isinstance(exc, (ValueError, TypeError, KeyError)):
                return JSONResponse({"error": str(exc)}, status_code=400)
            raise

        async def events():
            loop = asyncio.get_running_loop()
            queue = asyncio.Queue()
            stopped = Event()

            class StreamStopped(Exception):
                pass

            def emit(event):
                if stopped.is_set():
                    raise StreamStopped()
                loop.call_soon_threadsafe(queue.put_nowait, event)

            def evaluate():
                try:
                    result = runtime.evaluate(spec, questions, paths, True, emit)
                    emit({"type": "complete", "result": result})
                except StreamStopped:
                    pass
                except (ValueError, TypeError, KeyError) as exc:
                    loop.call_soon_threadsafe(
                        queue.put_nowait, {"type": "error", "error": str(exc)}
                    )
                except Exception:
                    LOGGER.exception("Streaming evaluation failed")
                    if not stopped.is_set():
                        loop.call_soon_threadsafe(
                            queue.put_nowait,
                            {
                                "type": "error",
                                "error": "Evaluation failed. Check the media or reduce the input size. Details are in the server log.",
                            },
                        )

            future = loop.run_in_executor(runtime.pool, evaluate)
            future.add_done_callback(lambda _: queue.put_nowait(None))
            try:
                yield json.dumps({"type": "accepted"}) + "\n"
                while (event := await queue.get()) is not None:
                    yield json.dumps(event, allow_nan=False) + "\n"
            finally:
                stopped.set()
                # Keep uploads and the GPU lock alive until the worker stops.
                with CancelScope(shield=True):
                    try:
                        await asyncio.shield(future)
                    finally:
                        directory.cleanup()
                        runtime.lock.release()

        return StreamingResponse(
            events(), media_type="application/x-ndjson", headers={"X-Accel-Buffering": "no"}
        )

    @app.get("/api/status")
    async def status():
        return {
            "ready": runtime.backend is not None and runtime.error is None,
            "busy": runtime.lock.locked(),
            "error": runtime.error,
            "model": "Gemma 4 E2B",
            "batch_size": 8,
            "load_seconds": runtime.load_seconds,
        }

    @app.post("/api/compare")
    @app.post("/api/run")
    async def run(request: Request):
        if runtime.backend is None or runtime.error:
            return JSONResponse(
                {"error": runtime.error or "The model is still loading. Try again in a moment."},
                status_code=503,
            )
        if runtime.lock.locked():
            return JSONResponse(
                {"error": "Another evaluation is running. Wait for it to finish, then try again."},
                status_code=429,
            )
        async with runtime.lock:
            started = time.perf_counter()
            try:
                with TemporaryDirectory(prefix="run-", dir=work_dir) as directory:
                    spec, questions, paths = await read_upload(request, directory)
                    future = asyncio.get_running_loop().run_in_executor(
                        runtime.pool,
                        runtime.evaluate,
                        spec,
                        questions,
                        paths,
                        request.url.path == "/api/compare",
                    )
                    try:
                        result = await asyncio.shield(future)
                    except asyncio.CancelledError:
                        await future
                        raise
                result["request_seconds"] = time.perf_counter() - started
                return JSONResponse(result)
            except (ValueError, TypeError, KeyError) as exc:
                return JSONResponse({"error": str(exc)}, status_code=400)
            except Exception:
                LOGGER.exception("Decision evaluation failed")
                return JSONResponse(
                    {
                        "error": "Evaluation failed. Check the media format or reduce the input size. Details are in the server log."
                    },
                    status_code=500,
                )

    @app.post("/api/validate")
    async def validate(request: Request):
        encoded = await request.body()
        if len(encoded) > 1024 * 1024:
            return JSONResponse({"error": "Schema exceeds 1 MB"}, status_code=413)
        try:
            spec, _ = read_spec(encoded.decode("utf-8"))
            return {"questions": spec["questions"]}
        except (ValueError, TypeError, KeyError) as exc:
            return JSONResponse({"error": str(exc)}, status_code=400)

    app.mount("/static", StaticFiles(directory=STATIC), name="static")
    return app


def main():
    import uvicorn

    parser = argparse.ArgumentParser(description="Local multimodal decision playground")
    parser.add_argument("--model", required=True)
    parser.add_argument("--port", type=int, default=8765)
    parser.add_argument("--work-dir", type=Path, default=Path("work/web"))
    args = parser.parse_args()
    uvicorn.run(create_app(args.model, args.work_dir.resolve()), host="127.0.0.1", port=args.port)


if __name__ == "__main__":
    main()