Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 2 |
+
from youtube_transcript_api.formatters import TextFormatter
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
def get_video_id(url):
|
| 9 |
+
"""Extracts video ID from YouTube URL"""
|
| 10 |
+
if 'youtube.com' in url:
|
| 11 |
+
# Extracting video ID from URL
|
| 12 |
+
return url.split('v=')[1].split('&')[0]
|
| 13 |
+
elif 'youtu.be' in url:
|
| 14 |
+
# Extracting video ID from shortened URL
|
| 15 |
+
return url.split('/')[-1]
|
| 16 |
+
else:
|
| 17 |
+
raise ValueError("Invalid YouTube URL")
|
| 18 |
+
|
| 19 |
+
def get_summary(transcript):
|
| 20 |
+
model_name="Falconsai/text_summarization"
|
| 21 |
+
text_summary_pipeline = pipeline("summarization", model=model_name, device=-1, torch_dtype=torch.bfloat16)
|
| 22 |
+
# use device=-1 for CPU and device=0 for GPU (using litserve we could automate it, but without using a wrapper class 'device' has to be manually specified)
|
| 23 |
+
video_summary = text_summary_pipeline(transcript)
|
| 24 |
+
return video_summary[0]["summary_text"]
|
| 25 |
+
|
| 26 |
+
def get_transcript_summary(url):
|
| 27 |
+
"""Fetches and prints the transcript of a YouTube video given its URL"""
|
| 28 |
+
try:
|
| 29 |
+
video_id = get_video_id(url)
|
| 30 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 31 |
+
formatter = TextFormatter()
|
| 32 |
+
formatted_transcript = formatter.format_transcript(transcript)
|
| 33 |
+
transcript_summary = get_summary(formatted_transcript)
|
| 34 |
+
return transcript_summary
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print("An error occurred:", e)
|
| 37 |
+
|
| 38 |
+
gr.close_all()
|
| 39 |
+
|
| 40 |
+
# version 0.1
|
| 41 |
+
# demo = gr.Interface(fn=summary, inputs="text", outputs="text")
|
| 42 |
+
# version 0.2
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=get_transcript_summary,
|
| 45 |
+
inputs=[gr.Textbox(label="Youtube Video URL", lines=2)],
|
| 46 |
+
outputs=[gr.Textbox(label="YouTube Video's Summary", lines=6)],
|
| 47 |
+
title="GenAI YouTube Video Summarizer",
|
| 48 |
+
description="This App Summarizes Youtube Videos")
|
| 49 |
+
demo.launch()
|