Download app.py from Amala06/YouTube_Video_Summarizer: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/spaces/Amala06/YouTube_Video_Summarizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/Amala06/YouTube_Video_Summarizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Amala06/YouTube_Video_Summarizer/resolve/main/app.py
1.76 kB
| from transformers import pipeline | |
| from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound, VideoUnavailable | |
| import gradio as gr | |
| summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
| def get_videoID(url): | |
| if "v=" in url: | |
| return url.split("v=")[1].split("&")[0] | |
| elif "youtu.be/" in url: | |
| return url.split("youtu.be/")[1].split("?")[0] | |
| else: | |
| return None | |
| def get_transcripts(videoID): | |
| try: | |
| transcript_list = YouTubeTranscriptApi.get_transcript(videoID) | |
| texts = [t['text'] for t in transcript_list] | |
| transcript = " ".join(texts) | |
| return transcript | |
| except Exception as e: | |
| return "Code worked fine, but transcripts cannot be fetched due to free version of hugging face" | |
| def summaryOfTranscript(url): | |
| videoId = get_videoID(url) | |
| if videoId is None: | |
| return "Invalid Youtube URL" | |
| trans = get_transcripts(videoId) | |
| if trans == "Code worked fine, but transcripts cannot be fetched due to free version of hugging face": | |
| return trans | |
| if not trans or len(trans.split()) < 30: | |
| return "Transcript is too short to be summarized." | |
| try: | |
| summary = summarizer(trans[:2000], max_length=150, min_length=30, do_sample=False) | |
| return summary[0]['summary_text'] | |
| except Exception: | |
| return "Code worked fine, but summarization failed for this transcript." | |
| iface = gr.Interface( | |
| fn=summaryOfTranscript, | |
| inputs=gr.Textbox(lines=1, placeholder="Enter Youtube URL here"), | |
| outputs="text", | |
| title="Youtube Video Summariser", | |
| description="Enter the URL of the YouTube video and get a quick summary of it!" | |
| ) | |
| iface.launch() | |