Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,32 @@ import gradio as gr
|
|
2 |
import whisper
|
3 |
import yt_dlp
|
4 |
import os
|
|
|
|
|
|
|
5 |
from transformers import pipeline
|
6 |
|
7 |
-
# Load
|
8 |
whisper_model = whisper.load_model("base")
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
def
|
14 |
filename = "audio.mp3"
|
15 |
ydl_opts = {
|
16 |
'format': 'bestaudio/best',
|
@@ -22,39 +39,75 @@ def download_youtube_audio(youtube_url):
|
|
22 |
'preferredquality': '192',
|
23 |
}],
|
24 |
}
|
25 |
-
|
26 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
27 |
ydl.download([youtube_url])
|
28 |
-
|
29 |
return filename
|
30 |
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
|
|
|
|
37 |
result = whisper_model.transcribe(audio_file)
|
38 |
transcript = result["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
# Clean up
|
44 |
os.remove(audio_file)
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
except Exception as e:
|
49 |
-
return f"β Error: {str(e)}"
|
50 |
|
51 |
# Gradio UI
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
demo.launch()
|
|
|
2 |
import whisper
|
3 |
import yt_dlp
|
4 |
import os
|
5 |
+
import re
|
6 |
+
import requests
|
7 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
8 |
from transformers import pipeline
|
9 |
|
10 |
+
# Load models
|
11 |
whisper_model = whisper.load_model("base")
|
12 |
+
multilingual_model = "csebuetnlp/mT5_multilingual_XLSum"
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(multilingual_model)
|
14 |
+
summarizer_model = AutoModelForSeq2SeqLM.from_pretrained(multilingual_model)
|
15 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
16 |
|
17 |
+
SUPPORTED_LANGUAGES = {
|
18 |
+
"bn": "Bengali",
|
19 |
+
"en": "English",
|
20 |
+
"gu": "Gujarati",
|
21 |
+
"hi": "Hindi",
|
22 |
+
"kn": "Kannada",
|
23 |
+
"ml": "Malayalam",
|
24 |
+
"mr": "Marathi",
|
25 |
+
"ta": "Tamil",
|
26 |
+
"te": "Telugu",
|
27 |
+
"ur": "Urdu"
|
28 |
+
}
|
29 |
|
30 |
+
def download_audio(youtube_url):
|
31 |
filename = "audio.mp3"
|
32 |
ydl_opts = {
|
33 |
'format': 'bestaudio/best',
|
|
|
39 |
'preferredquality': '192',
|
40 |
}],
|
41 |
}
|
|
|
42 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
43 |
ydl.download([youtube_url])
|
|
|
44 |
return filename
|
45 |
|
46 |
+
def extract_thumbnail(youtube_url):
|
47 |
+
video_id = None
|
48 |
+
match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", youtube_url)
|
49 |
+
if match:
|
50 |
+
video_id = match.group(1)
|
51 |
+
return f"https://img.youtube.com/vi/{video_id}/0.jpg"
|
52 |
+
return ""
|
53 |
+
|
54 |
+
def summarize_text(text, lang_code):
|
55 |
+
input_text = f"summarize: {text}"
|
56 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
57 |
+
summary_ids = summarizer_model.generate(inputs, max_length=150, min_length=30, num_beams=4, early_stopping=True)
|
58 |
+
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
59 |
|
60 |
+
def transcribe_and_summarize(youtube_url, translate_to_english):
|
61 |
+
try:
|
62 |
+
audio_file = download_audio(youtube_url)
|
63 |
result = whisper_model.transcribe(audio_file)
|
64 |
transcript = result["text"]
|
65 |
+
lang_code = result["language"]
|
66 |
+
|
67 |
+
thumbnail_url = extract_thumbnail(youtube_url)
|
68 |
+
|
69 |
+
if lang_code not in SUPPORTED_LANGUAGES:
|
70 |
+
return None, f"β Language '{lang_code}' not supported.", "", ""
|
71 |
|
72 |
+
summary = summarize_text(transcript, lang_code)
|
73 |
+
|
74 |
+
if translate_to_english and lang_code != "en":
|
75 |
+
translated_summary = translator(summary)[0]["translation_text"]
|
76 |
+
else:
|
77 |
+
translated_summary = summary
|
78 |
|
|
|
79 |
os.remove(audio_file)
|
80 |
|
81 |
+
summary_text = f"Transcript:\n{transcript}\n\nSummary:\n{translated_summary}"
|
82 |
+
|
83 |
+
# Save to file
|
84 |
+
with open("summary.txt", "w", encoding="utf-8") as f:
|
85 |
+
f.write(summary_text)
|
86 |
+
|
87 |
+
return thumbnail_url, f"π£οΈ Language: {SUPPORTED_LANGUAGES[lang_code]}", transcript, translated_summary, "summary.txt"
|
88 |
|
89 |
except Exception as e:
|
90 |
+
return None, f"β Error: {str(e)}", "", "", None
|
91 |
|
92 |
# Gradio UI
|
93 |
+
with gr.Blocks(css="style.css") as demo:
|
94 |
+
gr.Markdown("<h1 style='text-align: center;'>π¬ Multilingual YouTube Summarizer</h1>")
|
95 |
+
gr.Markdown("This tool transcribes any YouTube video and summarizes it in its original language or English.")
|
96 |
+
|
97 |
+
with gr.Row():
|
98 |
+
youtube_url = gr.Textbox(label="YouTube Video URL")
|
99 |
+
translate_check = gr.Checkbox(label="Translate Summary to English", value=True)
|
100 |
+
|
101 |
+
thumbnail = gr.Image(label="Video Thumbnail", type="filepath")
|
102 |
+
lang_out = gr.Text(label="Detected Language")
|
103 |
+
transcript_out = gr.Textbox(label="Transcript", lines=8)
|
104 |
+
summary_out = gr.Textbox(label="Summary", lines=6)
|
105 |
+
download_btn = gr.File(label="Download .txt")
|
106 |
+
|
107 |
+
btn = gr.Button("Generate Summary")
|
108 |
+
|
109 |
+
btn.click(fn=transcribe_and_summarize,
|
110 |
+
inputs=[youtube_url, translate_check],
|
111 |
+
outputs=[thumbnail, lang_out, transcript_out, summary_out, download_btn])
|
112 |
|
113 |
demo.launch()
|