Spaces:
Sleeping
Sleeping
hubsnippetai
commited on
Commit
•
a93e5a6
1
Parent(s):
eb995a9
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
6 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
7 |
+
|
8 |
+
model_id = "distil-whisper/distil-small.en"
|
9 |
+
|
10 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
11 |
+
model_id, torch_dtype=torch_dtype, use_safetensors=True
|
12 |
+
)
|
13 |
+
model.to(device)
|
14 |
+
|
15 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
16 |
+
|
17 |
+
pipe = pipeline(
|
18 |
+
"automatic-speech-recognition",
|
19 |
+
model=model,
|
20 |
+
tokenizer=processor.tokenizer,
|
21 |
+
feature_extractor=processor.feature_extractor,
|
22 |
+
max_new_tokens=128,
|
23 |
+
torch_dtype=torch_dtype,
|
24 |
+
device=device,
|
25 |
+
)
|
26 |
+
|
27 |
+
def audio2text(audio_file):
|
28 |
+
output=pipe(audio_file)
|
29 |
+
return output['text']
|
30 |
+
|
31 |
+
|
32 |
+
gr.Interface(fn=audio2text, inputs=[gr.Audio, label='upload your audio file', source='upload', type='filepath'], outputs=[gr.Textbox, label="transcription"]).launch()
|