das1mtb56 commited on
Commit
e645cfc
Β·
verified Β·
1 Parent(s): b057493

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -125
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 yt_dlp
10
- import whisper
11
- from transformers import pipeline, MarianMTModel, MarianTokenizer
12
-
13
- # Suppress Whisper CPU warning
14
- warnings.filterwarnings("ignore", message="FP16 is not supported on CPU; using FP32 instead")
15
-
16
- # Setup logging
17
- logging.basicConfig(level=logging.INFO)
18
- logger = logging.getLogger(__name__)
19
-
20
- # Constants
21
- WHISPER_MODEL_SIZE = "small"
22
- SUMMARIZER_MODEL_NAME = "Falconsai/text_summarization"
23
- TRANSLATION_MODEL_NAME = "Helsinki-NLP/opus-mt-mul-en"
24
- COOKIES_PATH = "cookies.txt"
25
-
26
- # Load models once at startup
27
- whisper_model = whisper.load_model(WHISPER_MODEL_SIZE)
28
- summarizer = pipeline("summarization", model=SUMMARIZER_MODEL_NAME)
29
- translation_tokenizer = MarianTokenizer.from_pretrained(TRANSLATION_MODEL_NAME)
30
- translation_model = MarianMTModel.from_pretrained(TRANSLATION_MODEL_NAME)
31
-
32
- def save_cookies(file):
33
- if file:
34
- shutil.copy(file.name, COOKIES_PATH)
35
- return "βœ… Cookies uploaded successfully!"
36
- return "⚠️ Please upload a valid cookies.txt file."
37
-
38
- def download_audio(youtube_url):
39
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".webm")
40
- ydl_opts = {
41
- 'format': 'bestaudio/best',
42
- 'outtmpl': temp_file.name,
43
- 'quiet': True,
44
- }
45
- if os.path.exists(COOKIES_PATH):
46
- ydl_opts['cookiefile'] = COOKIES_PATH
47
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
48
- ydl.download([youtube_url])
49
- return temp_file.name
50
-
51
- def get_thumbnail(youtube_url):
52
- try:
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()