Spaces:
Runtime error
Runtime error
File size: 1,660 Bytes
fd3d3dd |
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 |
import gradio as gr
from transformers import pipeline
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import TextFormatter
import re
# Load the summarization model
text_summary = pipeline("summarization", model="Falconsai/text_summarization")
def extract_video_id(url):
regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
match = re.search(regex, url)
if match:
return match.group(1)
return None
def get_youtube_transcript(video_url):
video_id = extract_video_id(video_url)
if not video_id:
return "Video ID could not be extracted."
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
formatter = TextFormatter()
text_transcript = formatter.format_transcript(transcript)
return text_transcript
except Exception as e:
return f"An error occurred: {e}"
def summarize_youtube_video(url):
transcript = get_youtube_transcript(url)
if "An error occurred" in transcript:
return transcript
summary = text_summary(transcript, min_length=10, max_length=1000, do_sample=False)
return summary[0]['summary_text']
# Define the Gradio interface
iface = gr.Interface(
fn=summarize_youtube_video,
inputs=gr.Textbox(label="Enter YouTube Video URL", placeholder="e.g. https://www.youtube.com/watch?v=abcdef12345"),
outputs=gr.Textbox(label="Video Summary"),
title="YouTube Video Summarizer",
description="Enter the URL of a YouTube video to get a summary of its transcript."
)
if __name__ == "__main__":
iface.launch()
|