Update app.py
Browse files
app.py
CHANGED
@@ -4,17 +4,22 @@ import yt_dlp
|
|
4 |
import whisper
|
5 |
from transformers import pipeline, MarianMTModel, MarianTokenizer
|
6 |
import torch
|
|
|
7 |
|
8 |
-
# Load
|
9 |
whisper_model = whisper.load_model("small")
|
10 |
-
|
11 |
-
# Load summarizer
|
12 |
summarizer = pipeline("summarization", model="Falconsai/text_summarization")
|
13 |
-
|
14 |
-
# Load translation model (multilingual to English)
|
15 |
translation_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
|
16 |
translation_model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def download_audio(youtube_url):
|
19 |
output_file = "audio.webm"
|
20 |
ydl_opts = {
|
@@ -22,15 +27,20 @@ def download_audio(youtube_url):
|
|
22 |
'outtmpl': output_file,
|
23 |
'quiet': True,
|
24 |
}
|
|
|
|
|
25 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
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)]
|
@@ -47,11 +57,7 @@ def process_video(url):
|
|
47 |
transcription = result["text"]
|
48 |
|
49 |
translated_text = translate_to_english(transcription)
|
50 |
-
|
51 |
-
# Summarize
|
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
|
@@ -80,7 +86,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π₯ YouTube Video Summarizer with
|
|
80 |
download_file = gr.File(label="Download Link")
|
81 |
video_thumb = gr.Image(label="ποΈ Video Thumbnail", width=256)
|
82 |
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
submit_btn.click(
|
85 |
fn=process_video,
|
86 |
inputs=[youtube_input],
|
@@ -93,4 +105,10 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π₯ YouTube Video Summarizer with
|
|
93 |
outputs=[download_file]
|
94 |
)
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
demo.launch(share=True)
|
|
|
4 |
import whisper
|
5 |
from transformers import pipeline, MarianMTModel, MarianTokenizer
|
6 |
import torch
|
7 |
+
import shutil
|
8 |
|
9 |
+
# Load models
|
10 |
whisper_model = whisper.load_model("small")
|
|
|
|
|
11 |
summarizer = pipeline("summarization", model="Falconsai/text_summarization")
|
|
|
|
|
12 |
translation_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
|
13 |
translation_model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
|
14 |
|
15 |
+
COOKIES_PATH = "cookies.txt"
|
16 |
+
|
17 |
+
def save_cookies(file):
|
18 |
+
if file:
|
19 |
+
shutil.copy(file.name, COOKIES_PATH)
|
20 |
+
return "β
Cookies uploaded successfully!"
|
21 |
+
return "β οΈ Please upload a valid cookies.txt file."
|
22 |
+
|
23 |
def download_audio(youtube_url):
|
24 |
output_file = "audio.webm"
|
25 |
ydl_opts = {
|
|
|
27 |
'outtmpl': output_file,
|
28 |
'quiet': True,
|
29 |
}
|
30 |
+
if os.path.exists(COOKIES_PATH):
|
31 |
+
ydl_opts['cookiefile'] = COOKIES_PATH
|
32 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
33 |
ydl.download([youtube_url])
|
34 |
return output_file
|
35 |
|
36 |
def get_thumbnail(youtube_url):
|
37 |
ydl_opts = {'quiet': True}
|
38 |
+
if os.path.exists(COOKIES_PATH):
|
39 |
+
ydl_opts['cookiefile'] = COOKIES_PATH
|
40 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
41 |
info = ydl.extract_info(youtube_url, download=False)
|
42 |
return info.get("thumbnail", "")
|
43 |
+
return ""
|
44 |
|
45 |
def translate_to_english(text):
|
46 |
chunks = [text[i:i+500] for i in range(0, len(text), 500)]
|
|
|
57 |
transcription = result["text"]
|
58 |
|
59 |
translated_text = translate_to_english(transcription)
|
|
|
|
|
60 |
summary = summarizer(translated_text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
|
|
|
|
61 |
thumbnail_url = get_thumbnail(url)
|
62 |
|
63 |
return transcription, translated_text, summary, thumbnail_url
|
|
|
86 |
download_file = gr.File(label="Download Link")
|
87 |
video_thumb = gr.Image(label="ποΈ Video Thumbnail", width=256)
|
88 |
|
89 |
+
with gr.Row():
|
90 |
+
gr.Markdown("### π Upload `cookies.txt` (for YouTube access)")
|
91 |
+
cookies_file = gr.File(label="Upload cookies.txt", file_types=[".txt"])
|
92 |
+
cookie_status = gr.Textbox(label="Status", interactive=False)
|
93 |
+
upload_btn = gr.Button("π€ Upload Cookies")
|
94 |
+
|
95 |
+
# Button logic
|
96 |
submit_btn.click(
|
97 |
fn=process_video,
|
98 |
inputs=[youtube_input],
|
|
|
105 |
outputs=[download_file]
|
106 |
)
|
107 |
|
108 |
+
upload_btn.click(
|
109 |
+
fn=save_cookies,
|
110 |
+
inputs=[cookies_file],
|
111 |
+
outputs=[cookie_status]
|
112 |
+
)
|
113 |
+
|
114 |
demo.launch(share=True)
|