Spaces:
Sleeping
Sleeping
File size: 2,471 Bytes
f54aa31 d8637b5 6d094f7 bc464ef 1906ac4 a64870c 1906ac4 6d094f7 f7899be 1906ac4 3a9fabd d8637b5 0938913 d8637b5 a64870c 3a9fabd bc464ef f54aa31 1906ac4 bc464ef a64870c 3a9fabd f54aa31 1906ac4 bc464ef 3a9fabd 0938913 6d094f7 bc464ef 1906ac4 3a9fabd 1906ac4 3a9fabd 1906ac4 3a9fabd 6d094f7 1906ac4 6d094f7 0938913 bc464ef 3a9fabd bc464ef 3a9fabd bc464ef 3a9fabd bc464ef 3a9fabd bc464ef 3a9fabd 1906ac4 3a9fabd 1906ac4 3a9fabd 1906ac4 3a9fabd bc464ef 3a9fabd bc464ef 3a9fabd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | import gradio as gr
from youtube_transcript_api import YouTubeTranscriptApi
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import re
import torch
# β
FORCE lightweight model
model_name = "sshleifer/distilbart-cnn-12-6"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
# move to CPU explicitly (safe)
device = "cpu"
model = model.to(device)
def extract_video_id(url):
regex = r"(?:v=|\/)([0-9A-Za-z_-]{11})"
match = re.search(regex, url)
return match.group(1) if match else None
def get_transcript(video_id):
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
return " ".join([t['text'] for t in transcript])
except:
return None
def summarize_text(text):
max_chunk = 500 # π₯ smaller chunks = safer
chunks = [text[i:i+max_chunk] for i in range(0, len(text), max_chunk)]
final_summary = ""
for chunk in chunks:
inputs = tokenizer(
"summarize: " + chunk,
return_tensors="pt",
max_length=512,
truncation=True
).to(device)
summary_ids = model.generate(
**inputs,
max_length=100,
min_length=25,
num_beams=2, # π₯ reduced for speed
)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
final_summary += summary + " "
return final_summary
def process_video(url):
video_id = extract_video_id(url)
if not video_id:
return "Invalid URL", "", ""
transcript = get_transcript(video_id)
if not transcript:
return "Transcript not available", "", ""
summary = summarize_text(transcript)
video_embed = f"""
<iframe width="100%" height="315"
src="https://www.youtube.com/embed/{video_id}"
frameborder="0" allowfullscreen></iframe>
"""
return summary, transcript[:2000], video_embed # π₯ limit transcript
with gr.Blocks() as demo:
gr.Markdown("# π₯ YouTube Video Summarizer")
url_input = gr.Textbox(label="Enter YouTube URL")
btn = gr.Button("Generate")
summary_output = gr.Textbox(label="Summary")
transcript_output = gr.Textbox(label="Transcript (trimmed)")
video_output = gr.HTML()
btn.click(
process_video,
inputs=url_input,
outputs=[summary_output, transcript_output, video_output]
)
demo.launch(ssr_mode=False) |