Ramosh99 commited on
Commit
afd5b3c
Β·
1 Parent(s): 9fd092c

perf: async endpoint + startup warmup to reduce ZeroGPU cold-start latency

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import spaces
2
  import os
 
3
  import gradio as gr
4
  from transformers import pipeline
5
  from fastapi import HTTPException
@@ -66,7 +67,6 @@ demo = gr.Interface(
66
  )
67
 
68
  # ── Launch Gradio (non-blocking) β€” properly initialises ZeroGPU ───────────────
69
- # demo.launch() returns Gradio's internal FastAPI app which we extend below.
70
  gr_app, _local_url, _share_url = demo.launch(
71
  server_name="0.0.0.0",
72
  server_port=7860,
@@ -76,7 +76,6 @@ gr_app, _local_url, _share_url = demo.launch(
76
 
77
  # ── REST API routes added to Gradio's FastAPI app ─────────────────────────────
78
 
79
-
80
  class DetectRequest(BaseModel):
81
  model_config = ConfigDict(
82
  json_schema_extra={"example": {"text": "Mage name Kamal, phone 071-1234567"}}
@@ -104,7 +103,7 @@ def health_check():
104
 
105
 
106
  @gr_app.post("/api/detect", response_model=DetectResponse)
107
- def api_detect_pii(request: DetectRequest):
108
  """
109
  Detect PII entities in Sinhala-English code-mixed text.
110
 
@@ -129,5 +128,16 @@ def api_detect_pii(request: DetectRequest):
129
  }
130
 
131
 
 
 
 
 
 
 
 
 
 
 
 
132
  # ── Keep the process alive ─────────────────────────────────────────────────────
133
  demo.block_thread()
 
1
  import spaces
2
  import os
3
+ import threading
4
  import gradio as gr
5
  from transformers import pipeline
6
  from fastapi import HTTPException
 
67
  )
68
 
69
  # ── Launch Gradio (non-blocking) β€” properly initialises ZeroGPU ───────────────
 
70
  gr_app, _local_url, _share_url = demo.launch(
71
  server_name="0.0.0.0",
72
  server_port=7860,
 
76
 
77
  # ── REST API routes added to Gradio's FastAPI app ─────────────────────────────
78
 
 
79
  class DetectRequest(BaseModel):
80
  model_config = ConfigDict(
81
  json_schema_extra={"example": {"text": "Mage name Kamal, phone 071-1234567"}}
 
103
 
104
 
105
  @gr_app.post("/api/detect", response_model=DetectResponse)
106
+ async def api_detect_pii(request: DetectRequest):
107
  """
108
  Detect PII entities in Sinhala-English code-mixed text.
109
 
 
128
  }
129
 
130
 
131
+ # ── Startup warmup β€” pre-warms ZeroGPU so first real request is fast ──────────
132
+ def _warmup():
133
+ try:
134
+ print("[warmup] Pre-warming ZeroGPU GPU allocation...")
135
+ detect_pii("warmup kamal@test.com 0771234567")
136
+ print("[warmup] Done.")
137
+ except Exception as e:
138
+ print(f"[warmup] Skipped: {e}")
139
+
140
+ threading.Thread(target=_warmup, daemon=True).start()
141
+
142
  # ── Keep the process alive ─────────────────────────────────────────────────────
143
  demo.block_thread()