Spaces:
Sleeping
Sleeping
| import os | |
| import re | |
| from youtube_transcript_api import YouTubeTranscriptApi | |
| from youtube_transcript_api.formatters import TextFormatter | |
| from transformers import pipeline | |
| import torch | |
| # Enable CUDA debugging | |
| os.environ['CUDA_LAUNCH_BLOCKING'] = '1' | |
| # Initialize summarization pipeline | |
| text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.float32, device=-1) | |
| def preprocess_input(input_text, max_length=1024): | |
| tokens = len(input_text.split()) | |
| if tokens > max_length: | |
| input_text = " ".join(input_text.split()[:max_length]) | |
| return input_text | |
| def split_text(text, max_length=1024): | |
| return [text[i:i + max_length] for i in range(0, len(text), max_length)] | |
| def summary(input): | |
| chunks = split_text(input) | |
| summaries = [text_summary(chunk)[0]['summary_text'] for chunk in chunks] | |
| return " ".join(summaries) | |
| def extract_video_id(url): | |
| regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})" | |
| match = re.search(regex, url) | |
| if match: | |
| return match.group(1) | |
| 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: | |
| transcript = YouTubeTranscriptApi.get_transcript(video_id) | |
| formatter = TextFormatter() | |
| text_transcript = formatter.format_transcript(transcript) | |
| text_transcript = preprocess_input(text_transcript) | |
| return summary(text_transcript) | |
| except Exception as e: | |
| return f"An error occurred: {e}" | |
| # Gradio interface | |
| import gradio as gr | |
| gr.close_all() | |
| demo = gr.Interface( | |
| fn=get_youtube_transcript, | |
| inputs=[gr.Textbox(label="Input YouTube Url to summarize", lines=1)], | |
| outputs=[gr.Textbox(label="Summarized text", lines=4)], | |
| title="YouTube Script Summarizer", | |
| description="THIS APPLICATION WILL BE USED TO SUMMARIZE THE YOUTUBE VIDEO SCRIPT." | |
| ) | |
| demo.launch() |