Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -134,69 +134,178 @@ def extract_video_id(url):
|
|
| 134 |
return None
|
| 135 |
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def process_youtube_video(url="", keywords=""):
|
| 138 |
try:
|
| 139 |
# Initialize variables
|
| 140 |
-
thumbnail = None
|
| 141 |
summary = "No transcript available"
|
| 142 |
sentiment_label = "N/A"
|
|
|
|
|
|
|
| 143 |
|
| 144 |
if not url.strip():
|
| 145 |
-
return None, "Please enter a YouTube URL", "N/A", ""
|
| 146 |
-
|
| 147 |
video_id = extract_video_id(url)
|
| 148 |
if not video_id:
|
| 149 |
-
return None, "Invalid YouTube URL", "N/A", ""
|
| 150 |
|
| 151 |
thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
|
| 152 |
|
| 153 |
try:
|
| 154 |
-
#
|
| 155 |
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 156 |
-
|
| 157 |
try:
|
| 158 |
transcript = transcript_list.find_transcript(['en'])
|
| 159 |
except:
|
| 160 |
-
|
| 161 |
-
transcript = transcript_list.find_transcript(['en-US'])
|
| 162 |
-
except:
|
| 163 |
-
try:
|
| 164 |
-
# Try auto-generated
|
| 165 |
-
transcript = transcript_list.find_generated_transcript(['en'])
|
| 166 |
-
except:
|
| 167 |
-
raise NoTranscriptFound()
|
| 168 |
|
| 169 |
text = " ".join([t['text'] for t in transcript.fetch()])
|
| 170 |
-
|
|
|
|
|
|
|
| 171 |
# Generate summary
|
| 172 |
model = genai.GenerativeModel("gemini-pro")
|
| 173 |
summary = model.generate_content(f"Summarize this: {text[:4000]}").text
|
| 174 |
-
|
| 175 |
-
#
|
|
|
|
|
|
|
|
|
|
| 176 |
sentiment = TextBlob(text[:1000]).sentiment
|
| 177 |
sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 178 |
|
| 179 |
except TranscriptsDisabled:
|
| 180 |
-
# Fallback: Use video metadata if available
|
| 181 |
metadata = get_video_metadata(video_id)
|
| 182 |
-
summary = metadata.get("description", "⚠️ This video has disabled subtitles.
|
|
|
|
|
|
|
| 183 |
except NoTranscriptFound:
|
| 184 |
-
# Fallback: Use video metadata if available
|
| 185 |
metadata = get_video_metadata(video_id)
|
| 186 |
-
summary = metadata.get("description", "⚠️ No English transcript available.
|
|
|
|
|
|
|
| 187 |
except Exception as e:
|
| 188 |
-
return thumbnail, f"⚠️ Error: {str(e)}", "N/A", ""
|
| 189 |
|
| 190 |
# Get recommendations
|
| 191 |
if keywords.strip():
|
| 192 |
recommendations = get_recommendations(keywords)
|
| 193 |
-
else:
|
| 194 |
-
recommendations = ""
|
| 195 |
|
| 196 |
-
return thumbnail, summary, sentiment_label, recommendations
|
| 197 |
|
| 198 |
except Exception as e:
|
| 199 |
-
return None, f"Error: {str(e)}", "N/A", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
|
| 202 |
def get_video_metadata(video_id):
|
|
@@ -207,8 +316,8 @@ def get_video_metadata(video_id):
|
|
| 207 |
from googleapiclient.discovery import build
|
| 208 |
|
| 209 |
# Replace with your YouTube Data API key
|
| 210 |
-
|
| 211 |
-
youtube = build("youtube", "v3", developerKey=
|
| 212 |
request = youtube.videos().list(part="snippet", id=video_id)
|
| 213 |
response = request.execute()
|
| 214 |
|
|
@@ -223,16 +332,36 @@ def get_video_metadata(video_id):
|
|
| 223 |
except Exception as e:
|
| 224 |
return {"title": "Error fetching metadata", "description": str(e)}
|
| 225 |
|
| 226 |
-
# Get recommendations
|
| 227 |
-
if keywords.strip():
|
| 228 |
-
recommendations = get_recommendations(keywords)
|
| 229 |
-
else:
|
| 230 |
-
recommendations = ""
|
| 231 |
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
|
|
|
| 234 |
except Exception as e:
|
| 235 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
def get_recommendations(keywords, max_results=5):
|
| 237 |
if not keywords:
|
| 238 |
return "Please provide search keywords"
|
|
|
|
| 134 |
return None
|
| 135 |
|
| 136 |
|
| 137 |
+
# def process_youtube_video(url="", keywords=""):
|
| 138 |
+
# try:
|
| 139 |
+
# # Initialize variables
|
| 140 |
+
# thumbnail = None # Default value for thumbnail
|
| 141 |
+
# summary = "No transcript available"
|
| 142 |
+
# sentiment_label = "N/A"
|
| 143 |
+
|
| 144 |
+
# if not url.strip():
|
| 145 |
+
# return None, "Please enter a YouTube URL", "N/A", ""
|
| 146 |
+
|
| 147 |
+
# video_id = extract_video_id(url)
|
| 148 |
+
# if not video_id:
|
| 149 |
+
# return None, "Invalid YouTube URL", "N/A", ""
|
| 150 |
+
|
| 151 |
+
# thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
|
| 152 |
+
|
| 153 |
+
# try:
|
| 154 |
+
# # Try multiple transcript options
|
| 155 |
+
# transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 156 |
+
|
| 157 |
+
# try:
|
| 158 |
+
# transcript = transcript_list.find_transcript(['en'])
|
| 159 |
+
# except:
|
| 160 |
+
# try:
|
| 161 |
+
# transcript = transcript_list.find_transcript(['en-US'])
|
| 162 |
+
# except:
|
| 163 |
+
# try:
|
| 164 |
+
# # Try auto-generated
|
| 165 |
+
# transcript = transcript_list.find_generated_transcript(['en'])
|
| 166 |
+
# except:
|
| 167 |
+
# raise NoTranscriptFound()
|
| 168 |
+
|
| 169 |
+
# text = " ".join([t['text'] for t in transcript.fetch()])
|
| 170 |
+
|
| 171 |
+
# # Generate summary
|
| 172 |
+
# model = genai.GenerativeModel("gemini-pro")
|
| 173 |
+
# summary = model.generate_content(f"Summarize this: {text[:4000]}").text
|
| 174 |
+
|
| 175 |
+
# # Analysis
|
| 176 |
+
# sentiment = TextBlob(text[:1000]).sentiment
|
| 177 |
+
# sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 178 |
+
|
| 179 |
+
# except TranscriptsDisabled:
|
| 180 |
+
# # Fallback: Use video metadata if available
|
| 181 |
+
# metadata = get_video_metadata(video_id)
|
| 182 |
+
# summary = metadata.get("description", "⚠️ This video has disabled subtitles. No transcript available.")
|
| 183 |
+
# except NoTranscriptFound:
|
| 184 |
+
# # Fallback: Use video metadata if available
|
| 185 |
+
# metadata = get_video_metadata(video_id)
|
| 186 |
+
# summary = metadata.get("description", "⚠️ No English transcript available. No transcript available.")
|
| 187 |
+
# except Exception as e:
|
| 188 |
+
# return thumbnail, f"⚠️ Error: {str(e)}", "N/A", ""
|
| 189 |
+
|
| 190 |
+
# # Get recommendations
|
| 191 |
+
# if keywords.strip():
|
| 192 |
+
# recommendations = get_recommendations(keywords)
|
| 193 |
+
# else:
|
| 194 |
+
# recommendations = ""
|
| 195 |
+
|
| 196 |
+
# return thumbnail, summary, sentiment_label, recommendations
|
| 197 |
+
|
| 198 |
+
# except Exception as e:
|
| 199 |
+
# return None, f"Error: {str(e)}", "N/A", ""
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# def get_video_metadata(video_id):
|
| 203 |
+
# """
|
| 204 |
+
# Fetches video metadata such as title and description using the YouTube Data API.
|
| 205 |
+
# """
|
| 206 |
+
# try:
|
| 207 |
+
# from googleapiclient.discovery import build
|
| 208 |
+
|
| 209 |
+
# # Replace with your YouTube Data API key
|
| 210 |
+
# API_KEY = "AIzaSyB7X-RYjZmUuDSMTQsvCfyzURw5bhqOto4"
|
| 211 |
+
# youtube = build("youtube", "v3", developerKey=API_KEY)
|
| 212 |
+
# request = youtube.videos().list(part="snippet", id=video_id)
|
| 213 |
+
# response = request.execute()
|
| 214 |
+
|
| 215 |
+
# if "items" in response and len(response["items"]) > 0:
|
| 216 |
+
# snippet = response["items"][0]["snippet"]
|
| 217 |
+
# return {
|
| 218 |
+
# "title": snippet.get("title", "No title available"),
|
| 219 |
+
# "description": snippet.get("description", "No description available"),
|
| 220 |
+
# }
|
| 221 |
+
# return {}
|
| 222 |
+
|
| 223 |
+
# except Exception as e:
|
| 224 |
+
# return {"title": "Error fetching metadata", "description": str(e)}
|
| 225 |
+
|
| 226 |
+
# # Get recommendations
|
| 227 |
+
# if keywords.strip():
|
| 228 |
+
# recommendations = get_recommendations(keywords)
|
| 229 |
+
# else:
|
| 230 |
+
# recommendations = ""
|
| 231 |
+
|
| 232 |
+
# return thumbnail, summary, sentiment_label, recommendations
|
| 233 |
+
|
| 234 |
+
# except Exception as e:
|
| 235 |
+
# return None, f"Error: {str(e)}", "N/A", ""
|
| 236 |
+
|
| 237 |
def process_youtube_video(url="", keywords=""):
|
| 238 |
try:
|
| 239 |
# Initialize variables
|
| 240 |
+
thumbnail = None
|
| 241 |
summary = "No transcript available"
|
| 242 |
sentiment_label = "N/A"
|
| 243 |
+
recommendations = ""
|
| 244 |
+
subtitle_info = "No additional information available"
|
| 245 |
|
| 246 |
if not url.strip():
|
| 247 |
+
return None, "Please enter a YouTube URL", "N/A", "", ""
|
| 248 |
+
|
| 249 |
video_id = extract_video_id(url)
|
| 250 |
if not video_id:
|
| 251 |
+
return None, "Invalid YouTube URL", "N/A", "", ""
|
| 252 |
|
| 253 |
thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
|
| 254 |
|
| 255 |
try:
|
| 256 |
+
# Fetch transcript
|
| 257 |
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 258 |
+
transcript = None
|
| 259 |
try:
|
| 260 |
transcript = transcript_list.find_transcript(['en'])
|
| 261 |
except:
|
| 262 |
+
transcript = transcript_list.find_generated_transcript(['en'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
text = " ".join([t['text'] for t in transcript.fetch()])
|
| 265 |
+
if not text.strip():
|
| 266 |
+
raise ValueError("Transcript is empty")
|
| 267 |
+
|
| 268 |
# Generate summary
|
| 269 |
model = genai.GenerativeModel("gemini-pro")
|
| 270 |
summary = model.generate_content(f"Summarize this: {text[:4000]}").text
|
| 271 |
+
|
| 272 |
+
# Extract subtitle information
|
| 273 |
+
subtitle_info = extract_subtitle_info(text)
|
| 274 |
+
|
| 275 |
+
# Sentiment analysis
|
| 276 |
sentiment = TextBlob(text[:1000]).sentiment
|
| 277 |
sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 278 |
|
| 279 |
except TranscriptsDisabled:
|
|
|
|
| 280 |
metadata = get_video_metadata(video_id)
|
| 281 |
+
summary = metadata.get("description", "⚠️ This video has disabled subtitles.")
|
| 282 |
+
sentiment_label = "N/A"
|
| 283 |
+
subtitle_info = "No subtitles available for analysis."
|
| 284 |
except NoTranscriptFound:
|
|
|
|
| 285 |
metadata = get_video_metadata(video_id)
|
| 286 |
+
summary = metadata.get("description", "⚠️ No English transcript available.")
|
| 287 |
+
sentiment_label = "N/A"
|
| 288 |
+
subtitle_info = "No subtitles available for analysis."
|
| 289 |
except Exception as e:
|
| 290 |
+
return thumbnail, f"⚠️ Error processing transcript: {str(e)}", "N/A", "", ""
|
| 291 |
|
| 292 |
# Get recommendations
|
| 293 |
if keywords.strip():
|
| 294 |
recommendations = get_recommendations(keywords)
|
|
|
|
|
|
|
| 295 |
|
| 296 |
+
return thumbnail, summary, sentiment_label, subtitle_info, recommendations
|
| 297 |
|
| 298 |
except Exception as e:
|
| 299 |
+
return None, f"Error: {str(e)}", "N/A", "", ""
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
def extract_video_id(url):
|
| 303 |
+
"""
|
| 304 |
+
Extracts the video ID from a YouTube URL.
|
| 305 |
+
"""
|
| 306 |
+
import re
|
| 307 |
+
match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
|
| 308 |
+
return match.group(1) if match else None
|
| 309 |
|
| 310 |
|
| 311 |
def get_video_metadata(video_id):
|
|
|
|
| 316 |
from googleapiclient.discovery import build
|
| 317 |
|
| 318 |
# Replace with your YouTube Data API key
|
| 319 |
+
YOUTUBE_API_KEY = "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98"
|
| 320 |
+
youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 321 |
request = youtube.videos().list(part="snippet", id=video_id)
|
| 322 |
response = request.execute()
|
| 323 |
|
|
|
|
| 332 |
except Exception as e:
|
| 333 |
return {"title": "Error fetching metadata", "description": str(e)}
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
+
def extract_subtitle_info(text):
|
| 337 |
+
"""
|
| 338 |
+
Extracts meaningful information from the subtitles.
|
| 339 |
+
This could include topics, key insights, or a breakdown of the content.
|
| 340 |
+
"""
|
| 341 |
+
try:
|
| 342 |
+
# Split text into sentences for better analysis
|
| 343 |
+
sentences = text.split(". ")
|
| 344 |
+
|
| 345 |
+
# Example: Extract key topics or keywords
|
| 346 |
+
from collections import Counter
|
| 347 |
+
words = text.split()
|
| 348 |
+
common_words = Counter(words).most_common(10)
|
| 349 |
+
key_topics = ", ".join([word for word, count in common_words])
|
| 350 |
+
|
| 351 |
+
# Example: Provide a breakdown of the content
|
| 352 |
+
info = f"Key topics discussed: {key_topics}. \nNumber of sentences: {len(sentences)}. \nTotal words: {len(words)}."
|
| 353 |
|
| 354 |
+
return info
|
| 355 |
except Exception as e:
|
| 356 |
+
return f"Error extracting subtitle information: {str(e)}"
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
|
| 365 |
def get_recommendations(keywords, max_results=5):
|
| 366 |
if not keywords:
|
| 367 |
return "Please provide search keywords"
|