File size: 1,741 Bytes
b6b44cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a63414
b6b44cb
 
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
#Hugging Face (HF)
import torch
import gradio as gr

# Use a pipeline as a high-level helper
from transformers import pipeline
from youtube_transcript_api import YouTubeTranscriptApi
import re

text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16)

def summary(input):
    output = text_summary(input)
    return output[0]['summary_text']

def get_video_id(url):
    """
    Extracts the video ID from the given YouTube URL.
    """
    # Regular expression to match the video ID
    video_id_match = re.match(r'.*(v=|\/)([a-zA-Z0-9_-]{11}).*', url)
    if video_id_match:
        return video_id_match.groups()[-1]
    else:
        raise ValueError("Invalid YouTube URL")

def get_transcript(video_url):
    """
    Fetches the transcript of a YouTube video given its URL.
    """
    try:
        # Extract video ID from the URL
        video_id = get_video_id(video_url)
        
        # Fetch the transcript
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        
        # Combine the transcript text
        transcript_text = " ".join([item['text'] for item in transcript])


        Summarized_text = summary(transcript_text)
        return Summarized_text
    except Exception as e:
        return str(e)

# Example usage
# video_url = input("Enter YouTube video URL: ")
# transcript = get_transcript(video_url)
# print("Transcript:\n", transcript)

gr.close_all()

demo = gr.Interface(fn=get_transcript, inputs=[gr.Textbox(label="Input Youtube Link",lines = 1)],outputs=[gr.Textbox(label="Summarized Text",lines = 6)],title="Project 2: Youtube Video Summary",
                    description="""This is a simple text summarization model.""")
demo.launch()