Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
2 |
+
import re
|
3 |
+
import torch
|
4 |
+
from transformers import pipeline
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6",
|
8 |
+
torch_dtype=torch.bfloat16)
|
9 |
+
|
10 |
+
def summary(input):
|
11 |
+
output = text_summary(input)
|
12 |
+
return output[0]['summary_text']
|
13 |
+
|
14 |
+
|
15 |
+
def get_video_id(url):
|
16 |
+
"""Extract the video ID from a YouTube URL."""
|
17 |
+
pattern = r"(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]{11})"
|
18 |
+
match = re.match(pattern, url)
|
19 |
+
if match:
|
20 |
+
return match.group(1)
|
21 |
+
else:
|
22 |
+
raise ValueError("Invalid YouTube URL")
|
23 |
+
|
24 |
+
|
25 |
+
def fetch_transcript_and_summary(url):
|
26 |
+
"""Fetch the full transcript of a YouTube video and its summary."""
|
27 |
+
video_id = get_video_id(url)
|
28 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
29 |
+
full_transcript = " ".join([item["text"] for item in transcript])
|
30 |
+
summarized_transcript = summary(full_transcript)
|
31 |
+
return summarized_transcript
|
32 |
+
|
33 |
+
|
34 |
+
# Example usage
|
35 |
+
def get_video_summary(url):
|
36 |
+
"""Main function to fetch and display transcript and summary."""
|
37 |
+
try:
|
38 |
+
summarized_transcript = fetch_transcript_and_summary(url)
|
39 |
+
print(summarized_transcript)
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error: {e}")
|
42 |
+
|
43 |
+
|
44 |
+
# Call the function with your desired URL command line based
|
45 |
+
# video_url = input("Enter the YouTube video URL: ")
|
46 |
+
# get_video_summary(video_url)
|
47 |
+
|
48 |
+
|
49 |
+
# Web app Gradio based
|
50 |
+
gr.close_all()
|
51 |
+
|
52 |
+
demo = gr.Interface(
|
53 |
+
fn=fetch_transcript_and_summary,
|
54 |
+
inputs=[gr.Textbox(label="Input YouTube video url to summarize", lines=1)],
|
55 |
+
outputs=[gr.Textbox(label="Summarized YouTube video Script", lines=4)],
|
56 |
+
title="Project 02: YouTube Video Script Summarizer",
|
57 |
+
description="As understood from the title, if not already, this application will summarize your YouTube video"
|
58 |
+
" Script"
|
59 |
+
)
|
60 |
+
|
61 |
+
demo.launch()
|