|
import gradio as gr |
|
import whisper |
|
import os |
|
import re |
|
import yt_dlp |
|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline |
|
|
|
|
|
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' |
|
} |
|
|
|
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() |
|
|