From 354d21f38c7cf1e100f1a5cb3b5d912a6d6a2d8c Mon Sep 17 00:00:00 2001 From: Isaac John Date: Sun, 9 Nov 2025 02:05:04 +0530 Subject: [PATCH] Fix: Corrected app.py and added .gitignore --- .gitignore | 10 +++ app.py | 48 +++++++++++- requirements.txt | 12 +++ summarize_text.py | 32 +++++--- templates/index.html | 129 ++++++++++++------------------- templates/login.html | 158 ++++++-------------------------------- templates/manual.html | 55 +++++++------ templates/summarizer.html | 131 +++++++++++++++++++++++++++++++ 8 files changed, 320 insertions(+), 255 deletions(-) create mode 100644 .gitignore create mode 100644 requirements.txt create mode 100644 templates/summarizer.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fe73e16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Virtual Environment +venv/ +__pycache__/ + +# Model files +*.pkl +distilbart-cnn-dailymail-finetuned/ + +# OS files +.DS_Store \ No newline at end of file diff --git a/app.py b/app.py index f9173de..6907ff0 100644 --- a/app.py +++ b/app.py @@ -22,7 +22,7 @@ def process_and_summarize_list(items): return "(Error: The summarization script app.secret_key = SECRET_KEY # --- API & Model Setup --- -API_KEY = os.environ.get('YOUTUBE_API_KEY', 'AIzaSyC4ZwDopo_________Q4ZcB5Gw') +API_KEY = os.environ.get('YOUTUBE_API_KEY', 'AIzaSyBnjFy3_______PHAMEyhD4') youtube = build("youtube", "v3", developerKey=API_KEY) # **FIX**: Load the correct advanced model file model = joblib.load("predictive_view_modelGBR.pkl") @@ -214,6 +214,52 @@ def analyzer(): return render_template("index.html", result=result, topic_summary=topic_summary, categories=list(CATEGORY_MAP.keys()), form_data=form_data) +@app.route('/summarizer', methods=['GET', 'POST']) +def summarizer(): + if 'user' not in session: + return redirect(url_for('login')) + + error = None + summary_result = None + video_link = '' # To keep the form populated + + if request.method == 'POST': + video_link = request.form.get('video_link') + if not video_link: + error = "Please enter a video link." + else: + video_id = get_video_id_from_url(video_link) + + if not video_id: + error = "Invalid YouTube video link. Please check the URL." + else: + try: + # 1. Fetch video details using your existing helper + details = get_video_details([video_id]) + if not details: + error = "Could not fetch video details. Check the link and API key." + else: + snippet = details[0]['snippet'] + title = snippet.get('title', 'N/A') + channel = snippet.get('channelTitle', 'N/A') + description = snippet.get('description', '') + + # 2. Summarize using your existing helper + summary = safe_summarize(description) + + # 3. Package the results for the template + summary_result = { + 'title': title, + 'channelTitle': channel, + 'summary': summary, + 'videoId': video_id + } + except Exception as e: + print(f"Error in /summarizer route: {e}") + error = f"An unexpected error occurred." + + return render_template('summarizer.html', summary_result=summary_result, error=error, video_link=video_link) + @app.route('/manual', methods=['GET', 'POST']) def manual(): diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7606fa0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +flask +pandas +numpy +python-dateutil +google-api-python-client +joblib +transformers +# Install torch separately with the appropriate CUDA support for your GPU. +# Example (Windows, CUDA 11.8): +# pip install --index-url https://download.pytorch.org/whl/cu118 torch torchvision torchaudio +# Or CPU-only fallback: +# pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu diff --git a/summarize_text.py b/summarize_text.py index c6b4893..159d262 100644 --- a/summarize_text.py +++ b/summarize_text.py @@ -4,9 +4,21 @@ # Load the summarizer once. It's recommended to handle model loading gracefully. # This assumes the model is available at the specified local path. local_model_path = "./distilbart-cnn-dailymail-finetuned" +# Detect available device: prefer GPU when torch.cuda is available, otherwise fall back to CPU. try: - # Using device=-1 ensures it runs on CPU, which is more compatible for general use. - summarizer = pipeline("summarization", model=local_model_path, device=-1) + import torch + if torch.cuda.is_available(): + device = 0 # first CUDA device for transformers pipeline + print("GPU detected (torch.cuda.is_available=True). Using GPU for summarization.") + else: + device = -1 + print("No GPU detected. Using CPU for summarization (device=-1).") +except Exception: + device = -1 + print("Could not import torch — defaulting to CPU (device=-1). To enable GPU, install torch with CUDA support.") + +try: + summarizer = pipeline("summarization", model=local_model_path, device=device) except Exception as e: summarizer = None print(f"CRITICAL: Could not load the summarization model from '{local_model_path}'. Error: {e}") @@ -22,14 +34,14 @@ def clean_text(text): # --- Specific Platform Link Removal --- platform_domains = [ - 'youtube\.com', 'youtu\.be', 'music\.youtube\.com', - 'facebook\.com', 'fb\.watch', 'fb\.com', - 'instagram\.com', - 'twitter\.com', 't\.co', - 'spotify\.com', 'open\.spotify\.com', - 'music\.apple\.com', - 'music\.amazon\.com', - 'discord\.gg', 'discord\.com' + r'youtube\.com', r'youtu\.be', r'music\.youtube\.com', + r'facebook\.com', r'fb\.watch', r'fb\.com', + r'instagram\.com', + r'twitter\.com', r't\.co', + r'spotify\.com', r'open\.spotify\.com', + r'music\.apple\.com', + r'music\.amazon\.com', + r'discord\.gg', r'discord\.com' ] platform_link_pattern = r'https?://(www\.)?(' + '|'.join(platform_domains) + r')\S*' text = re.sub(platform_link_pattern, '', text, flags=re.IGNORECASE) diff --git a/templates/index.html b/templates/index.html index f73064d..33a4dcf 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,6 +5,7 @@ +
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
+
+
+
+
+
+
+
+
+
+
-
Trending Analyzer by IIITDMJ
+
Youtube Trend Analyzer
@@ -90,15 +61,14 @@ Logout
-
-
-

Trending Video Analyzer

+

Trending Analyzer

- - - +
+ +
+ + +
+
- {% if result %} -
-

Analysis Results

- {% for video in result %} -
-
- Title: {{ video.title }}
- Channel: {{ video.channelTitle }}
- Views: {{ "{:,}".format(video.viewCount) }}
- Summary: {{ video.summary }}
-
-
- +
+

Analysis Results

+ {% for video in result %} +
+
+ Title: {{ video.title }}
+ Channel: {{ video.channelTitle }}
+ Views: {{ "{:,}".format(video.viewCount) }}
+ Summary: {{ video.summary }}
+
+
+ +
+ {% endfor %} +
+

📌 Quick Overview

+

{{ topic_summary | safe }}

- {% endfor %} - -
-

📌 Overall Topic Summary

-

{{ topic_summary }}

+ {% endif %}
-{% endif %} - -
- - - \ No newline at end of file + diff --git a/templates/login.html b/templates/login.html index 4d81066..9ab83d9 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,150 +1,40 @@ - Trending Analyzer + Login - Trending Analyzer + - - - +
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
+
+
+
+
-
-
Trending Analyzer by IIITDMJ
-
-
- - -
- Logout -
-
- -
-
- Trending Analyzer - Manual Predictor +