Spaces:
Runtime error
Runtime error
Download app.py from PreethiPS/YouTube_Transcript_Summarizer: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/spaces/PreethiPS/YouTube_Transcript_Summarizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/PreethiPS/YouTube_Transcript_Summarizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/PreethiPS/YouTube_Transcript_Summarizer/resolve/main/app.py
1.6 kB
| from youtube_transcript_api import YouTubeTranscriptApi | |
| from youtube_transcript_api.formatters import TextFormatter | |
| import re | |
| import torch | |
| import gradio as gr | |
| import os | |
| os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' | |
| from transformers import pipeline | |
| text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16) | |
| def summary (input): | |
| output = text_summary(input) | |
| return output[0] | |
| def extract_video_id(youtube_url): | |
| """ | |
| Extracts the video ID from a YouTube URL. | |
| """ | |
| video_id = re.search(r'(?<=v=)[\w-]+', youtube_url) | |
| if video_id: | |
| return video_id.group(0) | |
| video_id = re.search(r'(?<=be/)[\w-]+', youtube_url) | |
| if video_id: | |
| return video_id.group(0) | |
| return None | |
| def get_transcript(youtube_url): | |
| """ | |
| Fetches the transcript of a YouTube video given its URL. | |
| """ | |
| video_id = extract_video_id(youtube_url) | |
| if not video_id: | |
| return "Invalid YouTube URL" | |
| try: | |
| transcript_list = YouTubeTranscriptApi.get_transcript(video_id) | |
| formatter = TextFormatter() | |
| transcript = formatter.format_transcript(transcript_list) | |
| sum_text = summary(transcript) | |
| print(sum_text) | |
| return sum_text | |
| except Exception as e: | |
| return str(e) | |
| gr.close_all() | |
| op = gr.Interface(fn=get_transcript, | |
| inputs= [gr.Textbox(label="Enter YouTube URL:",lines=1)], | |
| outputs= [gr.Textbox(label="Summarized Transcript:", lines=4)], | |
| title = "YouTube Video Transcript Summarizer") | |
| op.launch() | |
| gr.close_all() | |