edwardgl commited on
Commit
4e26536
·
verified ·
1 Parent(s): 0f54627

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +19 -14
  2. example_result.json +133 -0
app.py CHANGED
@@ -3,6 +3,7 @@
3
  from __future__ import annotations
4
 
5
  import html
 
6
  import os
7
  import tempfile
8
  import time
@@ -251,8 +252,17 @@ def run(audio_path, language, line_width, progress=gr.Progress()):
251
  **kwargs,
252
  )
253
  took = time.perf_counter() - t0
 
 
 
 
 
 
 
 
 
 
254
  out_dir = Path(tempfile.mkdtemp(dir=OUT_ROOT))
255
- stem = Path(audio_path).stem or "audio"
256
  opts = WriterOptions(max_line_width=int(line_width) if line_width else None)
257
  files = rumblex.write_result(result, f"{stem}.wav", out_dir, ["srt", "vtt", "json"], opts)
258
  played = out_dir / "audio.wav"
@@ -261,26 +271,21 @@ def run(audio_path, language, line_width, progress=gr.Progress()):
261
  [w.word.strip(), round(w.start, 2), round(w.end, 2)] for s in result.segments for w in (s.words or [])
262
  ]
263
  audio_s = len(wav) / SR
264
- # On short clips the fixed start-up cost dominates, so a real-time factor would mislead.
265
- speed = f"done in {took:.1f} s"
266
- if audio_s >= 30:
267
- speed += f" ({audio_s / max(took, 1e-6):.0f}× real time)"
268
  stats = [f"{audio_s:.1f} s of audio", speed, f"language: {result.language}", f"{len(words)} words"]
269
  status = note or "Press ▶: each word lights up as it is spoken. Click any word to jump to it."
270
  view = result_html(result, f"/gradio_api/file={quote(str(played))}", audio_s, stats)
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(
@@ -313,7 +318,7 @@ with gr.Blocks(title="rumblex: word timestamps for Rumble-2-Flash") as demo:
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__":
 
3
  from __future__ import annotations
4
 
5
  import html
6
+ import json
7
  import os
8
  import tempfile
9
  import time
 
252
  **kwargs,
253
  )
254
  took = time.perf_counter() - t0
255
+ audio_s = len(wav) / SR
256
+ # On short clips the fixed start-up cost dominates, so a real-time factor would mislead.
257
+ speed = f"done in {took:.1f} s"
258
+ if audio_s >= 30:
259
+ speed += f" ({audio_s / max(took, 1e-6):.0f}× real time)"
260
+ return show(result, wav, Path(audio_path).stem or "audio", line_width, speed, note)
261
+
262
+
263
+ def show(result, wav, stem: str, line_width, speed: str, note: str = ""):
264
+ """Write the subtitle files and build the player, word table and status for a transcript."""
265
  out_dir = Path(tempfile.mkdtemp(dir=OUT_ROOT))
 
266
  opts = WriterOptions(max_line_width=int(line_width) if line_width else None)
267
  files = rumblex.write_result(result, f"{stem}.wav", out_dir, ["srt", "vtt", "json"], opts)
268
  played = out_dir / "audio.wav"
 
271
  [w.word.strip(), round(w.start, 2), round(w.end, 2)] for s in result.segments for w in (s.words or [])
272
  ]
273
  audio_s = len(wav) / SR
 
 
 
 
274
  stats = [f"{audio_s:.1f} s of audio", speed, f"language: {result.language}", f"{len(words)} words"]
275
  status = note or "Press ▶: each word lights up as it is spoken. Click any word to jump to it."
276
  view = result_html(result, f"/gradio_api/file={quote(str(played))}", audio_s, stats)
277
  return status, view, words, [str(p) for p in files]
278
 
279
 
280
+ # The example's transcript ships with the Space (made with make_example.py on CPU), so trying the example
281
+ # needs no GPU: it works for every visitor and uses none of their ZeroGPU quota.
282
+ EXAMPLE_RESULT = Path(__file__).with_name("example_result.json")
283
 
284
 
285
+ def run_example(line_width):
286
+ result = rumblex.TranscriptResult.from_dict(json.loads(EXAMPLE_RESULT.read_text(encoding="utf-8")))
287
+ wav = load_audio(EXAMPLE)
288
+ return show(result, wav, Path(EXAMPLE).stem, line_width, "precomputed, no GPU needed")
 
 
 
289
 
290
 
291
  THEME = gr.themes.Soft(
 
318
  audio.stop_recording(run, [audio, language, width], outputs)
319
  # One click: load the example clip into the audio box and transcribe it.
320
  example.click(lambda: (EXAMPLE, "Auto-detect"), None, [audio, language]).then(
321
+ run_example, [width], outputs
322
  )
323
 
324
  if __name__ == "__main__":
example_result.json ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "segments": [
3
+ {
4
+ "start": 0.51,
5
+ "end": 5.6,
6
+ "text": "Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.",
7
+ "words": [
8
+ {
9
+ "word": "Mr.",
10
+ "start": 0.51,
11
+ "end": 0.83,
12
+ "score": 0.375,
13
+ "speaker": null
14
+ },
15
+ {
16
+ "word": "Quilter",
17
+ "start": 0.83,
18
+ "end": 1.19,
19
+ "score": 0.241,
20
+ "speaker": null
21
+ },
22
+ {
23
+ "word": "is",
24
+ "start": 1.19,
25
+ "end": 1.35,
26
+ "score": 0.892,
27
+ "speaker": null
28
+ },
29
+ {
30
+ "word": "the",
31
+ "start": 1.35,
32
+ "end": 1.61,
33
+ "score": 0.679,
34
+ "speaker": null
35
+ },
36
+ {
37
+ "word": "apostle",
38
+ "start": 1.67,
39
+ "end": 2.11,
40
+ "score": 0.229,
41
+ "speaker": null
42
+ },
43
+ {
44
+ "word": "of",
45
+ "start": 2.11,
46
+ "end": 2.27,
47
+ "score": 0.504,
48
+ "speaker": null
49
+ },
50
+ {
51
+ "word": "the",
52
+ "start": 2.27,
53
+ "end": 2.35,
54
+ "score": 0.987,
55
+ "speaker": null
56
+ },
57
+ {
58
+ "word": "middle",
59
+ "start": 2.35,
60
+ "end": 2.62,
61
+ "score": 0.969,
62
+ "speaker": null
63
+ },
64
+ {
65
+ "word": "classes,",
66
+ "start": 2.67,
67
+ "end": 3.27,
68
+ "score": 0.447,
69
+ "speaker": null
70
+ },
71
+ {
72
+ "word": "and",
73
+ "start": 3.27,
74
+ "end": 3.43,
75
+ "score": 0.997,
76
+ "speaker": null
77
+ },
78
+ {
79
+ "word": "we",
80
+ "start": 3.43,
81
+ "end": 3.59,
82
+ "score": 0.953,
83
+ "speaker": null
84
+ },
85
+ {
86
+ "word": "are",
87
+ "start": 3.59,
88
+ "end": 3.67,
89
+ "score": 0.982,
90
+ "speaker": null
91
+ },
92
+ {
93
+ "word": "glad",
94
+ "start": 3.67,
95
+ "end": 4.03,
96
+ "score": 0.647,
97
+ "speaker": null
98
+ },
99
+ {
100
+ "word": "to",
101
+ "start": 4.03,
102
+ "end": 4.43,
103
+ "score": 0.54,
104
+ "speaker": null
105
+ },
106
+ {
107
+ "word": "welcome",
108
+ "start": 4.43,
109
+ "end": 4.59,
110
+ "score": 0.993,
111
+ "speaker": null
112
+ },
113
+ {
114
+ "word": "his",
115
+ "start": 4.59,
116
+ "end": 4.84,
117
+ "score": 0.403,
118
+ "speaker": null
119
+ },
120
+ {
121
+ "word": "gospel.",
122
+ "start": 4.91,
123
+ "end": 5.6,
124
+ "score": 0.586,
125
+ "speaker": null
126
+ }
127
+ ],
128
+ "speaker": null,
129
+ "language": "English"
130
+ }
131
+ ],
132
+ "language": "English"
133
+ }