Spaces:
Sleeping
Sleeping
| from youtube_transcript_api import YouTubeTranscriptApi | |
| from urllib.parse import urlparse, parse_qs | |
| import torch | |
| import gradio as gr | |
| # Use a pipeline as a high-level helper | |
| from transformers import pipeline | |
| # model_path = r"../Models/models--sshleifer--distilbart-cnn-12-6/snapshots/a4f8f3ea906ed274767e9906dbaede7531d660ff" | |
| # text_summary = pipeline ( task= "summarization", model= model_path, torch_dtype=torch.bfloat16) | |
| text_summary = pipeline ( task= "summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16) | |
| def summary(input): | |
| output = text_summary(input) | |
| return output[0]['summary_text'] | |
| def get_video_id(youtube_url): | |
| # Extract the video ID from the YouTube URL | |
| query = urlparse(youtube_url) | |
| if query.hostname == 'youtu.be': | |
| return query.path[1:] | |
| if query.hostname in ('www.youtube.com', 'youtube.com'): | |
| if query.path == '/watch': | |
| return parse_qs(query.query)['v'][0] | |
| if query.path[:7] == '/embed/': | |
| return query.path.split('/')[2] | |
| if query.path[:3] == '/v/': | |
| return query.path.split('/')[2] | |
| return None | |
| def get_transcript(youtube_url): | |
| video_id = get_video_id(youtube_url) | |
| if not video_id: | |
| return "Invalid YouTube URL." | |
| try: | |
| transcript = YouTubeTranscriptApi.get_transcript(video_id) | |
| transcript_text = "\n".join([entry['text'] for entry in transcript]) | |
| summary_text = summary(transcript_text) | |
| return summary_text | |
| except Exception as e: | |
| return f"Error fetching transcript: {e}" | |
| # Example usage | |
| # url = input("Enter YouTube URL: ") | |
| # transcript = get_transcript(url) | |
| # print("\n--- Transcript ---\n") | |
| # print(transcript) | |
| gr.close_all() | |
| demo = gr.Interface(fn=get_transcript, | |
| inputs=[gr.Textbox(label="Input Youtube URL to summarize",lines=1)], | |
| outputs=[gr.Textbox(label="summarized text",lines=4)], | |
| title="@GenAILearniverse Project 2: Youtube Script Summarizer", | |
| description="THIS APPLICATION WILL BE USED TO SUMMARIZE Youtube Video Script") | |
| print("π App is launching...") | |
| demo.launch(share=True) | |