aniketqxp commited on
Commit
537dc81
·
1 Parent(s): f048655

fix: expose ZeroGPU handler from Space entrypoint

Browse files
Files changed (2) hide show
  1. app.py +21 -2
  2. backend/app/space.py +23 -39
app.py CHANGED
@@ -3,6 +3,8 @@ import os
3
  import sys
4
  from pathlib import Path
5
 
 
 
6
 
7
  ROOT = Path(__file__).resolve().parent
8
  RUNTIME_ROOT = Path(os.environ.get("CAPSTONE_RUNTIME_ROOT", "/tmp/call-qa"))
@@ -33,11 +35,28 @@ def _prepare_runtime() -> None:
33
  _prepare_runtime()
34
  sys.path.insert(0, str(ROOT / "backend"))
35
 
36
- from app.space import app # noqa: E402
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
39
  if __name__ == "__main__":
40
  import uvicorn
41
 
42
- port = int(os.environ.get("GRADIO_SERVER_PORT", os.environ.get("PORT", "7860")))
 
43
  uvicorn.run(app, host="0.0.0.0", port=port)
 
3
  import sys
4
  from pathlib import Path
5
 
6
+ import spaces
7
+
8
 
9
  ROOT = Path(__file__).resolve().parent
10
  RUNTIME_ROOT = Path(os.environ.get("CAPSTONE_RUNTIME_ROOT", "/tmp/call-qa"))
 
35
  _prepare_runtime()
36
  sys.path.insert(0, str(ROOT / "backend"))
37
 
38
+
39
+ @spaces.GPU(duration=10)
40
+ def gpu_snapshot():
41
+ """Allocate a tiny tensor to verify that ZeroGPU scheduling is available."""
42
+ import torch
43
+
44
+ probe = torch.ones(1, device="cuda")
45
+ return {
46
+ "available": bool(torch.cuda.is_available()),
47
+ "device": torch.cuda.get_device_name(0),
48
+ "probe": float(probe.item()),
49
+ }
50
+
51
+
52
+ from app.space import build_space_app # noqa: E402
53
+
54
+ app = build_space_app(gpu_snapshot)
55
 
56
 
57
  if __name__ == "__main__":
58
  import uvicorn
59
 
60
+ default_port = "7861" if os.environ.get("SPACES_ZERO_GPU") else "7860"
61
+ port = int(os.environ.get("GRADIO_SERVER_PORT", os.environ.get("PORT", default_port)))
62
  uvicorn.run(app, host="0.0.0.0", port=port)
backend/app/space.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
 
3
  import gradio as gr
4
- import spaces
5
  from fastapi.responses import RedirectResponse
6
  from sqlalchemy import func
7
 
@@ -32,41 +31,26 @@ def runtime_snapshot():
32
  return snapshot
33
 
34
 
35
- @spaces.GPU(duration=10)
36
- def gpu_snapshot():
37
- """Allocate a tiny tensor to verify that ZeroGPU scheduling is available."""
38
- import torch
39
-
40
- probe = torch.ones(1, device="cuda")
41
- return {
42
- "available": bool(torch.cuda.is_available()),
43
- "device": torch.cuda.get_device_name(0),
44
- "probe": float(probe.item()),
45
- }
46
-
47
-
48
- with gr.Blocks(title="Call QA Processing") as demo:
49
- gr.Markdown("# Call QA Processing")
50
- state = gr.JSON(label="Runtime status")
51
- refresh = gr.Button("Refresh", variant="primary")
52
- refresh.click(runtime_snapshot, outputs=state, api_name="runtime_status")
53
- demo.load(runtime_snapshot, outputs=state)
54
- gpu_state = gr.JSON(label="ZeroGPU status")
55
- gpu_check = gr.Button("Check ZeroGPU")
56
- gpu_check.click(gpu_snapshot, outputs=gpu_state, api_name="gpu_status")
57
-
58
-
59
- # Replace the JSON root with the Space UI while preserving every API route.
60
- api.router.routes = [
61
- route
62
- for route in api.router.routes
63
- if not (getattr(route, "path", None) == "/" and "GET" in getattr(route, "methods", set()))
64
- ]
65
-
66
-
67
- @api.get("/", include_in_schema=False)
68
- def space_root():
69
- return RedirectResponse(url="/gradio/")
70
-
71
-
72
- app = gr.mount_gradio_app(api, demo, path="/gradio")
 
1
  import os
2
 
3
  import gradio as gr
 
4
  from fastapi.responses import RedirectResponse
5
  from sqlalchemy import func
6
 
 
31
  return snapshot
32
 
33
 
34
+ def build_space_app(gpu_snapshot):
35
+ with gr.Blocks(title="Call QA Processing") as demo:
36
+ gr.Markdown("# Call QA Processing")
37
+ state = gr.JSON(label="Runtime status")
38
+ refresh = gr.Button("Refresh", variant="primary")
39
+ refresh.click(runtime_snapshot, outputs=state, api_name="runtime_status")
40
+ demo.load(runtime_snapshot, outputs=state)
41
+ gpu_state = gr.JSON(label="ZeroGPU status")
42
+ gpu_check = gr.Button("Check ZeroGPU")
43
+ gpu_check.click(gpu_snapshot, outputs=gpu_state, api_name="gpu_status")
44
+
45
+ # Replace the JSON root with the Space UI while preserving every API route.
46
+ api.router.routes = [
47
+ route
48
+ for route in api.router.routes
49
+ if not (getattr(route, "path", None) == "/" and "GET" in getattr(route, "methods", set()))
50
+ ]
51
+
52
+ @api.get("/", include_in_schema=False)
53
+ def space_root():
54
+ return RedirectResponse(url="/gradio/")
55
+
56
+ return gr.mount_gradio_app(api, demo, path="/gradio")