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