maliahson commited on
Commit
473de99
·
verified ·
1 Parent(s): 5f63742

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import gradio as gr
3
+ from youtube_utils import get_youtube_captions, summarize_large_text_with_bart
4
+
5
+ def extract_video_id(url):
6
+ """
7
+ Extract video ID from YouTube URLs using regular expressions.
8
+ """
9
+ patterns = [
10
+ r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', # Standard and shortened URLs
11
+ r'(?:embed\/)([0-9A-Za-z_-]{11})', # Embed URLs
12
+ r'(?:youtu\.be\/)([0-9A-Za-z_-]{11})' # Shortened URLs
13
+ ]
14
+ for pattern in patterns:
15
+ match = re.search(pattern, url)
16
+ if match:
17
+ return match.group(1)
18
+ return None
19
+
20
+ def summarize_youtube_video(video_url):
21
+ """
22
+ Summarize YouTube video by extracting captions and applying summarization.
23
+ """
24
+ try:
25
+ # Extract video ID
26
+ video_id = extract_video_id(video_url)
27
+ if not video_id:
28
+ return "Invalid YouTube URL. Please try again.", None
29
+
30
+ # Fetch captions
31
+ captions = get_youtube_captions(video_id)
32
+ if not captions:
33
+ return "Unable to fetch captions for the video. Ensure the video has subtitles.", None
34
+
35
+ # Generate summary
36
+ summary = summarize_large_text_with_bart(captions)
37
+ return summary, f"https://www.youtube.com/watch?v={video_id}"
38
+
39
+ except Exception as e:
40
+ return f"An error occurred: {str(e)}", None
41
+
42
+ # Gradio Interface
43
+ def create_gradio_interface():
44
+ """
45
+ Create and return the Gradio Blocks interface.
46
+ """
47
+ with gr.Blocks(title="YouTube Video Summarizer") as interface:
48
+ gr.Markdown("# 🎥 YouTube Video Summarizer")
49
+ gr.Markdown(
50
+ "Provide a YouTube video URL, and this tool will extract captions and summarize the content using a BART model."
51
+ )
52
+
53
+ with gr.Row():
54
+ video_url_input = gr.Textbox(
55
+ label="YouTube Video URL",
56
+ placeholder="Enter the URL of the YouTube video"
57
+ )
58
+ summarize_button = gr.Button("Summarize")
59
+
60
+ with gr.Row():
61
+ summary_output = gr.Textbox(label="Summary", lines=10, interactive=False)
62
+ video_link_output = gr.Textbox(label="YouTube Video Link", interactive=False)
63
+
64
+ summarize_button.click(
65
+ fn=summarize_youtube_video,
66
+ inputs=[video_url_input],
67
+ outputs=[summary_output, video_link_output]
68
+ )
69
+
70
+ return interface
71
+
72
+ # Launch the interface
73
+ if __name__ == "__main__":
74
+ interface = create_gradio_interface()
75
+ interface.launch()