Spaces:
Runtime error
Runtime error
File size: 1,601 Bytes
ccdf8f4 | 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 | from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import TextFormatter
import re
import torch
import gradio as gr
import os
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
from transformers import pipeline
text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16)
def summary (input):
output = text_summary(input)
return output[0]
def extract_video_id(youtube_url):
"""
Extracts the video ID from a YouTube URL.
"""
video_id = re.search(r'(?<=v=)[\w-]+', youtube_url)
if video_id:
return video_id.group(0)
video_id = re.search(r'(?<=be/)[\w-]+', youtube_url)
if video_id:
return video_id.group(0)
return None
def get_transcript(youtube_url):
"""
Fetches the transcript of a YouTube video given its URL.
"""
video_id = extract_video_id(youtube_url)
if not video_id:
return "Invalid YouTube URL"
try:
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
formatter = TextFormatter()
transcript = formatter.format_transcript(transcript_list)
sum_text = summary(transcript)
print(sum_text)
return sum_text
except Exception as e:
return str(e)
gr.close_all()
op = gr.Interface(fn=get_transcript,
inputs= [gr.Textbox(label="Enter YouTube URL:",lines=1)],
outputs= [gr.Textbox(label="Summarized Transcript:", lines=4)],
title = "YouTube Video Transcript Summarizer")
op.launch()
gr.close_all()
|