Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
|
|
3 |
import pytube as pt
|
4 |
from transformers import pipeline
|
5 |
|
6 |
-
MODEL_NAME = "ales/whisper-small-belarusian"
|
7 |
lang = "be"
|
8 |
|
9 |
device = 0 if torch.cuda.is_available() else "cpu"
|
@@ -20,52 +20,48 @@ pipe = pipeline(
|
|
20 |
pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe")
|
21 |
|
22 |
|
23 |
-
def transcribe(
|
24 |
-
if
|
25 |
-
return "ERROR: Please
|
26 |
-
|
27 |
-
return text
|
28 |
-
|
29 |
-
|
30 |
-
def _return_yt_html_embed(yt_url):
|
31 |
-
video_id = yt_url.split("?v=")[-1].split("&")[0]
|
32 |
-
return (
|
33 |
-
f'<center><iframe width="500" height="320" '
|
34 |
-
f'src="https://www.youtube.com/embed/{video_id}" '
|
35 |
-
f'frameborder="0" allowfullscreen></iframe></center>'
|
36 |
-
)
|
37 |
|
38 |
|
39 |
def yt_transcribe(yt_url):
|
40 |
if not yt_url:
|
41 |
return "", "ERROR: You must provide a YouTube URL."
|
42 |
-
|
43 |
yt = pt.YouTube(yt_url)
|
44 |
-
|
45 |
-
|
46 |
stream = yt.streams.filter(only_audio=True).first()
|
47 |
stream.download(filename="audio.mp3")
|
48 |
-
|
49 |
text = pipe("audio.mp3")["text"]
|
50 |
-
return
|
51 |
|
52 |
|
53 |
with gr.Blocks() as demo:
|
54 |
with gr.Tab("🎤 Transcribe Audio"):
|
55 |
-
gr.Markdown("##
|
56 |
audio_input = gr.Audio(type="filepath", label="Record or Upload Audio")
|
57 |
transcribe_button = gr.Button("Transcribe")
|
58 |
-
transcription_output = gr.Textbox(label="
|
59 |
|
60 |
-
transcribe_button.click(
|
|
|
|
|
|
|
|
|
61 |
|
62 |
with gr.Tab("📺 Transcribe YouTube"):
|
63 |
-
gr.Markdown("##
|
64 |
yt_input = gr.Textbox(label="YouTube URL", placeholder="https://www.youtube.com/watch?v=...")
|
65 |
yt_button = gr.Button("Transcribe YouTube")
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
yt_button.click(
|
|
|
|
|
|
|
|
|
70 |
|
71 |
demo.launch()
|
|
|
3 |
import pytube as pt
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
MODEL_NAME = "ales/whisper-small-belarusian"
|
7 |
lang = "be"
|
8 |
|
9 |
device = 0 if torch.cuda.is_available() else "cpu"
|
|
|
20 |
pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe")
|
21 |
|
22 |
|
23 |
+
def transcribe(audio_file):
|
24 |
+
if audio_file is None:
|
25 |
+
return "ERROR: Please upload or record audio"
|
26 |
+
return pipe(audio_file)["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
def yt_transcribe(yt_url):
|
30 |
if not yt_url:
|
31 |
return "", "ERROR: You must provide a YouTube URL."
|
|
|
32 |
yt = pt.YouTube(yt_url)
|
33 |
+
video_id = yt_url.split("?v=")[-1].split("&")[0]
|
34 |
+
embed = f'<center><iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}" frameborder="0" allowfullscreen></iframe></center>'
|
35 |
stream = yt.streams.filter(only_audio=True).first()
|
36 |
stream.download(filename="audio.mp3")
|
|
|
37 |
text = pipe("audio.mp3")["text"]
|
38 |
+
return embed, text
|
39 |
|
40 |
|
41 |
with gr.Blocks() as demo:
|
42 |
with gr.Tab("🎤 Transcribe Audio"):
|
43 |
+
gr.Markdown("## Запішы або загрузі аўдыё")
|
44 |
audio_input = gr.Audio(type="filepath", label="Record or Upload Audio")
|
45 |
transcribe_button = gr.Button("Transcribe")
|
46 |
+
transcription_output = gr.Textbox(label="Transcription")
|
47 |
|
48 |
+
transcribe_button.click(
|
49 |
+
fn=transcribe,
|
50 |
+
inputs=[audio_input],
|
51 |
+
outputs=[transcription_output],
|
52 |
+
)
|
53 |
|
54 |
with gr.Tab("📺 Transcribe YouTube"):
|
55 |
+
gr.Markdown("## Устаў спасылку на YouTube-відэа")
|
56 |
yt_input = gr.Textbox(label="YouTube URL", placeholder="https://www.youtube.com/watch?v=...")
|
57 |
yt_button = gr.Button("Transcribe YouTube")
|
58 |
+
yt_embed = gr.HTML()
|
59 |
+
yt_text = gr.Textbox(label="Transcription")
|
60 |
+
|
61 |
+
yt_button.click(
|
62 |
+
fn=yt_transcribe,
|
63 |
+
inputs=[yt_input],
|
64 |
+
outputs=[yt_embed, yt_text],
|
65 |
+
)
|
66 |
|
67 |
demo.launch()
|