from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import TextFormatter from transformers import pipeline import re import gradio as gr pipe = pipeline("summarization", model="Falconsai/text_summarization", device=-1) def chunk_length(text, max_length = 1024): words = text.split() for i in range(0, len(words), max_length): yield " ".join(words[i:i + max_length]) def extract_youtube_id(url): """ Extracts the YouTube video ID from a given URL. Args: url (str): The YouTube video URL. Returns: str: The extracted video ID, or None if no match is found. """ # Regular expression to match YouTube video IDs pattern = r"(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})" match = re.search(pattern, url) if match: return match.group(1) return None def summary(text_transcript): try: chunks = chunk_text(text_transcript) summarized_chunks = [pipe(chunk)[0]['summary_text'] for chunk in chunks] return " ".join(summarized_chunks) except Exception as e: return f"Error during summarization: {e}" def get_youtube_transcript(link): video_id= extract_youtube_id(link) if not video_id: return "Video ID could not be extracted" try: transcript= YouTubeTranscriptApi.get_transcript(video_id) # fetches the trancript formatter= TextFormatter() text_transcript = formatter.format_transcript(transcript) # This will format the transcript summarized_text = summary(text_transcript) return summarized_text except Exception as e: return f"An error has occured: {e}" demo = gr.Interface(fn=get_youtube_transcript, inputs=[gr.Textbox(label="Input youtube url to generate the summary of video", lines=6)], outputs=[gr.Textbox(label="Summary of youtube video")], title='Generate Video Summary', description='This is a project to generate the summary of a video') demo.launch(share='True')