EYEDOL commited on
Commit
addb856
·
verified ·
1 Parent(s): b4eea26

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "small",
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:
43
+
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()