Spaces:
Sleeping
Sleeping
sotirios-slv
commited on
Commit
·
8197c6e
1
Parent(s):
6186718
Added in Whisper function to convert speech-to-text
Browse files
app.py
CHANGED
@@ -1,13 +1,51 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import numpy as np
|
4 |
|
5 |
import gradio as gr
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def reverse_audio(audio):
|
9 |
-
sr, data = audio
|
10 |
-
|
|
|
|
|
11 |
|
12 |
|
13 |
input_audio = gr.Audio(
|
|
|
1 |
+
# import numpy as np
|
|
|
|
|
2 |
|
3 |
import gradio as gr
|
4 |
|
5 |
+
import torch
|
6 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
7 |
+
|
8 |
+
# from datasets import load_dataset
|
9 |
+
|
10 |
+
|
11 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
12 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
13 |
+
|
14 |
+
model_id = "openai/small"
|
15 |
+
# model_id = "openai/whisper-large-v3"
|
16 |
+
|
17 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
18 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
19 |
+
)
|
20 |
+
model.to(device)
|
21 |
+
|
22 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
23 |
+
|
24 |
+
pipe = pipeline(
|
25 |
+
"automatic-speech-recognition",
|
26 |
+
model=model,
|
27 |
+
tokenizer=processor.tokenizer,
|
28 |
+
feature_extractor=processor.feature_extractor,
|
29 |
+
max_new_tokens=128,
|
30 |
+
chunk_length_s=30,
|
31 |
+
batch_size=16,
|
32 |
+
return_timestamps=True,
|
33 |
+
torch_dtype=torch_dtype,
|
34 |
+
device=device,
|
35 |
+
)
|
36 |
+
|
37 |
+
# dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
|
38 |
+
# sample = dataset[0]["audio"]
|
39 |
+
|
40 |
+
# result = pipe(sample)
|
41 |
+
# print(result["text"])
|
42 |
+
|
43 |
|
44 |
def reverse_audio(audio):
|
45 |
+
# sr, data = audio
|
46 |
+
result = pipe(audio)
|
47 |
+
print(result["text"])
|
48 |
+
return result
|
49 |
|
50 |
|
51 |
input_audio = gr.Audio(
|