Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,9 @@ import gradio as gr
|
|
3 |
import yt_dlp
|
4 |
import whisper
|
5 |
from transformers import pipeline, MarianMTModel, MarianTokenizer
|
6 |
-
from pytube import YouTube
|
7 |
import torch
|
8 |
|
9 |
-
# Load Whisper
|
10 |
whisper_model = whisper.load_model("small")
|
11 |
|
12 |
# Load summarizer
|
@@ -27,10 +26,20 @@ def download_audio(youtube_url):
|
|
27 |
ydl.download([youtube_url])
|
28 |
return output_file
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def translate_to_english(text):
|
31 |
-
|
32 |
-
translated =
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def process_video(url):
|
36 |
audio_path = download_audio(url)
|
@@ -43,10 +52,9 @@ def process_video(url):
|
|
43 |
summary = summarizer(translated_text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
44 |
|
45 |
# Get thumbnail
|
46 |
-
|
47 |
-
thumbnail_url = yt.thumbnail_url
|
48 |
|
49 |
-
return transcription, translated_text, summary, thumbnail_url
|
50 |
|
51 |
def download_summary(text):
|
52 |
filename = "summary.txt"
|
@@ -54,6 +62,7 @@ def download_summary(text):
|
|
54 |
f.write(text)
|
55 |
return filename
|
56 |
|
|
|
57 |
with gr.Blocks(theme=gr.themes.Soft(), title="π₯ YouTube Video Summarizer with LLaMA") as demo:
|
58 |
gr.Markdown("## π§ Multilingual YouTube Summarizer")
|
59 |
gr.Markdown("Upload a video link and get the transcript, English translation, and summary.")
|
@@ -71,8 +80,17 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π₯ YouTube Video Summarizer with
|
|
71 |
download_file = gr.File(label="Download Link")
|
72 |
video_thumb = gr.Image(label="ποΈ Video Thumbnail", width=256)
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
demo.launch()
|
|
|
3 |
import yt_dlp
|
4 |
import whisper
|
5 |
from transformers import pipeline, MarianMTModel, MarianTokenizer
|
|
|
6 |
import torch
|
7 |
|
8 |
+
# Load Whisper model
|
9 |
whisper_model = whisper.load_model("small")
|
10 |
|
11 |
# Load summarizer
|
|
|
26 |
ydl.download([youtube_url])
|
27 |
return output_file
|
28 |
|
29 |
+
def get_thumbnail(youtube_url):
|
30 |
+
ydl_opts = {'quiet': True}
|
31 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
32 |
+
info = ydl.extract_info(youtube_url, download=False)
|
33 |
+
return info.get("thumbnail", "")
|
34 |
+
|
35 |
def translate_to_english(text):
|
36 |
+
chunks = [text[i:i+500] for i in range(0, len(text), 500)]
|
37 |
+
translated = []
|
38 |
+
for chunk in chunks:
|
39 |
+
inputs = translation_tokenizer(chunk, return_tensors="pt", truncation=True, max_length=512)
|
40 |
+
output = translation_model.generate(**inputs, max_length=512)
|
41 |
+
translated.append(translation_tokenizer.decode(output[0], skip_special_tokens=True))
|
42 |
+
return " ".join(translated)
|
43 |
|
44 |
def process_video(url):
|
45 |
audio_path = download_audio(url)
|
|
|
52 |
summary = summarizer(translated_text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
53 |
|
54 |
# Get thumbnail
|
55 |
+
thumbnail_url = get_thumbnail(url)
|
|
|
56 |
|
57 |
+
return transcription, translated_text, summary, thumbnail_url
|
58 |
|
59 |
def download_summary(text):
|
60 |
filename = "summary.txt"
|
|
|
62 |
f.write(text)
|
63 |
return filename
|
64 |
|
65 |
+
# UI
|
66 |
with gr.Blocks(theme=gr.themes.Soft(), title="π₯ YouTube Video Summarizer with LLaMA") as demo:
|
67 |
gr.Markdown("## π§ Multilingual YouTube Summarizer")
|
68 |
gr.Markdown("Upload a video link and get the transcript, English translation, and summary.")
|
|
|
80 |
download_file = gr.File(label="Download Link")
|
81 |
video_thumb = gr.Image(label="ποΈ Video Thumbnail", width=256)
|
82 |
|
83 |
+
# Button actions
|
84 |
+
submit_btn.click(
|
85 |
+
fn=process_video,
|
86 |
+
inputs=[youtube_input],
|
87 |
+
outputs=[transcript_output, translation_output, summary_output, video_thumb]
|
88 |
+
)
|
89 |
+
|
90 |
+
download_btn.click(
|
91 |
+
fn=download_summary,
|
92 |
+
inputs=[summary_output],
|
93 |
+
outputs=[download_file]
|
94 |
+
)
|
95 |
|
96 |
+
demo.launch(share=True)
|