|
import whisper |
|
from pytube import YouTube |
|
import gradio as gr |
|
import os |
|
import re |
|
import logging |
|
|
|
logging.basicConfig(level=logging.INFO) |
|
model = whisper.load_model("base") |
|
|
|
def get_text(url): |
|
|
|
if url != '': |
|
output_text_transcribe = '' |
|
|
|
yt = YouTube(url) |
|
|
|
|
|
video = yt.streams.filter(only_audio=True).first() |
|
out_file=video.download(output_path=".") |
|
|
|
file_stats = os.stat(out_file) |
|
logging.info(f'Size of audio file in Bytes: {file_stats.st_size}') |
|
|
|
if file_stats.st_size <= 30000000: |
|
base, ext = os.path.splitext(out_file) |
|
new_file = base+'.mp3' |
|
os.rename(out_file, new_file) |
|
a = new_file |
|
|
|
result = model.transcribe(a) |
|
return result['text'].strip() |
|
else: |
|
logging.error('Videos for transcription on this demo are limited to about 1.5 hours.') |
|
|
|
|
|
|
|
def get_summary(article): |
|
first_sentences = ' '.join(re.split(r'(?<=[.:;])\s', article)[:5]) |
|
b = summarizer(first_sentences, min_length = 20, max_length = 120, do_sample = False) |
|
b = b[0]['summary_text'].replace(' .', '.').strip() |
|
return b |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("<h1><center>Free Fast YouTube URL Video-to-Text") |
|
|
|
gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video.</center>") |
|
gr.Markdown("<center>Transcription takes 5-10 seconds per minute of the video (bad audio/hard accents slow it down a bit).</center>") |
|
|
|
input_text_url = gr.Textbox(placeholder='Youtube video URL', label='YouTube URL') |
|
result_button_transcribe = gr.Button('Transcribe') |
|
output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript') |
|
|
|
|
|
|
|
|
|
result_button_transcribe.click(get_text, inputs = input_text_url, outputs = output_text_transcribe) |
|
|
|
|
|
demo.queue(default_enabled = True).launch(debug = True) |