Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,55 @@
|
|
| 1 |
-
import re
|
| 2 |
import gradio as gr
|
| 3 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 4 |
from transformers import pipeline
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
summarizer =
|
| 8 |
-
qa_pipeline = None
|
| 9 |
-
|
| 10 |
-
def load_models():
|
| 11 |
-
global summarizer, qa_pipeline
|
| 12 |
-
if summarizer is None:
|
| 13 |
-
summarizer = pipeline(
|
| 14 |
-
task="text2text-generation",
|
| 15 |
-
model="sshleifer/distilbart-cnn-12-6"
|
| 16 |
-
)
|
| 17 |
-
if qa_pipeline is None:
|
| 18 |
-
qa_pipeline = pipeline(
|
| 19 |
-
task="question-answering",
|
| 20 |
-
model="distilbert-base-cased-distilled-squad"
|
| 21 |
-
)
|
| 22 |
|
| 23 |
def extract_video_id(url):
|
| 24 |
-
regex = r"(?:v=|
|
| 25 |
match = re.search(regex, url)
|
| 26 |
return match.group(1) if match else None
|
| 27 |
|
| 28 |
-
def get_transcript(
|
| 29 |
-
video_id = extract_video_id(url)
|
| 30 |
-
if not video_id:
|
| 31 |
-
return None, "❌ Invalid YouTube URL"
|
| 32 |
try:
|
| 33 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 34 |
-
text = " ".join([t[
|
| 35 |
-
return text
|
| 36 |
-
except
|
| 37 |
-
return None
|
| 38 |
-
|
| 39 |
-
def chunk_text(text, size=400):
|
| 40 |
-
words = text.split()
|
| 41 |
-
for i in range(0, len(words), size):
|
| 42 |
-
yield " ".join(words[i:i+size])
|
| 43 |
|
| 44 |
def summarize_text(text):
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
inputs=[
|
| 75 |
-
gr.Textbox(label="YouTube URL"),
|
| 76 |
-
gr.Textbox(label="Ask a question (optional)")
|
| 77 |
-
],
|
| 78 |
-
outputs=[
|
| 79 |
-
gr.Textbox(label="Summary"),
|
| 80 |
-
gr.Textbox(label="Answer")
|
| 81 |
-
],
|
| 82 |
-
title="YouTube Video Summarizer + Chat"
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 3 |
from transformers import pipeline
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
+
# Load summarizer (lightweight for Spaces)
|
| 7 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def extract_video_id(url):
|
| 10 |
+
regex = r"(?:v=|\/)([0-9A-Za-z_-]{11}).*"
|
| 11 |
match = re.search(regex, url)
|
| 12 |
return match.group(1) if match else None
|
| 13 |
|
| 14 |
+
def get_transcript(video_id):
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 17 |
+
text = " ".join([t['text'] for t in transcript])
|
| 18 |
+
return text
|
| 19 |
+
except:
|
| 20 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def summarize_text(text):
|
| 23 |
+
max_chunk = 1000
|
| 24 |
+
chunks = [text[i:i+max_chunk] for i in range(0, len(text), max_chunk)]
|
| 25 |
+
|
| 26 |
+
summary = ""
|
| 27 |
+
for chunk in chunks:
|
| 28 |
+
result = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
|
| 29 |
+
summary += result[0]['summary_text'] + " "
|
| 30 |
+
|
| 31 |
+
return summary
|
| 32 |
+
|
| 33 |
+
def process_video(url):
|
| 34 |
+
video_id = extract_video_id(url)
|
| 35 |
+
if not video_id:
|
| 36 |
+
return "Invalid YouTube URL"
|
| 37 |
+
|
| 38 |
+
transcript = get_transcript(video_id)
|
| 39 |
+
if not transcript:
|
| 40 |
+
return "Could not fetch transcript"
|
| 41 |
+
|
| 42 |
+
summary = summarize_text(transcript)
|
| 43 |
+
return summary
|
| 44 |
+
|
| 45 |
+
# Gradio UI
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=process_video,
|
| 48 |
+
inputs=gr.Textbox(label="Enter YouTube URL"),
|
| 49 |
+
outputs=gr.Textbox(label="Summary"),
|
| 50 |
+
title="YouTube Video Summarizer",
|
| 51 |
+
description="Paste a YouTube link to get a summarized transcript."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
iface.launch()
|