dipenvekaria commited on
Commit
ea330eb
·
verified ·
1 Parent(s): 4fc36e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -3
app.py CHANGED
@@ -8,7 +8,6 @@ from langgraph.graph import StateGraph, END
8
  from typing import Dict, List, TypedDict, Optional
9
  from langgraph.checkpoint.memory import MemorySaver
10
 
11
-
12
  # Set up logging to debug issues
13
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
14
  logger = logging.getLogger(__name__)
@@ -31,7 +30,6 @@ class SummarizerState(TypedDict):
31
  individual_output: str
32
  error_output: str
33
 
34
- # Instructions for the front-end
35
  # Instructions for the front-end
36
  INSTRUCTIONS = """
37
  # YouTube Video Summarizer
@@ -41,6 +39,9 @@ Save time with this app that summarizes up to 3 YouTube videos into concise 2-4
41
  ## Instructions
42
  1. Enter a YouTube URL and click **"Add URL"** to include it (up to 3 videos). View added URLs and their titles below. Click **"Clear"** to start over.
43
  2. Click **"Summarize"** to generate a master summary (combining insights from all videos) and individual summaries for each video in bullet points.
 
 
 
44
  """
45
 
46
  # Function to get video title using yt-dlp
@@ -49,12 +50,15 @@ def get_video_title(video_url: str) -> str:
49
  ydl_opts = {
50
  'quiet': True,
51
  'no_warnings': True,
 
52
  }
53
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
54
  info = ydl.extract_info(video_url, download=False)
55
  return info['title']
56
  except Exception as e:
57
  logger.error(f"Error fetching title for {video_url}: {str(e)}")
 
 
58
  return f"Unknown Title (Error: {str(e)})"
59
 
60
  # Function to fetch transcript using youtube_transcript_api
@@ -62,12 +66,20 @@ def get_transcript(video_url: str) -> tuple[Optional[str], Optional[str]]:
62
  try:
63
  video_id = video_url.split("v=")[-1].split("&")[0]
64
  logger.info(f"Fetching transcript for video_id: {video_id}")
65
- transcript = YouTubeTranscriptApi.get_transcript(video_id)
 
 
 
 
 
 
66
  text = " ".join([entry["text"] for entry in transcript])
67
  logger.info(f"Transcript fetched for {video_url}: {text[:100]}...")
68
  return text, None
69
  except Exception as e:
70
  logger.error(f"Error fetching transcript for {video_url}: {str(e)}")
 
 
71
  return None, f"Error fetching transcript for {video_url}: {str(e)}"
72
 
73
  # Function to summarize text using ChatGPT 4o-mini in bullet points
 
8
  from typing import Dict, List, TypedDict, Optional
9
  from langgraph.checkpoint.memory import MemorySaver
10
 
 
11
  # Set up logging to debug issues
12
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13
  logger = logging.getLogger(__name__)
 
30
  individual_output: str
31
  error_output: str
32
 
 
33
  # Instructions for the front-end
34
  INSTRUCTIONS = """
35
  # YouTube Video Summarizer
 
39
  ## Instructions
40
  1. Enter a YouTube URL and click **"Add URL"** to include it (up to 3 videos). View added URLs and their titles below. Click **"Clear"** to start over.
41
  2. Click **"Summarize"** to generate a master summary (combining insights from all videos) and individual summaries for each video in bullet points.
42
+
43
+ ## Note
44
+ - Some videos may trigger YouTube's bot detection, resulting in errors like "Sign in to confirm you’re not a bot." This is a limitation when running in cloud environments like Hugging Face Spaces. Try using videos with publicly available transcripts, or test different videos to avoid this issue.
45
  """
46
 
47
  # Function to get video title using yt-dlp
 
50
  ydl_opts = {
51
  'quiet': True,
52
  'no_warnings': True,
53
+ 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
54
  }
55
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
56
  info = ydl.extract_info(video_url, download=False)
57
  return info['title']
58
  except Exception as e:
59
  logger.error(f"Error fetching title for {video_url}: {str(e)}")
60
+ if "Sign in to confirm" in str(e):
61
+ return f"Unknown Title (Error: YouTube bot detection triggered. Please try a different video or ensure the video is publicly accessible.)"
62
  return f"Unknown Title (Error: {str(e)})"
63
 
64
  # Function to fetch transcript using youtube_transcript_api
 
66
  try:
67
  video_id = video_url.split("v=")[-1].split("&")[0]
68
  logger.info(f"Fetching transcript for video_id: {video_id}")
69
+ # Add headers to mimic a browser request
70
+ transcript = YouTubeTranscriptApi.get_transcript(
71
+ video_id,
72
+ headers={
73
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
74
+ }
75
+ )
76
  text = " ".join([entry["text"] for entry in transcript])
77
  logger.info(f"Transcript fetched for {video_url}: {text[:100]}...")
78
  return text, None
79
  except Exception as e:
80
  logger.error(f"Error fetching transcript for {video_url}: {str(e)}")
81
+ if "Sign in to confirm" in str(e):
82
+ return None, f"Error fetching transcript for {video_url}: YouTube bot detection triggered. Please try a different video or ensure the video has publicly available transcripts."
83
  return None, f"Error fetching transcript for {video_url}: {str(e)}"
84
 
85
  # Function to summarize text using ChatGPT 4o-mini in bullet points