Dariusfar commited on
Commit
89342ca
·
verified ·
1 Parent(s): 5fb57bc

Use native Gradio inference endpoints

Browse files
Files changed (29) hide show
  1. README.md +1 -0
  2. app.py +35 -45
  3. pff_pk/__pycache__/__init__.cpython-311.pyc +0 -0
  4. pff_pk/__pycache__/config.cpython-311.pyc +0 -0
  5. pff_pk/__pycache__/measure.cpython-311.pyc +0 -0
  6. pff_pk/__pycache__/model.cpython-311.pyc +0 -0
  7. pff_pk/__pycache__/source_process.cpython-311.pyc +0 -0
  8. pff_pk/data/__pycache__/__init__.cpython-311.pyc +0 -0
  9. pff_pk/data/__pycache__/acquisition.cpython-311.pyc +0 -0
  10. pff_pk/data/__pycache__/corpus.cpython-311.pyc +0 -0
  11. pff_pk/data/__pycache__/sampling.cpython-311.pyc +0 -0
  12. pff_pk/data/__pycache__/splits.cpython-311.pyc +0 -0
  13. pff_pk/data/__pycache__/studies.cpython-311.pyc +0 -0
  14. pff_pk/data/__pycache__/tasks.cpython-311.pyc +0 -0
  15. pff_pk/inference/__pycache__/__init__.cpython-311.pyc +0 -0
  16. pff_pk/inference/__pycache__/empirical.cpython-311.pyc +0 -0
  17. pff_pk/inference/__pycache__/model.cpython-311.pyc +0 -0
  18. pff_pk/inference/__pycache__/preprocessing.cpython-311.pyc +0 -0
  19. pff_pk/models/__pycache__/__init__.cpython-311.pyc +0 -0
  20. pff_pk/models/architectures/__pycache__/__init__.cpython-311.pyc +0 -0
  21. pff_pk/models/architectures/__pycache__/blocks.cpython-311.pyc +0 -0
  22. pff_pk/models/architectures/__pycache__/operator_attn.cpython-311.pyc +0 -0
  23. pff_pk/models/architectures/__pycache__/vector_fields_pk.cpython-311.pyc +0 -0
  24. requirements.txt +1 -2
  25. services/__pycache__/__init__.cpython-311.pyc +0 -0
  26. services/inference/__pycache__/__init__.cpython-311.pyc +0 -0
  27. services/inference/__pycache__/pff_service.cpython-311.pyc +0 -0
  28. synthetic_priors/__pycache__/__init__.cpython-311.pyc +0 -0
  29. synthetic_priors/__pycache__/contracts.cpython-311.pyc +0 -0
README.md CHANGED
@@ -4,6 +4,7 @@ emoji: 💊
4
  colorFrom: blue
5
  colorTo: indigo
6
  sdk: gradio
 
7
  python_version: "3.12"
8
  app_file: app.py
9
  pinned: false
 
4
  colorFrom: blue
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 6.2.0
8
  python_version: "3.12"
9
  app_file: app.py
10
  pinned: false
app.py CHANGED
@@ -1,15 +1,12 @@
1
- """Public HTTP surface for private-model PFF-PK inference on Hugging Face."""
2
 
3
  from __future__ import annotations
4
 
5
- import asyncio
6
  import os
7
  from pathlib import Path
8
 
9
- from fastapi import FastAPI, HTTPException, Request
10
- from fastapi.middleware.cors import CORSMiddleware
11
  from huggingface_hub import hf_hub_download
12
- from starlette.concurrency import run_in_threadpool
13
 
14
  ROOT = Path(__file__).resolve().parent
15
  MODEL_REPO_ID = os.environ.get("MODEL_REPO_ID", "").strip()
@@ -39,31 +36,9 @@ os.environ.setdefault("PFF_CPU_THREADS", "2")
39
 
40
  from services.inference.pff_service import RUNTIME, cached_inference # noqa: E402
41
 
42
- app = FastAPI(
43
- title="PFF-PK inference API",
44
- description="Server-side zero-shot PK cohort generation and dose intervention.",
45
- version="1.0.0",
46
- )
47
- app.add_middleware(
48
- CORSMiddleware,
49
- allow_origins=["https://dfaroughy.github.io"],
50
- allow_origin_regex=r"^https?://(localhost|127\.0\.0\.1)(:\d+)?$",
51
- allow_methods=["GET", "POST", "OPTIONS"],
52
- allow_headers=["content-type"],
53
- )
54
- inference_slots = asyncio.Semaphore(1)
55
-
56
-
57
- @app.get("/")
58
- def root() -> dict[str, str]:
59
- return {
60
- "service": "PFF-PK inference API",
61
- "dashboard": "https://dfaroughy.github.io/pff-pk-dashboard/empirical/",
62
- }
63
 
64
-
65
- @app.get("/health")
66
  def health() -> dict[str, str | bool]:
 
67
  metadata = RUNTIME.metadata()
68
  return {
69
  "ready": bool(metadata["ready"]),
@@ -73,25 +48,40 @@ def health() -> dict[str, str | bool]:
73
  }
74
 
75
 
76
- @app.post("/inference")
77
- async def inference(request: Request) -> dict:
78
- content_length = request.headers.get("content-length")
79
- if content_length and int(content_length) > 10_000_000:
80
- raise HTTPException(status_code=413, detail="inference request is too large")
81
- try:
82
- payload = await request.json()
83
- except Exception as error:
84
- raise HTTPException(status_code=400, detail="request body must be JSON") from error
85
  try:
86
- async with inference_slots:
87
- return await run_in_threadpool(cached_inference, payload)
88
  except (ValueError, KeyError, TypeError, FileNotFoundError) as error:
89
- raise HTTPException(status_code=400, detail=str(error)) from error
90
  except Exception as error:
91
- raise HTTPException(status_code=500, detail="PFF inference failed") from error
92
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  if __name__ == "__main__":
95
- import uvicorn
96
-
97
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ """Public Gradio API for private-model PFF-PK inference."""
2
 
3
  from __future__ import annotations
4
 
 
5
  import os
6
  from pathlib import Path
7
 
8
+ import gradio as gr
 
9
  from huggingface_hub import hf_hub_download
 
10
 
11
  ROOT = Path(__file__).resolve().parent
12
  MODEL_REPO_ID = os.environ.get("MODEL_REPO_ID", "").strip()
 
36
 
37
  from services.inference.pff_service import RUNTIME, cached_inference # noqa: E402
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
 
40
  def health() -> dict[str, str | bool]:
41
+ """Return only non-sensitive deployment metadata."""
42
  metadata = RUNTIME.metadata()
43
  return {
44
  "ready": bool(metadata["ready"]),
 
48
  }
49
 
50
 
51
+ def inference(payload: dict) -> dict:
52
+ """Generate a PK cohort from a validated dashboard request."""
53
+ if not isinstance(payload, dict):
54
+ raise gr.Error("The inference request must be a JSON object")
 
 
 
 
 
55
  try:
56
+ return cached_inference(payload)
 
57
  except (ValueError, KeyError, TypeError, FileNotFoundError) as error:
58
+ raise gr.Error(str(error)) from error
59
  except Exception as error:
60
+ raise gr.Error("PFF inference failed") from error
61
+
62
+
63
+ with gr.Blocks(title="PFF-PK inference API") as demo:
64
+ gr.Markdown(
65
+ "# PFF-PK inference API\n"
66
+ "Server-side zero-shot PK cohort generation for the "
67
+ "[PFF dashboard](https://dfaroughy.github.io/pff-pk-dashboard/empirical/)."
68
+ )
69
+ request = gr.JSON(label="Inference request", value={})
70
+ result = gr.JSON(label="Inference response")
71
+ run = gr.Button("Run inference", variant="primary")
72
+ run.click(
73
+ inference,
74
+ inputs=request,
75
+ outputs=result,
76
+ api_name="inference",
77
+ concurrency_limit=1,
78
+ concurrency_id="pff-inference",
79
+ )
80
+ status = gr.JSON(label="Service status")
81
+ check = gr.Button("Check service")
82
+ check.click(health, inputs=None, outputs=status, api_name="health")
83
+
84
+ demo.queue(max_size=32, default_concurrency_limit=1)
85
 
86
  if __name__ == "__main__":
87
+ demo.launch()
 
 
pff_pk/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (754 Bytes)
 
pff_pk/__pycache__/config.cpython-311.pyc DELETED
Binary file (23.9 kB)
 
pff_pk/__pycache__/measure.cpython-311.pyc DELETED
Binary file (11.7 kB)
 
pff_pk/__pycache__/model.cpython-311.pyc DELETED
Binary file (44.8 kB)
 
pff_pk/__pycache__/source_process.cpython-311.pyc DELETED
Binary file (13.7 kB)
 
pff_pk/data/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (1.15 kB)
 
pff_pk/data/__pycache__/acquisition.cpython-311.pyc DELETED
Binary file (9.51 kB)
 
pff_pk/data/__pycache__/corpus.cpython-311.pyc DELETED
Binary file (18.6 kB)
 
pff_pk/data/__pycache__/sampling.cpython-311.pyc DELETED
Binary file (679 Bytes)
 
pff_pk/data/__pycache__/splits.cpython-311.pyc DELETED
Binary file (15.2 kB)
 
pff_pk/data/__pycache__/studies.cpython-311.pyc DELETED
Binary file (834 Bytes)
 
pff_pk/data/__pycache__/tasks.cpython-311.pyc DELETED
Binary file (24.2 kB)
 
pff_pk/inference/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (794 Bytes)
 
pff_pk/inference/__pycache__/empirical.cpython-311.pyc DELETED
Binary file (38.6 kB)
 
pff_pk/inference/__pycache__/model.cpython-311.pyc DELETED
Binary file (3.41 kB)
 
pff_pk/inference/__pycache__/preprocessing.cpython-311.pyc DELETED
Binary file (2.85 kB)
 
pff_pk/models/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (267 Bytes)
 
pff_pk/models/architectures/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (412 Bytes)
 
pff_pk/models/architectures/__pycache__/blocks.cpython-311.pyc DELETED
Binary file (43.4 kB)
 
pff_pk/models/architectures/__pycache__/operator_attn.cpython-311.pyc DELETED
Binary file (5.39 kB)
 
pff_pk/models/architectures/__pycache__/vector_fields_pk.cpython-311.pyc DELETED
Binary file (26.5 kB)
 
requirements.txt CHANGED
@@ -1,7 +1,6 @@
1
- fastapi>=0.116,<1
2
  huggingface-hub>=1.0,<2
3
  lightning>=2.6,<2.7
4
  numpy>=1.26,<3
5
  pyyaml>=6,<7
6
  torch==2.8.0
7
- uvicorn>=0.35,<1
 
1
+ gradio>=6,<7
2
  huggingface-hub>=1.0,<2
3
  lightning>=2.6,<2.7
4
  numpy>=1.26,<3
5
  pyyaml>=6,<7
6
  torch==2.8.0
 
services/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (190 Bytes)
 
services/inference/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (200 Bytes)
 
services/inference/__pycache__/pff_service.cpython-311.pyc DELETED
Binary file (26.9 kB)
 
synthetic_priors/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (271 Bytes)
 
synthetic_priors/__pycache__/contracts.cpython-311.pyc DELETED
Binary file (39.1 kB)