Spaces:
Sleeping
Sleeping
| # Import necessary libraries | |
| import gradio as gr | |
| from youtube_transcript_api import YouTubeTranscriptApi | |
| from transformers import pipeline | |
| # Load a pre-trained summarization model from Hugging Face | |
| # 'sshleifer/distilbart-cnn-12-6' is a good general-purpose summarization model | |
| summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
| def get_youtube_id(url): | |
| """Extracts the YouTube video ID from a URL.""" | |
| if "youtu.be/" in url: | |
| return url.split("youtu.be/")[1].split("?")[0] | |
| elif "v=" in url: | |
| return url.split("v=")[1].split("&")[0] | |
| return None | |
| def summarize_youtube_transcript(youtube_url): | |
| """ | |
| Fetches the transcript of a YouTube video and summarizes it. | |
| Args: | |
| youtube_url (str): The URL of the YouTube video. | |
| Returns: | |
| str: The summarized transcript or an error message. | |
| """ | |
| video_id = get_youtube_id(youtube_url) | |
| if not video_id: | |
| return "Error: Could not extract YouTube video ID from the provided URL." | |
| try: | |
| # Get the transcript for the video ID | |
| transcript_list = YouTubeTranscriptApi.get_transcript(video_id) | |
| # Concatenate the transcript text | |
| transcript_text = " ".join([d['text'] for d in transcript_list]) | |
| # Summarize the transcript | |
| # The summarizer pipeline handles splitting long texts if necessary, | |
| # but for very long videos, you might need more advanced chunking. | |
| # max_length and min_length control the summary length. | |
| summary = summarizer(transcript_text, max_length=200, min_length=50, do_sample=False) | |
| return summary[0]['summary_text'] | |
| except Exception as e: | |
| return f"Error fetching or summarizing transcript: {e}" | |
| # Create the Gradio interface | |
| # The interface takes a text input (for the YouTube URL) | |
| # and provides a text output (for the summarized transcript) | |
| iface = gr.Interface( | |
| fn=summarize_youtube_transcript, | |
| inputs=gr.Textbox(label="Enter YouTube Video URL"), | |
| outputs=gr.Textbox(label="Summarized Transcript"), | |
| title="YouTube Transcript Summarizer", | |
| description="Enter a YouTube video URL to get a summary of its transcript." | |
| ) | |
| # Launch the Gradio app | |
| # share=True creates a temporary public link (useful for testing) | |
| # Setting debug=True provides detailed logs | |
| # iface.launch(share=True, debug=True) | |
| # To deploy on Hugging Face Spaces, you just need this file (e.g., app.py) | |
| # and a requirements.txt file. Hugging Face Spaces will automatically run | |
| # the Gradio app if it finds an interface defined. | |
| # Remove the iface.launch() call when deploying to Hugging Face Spaces. | |
| # The last line should be the interface object itself. | |
| iface.launch() # Use this line for local testing | |
| # iface # Use this line for Hugging Face Spaces deployment | |