Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -675,6 +675,132 @@ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, No
|
|
| 675 |
import re
|
| 676 |
from collections import Counter
|
| 677 |
from googleapiclient.discovery import build
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
def process_youtube_video(url="", keywords=""):
|
| 679 |
try:
|
| 680 |
# Initialize variables
|
|
@@ -714,7 +840,7 @@ def process_youtube_video(url="", keywords=""):
|
|
| 714 |
subtitle_info = extract_subtitle_info(text)
|
| 715 |
|
| 716 |
# Sentiment analysis
|
| 717 |
-
sentiment = TextBlob(text
|
| 718 |
sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 719 |
|
| 720 |
except TranscriptsDisabled:
|
|
@@ -744,7 +870,6 @@ def extract_video_id(url):
|
|
| 744 |
"""
|
| 745 |
Extracts the video ID from a YouTube URL.
|
| 746 |
"""
|
| 747 |
-
import re
|
| 748 |
match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
|
| 749 |
return match.group(1) if match else None
|
| 750 |
|
|
@@ -754,10 +879,7 @@ def get_video_metadata(video_id):
|
|
| 754 |
Fetches video metadata such as title and description using the YouTube Data API.
|
| 755 |
"""
|
| 756 |
try:
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
# Replace with your YouTube Data API key
|
| 760 |
-
YOUTUBE_API_KEY = "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98"
|
| 761 |
youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 762 |
request = youtube.videos().list(part="snippet", id=video_id)
|
| 763 |
response = request.execute()
|
|
@@ -784,7 +906,6 @@ def extract_subtitle_info(text):
|
|
| 784 |
sentences = text.split(". ")
|
| 785 |
|
| 786 |
# Example: Extract key topics or keywords
|
| 787 |
-
from collections import Counter
|
| 788 |
words = text.split()
|
| 789 |
common_words = Counter(words).most_common(10)
|
| 790 |
key_topics = ", ".join([word for word, count in common_words])
|
|
@@ -797,6 +918,15 @@ def extract_subtitle_info(text):
|
|
| 797 |
return f"Error extracting subtitle information: {str(e)}"
|
| 798 |
|
| 799 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 800 |
|
| 801 |
|
| 802 |
|
|
|
|
| 675 |
import re
|
| 676 |
from collections import Counter
|
| 677 |
from googleapiclient.discovery import build
|
| 678 |
+
# def process_youtube_video(url="", keywords=""):
|
| 679 |
+
# try:
|
| 680 |
+
# # Initialize variables
|
| 681 |
+
# thumbnail = None
|
| 682 |
+
# summary = "No transcript available"
|
| 683 |
+
# sentiment_label = "N/A"
|
| 684 |
+
# recommendations = ""
|
| 685 |
+
# subtitle_info = "No additional information available"
|
| 686 |
+
|
| 687 |
+
# if not url.strip():
|
| 688 |
+
# return None, "Please enter a YouTube URL", "N/A", "", ""
|
| 689 |
+
|
| 690 |
+
# video_id = extract_video_id(url)
|
| 691 |
+
# if not video_id:
|
| 692 |
+
# return None, "Invalid YouTube URL", "N/A", "", ""
|
| 693 |
+
|
| 694 |
+
# thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
|
| 695 |
+
|
| 696 |
+
# try:
|
| 697 |
+
# # Fetch transcript
|
| 698 |
+
# transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 699 |
+
# transcript = None
|
| 700 |
+
# try:
|
| 701 |
+
# transcript = transcript_list.find_transcript(['en'])
|
| 702 |
+
# except:
|
| 703 |
+
# transcript = transcript_list.find_generated_transcript(['en'])
|
| 704 |
+
|
| 705 |
+
# text = " ".join([t['text'] for t in transcript.fetch()])
|
| 706 |
+
# if not text.strip():
|
| 707 |
+
# raise ValueError("Transcript is empty")
|
| 708 |
+
|
| 709 |
+
# # Generate summary
|
| 710 |
+
# model = genai.GenerativeModel("gemini-pro")
|
| 711 |
+
# summary = model.generate_content(f"Summarize this: {text[:4000]}").text
|
| 712 |
+
|
| 713 |
+
# # Extract subtitle information
|
| 714 |
+
# subtitle_info = extract_subtitle_info(text)
|
| 715 |
+
|
| 716 |
+
# # Sentiment analysis
|
| 717 |
+
# sentiment = TextBlob(text[:1000]).sentiment
|
| 718 |
+
# sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 719 |
+
|
| 720 |
+
# except TranscriptsDisabled:
|
| 721 |
+
# metadata = get_video_metadata(video_id)
|
| 722 |
+
# summary = metadata.get("description", "⚠️ This video has disabled subtitles.")
|
| 723 |
+
# sentiment_label = "N/A"
|
| 724 |
+
# subtitle_info = "No subtitles available for analysis."
|
| 725 |
+
# except NoTranscriptFound:
|
| 726 |
+
# metadata = get_video_metadata(video_id)
|
| 727 |
+
# summary = metadata.get("description", "⚠️ No English transcript available.")
|
| 728 |
+
# sentiment_label = "N/A"
|
| 729 |
+
# subtitle_info = "No subtitles available for analysis."
|
| 730 |
+
# except Exception as e:
|
| 731 |
+
# return thumbnail, f"⚠️ Error processing transcript: {str(e)}", "N/A", "", ""
|
| 732 |
+
|
| 733 |
+
# # Get recommendations
|
| 734 |
+
# if keywords.strip():
|
| 735 |
+
# recommendations = get_recommendations(keywords)
|
| 736 |
+
|
| 737 |
+
# return thumbnail, summary, sentiment_label, subtitle_info, recommendations
|
| 738 |
+
|
| 739 |
+
# except Exception as e:
|
| 740 |
+
# return None, f"Error: {str(e)}", "N/A", "", ""
|
| 741 |
+
|
| 742 |
+
|
| 743 |
+
# def extract_video_id(url):
|
| 744 |
+
# """
|
| 745 |
+
# Extracts the video ID from a YouTube URL.
|
| 746 |
+
# """
|
| 747 |
+
# import re
|
| 748 |
+
# match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
|
| 749 |
+
# return match.group(1) if match else None
|
| 750 |
+
|
| 751 |
+
|
| 752 |
+
# def get_video_metadata(video_id):
|
| 753 |
+
# """
|
| 754 |
+
# Fetches video metadata such as title and description using the YouTube Data API.
|
| 755 |
+
# """
|
| 756 |
+
# try:
|
| 757 |
+
# from googleapiclient.discovery import build
|
| 758 |
+
|
| 759 |
+
# # Replace with your YouTube Data API key
|
| 760 |
+
# YOUTUBE_API_KEY = "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98"
|
| 761 |
+
# youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 762 |
+
# request = youtube.videos().list(part="snippet", id=video_id)
|
| 763 |
+
# response = request.execute()
|
| 764 |
+
|
| 765 |
+
# if "items" in response and len(response["items"]) > 0:
|
| 766 |
+
# snippet = response["items"][0]["snippet"]
|
| 767 |
+
# return {
|
| 768 |
+
# "title": snippet.get("title", "No title available"),
|
| 769 |
+
# "description": snippet.get("description", "No description available"),
|
| 770 |
+
# }
|
| 771 |
+
# return {}
|
| 772 |
+
|
| 773 |
+
# except Exception as e:
|
| 774 |
+
# return {"title": "Error fetching metadata", "description": str(e)}
|
| 775 |
+
|
| 776 |
+
|
| 777 |
+
# def extract_subtitle_info(text):
|
| 778 |
+
# """
|
| 779 |
+
# Extracts meaningful information from the subtitles.
|
| 780 |
+
# This could include topics, key insights, or a breakdown of the content.
|
| 781 |
+
# """
|
| 782 |
+
# try:
|
| 783 |
+
# # Split text into sentences for better analysis
|
| 784 |
+
# sentences = text.split(". ")
|
| 785 |
+
|
| 786 |
+
# # Example: Extract key topics or keywords
|
| 787 |
+
# from collections import Counter
|
| 788 |
+
# words = text.split()
|
| 789 |
+
# common_words = Counter(words).most_common(10)
|
| 790 |
+
# key_topics = ", ".join([word for word, count in common_words])
|
| 791 |
+
|
| 792 |
+
# # Example: Provide a breakdown of the content
|
| 793 |
+
# info = f"Key topics discussed: {key_topics}. \nNumber of sentences: {len(sentences)}. \nTotal words: {len(words)}."
|
| 794 |
+
|
| 795 |
+
# return info
|
| 796 |
+
# except Exception as e:
|
| 797 |
+
# return f"Error extracting subtitle information: {str(e)}"
|
| 798 |
+
from textblob import TextBlob
|
| 799 |
+
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
| 800 |
+
import re
|
| 801 |
+
from collections import Counter
|
| 802 |
+
from googleapiclient.discovery import build
|
| 803 |
+
|
| 804 |
def process_youtube_video(url="", keywords=""):
|
| 805 |
try:
|
| 806 |
# Initialize variables
|
|
|
|
| 840 |
subtitle_info = extract_subtitle_info(text)
|
| 841 |
|
| 842 |
# Sentiment analysis
|
| 843 |
+
sentiment = TextBlob(text).sentiment # Use the entire text for sentiment analysis
|
| 844 |
sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 845 |
|
| 846 |
except TranscriptsDisabled:
|
|
|
|
| 870 |
"""
|
| 871 |
Extracts the video ID from a YouTube URL.
|
| 872 |
"""
|
|
|
|
| 873 |
match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
|
| 874 |
return match.group(1) if match else None
|
| 875 |
|
|
|
|
| 879 |
Fetches video metadata such as title and description using the YouTube Data API.
|
| 880 |
"""
|
| 881 |
try:
|
| 882 |
+
YOUTUBE_API_KEY = "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98" # Replace with your YouTube Data API key
|
|
|
|
|
|
|
|
|
|
| 883 |
youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 884 |
request = youtube.videos().list(part="snippet", id=video_id)
|
| 885 |
response = request.execute()
|
|
|
|
| 906 |
sentences = text.split(". ")
|
| 907 |
|
| 908 |
# Example: Extract key topics or keywords
|
|
|
|
| 909 |
words = text.split()
|
| 910 |
common_words = Counter(words).most_common(10)
|
| 911 |
key_topics = ", ".join([word for word, count in common_words])
|
|
|
|
| 918 |
return f"Error extracting subtitle information: {str(e)}"
|
| 919 |
|
| 920 |
|
| 921 |
+
def get_recommendations(keywords):
|
| 922 |
+
"""
|
| 923 |
+
Fetches related video recommendations based on the provided keywords.
|
| 924 |
+
This function can be expanded with a proper API or custom logic.
|
| 925 |
+
"""
|
| 926 |
+
# Placeholder for fetching recommendations based on keywords
|
| 927 |
+
return f"Recommendations for: {keywords}" # Dummy return for now
|
| 928 |
+
|
| 929 |
+
|
| 930 |
|
| 931 |
|
| 932 |
|