File size: 2,141 Bytes
0b19b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
from youtube_transcript_api import YouTubeTranscriptApi
from urllib.parse import urlparse, parse_qs
import torch
import gradio as gr
# Use a pipeline as a high-level helper
from transformers import pipeline

# model_path = r"../Models/models--sshleifer--distilbart-cnn-12-6/snapshots/a4f8f3ea906ed274767e9906dbaede7531d660ff"
# text_summary = pipeline ( task= "summarization", model= model_path, torch_dtype=torch.bfloat16)

text_summary = pipeline ( task= "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(youtube_url):
    # Extract the video ID from the YouTube URL
    query = urlparse(youtube_url)
    if query.hostname == 'youtu.be':
        return query.path[1:]
    if query.hostname in ('www.youtube.com', 'youtube.com'):
        if query.path == '/watch':
            return parse_qs(query.query)['v'][0]
        if query.path[:7] == '/embed/':
            return query.path.split('/')[2]
        if query.path[:3] == '/v/':
            return query.path.split('/')[2]
    return None

def get_transcript(youtube_url):
    video_id = get_video_id(youtube_url)
    if not video_id:
        return "Invalid YouTube URL."

    try:
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        transcript_text = "\n".join([entry['text'] for entry in transcript])
        summary_text = summary(transcript_text)
        return summary_text
    except Exception as e:
        return f"Error fetching transcript: {e}"

# Example usage
# url = input("Enter YouTube URL: ")
# transcript = get_transcript(url)
# print("\n--- Transcript ---\n")
# print(transcript)


gr.close_all()

demo =  gr.Interface(fn=get_transcript,
        inputs=[gr.Textbox(label="Input Youtube URL to summarize",lines=1)],
        outputs=[gr.Textbox(label="summarized text",lines=4)],
        title="@GenAILearniverse Project 2: Youtube Script Summarizer",
        description="THIS APPLICATION WILL BE USED TO SUMMARIZE Youtube Video Script")


print("πŸš€ App is launching...")
demo.launch(share=True)