itsmariamaraki commited on
Commit
470a6ad
·
1 Parent(s): 1644496

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -1,26 +1,39 @@
1
- import gradio as gr
2
- import librosa
3
- import io
4
- from IPython.display import Audio
5
  from transformers import pipeline
6
- import numpy as np
7
 
8
- transcription = pipeline('automatic-speech-recognition', model='openai/whisper-base')
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def asr_app(audio):
11
- audio = np.frombuffer(audio['data'], dtype=np.int16)
12
- librosa.output.write_wav('audio_input.wav', audio, 16000)
13
- y, sr = librosa.load('audio_input.wav', sr=None)
14
- result = transcription(y)
15
 
16
- return result[0]['text'] #since my original code had an error as output, i tried to apply the command you uploaded for clarification (regarding the variable audio as a tuple)
17
 
 
 
 
 
 
18
 
19
- audio_input = gr.Audio()
20
- demo = gr.Interface(
21
- fn = asr_app,
22
- inputs = audio_input,
23
- outputs = 'text'
24
  )
25
 
 
 
 
 
 
 
26
  demo.launch()
 
 
 
 
 
1
  from transformers import pipeline
2
+ pipe = pipeline('automatic-speech-recognition', model='openai/whisper-small')
3
 
4
+ def transcribe_speech(filepath):
5
+ output = pipe(
6
+ filepath,
7
+ max_new_tokens=256,
8
+ generate_kwargs={
9
+ "task": "transcribe",
10
+ "language": "english",
11
+ },
12
+ chunk_length_s=30,
13
+ batch_size=8,
14
+ )
15
+ return output["text"]
16
 
17
+ import gradio as gr
 
 
 
 
18
 
19
+ demo = gr.Blocks()
20
 
21
+ mic_transcribe = gr.Interface(
22
+ fn=transcribe_speech,
23
+ inputs=gr.Audio(sources="microphone", type="filepath"),
24
+ outputs='text',
25
+ )
26
 
27
+ file_transcribe = gr.Interface(
28
+ fn=transcribe_speech,
29
+ inputs=gr.Audio(sources="upload", type="filepath"),
30
+ outputs='text',
 
31
  )
32
 
33
+ with demo:
34
+ gr.TabbedInterface(
35
+ [mic_transcribe, file_transcribe],
36
+ ["Transcribe Microphone", "Transcribe Audio File"],
37
+ )
38
+
39
  demo.launch()