File size: 24,247 Bytes
9cf750e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0958342
85a0c20
60b79f9
 
9cf750e
85a0c20
 
9cf750e
60b79f9
9cf750e
 
304a534
60b79f9
 
9cf750e
848ca93
9cf750e
 
 
 
 
 
848ca93
9cf750e
 
 
 
304a534
9cf750e
 
848ca93
9cf750e
 
 
60b79f9
 
 
 
 
 
 
 
9cf750e
0958342
 
9cf750e
85a0c20
60b79f9
 
 
 
 
 
 
 
 
 
 
 
 
 
0958342
60b79f9
 
0958342
 
 
60b79f9
0958342
60b79f9
 
 
 
 
 
9cf750e
 
 
 
 
 
 
 
 
 
 
85a0c20
 
9cf750e
 
0958342
9cf750e
85a0c20
9cf750e
60b79f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cf750e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a0c20
9cf750e
 
0958342
9cf750e
 
0958342
9cf750e
 
 
60b79f9
 
 
 
 
 
 
 
 
 
0958342
60b79f9
 
0958342
60b79f9
 
 
9cf750e
 
 
 
 
 
 
 
 
 
 
 
 
0958342
 
9cf750e
 
60b79f9
 
0958342
60b79f9
 
 
 
 
 
 
 
9cf750e
 
0958342
9cf750e
 
 
 
 
 
 
 
 
0958342
304a534
 
 
 
 
9cf750e
304a534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc79b77
304a534
 
 
 
 
bc79b77
304a534
9cf750e
304a534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cf750e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0958342
 
9cf750e
 
60b79f9
 
0958342
60b79f9
 
 
 
 
 
 
 
9cf750e
 
0958342
9cf750e
 
 
 
 
 
 
 
 
0958342
304a534
 
 
 
 
9cf750e
304a534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc79b77
304a534
 
 
 
 
bc79b77
304a534
9cf750e
304a534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cf750e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0958342
 
9cf750e
 
60b79f9
 
0958342
60b79f9
 
 
 
 
 
 
 
9cf750e
 
0958342
9cf750e
 
 
 
 
 
 
 
 
0958342
304a534
 
 
 
9cf750e
304a534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc79b77
9cf750e
304a534
 
 
 
 
 
 
 
 
 
9cf750e
304a534
 
 
 
 
 
 
 
85a0c20
304a534
 
 
 
 
 
848ca93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
304a534
 
 
 
 
848ca93
304a534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
848ca93
304a534
 
 
 
 
848ca93
304a534
848ca93
304a534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
FastAPI entrypoint for the PDF-plots Hugging Face Space.

This file only wires HTTP endpoints to the logic living in `services/`.
Update the plotting/storage/color logic there without ever touching
this file (and vice versa).

Every endpoint follows the same pattern:
  1. Hash the request parameters -> deterministic plot_id.
  2. If use_cache and a matching plot already exists -> return it.
  3. Otherwise: load + validate the PDF set's error type, build the
     figure, upload it under its conventional filename, update the
     grid's index.json, and return the new entry.

Only PDF sets with ErrorType in {"hessian", "symmhessian", "replicas"}
are supported right now (see services/error_types.py) β€” anything else
returns HTTP 400 with an explanation, rather than silently computing
wrong uncertainties.
"""

import logging
import os
import threading
import time

import numpy as np
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse

from services.config import (
    COMPUTE_SEMAPHORE_LIMIT,
    FLUSH_INTERVAL_SECONDS,
    TMP_DIR,
    correlation_plot_filename,
    main_plot_filename,
    ratio_plot_filename,
    standard_plot_filename,
)
from services.error_types import UnsupportedErrorTypeError
from services.models import (
    CorrelationPlotRequest,
    MainPlotRequest,
    PlotResponse,
    RatioPlotRequest,
    StandardPlotRequest,
)
from services.pdf_utils import COMPUTE_SEMAPHORE, LHAPDF_LOCK, get_pdf_members, invalidate_cache, load_validated_pdf_set
from services.plotting import (
    build_correlation_plot,
    build_main_plot,
    build_standard_plot,
    build_standard_ratio_plot,
)
from services.storage import (
    find_cached_plot,
    find_pending_plot,
    flush_pending,
    make_plot_id,
    pending_status,
    queue_plot,
)

logger = logging.getLogger(__name__)

app = FastAPI(title="LHAPDF Plotly Plots Space")


def _background_flush_loop():
    """
    Runs for the lifetime of the container: every FLUSH_INTERVAL_SECONDS,
    batches every plot queued since the last flush into one HF commit per
    dataset_repo (see services/storage.py::flush_pending). Errors are
    swallowed and retried next cycle rather than crashing the Space β€”
    a missed flush just means plots stay queued (and locally servable)
    a bit longer.
    """
    while True:
        time.sleep(FLUSH_INTERVAL_SECONDS)
        token = os.environ.get("HF_TOKEN")
        if not token:
            logger.warning("[flush loop] HF_TOKEN not set, skipping this cycle")
            continue
        try:
            logger.info("[flush loop] running scheduled flush...")
            result = flush_pending(token)
            logger.info("[flush loop] scheduled flush result: %s", result)
        except Exception as e:
            logger.exception("[flush loop] flush_pending failed: %s", e)


@app.on_event("startup")
def _start_background_flush():
    threading.Thread(target=_background_flush_loop, daemon=True).start()

# This Space is called directly from the CTEQ-TEA website's client-side JS
# (a different origin), which the browser will block without CORS headers.
# The API is read-mostly (it only ever writes to one known dataset repo
# using the server-side token below), so allowing any origin is acceptable;
# tighten allow_origins to the site's domain if that changes.
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/health")
def health():
    logger.info("GET /health (likely a wake-up ping from the website)")
    return {"status": "ok"}


@app.get("/plots/{folder}/{filename}")
def get_plot_file(folder: str, filename: str):
    """
    Serves a generated plot's JSON straight off local disk. This is the
    URL returned as `plotly_json_url` for any plot queued but not yet
    committed to the HF dataset (see services/storage.py::queue_plot), so
    the requesting page can render it immediately without waiting for the
    next batch flush. Files stay here even after a flush, so this keeps
    working afterwards too β€” it only goes stale if the container restarts
    (ephemeral disk), at which point a fresh request just regenerates it.
    """
    path = os.path.join(TMP_DIR, folder, filename)
    if not os.path.isfile(path):
        raise HTTPException(
            status_code=404,
            detail="Plot not found locally β€” it may not have been generated yet, or this Space container restarted since it was.",
        )
    return FileResponse(path, media_type="application/json")


@app.get("/pending-status")
def get_pending_status():
    """Number of plots queued and waiting for the next batched commit, per dataset_repo|grid_name."""
    return pending_status()


@app.post("/flush-cache")
def force_flush():
    """Manually trigger a batch commit now instead of waiting for the next scheduled flush."""
    token = _server_hf_token()
    return flush_pending(token)


def _server_hf_token() -> str:
    """
    The HF token with write access to dataset_repo, read from this Space's
    own HF_TOKEN secret. Never accepted from the caller (see
    services.models.BasePlotRequest) so a public webpage can call these
    endpoints directly without ever handling a write-capable credential.
    """
    token = os.environ.get("HF_TOKEN")
    if not token:
        raise HTTPException(
            status_code=500,
            detail="Server misconfigured: HF_TOKEN secret is not set on this Space.",
        )
    return token


def _get_pdf_set_or_400(dataset_repo: str, grid_name: str, hf_token: str):
    """Shared error-type gate for every endpoint below."""
    try:
        return load_validated_pdf_set(dataset_repo, grid_name, hf_token)
    except UnsupportedErrorTypeError as e:
        logger.warning("grid=%s dataset_repo=%s rejected: %s", grid_name, dataset_repo, e)
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.exception("grid=%s dataset_repo=%s failed to load PDFSet: %s", grid_name, dataset_repo, e)
        raise HTTPException(status_code=500, detail=str(e))


def _get_pdf_members_or_400(dataset_repo: str, grid_name: str, hf_token: str):
    """
    Shared member-loading gate, mirroring _get_pdf_set_or_400. Returns
    `pdf_set.mkPDFs()`, cached per (dataset_repo, grid_name) so requests
    for the same PDF set (any Q/flavor/plot type) reuse the same loaded
    members instead of re-parsing every member's grid file from disk.
    """
    try:
        return get_pdf_members(dataset_repo, grid_name, hf_token)
    except UnsupportedErrorTypeError as e:
        logger.warning("grid=%s dataset_repo=%s rejected: %s", grid_name, dataset_repo, e)
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.exception("grid=%s dataset_repo=%s failed to load PDF members: %s", grid_name, dataset_repo, e)
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/compute-standard-plot", response_model=PlotResponse)
def compute_standard_plot(request: StandardPlotRequest):
    hf_token = _server_hf_token()
    parameters = {
        "q_scale": request.q_scale,
        "parton_id": request.parton_id,
        "plot_color": request.plot_color,
        "custom_title": request.custom_title,
        "x_min": request.x_min,
        "x_max": request.x_max,
        "n_points": request.n_points,
    }
    plot_id = make_plot_id("standard_plot", request.grid_name, parameters)
    logger.info("POST /compute-standard-plot: grid=%s dataset_repo=%s plot_id=%s params=%s", request.grid_name, request.dataset_repo, plot_id, parameters)
    t0 = time.monotonic()

    if request.use_cache:
        pending = find_pending_plot(plot_id)
        if pending:
            logger.info("plot_id=%s served from pending cache in %.3fs", plot_id, time.monotonic() - t0)
            return PlotResponse(
                status="success",
                cached=True,
                message="Plot already generated this session, awaiting next batch upload.",
                plot_id=plot_id,
                plot_url=pending["plot_url"],
                plotly_json_url=pending["plotly_json_url"],
            )
        cached = find_cached_plot(request.dataset_repo, request.grid_name, plot_id, hf_token)
        if cached:
            logger.info("plot_id=%s served from committed HF cache in %.3fs", plot_id, time.monotonic() - t0)
            return PlotResponse(
                status="success",
                cached=True,
                message="Plot already existed, returning cached version.",
                plot_id=plot_id,
                plot_url=cached["plot_url"],
                plotly_json_url=cached["plotly_json_url"],
            )

    logger.info("plot_id=%s not cached, generating...", plot_id)
    logger.info("plot_id=%s waiting for compute slot (max %d concurrent)...", plot_id, COMPUTE_SEMAPHORE_LIMIT)
    with COMPUTE_SEMAPHORE:
        logger.info("plot_id=%s acquired compute slot", plot_id)
        pdf_set = _get_pdf_set_or_400(request.dataset_repo, request.grid_name, hf_token)
        pdfs = _get_pdf_members_or_400(request.dataset_repo, request.grid_name, hf_token)

        try:
            x_vals = np.logspace(np.log10(request.x_min), np.log10(request.x_max), request.n_points)

            # See services/pdf_utils.py::LHAPDF_LOCK β€” pdf.xfxQ()/pdf_set.uncertainty()
            # below are native lhapdf calls and are not safe to run concurrently
            # with any other request's lhapdf calls.
            logger.info("plot_id=%s waiting for LHAPDF_LOCK...", plot_id)
            with LHAPDF_LOCK:
                logger.info("plot_id=%s acquired LHAPDF_LOCK, building figure...", plot_id)
                fig = build_standard_plot(
                    pdf_set=pdf_set,
                    pdfs=pdfs,
                    grid_name=request.grid_name,
                    q_scale=request.q_scale,
                    parton_id=request.parton_id,
                    plot_color=request.plot_color,
                    x_vals=x_vals,
                    custom_title=request.custom_title,
                )
            logger.info("plot_id=%s figure built, released LHAPDF_LOCK", plot_id)

            json_filename = standard_plot_filename(request.grid_name, request.parton_id, request.q_scale)

            entry = queue_plot(
                dataset_repo=request.dataset_repo,
                grid_name=request.grid_name,
                plot_id=plot_id,
                plot_type="standard_plot",
                parameters=parameters,
                fig=fig,
                json_filename=json_filename,
            )
            logger.info("plot_id=%s done in %.2fs total", plot_id, time.monotonic() - t0)

            return PlotResponse(
                status="success",
                cached=False,
                message="Plot generated; queued for the next batched upload to " + request.dataset_repo + ".",
                plot_id=plot_id,
                plot_url=entry["plot_url"],
                plotly_json_url=entry["plotly_json_url"],
            )

        except HTTPException:
            raise
        except Exception as e:
            logger.exception("plot_id=%s compute-standard-plot failed: %s", plot_id, e)
            invalidate_cache(request.dataset_repo, request.grid_name)
            raise HTTPException(status_code=500, detail=str(e))


@app.post("/compute-ratio-plot", response_model=PlotResponse)
def compute_ratio_plot(request: RatioPlotRequest):
    hf_token = _server_hf_token()
    parameters = {
        "q_scale": request.q_scale,
        "parton_id": request.parton_id,
        "plot_color": request.plot_color,
        "custom_title": request.custom_title,
        "x_min": request.x_min,
        "x_max": request.x_max,
        "n_points": request.n_points,
    }
    plot_id = make_plot_id("ratio_plot", request.grid_name, parameters)
    logger.info("POST /compute-ratio-plot: grid=%s dataset_repo=%s plot_id=%s params=%s", request.grid_name, request.dataset_repo, plot_id, parameters)
    t0 = time.monotonic()

    if request.use_cache:
        pending = find_pending_plot(plot_id)
        if pending:
            logger.info("plot_id=%s served from pending cache in %.3fs", plot_id, time.monotonic() - t0)
            return PlotResponse(
                status="success",
                cached=True,
                message="Plot already generated this session, awaiting next batch upload.",
                plot_id=plot_id,
                plot_url=pending["plot_url"],
                plotly_json_url=pending["plotly_json_url"],
            )
        cached = find_cached_plot(request.dataset_repo, request.grid_name, plot_id, hf_token)
        if cached:
            logger.info("plot_id=%s served from committed HF cache in %.3fs", plot_id, time.monotonic() - t0)
            return PlotResponse(
                status="success",
                cached=True,
                message="Plot already existed, returning cached version.",
                plot_id=plot_id,
                plot_url=cached["plot_url"],
                plotly_json_url=cached["plotly_json_url"],
            )

    logger.info("plot_id=%s not cached, generating...", plot_id)
    logger.info("plot_id=%s waiting for compute slot (max %d concurrent)...", plot_id, COMPUTE_SEMAPHORE_LIMIT)
    with COMPUTE_SEMAPHORE:
        logger.info("plot_id=%s acquired compute slot", plot_id)
        pdf_set = _get_pdf_set_or_400(request.dataset_repo, request.grid_name, hf_token)
        pdfs = _get_pdf_members_or_400(request.dataset_repo, request.grid_name, hf_token)

        try:
            x_vals = np.logspace(np.log10(request.x_min), np.log10(request.x_max), request.n_points)

            # See services/pdf_utils.py::LHAPDF_LOCK.
            logger.info("plot_id=%s waiting for LHAPDF_LOCK...", plot_id)
            with LHAPDF_LOCK:
                logger.info("plot_id=%s acquired LHAPDF_LOCK, building figure...", plot_id)
                fig = build_standard_ratio_plot(
                    pdf_set=pdf_set,
                    pdfs=pdfs,
                    grid_name=request.grid_name,
                    q_scale=request.q_scale,
                    parton_id=request.parton_id,
                    plot_color=request.plot_color,
                    x_vals=x_vals,
                    custom_title=request.custom_title,
                )
            logger.info("plot_id=%s figure built, released LHAPDF_LOCK", plot_id)

            json_filename = ratio_plot_filename(request.grid_name, request.parton_id, request.q_scale)

            entry = queue_plot(
                dataset_repo=request.dataset_repo,
                grid_name=request.grid_name,
                plot_id=plot_id,
                plot_type="ratio_plot",
                parameters=parameters,
                fig=fig,
                json_filename=json_filename,
            )
            logger.info("plot_id=%s done in %.2fs total", plot_id, time.monotonic() - t0)

            return PlotResponse(
                status="success",
                cached=False,
                message="Plot generated; queued for the next batched upload to " + request.dataset_repo + ".",
                plot_id=plot_id,
                plot_url=entry["plot_url"],
                plotly_json_url=entry["plotly_json_url"],
            )

        except HTTPException:
            raise
        except Exception as e:
            logger.exception("plot_id=%s compute-ratio-plot failed: %s", plot_id, e)
            invalidate_cache(request.dataset_repo, request.grid_name)
            raise HTTPException(status_code=500, detail=str(e))


@app.post("/compute-correlation-plot", response_model=PlotResponse)
def compute_correlation_plot(request: CorrelationPlotRequest):
    hf_token = _server_hf_token()
    parameters = {
        "q_scale": request.q_scale,
        "parton_id_1": request.parton_id_1,
        "parton_id_2": request.parton_id_2,
        "color_correlated": request.color_correlated,
        "color_anti_correlated": request.color_anti_correlated,
        "x_min": request.x_min,
        "x_max": request.x_max,
        "n_points": request.n_points,
    }
    plot_id = make_plot_id("correlation_plot", request.grid_name, parameters)
    logger.info("POST /compute-correlation-plot: grid=%s dataset_repo=%s plot_id=%s params=%s", request.grid_name, request.dataset_repo, plot_id, parameters)
    t0 = time.monotonic()

    if request.use_cache:
        pending = find_pending_plot(plot_id)
        if pending:
            logger.info("plot_id=%s served from pending cache in %.3fs", plot_id, time.monotonic() - t0)
            return PlotResponse(
                status="success",
                cached=True,
                message="Plot already generated this session, awaiting next batch upload.",
                plot_id=plot_id,
                plot_url=pending["plot_url"],
                plotly_json_url=pending["plotly_json_url"],
            )
        cached = find_cached_plot(request.dataset_repo, request.grid_name, plot_id, hf_token)
        if cached:
            logger.info("plot_id=%s served from committed HF cache in %.3fs", plot_id, time.monotonic() - t0)
            return PlotResponse(
                status="success",
                cached=True,
                message="Plot already existed, returning cached version.",
                plot_id=plot_id,
                plot_url=cached["plot_url"],
                plotly_json_url=cached["plotly_json_url"],
            )

    logger.info("plot_id=%s not cached, generating...", plot_id)
    logger.info("plot_id=%s waiting for compute slot (max %d concurrent)...", plot_id, COMPUTE_SEMAPHORE_LIMIT)
    with COMPUTE_SEMAPHORE:
        logger.info("plot_id=%s acquired compute slot", plot_id)
        pdfs = _get_pdf_members_or_400(request.dataset_repo, request.grid_name, hf_token)

        try:
            x_vals = np.logspace(np.log10(request.x_min), np.log10(request.x_max), request.n_points)

            # See services/pdf_utils.py::LHAPDF_LOCK.
            logger.info("plot_id=%s waiting for LHAPDF_LOCK...", plot_id)
            with LHAPDF_LOCK:
                logger.info("plot_id=%s acquired LHAPDF_LOCK, building figure...", plot_id)
                fig = build_correlation_plot(
                    pdfs=pdfs,
                    q_scale=request.q_scale,
                    parton_id_1=request.parton_id_1,
                    parton_id_2=request.parton_id_2,
                    x_vals=x_vals,
                    color_correlated=request.color_correlated,
                    color_anti_correlated=request.color_anti_correlated,
                )
            logger.info("plot_id=%s figure built, released LHAPDF_LOCK", plot_id)

            json_filename = correlation_plot_filename(
                request.grid_name, request.parton_id_1, request.parton_id_2, request.q_scale
            )

            entry = queue_plot(
                dataset_repo=request.dataset_repo,
                grid_name=request.grid_name,
                plot_id=plot_id,
                plot_type="correlation_plot",
                parameters=parameters,
                fig=fig,
                json_filename=json_filename,
            )
            logger.info("plot_id=%s done in %.2fs total", plot_id, time.monotonic() - t0)

            return PlotResponse(
                status="success",
                cached=False,
                message="Plot generated; queued for the next batched upload to " + request.dataset_repo + ".",
                plot_id=plot_id,
                plot_url=entry["plot_url"],
                plotly_json_url=entry["plotly_json_url"],
            )

        except HTTPException:
            raise
        except Exception as e:
            logger.exception("plot_id=%s compute-correlation-plot failed: %s", plot_id, e)
            invalidate_cache(request.dataset_repo, request.grid_name)
            raise HTTPException(status_code=500, detail=str(e))


@app.post("/compute-main-plot", response_model=PlotResponse)
def compute_main_plot(request: MainPlotRequest):
    hf_token = _server_hf_token()
    parameters = {
        "q_scale": request.q_scale,
        "parton_ids": request.parton_ids,
        "custom_title": request.custom_title,
        "x_min": request.x_min,
        "x_max": request.x_max,
        "n_points": request.n_points,
    }
    plot_id = make_plot_id("main_plot", request.grid_name, parameters)
    logger.info("POST /compute-main-plot: grid=%s dataset_repo=%s plot_id=%s params=%s", request.grid_name, request.dataset_repo, plot_id, parameters)
    t0 = time.monotonic()

    if request.use_cache:
        pending = find_pending_plot(plot_id)
        if pending:
            logger.info("plot_id=%s served from pending cache in %.3fs", plot_id, time.monotonic() - t0)
            return PlotResponse(
                status="success",
                cached=True,
                message="Plot already generated this session, awaiting next batch upload.",
                plot_id=plot_id,
                plot_url=pending["plot_url"],
                plotly_json_url=pending["plotly_json_url"],
            )
        cached = find_cached_plot(request.dataset_repo, request.grid_name, plot_id, hf_token)
        if cached:
            logger.info("plot_id=%s served from committed HF cache in %.3fs", plot_id, time.monotonic() - t0)
            return PlotResponse(
                status="success",
                cached=True,
                message="Plot already existed, returning cached version.",
                plot_id=plot_id,
                plot_url=cached["plot_url"],
                plotly_json_url=cached["plotly_json_url"],
            )

    logger.info("plot_id=%s not cached, generating...", plot_id)
    logger.info("plot_id=%s waiting for compute slot (max %d concurrent)...", plot_id, COMPUTE_SEMAPHORE_LIMIT)
    with COMPUTE_SEMAPHORE:
        logger.info("plot_id=%s acquired compute slot", plot_id)
        pdf_set = _get_pdf_set_or_400(request.dataset_repo, request.grid_name, hf_token)
        pdfs = _get_pdf_members_or_400(request.dataset_repo, request.grid_name, hf_token)

        try:
            x_vals = np.logspace(np.log10(request.x_min), np.log10(request.x_max), request.n_points)

            # See services/pdf_utils.py::LHAPDF_LOCK.
            logger.info("plot_id=%s waiting for LHAPDF_LOCK...", plot_id)
            with LHAPDF_LOCK:
                logger.info("plot_id=%s acquired LHAPDF_LOCK, building figure...", plot_id)
                fig = build_main_plot(
                    pdf_set=pdf_set,
                    pdfs=pdfs,
                    grid_name=request.grid_name,
                    q_scale=request.q_scale,
                    parton_ids=request.parton_ids,
                    x_vals=x_vals,
                    custom_title=request.custom_title,
                )
            logger.info("plot_id=%s figure built, released LHAPDF_LOCK", plot_id)

            json_filename = main_plot_filename(request.grid_name, request.parton_ids, request.q_scale)

            entry = queue_plot(
                dataset_repo=request.dataset_repo,
                grid_name=request.grid_name,
                plot_id=plot_id,
                plot_type="main_plot",
                parameters=parameters,
                fig=fig,
                json_filename=json_filename,
            )
            logger.info("plot_id=%s done in %.2fs total", plot_id, time.monotonic() - t0)

            return PlotResponse(
                status="success",
                cached=False,
                message="Plot generated; queued for the next batched upload to " + request.dataset_repo + ".",
                plot_id=plot_id,
                plot_url=entry["plot_url"],
                plotly_json_url=entry["plotly_json_url"],
            )

        except HTTPException:
            raise
        except Exception as e:
            logger.exception("plot_id=%s compute-main-plot failed: %s", plot_id, e)
            invalidate_cache(request.dataset_repo, request.grid_name)
            raise HTTPException(status_code=500, detail=str(e))