| import gradio as gr |
| import whisper |
| |
| from pytubefix import YouTube |
| from pytubefix.cli import on_progress |
|
|
| loaded_model = whisper.load_model("medium") |
|
|
| current_size = 'medium' |
|
|
| def inference(link): |
| |
| |
| |
| yt = YouTube(link, on_progress_callback=on_progress, client="WEB") |
| global audio_stream |
| audio_stream = yt.streams.filter(only_audio=True, file_extension='mp4').first() |
| path = audio_stream.download() |
| |
| |
| options = whisper.DecodingOptions(language= 'Spanish', without_timestamps=True) |
| results = loaded_model.transcribe(path) |
| return results['text'] |
|
|
| def change_model(size): |
| global loaded_model, current_size |
| if size == current_size: |
| return |
| loaded_model = whisper.load_model(size) |
| current_size = size |
|
|
| def populate_metadata(link): |
| yt = YouTube(link) |
| return yt.thumbnail_url, yt.title |
|
|
| title="" |
| description="" |
| block = gr.Blocks() |
| with block: |
| gr.HTML( |
| """ |
| <div style="text-align: center; max-width: 500px; margin: 0 auto;"> |
| <div> |
| </div> |
| </div> |
| """ |
| ) |
| with gr.Group(): |
| with gr.Group(): |
| sz = gr.Dropdown(label="Model Size", choices=['tiny', 'base','small', 'medium', 'large'], value='medium') |
| |
| link = gr.Textbox(label="YouTube Link") |
| |
| gr.Markdown("Ejemplo: https://www.youtube.com/watch?v=bnvgcQB01mQ") |
| |
| |
| with gr.Row(): |
| title = gr.Label(label="Video Title") |
| img = gr.Image(label="Thumbnail") |
| text = gr.Textbox( |
| label="Transcription", |
| placeholder="Transcription Output", |
| lines=5) |
| with gr.Row(): |
| btn = gr.Button("Transcribe") |
| |
| |
| |
| btn.click(inference, inputs=[link], outputs=[text]) |
| link.change(populate_metadata, inputs=[link], outputs=[img, title]) |
| sz.change(change_model, inputs=[sz], outputs=[]) |
|
|
| block.launch() |