File size: 2,234 Bytes
f4c0ad6
5bd0b3b
 
473de99
 
5bd0b3b
473de99
 
 
 
 
353620b
473de99
 
 
 
 
 
5bd0b3b
f4c0ad6
5bd0b3b
473de99
 
5bd0b3b
f4c0ad6
5bd0b3b
473de99
 
5bd0b3b
f4c0ad6
 
473de99
5bd0b3b
 
 
 
 
f4c0ad6
473de99
5bd0b3b
473de99
5bd0b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4c0ad6
5bd0b3b
 
 
 
f4c0ad6
5bd0b3b
 
 
 
 
473de99
5bd0b3b
 
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
65
66
67
68
69
70
71
72
import gradio as gr
import re
from youtube_utils import get_youtube_captions, summarize_large_text_with_bart

def extract_video_id(url):
    # Handle different YouTube URL formats
    patterns = [
        r'(?:v=|\/)([0-9A-Za-z_-]{11}).*',  # Standard and shortened URLs
        r'(?:embed\/)([0-9A-Za-z_-]{11})',   # Embed URLs
        r'(?:youtu\.be\/)([0-9A-Za-z_-]{11})'  # Shortened URLs
    ]
    
    for pattern in patterns:
        match = re.search(pattern, url)
        if match:
            return match.group(1)
    return None

def summarize_video(video_url):
    try:
        # Extract video ID
        video_id = extract_video_id(video_url)
        if not video_id:
            return "Error: Invalid YouTube URL.", None
        
        # Get captions
        captions = get_youtube_captions(video_id)
        if not captions:
            return "Error: Unable to fetch video captions.", None
        
        # Generate summary
        summary = summarize_large_text_with_bart(captions)
        
        # Create embed URL for video preview
        video_embed_url = f"https://www.youtube.com/embed/{video_id}"
        
        return summary, video_embed_url
    
    except Exception as e:
        return f"Error: {str(e)}", None

# Gradio interface
with gr.Blocks() as demo:
    with gr.Row():
        gr.Markdown("# YouTube Video Summarizer")
    
    with gr.Row():
        video_url_input = gr.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL")
    
    with gr.Row():
        summarize_button = gr.Button("Summarize Video")
    
    with gr.Row():
        summary_output = gr.Textbox(label="Summary", interactive=False, lines=10)
    
    with gr.Row():
        video_preview = gr.HTML(label="Video Preview")
    
    def handle_button_click(video_url):
        summary, video_embed_url = summarize_video(video_url)
        video_html = f'<iframe width="560" height="315" src="{video_embed_url}" frameborder="0" allowfullscreen></iframe>' if video_embed_url else ""
        return summary, video_html
    
    summarize_button.click(
        fn=handle_button_click,
        inputs=[video_url_input],
        outputs=[summary_output, video_preview]
    )

# Launch the app
demo.launch()