Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
4 |
+
from youtube_transcript_api.formatters import TextFormatter
|
5 |
+
import re
|
6 |
+
|
7 |
+
# Load the summarization model
|
8 |
+
text_summary = pipeline("summarization", model="Falconsai/text_summarization")
|
9 |
+
|
10 |
+
def extract_video_id(url):
|
11 |
+
regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
|
12 |
+
match = re.search(regex, url)
|
13 |
+
if match:
|
14 |
+
return match.group(1)
|
15 |
+
return None
|
16 |
+
|
17 |
+
def get_youtube_transcript(video_url):
|
18 |
+
video_id = extract_video_id(video_url)
|
19 |
+
if not video_id:
|
20 |
+
return "Video ID could not be extracted."
|
21 |
+
try:
|
22 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
23 |
+
formatter = TextFormatter()
|
24 |
+
text_transcript = formatter.format_transcript(transcript)
|
25 |
+
return text_transcript
|
26 |
+
except Exception as e:
|
27 |
+
return f"An error occurred: {e}"
|
28 |
+
|
29 |
+
def summarize_youtube_video(url):
|
30 |
+
transcript = get_youtube_transcript(url)
|
31 |
+
if "An error occurred" in transcript:
|
32 |
+
return transcript
|
33 |
+
summary = text_summary(transcript, min_length=10, max_length=1000, do_sample=False)
|
34 |
+
return summary[0]['summary_text']
|
35 |
+
|
36 |
+
# Define the Gradio interface
|
37 |
+
iface = gr.Interface(
|
38 |
+
fn=summarize_youtube_video,
|
39 |
+
inputs=gr.Textbox(label="Enter YouTube Video URL", placeholder="e.g. https://www.youtube.com/watch?v=abcdef12345"),
|
40 |
+
outputs=gr.Textbox(label="Video Summary"),
|
41 |
+
title="YouTube Video Summarizer",
|
42 |
+
description="Enter the URL of a YouTube video to get a summary of its transcript."
|
43 |
+
)
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
iface.launch()
|