Spaces:
Sleeping
Sleeping
| import re | |
| from youtube_transcript_api import YouTubeTranscriptApi | |
| from youtube_transcript_api.formatters import TextFormatter | |
| import torch | |
| import gradio as gr | |
| from transformers import pipeline | |
| text_summary = pipeline('summarization', model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16) | |
| # model_path = ("../.venv/Modal/models--sshleifer--distilbart-cnn-12-6/snapshots" | |
| # "/a4f8f3ea906ed274767e9906dbaede7531d660ff") | |
| # text_summary = pipeline("summarization", model=model_path, | |
| # torch_dtype=torch.bfloat16) | |
| def summary (input): | |
| output = text_summary(input) | |
| return output[0]['summary_text'] | |
| def extract_video_id(url): | |
| # Regex to extract the video ID from various YouTube URL formats | |
| regex = r"(?:youtube\.com/(?:[^/\n\s]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([a-zA-Z0-9_-]{11})" | |
| match = re.search(regex, url) | |
| if match: | |
| return match.group(1) | |
| else: | |
| print("❌ Could not extract video ID from the URL.") | |
| return None | |
| def get_youtube_transcript(video_url): | |
| video_id = extract_video_id(video_url) | |
| if not video_id: | |
| return "Video ID could not be extracted." | |
| try: | |
| # Get the transcript using the video ID | |
| transcript = YouTubeTranscriptApi.get_transcript(video_id) | |
| # Format the transcript into plain text | |
| formatter = TextFormatter() | |
| text_transcript = formatter.format_transcript(transcript) | |
| summary_text = summary(text_transcript) | |
| return summary_text | |
| except Exception as e: | |
| print(f"⚠️ Error retrieving transcript: {e}") | |
| return None | |
| # Example URL (Replace this with the actual URL when using the script) | |
| # video_url = "https://youtu.be/SP1bknhIsTc" | |
| # print(get_youtube_transcript(video_url)) | |
| gr.close_all() | |
| # demo = gr.Interface(fn=summary, inputs="text", outputs="text") | |
| demo = gr.Interface(fn=get_youtube_transcript, | |
| inputs=[gr.Textbox(label="Input YouTube Url to summarize", lines=6)], | |
| outputs=[gr.Textbox(label="Summarized text", lines=4)], | |
| title="@GenAILearning Project 2: YouTube Script Summarizer", | |
| description="THIS APPLICATIONS WILL BE USED TO SUMMARIZE THE YOUTUBE VIDEO SCRIPT") | |
| demo.launch() |