edwardgl commited on
Commit
0f54627
·
verified ·
1 Parent(s): 84bee02

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -18,15 +18,6 @@ import rumblex
18
  from rumblex.audio import load_audio
19
  from rumblex.writers import WriterOptions
20
 
21
- try: # Hugging Face's ZeroGPU helper; a no-op outside ZeroGPU hardware
22
- import spaces
23
-
24
- gpu = spaces.GPU(duration=60)
25
- except ImportError:
26
-
27
- def gpu(fn):
28
- return fn
29
-
30
 
31
  ZERO_GPU = os.environ.get("SPACES_ZERO_GPU", "").lower() in ("1", "true")
32
  ON_GPU = ZERO_GPU or torch.cuda.is_available()
@@ -38,6 +29,27 @@ SPEAKER_COLOURS = ["#6366f1", "#d97706", "#059669", "#db2777"]
38
  OUT_ROOT = Path(tempfile.gettempdir()) / "rumblex_demo" # served to the browser (allowed_paths below)
39
  OUT_ROOT.mkdir(exist_ok=True)
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # On ZeroGPU the model is placed on cuda at import time (the GPU is attached inside @gpu calls). The dtype is
42
  # given explicitly so nothing queries the device before a GPU is attached.
43
  DEVICE = "cuda" if ON_GPU else "cpu"
@@ -259,6 +271,18 @@ def run(audio_path, language, line_width, progress=gr.Progress()):
259
  return status, view, words, [str(p) for p in files]
260
 
261
 
 
 
 
 
 
 
 
 
 
 
 
 
262
  THEME = gr.themes.Soft(
263
  primary_hue="indigo", font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"]
264
  )
@@ -289,7 +313,7 @@ with gr.Blocks(title="rumblex: word timestamps for Rumble-2-Flash") as demo:
289
  audio.stop_recording(run, [audio, language, width], outputs)
290
  # One click: load the example clip into the audio box and transcribe it.
291
  example.click(lambda: (EXAMPLE, "Auto-detect"), None, [audio, language]).then(
292
- run, [audio, language, width], outputs
293
  )
294
 
295
  if __name__ == "__main__":
 
18
  from rumblex.audio import load_audio
19
  from rumblex.writers import WriterOptions
20
 
 
 
 
 
 
 
 
 
 
21
 
22
  ZERO_GPU = os.environ.get("SPACES_ZERO_GPU", "").lower() in ("1", "true")
23
  ON_GPU = ZERO_GPU or torch.cuda.is_available()
 
29
  OUT_ROOT = Path(tempfile.gettempdir()) / "rumblex_demo" # served to the browser (allowed_paths below)
30
  OUT_ROOT.mkdir(exist_ok=True)
31
 
32
+
33
+ def gpu_seconds(audio_path, language=None, line_width=None, progress=None) -> int:
34
+ """GPU time to reserve on ZeroGPU. Each call uses up this much of the visitor's daily quota, so short
35
+ clips ask for little (logged-out visitors only get a few minutes)."""
36
+ try:
37
+ seconds = min(len(load_audio(audio_path)) / SR, MAX_SECONDS)
38
+ except Exception:
39
+ return 60
40
+ return int(min(120, 20 + seconds / 10))
41
+
42
+
43
+ try: # Hugging Face's ZeroGPU helper; a no-op outside ZeroGPU hardware
44
+ import spaces
45
+
46
+ gpu = spaces.GPU(duration=gpu_seconds)
47
+ except ImportError:
48
+
49
+ def gpu(fn):
50
+ return fn
51
+
52
+
53
  # On ZeroGPU the model is placed on cuda at import time (the GPU is attached inside @gpu calls). The dtype is
54
  # given explicitly so nothing queries the device before a GPU is attached.
55
  DEVICE = "cuda" if ON_GPU else "cpu"
 
271
  return status, view, words, [str(p) for p in files]
272
 
273
 
274
+ _example_results: dict = {}
275
+
276
+
277
+ def run_example(language, line_width, progress=gr.Progress()):
278
+ """The example clip, transcribed once per setting and then served from memory, so trying it costs
279
+ visitors none of their GPU quota after the first click."""
280
+ key = (language, line_width)
281
+ if key not in _example_results:
282
+ _example_results[key] = run(EXAMPLE, language, line_width, progress=progress)
283
+ return _example_results[key]
284
+
285
+
286
  THEME = gr.themes.Soft(
287
  primary_hue="indigo", font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"]
288
  )
 
313
  audio.stop_recording(run, [audio, language, width], outputs)
314
  # One click: load the example clip into the audio box and transcribe it.
315
  example.click(lambda: (EXAMPLE, "Auto-detect"), None, [audio, language]).then(
316
+ run_example, [language, width], outputs
317
  )
318
 
319
  if __name__ == "__main__":