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'' 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()