Konstantin Dorichev commited on
Commit
9f08cb3
·
unverified ·
1 Parent(s): 68c743d

Block based app

Browse files
Files changed (1) hide show
  1. app.py +27 -18
app.py CHANGED
@@ -6,9 +6,11 @@ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-bas
6
 
7
 
8
  def transcribe(stream, new_chunk):
9
- # if stream is not None:
10
- # print(stream.shape, new_chunk)
11
- sr, y = new_chunk
 
 
12
 
13
  # Convert to mono if stereo
14
  if y.ndim > 1:
@@ -17,20 +19,27 @@ def transcribe(stream, new_chunk):
17
  y = y.astype(np.float32)
18
  y /= np.max(np.abs(y))
19
 
20
- if stream is not None:
21
- stream = np.concatenate([stream, y])
22
- else:
23
- stream = y
24
- text = transcriber({"sampling_rate": sr, "raw": stream})["text"]
25
- # print(text)
26
- return stream, text
27
-
28
-
29
- demo = gr.Interface(
30
- transcribe,
31
- ["state", gr.Audio(sources=["upload"], streaming=False)],
32
- ["state", "text"],
33
- # live=True,
34
- )
 
 
 
 
 
 
 
35
 
36
  demo.launch()
 
6
 
7
 
8
  def transcribe(stream, new_chunk):
9
+
10
+ if stream is None:
11
+ return ""
12
+
13
+ sr, y = stream
14
 
15
  # Convert to mono if stereo
16
  if y.ndim > 1:
 
19
  y = y.astype(np.float32)
20
  y /= np.max(np.abs(y))
21
 
22
+ text = transcriber({"sampling_rate": sr, "raw": y})["text"]
23
+ return text
24
+
25
+
26
+ def clear(audio, transcribed):
27
+ audio = None
28
+ transcribed = None
29
+ return audio, transcribed
30
+
31
+
32
+ with gr.Blocks() as demo:
33
+ gr.HTML(value="<h1>Transcribe Audio to Text Demo</h1>")
34
+ with gr.Row():
35
+ with gr.Column():
36
+ audio = gr.Audio(sources=["upload"], streaming=False, label="wav")
37
+ with gr.Row():
38
+ clr = gr.Button(value="Clear", variant="huggingface")
39
+ btn = gr.Button(value="Transcribe", variant="primary")
40
+ transcribed = gr.TextArea(label="Transcribed", lines=9)
41
+
42
+ btn.click(fn=transcribe, inputs=audio, outputs=transcribed)
43
+ clr.click(fn=clear, inputs=[audio, transcribed], outputs=[audio, transcribed])
44
 
45
  demo.launch()