sleeper371 commited on
Commit
0189aeb
·
1 Parent(s): 1cb4b1f

Add @spaces.GPU decorator for ZeroGPU Space compatibility

Browse files

ZeroGPU Spaces do not attach a GPU to the main process; they require at least one @spaces.GPU-decorated function so the scheduler knows what to attach a GPU to for the duration of the call, or startup fails with "No @spaces.GPU function detected during startup".

Decorate the button event handler (run_alignment), and import spaces before torch per ZeroGPU's required import order. Model loading already happened lazily on first align() call, so it now runs inside the GPU-attached window instead of at startup.

Files changed (3) hide show
  1. app.py +13 -0
  2. pyproject.toml +3 -0
  3. requirements.txt +5 -4
app.py CHANGED
@@ -9,6 +9,11 @@ Serves both:
9
 
10
  from __future__ import annotations
11
 
 
 
 
 
 
12
  import gradio as gr
13
 
14
  from aligner import MAX_AUDIO_SECONDS, SUPPORTED_LANGUAGES, align
@@ -21,11 +26,19 @@ EXAMPLE_AUDIO_URL = (
21
  EXAMPLE_TEXT = "甚至出现交易几乎停滞的情况。"
22
 
23
 
 
24
  def run_alignment(audio, text: str, language: str):
25
  """Event handler for the UI button, and the function exposed as the API.
26
 
27
  Returns (dataframe_rows, raw_json) so the UI shows both a readable
28
  table and the full structured payload; API callers get the same tuple.
 
 
 
 
 
 
 
29
  """
30
  try:
31
  spans = align(audio=audio, text=text, language=language)
 
9
 
10
  from __future__ import annotations
11
 
12
+ # `spaces` must be imported before `torch` (imported transitively via
13
+ # `aligner`) so it can patch CUDA initialization for ZeroGPU Spaces. On
14
+ # non-ZeroGPU hardware (or local dev), `spaces.GPU` is a harmless no-op.
15
+ import spaces
16
+
17
  import gradio as gr
18
 
19
  from aligner import MAX_AUDIO_SECONDS, SUPPORTED_LANGUAGES, align
 
26
  EXAMPLE_TEXT = "甚至出现交易几乎停滞的情况。"
27
 
28
 
29
+ @spaces.GPU(duration=120)
30
  def run_alignment(audio, text: str, language: str):
31
  """Event handler for the UI button, and the function exposed as the API.
32
 
33
  Returns (dataframe_rows, raw_json) so the UI shows both a readable
34
  table and the full structured payload; API callers get the same tuple.
35
+
36
+ `@spaces.GPU` is required on ZeroGPU Spaces: it's how the ZeroGPU
37
+ scheduler detects which functions need a GPU attached and allocates one
38
+ for the duration of the call. Model loading happens lazily inside
39
+ `aligner.get_model()`, on first call to `align()` below, so it also
40
+ runs inside this GPU-attached window (required — CUDA calls made
41
+ outside a `@spaces.GPU` call fail on ZeroGPU hardware).
42
  """
43
  try:
44
  spans = align(audio=audio, text=text, language=language)
pyproject.toml CHANGED
@@ -12,6 +12,9 @@ dependencies = [
12
  # via qwen-asr) — see requirements.txt for details. Matches README.md's
13
  # sdk_version.
14
  "gradio==6.17.3",
 
 
 
15
  "qwen-asr==0.0.6",
16
  "torch==2.11.0",
17
  "numpy==2.4.6",
 
12
  # via qwen-asr) — see requirements.txt for details. Matches README.md's
13
  # sdk_version.
14
  "gradio==6.17.3",
15
+ # ZeroGPU SDK: provides the `@spaces.GPU` decorator app.py uses. A
16
+ # no-op on non-ZeroGPU hardware / local dev.
17
+ "spaces==0.50.4",
18
  "qwen-asr==0.0.6",
19
  "torch==2.11.0",
20
  "numpy==2.4.6",
requirements.txt CHANGED
@@ -1,10 +1,11 @@
1
  # This is what the Hugging Face Space build actually installs (Spaces read
2
  # requirements.txt, not pyproject.toml). Keep in sync with pyproject.toml.
3
  #
4
- # gradio itself is intentionally NOT pinned here: the Space builder already
5
- # installs the exact `sdk_version` from README.md's frontmatter, and adding
6
- # a second, looser requirement for it here only grows the resolver's search
7
- # space.
 
8
  #
9
  # qwen-asr's own metadata leaves several transitive deps unpinned
10
  # (librosa, qwen-omni-utils, flask, sox, pytz), which combined with a bare
 
1
  # This is what the Hugging Face Space build actually installs (Spaces read
2
  # requirements.txt, not pyproject.toml). Keep in sync with pyproject.toml.
3
  #
4
+ # gradio and spaces are intentionally NOT pinned here: on a ZeroGPU Space,
5
+ # the builder already force-installs an exact `gradio[oauth,mcp]==...`
6
+ # (matching README.md's `sdk_version`) and `spaces==...` (the ZeroGPU SDK
7
+ # app.py imports for the `@spaces.GPU` decorator). Adding a second, looser
8
+ # requirement for either here only grows the resolver's search space.
9
  #
10
  # qwen-asr's own metadata leaves several transitive deps unpinned
11
  # (librosa, qwen-omni-utils, flask, sox, pytz), which combined with a bare