File size: 2,032 Bytes
c494b72
efc0dfb
0c0335c
efc0dfb
 
0c0335c
 
 
 
 
 
d7d5bfb
 
0c0335c
 
 
d7d5bfb
 
 
0c0335c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7d5bfb
 
 
 
 
 
 
 
 
 
0c0335c
 
 
 
 
 
 
 
 
 
d7d5bfb
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from transformers import pipeline
import gradio as gr
from pytube import YouTube


def dl_youtube_audio(youtube_link):
    yt = YouTube(youtube_link)
    audio_stream = yt.streams.filter(only_audio=True).first()
    audio_path = f"youtube_audio_{yt.video_id}.mp3"
    audio_stream.download(output_path=".", filename="youtube_audio")
    return audio_path


def transcribe_audio(audio_path):
    model = pipeline(model="SofiaK/checkpoints")
    return model(audio_path)["text"]


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            radio = gr.Radio(
                choices=["Audio", "Youtube"],
                label="Choose your input type",
                value="Audio",
            )
            audio_input = gr.Audio(
                sources=["upload", "microphone"],
                type="filepath",
                label="Upload Audio, or speak in the microphone",
                visible=True,
                interactive=True,
            )
            youtube_input = gr.Textbox(
                value="https://www.youtube.com/",
                label="Youtube Link",
                visible=False,
                interactive=True,
            )
            btn = gr.Button(
                "Transcript",
            )
        with gr.Column():
            output = gr.Text(label="Model output")

    def make_visible(val):
        audio_visible = val == "Audio"
        return {
            audio_input: {"visible": audio_visible, "__type__": "update"},
            youtube_input: {"visible": not audio_visible, "__type__": "update"},
        }

    radio.change(make_visible, inputs=radio, outputs=[audio_input, youtube_input])

    def on_button_click():
        if radio.value == "Audio":
            audio_path = audio_input.value
        else:
            audio_path = dl_youtube_audio(youtube_input.value)
        output.value = transcribe_audio(audio_path)

    btn.click(
        fn=on_button_click, inputs=[radio, audio_input, youtube_input], outputs=output
    )

demo.launch()