denis-kazakov commited on
Commit
6b72e76
1 Parent(s): 79d8694

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+ import torch
4
+
5
+ device = 'cuda' if torch.cuda.is_available else 'cpu'
6
+
7
+ model = pipeline(
8
+ "automatic-speech-recognition",
9
+ device=device,
10
+ model='openai/whisper-large-v3',
11
+ chunk_length_s=30,
12
+ generate_kwargs={"task": "transcribe"}
13
+ )
14
+
15
+ def transcribe_audio(mic=None, file=None, return_timestamps=False):
16
+ if mic is not None:
17
+ audio = mic
18
+ elif file is not None:
19
+ audio = file
20
+ else:
21
+ return "You must either provide a mic recording or a file"
22
+
23
+ gr.Info(f"Device: {device}")
24
+ with torch.no_grad():
25
+ result = model(audio, return_timestamps=return_timestamps, batch_size=8)
26
+ if return_timestamps:
27
+ return result['chunks']
28
+ else:
29
+ return result['text']
30
+
31
+
32
+ gr.Interface(
33
+ fn=transcribe_audio,
34
+ inputs=[
35
+ gr.Audio(sources=["microphone"], type="filepath"),
36
+ gr.Audio(sources=["upload"], type="filepath"),
37
+ gr.Checkbox(label="Add timestamps?")
38
+ ],
39
+ outputs="text",
40
+ ).launch()
41
+
42
+