EYEDOL commited on
Commit
db4586b
·
verified ·
1 Parent(s): 594815b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -25
app.py CHANGED
@@ -1,42 +1,62 @@
1
  import gradio as gr
2
- import tempfile
3
  import numpy as np
 
 
4
  from faster_whisper import WhisperModel
5
- from scipy.io.wavfile import write
6
 
 
7
  model = WhisperModel(
8
- "tiny",
9
  device="cpu",
10
  compute_type="int8"
11
  )
12
 
13
- last_text = ""
 
14
 
15
  def transcribe(audio):
16
- global last_text
 
 
17
 
18
  if audio is None:
19
- return ""
20
 
21
- sr, y = audio
22
 
23
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
24
- write(f.name, sr, y.astype(np.int16))
25
 
26
- segments, _ = model.transcribe(
 
 
 
 
27
  f.name,
28
- language="en"
 
 
 
 
29
  )
30
 
31
- text = " ".join(
32
- segment.text for segment in segments
33
- ).strip()
 
34
 
35
- if text == last_text:
36
- return last_text
37
 
38
- last_text = text
39
- return text
 
 
 
 
 
 
 
40
 
41
 
42
  with gr.Blocks() as demo:
@@ -44,20 +64,21 @@ with gr.Blocks() as demo:
44
  gr.Markdown("# Real-Time English Speech Recognition")
45
 
46
  audio = gr.Audio(
47
- streaming=True,
48
  sources=["microphone"],
 
49
  type="numpy"
50
  )
51
 
52
- text = gr.Textbox(
53
- label="Live Transcript",
54
- lines=10
55
  )
56
 
57
  audio.stream(
58
- transcribe,
59
- audio,
60
- text
 
61
  )
62
 
63
  demo.launch()
 
1
  import gradio as gr
 
2
  import numpy as np
3
+ import soundfile as sf
4
+ import tempfile
5
  from faster_whisper import WhisperModel
 
6
 
7
+ # Fastest practical model
8
  model = WhisperModel(
9
+ "turbo",
10
  device="cpu",
11
  compute_type="int8"
12
  )
13
 
14
+ full_transcript = ""
15
+ last_segment = ""
16
 
17
  def transcribe(audio):
18
+
19
+ global full_transcript
20
+ global last_segment
21
 
22
  if audio is None:
23
+ return full_transcript
24
 
25
+ sr, data = audio
26
 
27
+ if len(data) < sr // 2:
28
+ return full_transcript
29
 
30
+ with tempfile.NamedTemporaryFile(suffix=".wav") as f:
31
+
32
+ sf.write(f.name, data, sr)
33
+
34
+ segments, info = model.transcribe(
35
  f.name,
36
+ language="en",
37
+ vad_filter=True,
38
+ beam_size=1,
39
+ best_of=1,
40
+ temperature=0
41
  )
42
 
43
+ current_text = " ".join(
44
+ segment.text.strip()
45
+ for segment in segments
46
+ )
47
 
48
+ if not current_text:
49
+ return full_transcript
50
 
51
+ if current_text != last_segment:
52
+
53
+ if full_transcript:
54
+ full_transcript += " "
55
+
56
+ full_transcript += current_text
57
+ last_segment = current_text
58
+
59
+ return full_transcript
60
 
61
 
62
  with gr.Blocks() as demo:
 
64
  gr.Markdown("# Real-Time English Speech Recognition")
65
 
66
  audio = gr.Audio(
 
67
  sources=["microphone"],
68
+ streaming=True,
69
  type="numpy"
70
  )
71
 
72
+ output = gr.Textbox(
73
+ label="Transcript",
74
+ lines=12
75
  )
76
 
77
  audio.stream(
78
+ fn=transcribe,
79
+ inputs=audio,
80
+ outputs=output,
81
+ stream_every=1
82
  )
83
 
84
  demo.launch()