Magic51 commited on
Commit
0854e14
·
verified ·
1 Parent(s): 56da75f

Initialize ZeroGPU startup hook for mounted Gradio service

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -26,6 +26,12 @@ import gradio as gr
26
  import spaces
27
  from fastapi import FastAPI
28
  from interval.server import app as research_app, status
 
 
 
 
 
 
29
 
30
 
31
  @spaces.GPU(duration=30)
@@ -33,13 +39,11 @@ def check_gpu_planner(request):
33
  """Optional GPU inference check; the research workspace executes on CPU."""
34
  if not isinstance(request, str) or not 3 <= len(request) <= 1500:
35
  raise gr.Error("Enter a research request of 3–1500 characters.")
36
- import torch
37
- from interval.planner import load_model, score_candidates
38
- tokenizer, model = load_model()
39
- model.to("cuda")
40
- scores = score_candidates(tokenizer, model, request)
41
  return {"recipe": max(scores, key=scores.get), "candidate_scores": scores,
42
- "device": str(next(model.parameters()).device),
43
  "scope": "Diagnostic inference only; numerical execution is available in the main workspace."}
44
 
45
 
@@ -52,9 +56,15 @@ with gr.Blocks() as diagnostics:
52
  gpu_output = gr.JSON(label="GPU procedure proposal")
53
  gr.Button("Check adapted planner on GPU").click(check_gpu_planner, inputs=request, outputs=gpu_output, api_name="gpu_planner_check")
54
 
 
55
  app = gr.mount_gradio_app(FastAPI(), diagnostics, path="/runtime-status")
56
  app.mount("/", research_app)
57
 
58
  if __name__ == "__main__":
59
  import uvicorn
 
 
 
 
 
60
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
26
  import spaces
27
  from fastapi import FastAPI
28
  from interval.server import app as research_app, status
29
+ from interval.planner import load_model, score_candidates
30
+
31
+ # ZeroGPU intercepts this placement, then packs weights before serving requests.
32
+ # The CPU research planner retains its independent cache and numerical path.
33
+ gpu_tokenizer, gpu_model = load_model()
34
+ gpu_model.to("cuda")
35
 
36
 
37
  @spaces.GPU(duration=30)
 
39
  """Optional GPU inference check; the research workspace executes on CPU."""
40
  if not isinstance(request, str) or not 3 <= len(request) <= 1500:
41
  raise gr.Error("Enter a research request of 3–1500 characters.")
42
+ from time import perf_counter
43
+ started = perf_counter()
44
+ scores = score_candidates(gpu_tokenizer, gpu_model, request)
 
 
45
  return {"recipe": max(scores, key=scores.get), "candidate_scores": scores,
46
+ "device": str(next(gpu_model.parameters()).device), "inference_seconds": perf_counter() - started,
47
  "scope": "Diagnostic inference only; numerical execution is available in the main workspace."}
48
 
49
 
 
56
  gpu_output = gr.JSON(label="GPU procedure proposal")
57
  gr.Button("Check adapted planner on GPU").click(check_gpu_planner, inputs=request, outputs=gpu_output, api_name="gpu_planner_check")
58
 
59
+ diagnostics.queue()
60
  app = gr.mount_gradio_app(FastAPI(), diagnostics, path="/runtime-status")
61
  app.mount("/", research_app)
62
 
63
  if __name__ == "__main__":
64
  import uvicorn
65
+ # mount_gradio_app uses Uvicorn instead of Blocks.launch, so explicitly run
66
+ # the SDK's normal launch hook after registration of the real GPU function.
67
+ from spaces import zero
68
+ if hasattr(zero, "startup"):
69
+ zero.startup()
70
  uvicorn.run(app, host="0.0.0.0", port=7860)