Update app.py
Browse files
app.py
CHANGED
@@ -1,126 +1,45 @@
|
|
1 |
-
import os
|
2 |
-
import tempfile
|
3 |
-
import uuid
|
4 |
-
import shutil
|
5 |
-
import warnings
|
6 |
-
import logging
|
7 |
-
|
8 |
import gradio as gr
|
9 |
-
import
|
10 |
-
import
|
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 |
-
ydl_opts = {'quiet': True}
|
54 |
-
if os.path.exists(COOKIES_PATH):
|
55 |
-
ydl_opts['cookiefile'] = COOKIES_PATH
|
56 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
57 |
-
info = ydl.extract_info(youtube_url, download=False)
|
58 |
-
return info.get("thumbnail", "")
|
59 |
-
except Exception as e:
|
60 |
-
logger.error(f"Thumbnail fetch error: {e}")
|
61 |
-
return ""
|
62 |
-
|
63 |
-
def translate_to_english(text):
|
64 |
-
chunks = [text[i:i+500] for i in range(0, len(text), 500)]
|
65 |
-
translated = []
|
66 |
-
for chunk in chunks:
|
67 |
-
inputs = translation_tokenizer(chunk, return_tensors="pt", truncation=True, max_length=512)
|
68 |
-
output = translation_model.generate(**inputs, max_length=512)
|
69 |
-
translated.append(translation_tokenizer.decode(output[0], skip_special_tokens=True))
|
70 |
-
return " ".join(translated)
|
71 |
-
|
72 |
-
def process_video(url):
|
73 |
-
try:
|
74 |
-
audio_path = download_audio(url)
|
75 |
-
result = whisper_model.transcribe(audio_path)
|
76 |
-
transcription = result["text"]
|
77 |
-
|
78 |
-
translated_text = translate_to_english(transcription)
|
79 |
-
summary = summarizer(translated_text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
80 |
-
thumbnail_url = get_thumbnail(url)
|
81 |
-
|
82 |
-
os.remove(audio_path) # Clean up temp audio file
|
83 |
-
|
84 |
-
return transcription, translated_text, summary, thumbnail_url
|
85 |
-
except Exception as e:
|
86 |
-
logger.exception("Error processing video")
|
87 |
-
return f"β Error: {str(e)}", "", "", ""
|
88 |
-
|
89 |
-
def download_summary(text):
|
90 |
-
filename = os.path.join(tempfile.gettempdir(), f"summary_{uuid.uuid4().hex}.txt")
|
91 |
-
with open(filename, "w", encoding="utf-8") as f:
|
92 |
-
f.write(text)
|
93 |
-
return filename
|
94 |
-
|
95 |
-
# Gradio UI
|
96 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="π₯ YouTube Video Summarizer") as demo:
|
97 |
-
gr.Markdown("## π§ Multilingual YouTube Summarizer")
|
98 |
-
gr.Markdown("Upload a video link and get the transcript, English translation, and summary.")
|
99 |
-
|
100 |
-
with gr.Row():
|
101 |
-
youtube_input = gr.Text(label="YouTube Video URL", placeholder="https://www.youtube.com/watch?v=...")
|
102 |
-
submit_btn = gr.Button("π― Transcribe & Summarize")
|
103 |
-
|
104 |
-
with gr.Row():
|
105 |
-
with gr.Column():
|
106 |
-
transcript_output = gr.Textbox(label="π Original Transcript", lines=10)
|
107 |
-
translation_output = gr.Textbox(label="π Translated to English", lines=10)
|
108 |
-
summary_output = gr.Textbox(label="π§Ύ Summary", lines=10)
|
109 |
-
download_btn = gr.Button("π₯ Download Summary")
|
110 |
-
download_file = gr.File(label="Download Link")
|
111 |
-
video_thumb = gr.Image(label="ποΈ Video Thumbnail", width=256)
|
112 |
-
|
113 |
-
with gr.Row():
|
114 |
-
gr.Markdown("### π Upload `cookies.txt` (for YouTube access)")
|
115 |
-
cookies_file = gr.File(label="Upload cookies.txt", file_types=[".txt"])
|
116 |
-
cookie_status = gr.Textbox(label="Status", interactive=False)
|
117 |
-
upload_btn = gr.Button("π€ Upload Cookies")
|
118 |
-
|
119 |
-
# Button logic
|
120 |
-
submit_btn.click(
|
121 |
-
fn=process_video,
|
122 |
-
inputs=[youtube_input],
|
123 |
-
outputs=[transcript_output, translation_output, summary_output, video_thumb]
|
124 |
-
)
|
125 |
-
|
126 |
-
dow
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import datetime
|
4 |
+
|
5 |
+
# Initialize components
|
6 |
+
note_generator = pipeline("text-generation", model="gpt2")
|
7 |
+
notes_db = []
|
8 |
+
|
9 |
+
def save_note(note):
|
10 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
11 |
+
notes_db.append({"text": note, "time": timestamp})
|
12 |
+
return f"Note saved at {timestamp}"
|
13 |
+
|
14 |
+
def get_notes():
|
15 |
+
return "\n\n".join([f"{note['time']}:\n{note['text']}" for note in notes_db])
|
16 |
+
|
17 |
+
def process_note(input_text, action):
|
18 |
+
if action == "Save":
|
19 |
+
return save_note(input_text)
|
20 |
+
elif action != "Default":
|
21 |
+
prompt = f"{action} this text: {input_text}\n\nResult:"
|
22 |
+
result = note_generator(prompt, max_length=200)[0]['generated_text']
|
23 |
+
return result
|
24 |
+
return input_text
|
25 |
+
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
gr.Markdown("# Advanced NoteGPT Clone")
|
28 |
+
with gr.Tab("Note Editor"):
|
29 |
+
with gr.Row():
|
30 |
+
with gr.Column():
|
31 |
+
user_input = gr.Textbox(label="Your Notes", lines=10)
|
32 |
+
action = gr.Radio(["Default", "Summarize", "Expand", "Rephrase", "Save"],
|
33 |
+
label="Action")
|
34 |
+
generate_btn = gr.Button("Process Note")
|
35 |
+
with gr.Column():
|
36 |
+
output = gr.Textbox(label="Result", lines=10)
|
37 |
+
|
38 |
+
with gr.Tab("Saved Notes"):
|
39 |
+
notes_display = gr.Textbox(label="Your Saved Notes", lines=20)
|
40 |
+
refresh_btn = gr.Button("Refresh Notes")
|
41 |
+
|
42 |
+
generate_btn.click(fn=process_note, inputs=[user_input, action], outputs=output)
|
43 |
+
refresh_btn.click(fn=get_notes, inputs=None, outputs=notes_display)
|
44 |
+
|
45 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|