Sayiqa commited on
Commit
9c6e92e
·
verified ·
1 Parent(s): 85f9afa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -15
app.py CHANGED
@@ -801,6 +801,13 @@ 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
@@ -832,16 +839,19 @@ def process_youtube_video(url="", keywords=""):
832
  if not text.strip():
833
  raise ValueError("Transcript is empty")
834
 
 
 
 
 
 
 
 
835
  # Generate summary
836
  model = genai.GenerativeModel("gemini-pro")
837
- summary = model.generate_content(f"Summarize this: {text[:4000]}").text
838
 
839
  # Extract subtitle information
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:
847
  metadata = get_video_metadata(video_id)
@@ -879,7 +889,7 @@ def get_video_metadata(video_id):
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()
@@ -918,16 +928,22 @@ def extract_subtitle_info(text):
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
 
933
 
 
801
  from collections import Counter
802
  from googleapiclient.discovery import build
803
 
804
+
805
+ from textblob import TextBlob
806
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
807
+ import re
808
+ from collections import Counter
809
+ from googleapiclient.discovery import build
810
+
811
  def process_youtube_video(url="", keywords=""):
812
  try:
813
  # Initialize variables
 
839
  if not text.strip():
840
  raise ValueError("Transcript is empty")
841
 
842
+ # Clean up the text for sentiment analysis
843
+ cleaned_text = clean_text_for_analysis(text)
844
+
845
+ # Sentiment analysis
846
+ sentiment = TextBlob(cleaned_text).sentiment # Use cleaned text for sentiment analysis
847
+ sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
848
+
849
  # Generate summary
850
  model = genai.GenerativeModel("gemini-pro")
851
+ summary = model.generate_content(f"Summarize this: {cleaned_text[:4000]}").text
852
 
853
  # Extract subtitle information
854
+ subtitle_info = extract_subtitle_info(cleaned_text)
 
 
 
 
855
 
856
  except TranscriptsDisabled:
857
  metadata = get_video_metadata(video_id)
 
889
  Fetches video metadata such as title and description using the YouTube Data API.
890
  """
891
  try:
892
+ YOUTUBE_API_KEY = "YOUR_YOUTUBE_API_KEY" # Replace with your YouTube Data API key
893
  youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
894
  request = youtube.videos().list(part="snippet", id=video_id)
895
  response = request.execute()
 
928
  return f"Error extracting subtitle information: {str(e)}"
929
 
930
 
931
+ def clean_text_for_analysis(text):
932
+ """
933
+ Cleans the transcript text by removing extra spaces, line breaks, and non-text elements.
934
+ """
935
+ # Remove extra spaces and line breaks
936
+ cleaned_text = " ".join(text.split())
937
+ return cleaned_text
 
938
 
939
 
940
+ def get_recommendations(keywords):
941
+ """
942
+ Fetches related video recommendations based on the provided keywords.
943
+ This function can be expanded with a proper API or custom logic.
944
+ """
945
+ # Placeholder for fetching recommendations based on keywords
946
+ return f"Recommendations for: {keywords}" # Dummy return for now
947
 
948
 
949