Spaces:
Build error
Build error
Download app.py from scranjith/YouTubeSummarizer: direct link, hf CLI and curl.
- Browser
- Download file 2.52 kB
-
https://huggingface.co/spaces/scranjith/YouTubeSummarizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/scranjith/YouTubeSummarizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/scranjith/YouTubeSummarizer/resolve/main/app.py
2.52 kB
| import os | |
| import re | |
| from youtube_transcript_api import YouTubeTranscriptApi | |
| from transformers import pipeline | |
| import gradio as gr | |
| # Suppress Hugging Face symlink warning | |
| os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1" | |
| def get_video_id(url): | |
| """ | |
| Extract the video ID from a YouTube URL. | |
| """ | |
| pattern = re.compile( | |
| r'(?:https?://)?(?:www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)|.*[?&]v=)|youtu\.be/|youtube\.com/shorts/)([^"&?/ ]{11})') | |
| match = pattern.search(url) | |
| if match: | |
| return match.group(1) | |
| return None | |
| def get_transcript(video_id): | |
| """ | |
| Retrieve the transcript for a given YouTube video ID. | |
| """ | |
| transcript = YouTubeTranscriptApi.get_transcript(video_id) | |
| return ' '.join([entry['text'] for entry in transcript]) | |
| def chunk_text(text, max_tokens=1024, overlap=200): | |
| """ | |
| Split the text into chunks that fit within the token limit with some overlap. | |
| """ | |
| words = text.split() | |
| chunks = [] | |
| start = 0 | |
| while start < len(words): | |
| end = start + max_tokens | |
| chunks.append(' '.join(words[start:end])) | |
| start += max_tokens - overlap | |
| return chunks | |
| def summarize_text(text, max_length=150): | |
| """ | |
| Summarize the given text using a Hugging Face summarization pipeline. | |
| """ | |
| summarizer = pipeline('summarization', model='facebook/bart-large-cnn') | |
| chunks = chunk_text(text, max_tokens=1024) | |
| summaries = [summarizer(chunk, max_length=max_length, min_length=30, do_sample=False)[0]['summary_text'] for chunk | |
| in chunks] | |
| return ' '.join(summaries) | |
| def main(url): | |
| video_id = get_video_id(url) | |
| if not video_id: | |
| return "Invalid YouTube URL" | |
| try: | |
| transcript = get_transcript(video_id) | |
| except Exception as e: | |
| return f"Error retrieving transcript: {str(e)}" | |
| summary = summarize_text(transcript) | |
| return summary | |
| """ | |
| if __name__ == "__main__": | |
| youtube_url = input("Enter the YouTube URL: ") | |
| summary = main(youtube_url) | |
| print("Summary:") | |
| print(summary) | |
| """ | |
| gr.close_all() | |
| # demo = gr.Interface(fn=summary, inputs="text", outputs="text") | |
| demo = gr.Interface(fn=main, | |
| inputs=[gr.Textbox(label="Input Youtube URL to summarize", lines=1)], | |
| outputs=[gr.Textbox(label="Summarized text", lines=4)], | |
| title="@GenAILearning Project 1: Youtube Summarizer", | |
| description="This application will summarize youtube videos") | |
| demo.launch() |