File size: 3,846 Bytes
81000a9 11a3ad8 f017bfe 81000a9 11a3ad8 81000a9 11a3ad8 81000a9 11a3ad8 f017bfe 11a3ad8 81000a9 11a3ad8 f017bfe 81000a9 11a3ad8 f017bfe 11a3ad8 f017bfe 11a3ad8 f017bfe 11a3ad8 81000a9 11a3ad8 81000a9 11a3ad8 98848b6 81000a9 f017bfe 11a3ad8 81000a9 11a3ad8 81000a9 11a3ad8 81000a9 11a3ad8 f017bfe 11a3ad8 81000a9 |
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 98 99 100 101 102 |
import gradio as gr
import whisper
import os
import re
import yt_dlp
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
# Load models
whisper_model = whisper.load_model("base")
multilingual_model = "csebuetnlp/mT5_multilingual_XLSum"
tokenizer = AutoTokenizer.from_pretrained(multilingual_model)
summarizer_model = AutoModelForSeq2SeqLM.from_pretrained(multilingual_model)
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
SUPPORTED_LANGUAGES = {
"bn": "Bengali", "en": "English", "gu": "Gujarati", "hi": "Hindi",
"kn": "Kannada", "ml": "Malayalam", "mr": "Marathi", "ta": "Tamil",
"te": "Telugu", "ur": "Urdu"
}
def download_audio(youtube_url):
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'audio.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
}],
'cookiefile': 'cookies.txt' # <β Uses your uploaded cookie file
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(youtube_url, download=True)
filename = ydl.prepare_filename(info_dict).replace(".webm", ".mp3").replace(".m4a", ".mp3")
return filename
def extract_thumbnail(youtube_url):
match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", youtube_url)
if match:
video_id = match.group(1)
return f"https://img.youtube.com/vi/{video_id}/0.jpg"
return ""
def summarize_text(text):
input_text = f"summarize: {text}"
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
summary_ids = summarizer_model.generate(inputs, max_length=150, min_length=30, num_beams=4)
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
def transcribe_and_summarize(youtube_url, translate_to_english):
try:
audio_file = download_audio(youtube_url)
result = whisper_model.transcribe(audio_file)
transcript = result["text"]
lang_code = result["language"]
thumbnail_url = extract_thumbnail(youtube_url)
if lang_code not in SUPPORTED_LANGUAGES:
return None, f"β Language '{lang_code}' not supported.", "", "", None
summary = summarize_text(transcript)
if translate_to_english and lang_code != "en":
translated_summary = translator(summary)[0]["translation_text"]
else:
translated_summary = summary
os.remove(audio_file)
summary_text = f"Transcript:\n{transcript}\n\nSummary:\n{translated_summary}"
with open("summary.txt", "w", encoding="utf-8") as f:
f.write(summary_text)
return thumbnail_url, f"π£οΈ Language: {SUPPORTED_LANGUAGES[lang_code]}", transcript, translated_summary, "summary.txt"
except Exception as e:
return None, f"β Error: {str(e)}", "", "", None
with gr.Blocks(css="style.css") as demo:
gr.Markdown("<h1 style='text-align: center;'>π¬ Multilingual YouTube Summarizer</h1>")
gr.Markdown("Paste any YouTube video link, and get transcript + summary. Works for Hindi, Bengali, Tamil, Urdu, and more!")
with gr.Row():
youtube_url = gr.Textbox(label="YouTube Video URL")
translate_check = gr.Checkbox(label="Translate Summary to English", value=True)
thumbnail = gr.Image(label="Video Thumbnail", type="filepath")
lang_out = gr.Text(label="Detected Language")
transcript_out = gr.Textbox(label="Transcript", lines=8)
summary_out = gr.Textbox(label="Summary", lines=6)
download_btn = gr.File(label="Download .txt")
btn = gr.Button("Generate Summary")
btn.click(fn=transcribe_and_summarize,
inputs=[youtube_url, translate_check],
outputs=[thumbnail, lang_out, transcript_out, summary_out, download_btn])
demo.launch()
|