Spaces:
Running
Running
upgrade interface
Browse files
app.py
CHANGED
@@ -1,26 +1,24 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import soundfile as sf
|
3 |
-
import torch
|
4 |
import numpy as np
|
|
|
5 |
import librosa
|
6 |
from transformers import AutoProcessor, Wav2Vec2BertForCTC
|
7 |
-
import spaces
|
8 |
|
9 |
MODEL_NAME = "mikr/w2v-bert-2.0-czech-colab-cv16"
|
10 |
|
11 |
-
device = 0 if torch.cuda.is_available() else "cpu"
|
12 |
|
13 |
-
|
14 |
|
15 |
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
16 |
model = Wav2Vec2BertForCTC.from_pretrained(MODEL_NAME).to(device)
|
17 |
|
18 |
|
19 |
@spaces.GPU
|
20 |
-
def
|
21 |
a, s = librosa.load(audio_path, sr=16_000)
|
22 |
-
|
23 |
-
# inputs = processor(a, sampling_rate=s, return_tensors="pt")
|
24 |
input_values = processor(a, sampling_rate=s, return_tensors="pt").input_features
|
25 |
|
26 |
with torch.no_grad():
|
@@ -30,23 +28,80 @@ def transcribe(audio_path):
|
|
30 |
|
31 |
# transcribe speech
|
32 |
transcription = processor.batch_decode(predicted_ids)
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
fn=transcribe,
|
38 |
inputs=[
|
39 |
-
gr.Audio(sources="
|
|
|
40 |
],
|
41 |
outputs="text",
|
42 |
-
|
43 |
-
title="Czech W2V-BERT 2.0 speech encoder demo - transcribe Czech Audio",
|
44 |
description=(
|
45 |
-
"Transcribe audio inputs with the click of a button! Demo uses the fine-tuned"
|
46 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME})
|
47 |
"and 🤗 Transformers to transcribe audio files of arbitrary length."
|
48 |
),
|
49 |
allow_flagging="never",
|
50 |
)
|
51 |
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import spaces
|
3 |
import gradio as gr
|
4 |
import soundfile as sf
|
|
|
5 |
import numpy as np
|
6 |
+
import pytube as pt
|
7 |
import librosa
|
8 |
from transformers import AutoProcessor, Wav2Vec2BertForCTC
|
|
|
9 |
|
10 |
MODEL_NAME = "mikr/w2v-bert-2.0-czech-colab-cv16"
|
11 |
|
|
|
12 |
|
13 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
14 |
|
15 |
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
16 |
model = Wav2Vec2BertForCTC.from_pretrained(MODEL_NAME).to(device)
|
17 |
|
18 |
|
19 |
@spaces.GPU
|
20 |
+
def text_from_audio(audio_path):
|
21 |
a, s = librosa.load(audio_path, sr=16_000)
|
|
|
|
|
22 |
input_values = processor(a, sampling_rate=s, return_tensors="pt").input_features
|
23 |
|
24 |
with torch.no_grad():
|
|
|
28 |
|
29 |
# transcribe speech
|
30 |
transcription = processor.batch_decode(predicted_ids)
|
31 |
+
text = transcription[0]
|
32 |
+
return text
|
33 |
+
|
34 |
+
|
35 |
+
def transcribe(microphone, file_upload):
|
36 |
+
warn_output = ""
|
37 |
+
if (microphone is not None) and (file_upload is not None):
|
38 |
+
warn_output = (
|
39 |
+
"WARNING: You've uploaded an audio file and used the microphone. "
|
40 |
+
"The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
|
41 |
+
)
|
42 |
+
|
43 |
+
elif (microphone is None) and (file_upload is None):
|
44 |
+
return "ERROR: You have to either use the microphone or upload an audio file"
|
45 |
+
|
46 |
+
audio_path = microphone if microphone is not None else file_upload
|
47 |
+
|
48 |
+
text = text_from_audio(audio_path)
|
49 |
+
|
50 |
+
return warn_output + text
|
51 |
|
52 |
|
53 |
+
def _return_yt_html_embed(yt_url):
|
54 |
+
video_id = yt_url.split("?v=")[-1]
|
55 |
+
HTML_str = (
|
56 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
57 |
+
" </center>"
|
58 |
+
)
|
59 |
+
return HTML_str
|
60 |
+
|
61 |
+
|
62 |
+
def yt_transcribe(yt_url):
|
63 |
+
yt = pt.YouTube(yt_url)
|
64 |
+
html_embed_str = _return_yt_html_embed(yt_url)
|
65 |
+
stream = yt.streams.filter(only_audio=True)[0]
|
66 |
+
stream.download(filename="audio.mp3")
|
67 |
+
|
68 |
+
text = text_from_audio("audio.mp3")
|
69 |
+
|
70 |
+
return html_embed_str, text
|
71 |
+
|
72 |
+
|
73 |
+
demo = gr.Blocks()
|
74 |
+
|
75 |
+
mf_transcribe = gr.Interface(
|
76 |
fn=transcribe,
|
77 |
inputs=[
|
78 |
+
gr.Audio(sources="microphone", type="filepath"),
|
79 |
+
gr.Audio(sources="upload", type="filepath"),
|
80 |
],
|
81 |
outputs="text",
|
82 |
+
title="W2V Bert 2.0 Demo: Transcribe Czech Audio",
|
|
|
83 |
description=(
|
84 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the fine-tuned"
|
85 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) "
|
86 |
"and 🤗 Transformers to transcribe audio files of arbitrary length."
|
87 |
),
|
88 |
allow_flagging="never",
|
89 |
)
|
90 |
|
91 |
+
yt_transcribe = gr.Interface(
|
92 |
+
fn=yt_transcribe,
|
93 |
+
inputs=[gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
|
94 |
+
outputs=["html", "text"],
|
95 |
+
title="W2V Bert 2.0 Demo: Transcribe Czech YouTube Video",
|
96 |
+
description=(
|
97 |
+
"Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
|
98 |
+
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
|
99 |
+
" arbitrary length."
|
100 |
+
),
|
101 |
+
allow_flagging="never",
|
102 |
+
)
|
103 |
+
|
104 |
+
with demo:
|
105 |
+
gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
|
106 |
+
|
107 |
+
demo.launch(server_name="0.0.0.0")
|