alamin655's picture
Update video transcription code to use yt_dlp instead of pytube
a821f59
raw
history blame
2.76 kB
import whisper
import yt_dlp
#from transformers import pipeline
import gradio as gr
import os
import re
model = whisper.load_model("base")
#summarizer = pipeline("summarization")
def get_text(url):
try:
if url != '':
output_text_transcribe = ''
with yt_dlp.YoutubeDL({'format': 'bestaudio', 'outtmpl': '%(id)s.%(ext)s'}) as ydl:
info_dict = ydl.extract_info(url, download=True)
audio_file = ydl.prepare_filename(info_dict)
result = model.transcribe(audio_file)
return result['text'].strip()
except Exception as e:
raise gr.InterfaceError(f"Exception: {e}. There was a problem getting the video or audio of the URL provided.")
#def get_summary(article):
#try:
#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
#except Exception as e:
#raise gr.InterfaceError(f"Exception: {e}. There was a problem summarizing the transcript.")
with gr.Blocks() as demo:
gr.Markdown("<h1><center>Free Fast YouTube URL Video-to-Text using <a href=https://openai.com/blog/whisper/ target=_blank>OpenAI's Whisper</a> Model</center></h1>")
#gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video and then create a summary of the video transcript.</center>")
gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video.</center>")
gr.Markdown("<center><b>'Whisper is a neural net that approaches human level robustness and accuracy on English speech recognition.'</b></center>")
gr.Markdown("<center>Transcription takes 5-10 seconds per minute of the video (bad audio/hard accents slow it down a bit). #patience<br />If you have time while waiting, check out my <a href=https://www.artificial-intelligence.blog target=_blank>AI blog</a> (opens in new tab).</center>")
input_text_url = gr.Textbox(placeholder='Youtube video URL', label='URL')
result_button_transcribe = gr.Button('1. Transcribe')
output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript')
#result_button_summary = gr.Button('2. Create Summary')
#output_text_summary = gr.Textbox(placeholder='Summary of the YouTube video transcript.', label='Summary')
result_button_transcribe.click(get_text, inputs = input_text_url, outputs = output_text_transcribe)
#result_button_summary.click(get_summary, inputs = output_text_transcribe, outputs = output_text_summary)
demo.queue(default_enabled = True).launch(debug = True)