Sayiqa commited on
Commit
d778a04
·
verified ·
1 Parent(s): 226349c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -208
app.py CHANGED
@@ -267,232 +267,116 @@ from googleapiclient.discovery import build
267
  # # Placeholder for fetching recommendations based on keywords
268
  # return f"Recommendations for: {keywords}" # Dummy return for now
269
  ######################################
270
- # from textblob import TextBlob
271
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
272
- # import re
273
- # from collections import Counter
274
- # from googleapiclient.discovery import build
275
- # import os
276
-
277
- # # Set your YouTube API key
278
- # YOUTUBE_API_KEY = "YOUR_API_KEY_HERE" # Replace with your actual API key
279
- # # Alternatively, you can set it as an environment variable:
280
- # # YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY')
281
-
282
- # def process_youtube_video(url=""):
283
- # """
284
- # Process a YouTube video URL and return sentiment analysis of its content.
285
- # """
286
- # try:
287
- # # Input validation
288
- # if not url.strip():
289
- # return {"error": "Please enter a YouTube URL"}
290
-
291
- # # Extract video ID
292
- # video_id = extract_video_id(url)
293
- # if not video_id:
294
- # return {"error": "Invalid YouTube URL"}
295
-
296
- # # Get video transcript
297
- # text = get_video_transcript(video_id)
298
- # if isinstance(text, dict) and "error" in text:
299
- # return text
300
-
301
- # # Get video metadata
302
- # metadata = get_video_metadata(video_id)
303
- # if "error" in metadata:
304
- # return metadata
305
-
306
- # # Perform sentiment analysis
307
- # sentiment_result = analyze_sentiment(text)
308
-
309
- # return {
310
- # "success": True,
311
- # "metadata": metadata,
312
- # "sentiment": sentiment_result,
313
- # "video_id": video_id
314
- # }
315
-
316
- # except Exception as e:
317
- # return {"error": f"An error occurred: {str(e)}"}
318
-
319
- # def get_video_metadata(video_id):
320
- # """
321
- # Fetches video metadata using the YouTube Data API.
322
- # """
323
- # try:
324
- # youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
325
- # request = youtube.videos().list(
326
- # part="snippet",
327
- # id=video_id
328
- # )
329
- # response = request.execute()
330
-
331
- # if response.get("items"):
332
- # snippet = response["items"][0]["snippet"]
333
- # return {
334
- # "title": snippet.get("title", ""),
335
- # "description": snippet.get("description", ""),
336
- # "publishedAt": snippet.get("publishedAt", ""),
337
- # "channelTitle": snippet.get("channelTitle", "")
338
- # }
339
- # return {"error": "Video not found"}
340
-
341
- # except Exception as e:
342
- # return {"error": f"Error fetching metadata: {str(e)}"}
343
-
344
- # # [Previous functions remain the same: get_video_transcript, analyze_sentiment,
345
- # # extract_video_id, clean_text_for_analysis, get_detailed_sentiment]
346
-
347
- # # Example usage with proper error handling:
348
- # if __name__ == "__main__":
349
- # # Example with a real YouTube URL
350
- # test_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" # Replace with any YouTube URL
351
-
352
- # # Check if API key is set
353
- # if YOUTUBE_API_KEY == "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98":
354
- # print("Error: Please set your YouTube API key first!")
355
- # else:
356
- # result = process_youtube_video(test_url)
357
-
358
- # if "error" in result:
359
- # print(f"Error: {result['error']}")
360
- # else:
361
- # print("\n=== Video Information ===")
362
- # print(f"Title: {result['metadata']['title']}")
363
- # print(f"Channel: {result['metadata']['channelTitle']}")
364
-
365
- # print("\n=== Sentiment Analysis Results ===")
366
- # sentiment = result['sentiment']
367
- # print(f"Overall Sentiment: {sentiment['overall_sentiment']}")
368
- # print(f"Average Polarity: {sentiment['average_polarity']}")
369
-
370
- # print("\nSentiment Distribution:")
371
- # dist = sentiment['sentiment_distribution']
372
- # total = sum(dist.values())
373
- # if total > 0:
374
- # print(f"Positive: {dist['positive']} ({(dist['positive']/total*100):.1f}%)")
375
- # print(f"Neutral: {dist['neutral']} ({(dist['neutral']/total*100):.1f}%)")
376
- # print(f"Negative: {dist['negative']} ({(dist['negative']/total*100):.1f}%)")
377
-
378
- # print(f"\nTotal Sentences Analyzed: {sentiment['total_sentences']}")
379
- #####################################################################################################
380
- from pytube import YouTube
381
- import re
382
  from textblob import TextBlob
383
  from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
384
- import google.generativeai as genai
 
 
 
385
 
386
- def process_youtube_video(url="", keywords=""):
387
- try:
388
- thumbnail = None
389
- summary = ""
390
- sentiment_label = "N/A"
391
- recommendations = ""
392
 
 
 
 
 
 
 
393
  if not url.strip():
394
- return thumbnail, "Please enter a YouTube URL", sentiment_label, recommendations
395
 
 
396
  video_id = extract_video_id(url)
397
  if not video_id:
398
- return thumbnail, "Invalid YouTube URL", sentiment_label, recommendations
399
-
400
- thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
401
-
402
- # Initialize variables for transcript fetching
403
- text = ""
404
- error_messages = []
405
-
406
- # Method 1: Using YouTube Transcript API
407
- try:
408
- transcript = YouTubeTranscriptApi.get_transcript(video_id)
409
- text = " ".join([t['text'] for t in transcript])
410
- except (TranscriptsDisabled, NoTranscriptFound) as e:
411
- error_messages.append(f"Transcript API error: {str(e)}")
412
- except Exception as e:
413
- error_messages.append(f"Transcript API general error: {str(e)}")
414
-
415
- # Method 2: Using PyTube if the first method fails
416
- if not text:
417
- try:
418
- yt = YouTube(url)
419
- captions = yt.captions
420
- if 'en' in captions:
421
- text = captions['en'].generate_srt_captions()
422
- elif 'a.en' in captions:
423
- text = captions['a.en'].generate_srt_captions()
424
- except Exception as e:
425
- error_messages.append(f"PyTube error: {str(e)}")
426
-
427
- # Method 3: Using auto-generated captions via Transcript API
428
- if not text:
429
- try:
430
- transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
431
- auto_transcript = transcript_list.find_generated_transcript(['en'])
432
- text = " ".join([t['text'] for t in auto_transcript.fetch()])
433
- except Exception as e:
434
- error_messages.append(f"Auto-generated captions error: {str(e)}")
435
-
436
- # Check if transcript was successfully fetched
437
- if not text:
438
- error_msg = "\n".join(error_messages)
439
- return thumbnail, f"⚠️ Could not access video content. Details: {error_msg}", sentiment_label, recommendations
440
-
441
- # Process valid transcript
442
- try:
443
- # Clean text for analysis
444
- cleaned_text = re.sub(r'[^\w\s.]', '', text)
445
- cleaned_text = ' '.join(cleaned_text.split())
446
-
447
- # Sentiment Analysis
448
- blob = TextBlob(cleaned_text[:2000]) # Analyze first 2000 characters for performance
449
- polarity = blob.sentiment.polarity
450
- subjectivity = blob.sentiment.subjectivity
451
-
452
- sentiment_label = (
453
- f"Sentiment: {'Positive' if polarity > 0 else 'Negative' if polarity < 0 else 'Neutral'}\n"
454
- f"Confidence: {abs(polarity):.2f}\n"
455
- f"Subjectivity: {subjectivity:.2f}"
456
- )
457
-
458
- # Generate summary using Gemini (Generative AI)
459
- genai.configure(api_key="AIzaSyDw4LHOzdkRrU7GunTTC3_f6iS1OsAbmKA") # Replace with your actual API key
460
- model = genai.GenerativeModel("gemini-pro")
461
- prompt = f"""Provide a comprehensive summary of this content in clear points:
462
- {cleaned_text[:4000]}
463
- Include:
464
- 1. Main topics
465
- 2. Key points
466
- 3. Important takeaways"""
467
-
468
- summary = model.generate_content(prompt).text
469
 
470
- except Exception as e:
471
- return thumbnail, f"⚠️ Error processing content: {str(e)}", sentiment_label, recommendations
 
 
472
 
473
- # Get recommendations based on keywords
474
- if keywords.strip():
475
- recommendations = get_recommendations(keywords)
 
476
 
477
- return thumbnail, summary, sentiment_label, recommendations
 
 
 
 
 
 
 
 
478
 
479
  except Exception as e:
480
- return None, f"Error: {str(e)}", "N/A", ""
481
 
482
- def extract_video_id(url):
483
  """
484
- Extracts the video ID from a YouTube URL.
485
  """
486
- match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
487
- return match.group(1) if match else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488
 
489
- def get_recommendations(keywords):
490
- """
491
- Fetches related video recommendations based on the provided keywords.
492
- """
493
- # Placeholder for fetching recommendations based on keywords
494
- return f"Recommendations for: {keywords}" # Dummy return for now
495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
 
497
  def get_recommendations(keywords, max_results=5):
498
  if not keywords:
 
267
  # # Placeholder for fetching recommendations based on keywords
268
  # return f"Recommendations for: {keywords}" # Dummy return for now
269
  ######################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  from textblob import TextBlob
271
  from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
272
+ import re
273
+ from collections import Counter
274
+ from googleapiclient.discovery import build
275
+ import os
276
 
277
+ # Set your YouTube API key
278
+ YOUTUBE_API_KEY = "AIzaSyB7X-RYjZmUuDSMTQsvCfyzURw5bhqOto4" # Replace with your actual API key
279
+ # Alternatively, you can set it as an environment variable:
280
+ # YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY')
 
 
281
 
282
+ def process_youtube_video(url=""):
283
+ """
284
+ Process a YouTube video URL and return sentiment analysis of its content.
285
+ """
286
+ try:
287
+ # Input validation
288
  if not url.strip():
289
+ return {"error": "Please enter a YouTube URL"}
290
 
291
+ # Extract video ID
292
  video_id = extract_video_id(url)
293
  if not video_id:
294
+ return {"error": "Invalid YouTube URL"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
 
296
+ # Get video transcript
297
+ text = get_video_transcript(video_id)
298
+ if isinstance(text, dict) and "error" in text:
299
+ return text
300
 
301
+ # Get video metadata
302
+ metadata = get_video_metadata(video_id)
303
+ if "error" in metadata:
304
+ return metadata
305
 
306
+ # Perform sentiment analysis
307
+ sentiment_result = analyze_sentiment(text)
308
+
309
+ return {
310
+ "success": True,
311
+ "metadata": metadata,
312
+ "sentiment": sentiment_result,
313
+ "video_id": video_id
314
+ }
315
 
316
  except Exception as e:
317
+ return {"error": f"An error occurred: {str(e)}"}
318
 
319
+ def get_video_metadata(video_id):
320
  """
321
+ Fetches video metadata using the YouTube Data API.
322
  """
323
+ try:
324
+ youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
325
+ request = youtube.videos().list(
326
+ part="snippet",
327
+ id=video_id
328
+ )
329
+ response = request.execute()
330
+
331
+ if response.get("items"):
332
+ snippet = response["items"][0]["snippet"]
333
+ return {
334
+ "title": snippet.get("title", ""),
335
+ "description": snippet.get("description", ""),
336
+ "publishedAt": snippet.get("publishedAt", ""),
337
+ "channelTitle": snippet.get("channelTitle", "")
338
+ }
339
+ return {"error": "Video not found"}
340
 
341
+ except Exception as e:
342
+ return {"error": f"Error fetching metadata: {str(e)}"}
 
 
 
 
343
 
344
+ # [Previous functions remain the same: get_video_transcript, analyze_sentiment,
345
+ # extract_video_id, clean_text_for_analysis, get_detailed_sentiment]
346
+
347
+ # Example usage with proper error handling:
348
+ if __name__ == "__main__":
349
+ # Example with a real YouTube URL
350
+ test_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" # Replace with any YouTube URL
351
+
352
+ # Check if API key is set
353
+ if YOUTUBE_API_KEY == "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98":
354
+ print("Error: Please set your YouTube API key first!")
355
+ else:
356
+ result = process_youtube_video(test_url)
357
+
358
+ if "error" in result:
359
+ print(f"Error: {result['error']}")
360
+ else:
361
+ print("\n=== Video Information ===")
362
+ print(f"Title: {result['metadata']['title']}")
363
+ print(f"Channel: {result['metadata']['channelTitle']}")
364
+
365
+ print("\n=== Sentiment Analysis Results ===")
366
+ sentiment = result['sentiment']
367
+ print(f"Overall Sentiment: {sentiment['overall_sentiment']}")
368
+ print(f"Average Polarity: {sentiment['average_polarity']}")
369
+
370
+ print("\nSentiment Distribution:")
371
+ dist = sentiment['sentiment_distribution']
372
+ total = sum(dist.values())
373
+ if total > 0:
374
+ print(f"Positive: {dist['positive']} ({(dist['positive']/total*100):.1f}%)")
375
+ print(f"Neutral: {dist['neutral']} ({(dist['neutral']/total*100):.1f}%)")
376
+ print(f"Negative: {dist['negative']} ({(dist['negative']/total*100):.1f}%)")
377
+
378
+ print(f"\nTotal Sentences Analyzed: {sentiment['total_sentences']}")
379
+ #####################################################################################################
380
 
381
  def get_recommendations(keywords, max_results=5):
382
  if not keywords: