Joachim M. Vis commited on
Commit
6eba77c
·
verified ·
1 Parent(s): f4a6c42

Upload multimodal_tools.py

Browse files
Files changed (1) hide show
  1. tools/multimodal_tools.py +524 -60
tools/multimodal_tools.py CHANGED
@@ -16,8 +16,21 @@ import json
16
  import os
17
  import tempfile
18
  import re
19
- from typing import Dict, Any, List, Optional, Union, BinaryIO
 
20
  from pathlib import Path
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  try:
23
  from PIL import Image
@@ -35,19 +48,45 @@ except ImportError:
35
  pdf2image = None
36
  docx2txt = None
37
 
 
 
 
 
 
38
  try:
39
- from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
40
- import requests
41
- except ImportError:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  YouTubeTranscriptApi = None
 
 
 
 
 
 
 
 
43
 
44
  from agent.config import get_model_config, get_tool_config
45
  from langchain_openai import ChatOpenAI
46
  from langchain.prompts import PromptTemplate
47
  from langchain_core.output_parsers import StrOutputParser
48
 
49
- logger = logging.getLogger("gaia_agent.tools.multimodal")
50
-
51
  class ImageAnalyzer:
52
  """Tool for analyzing and describing images."""
53
 
@@ -575,12 +614,25 @@ class YouTubeVideoTool:
575
  """
576
  self.config = config or get_tool_config().get("youtube_video", {})
577
  self.model_config = get_model_config()
578
- self.model = ChatOpenAI(
579
- model=self.model_config.get("text_model", "gpt-4o"),
580
- temperature=self.model_config.get("temperature", 0.1),
581
- max_tokens=self.model_config.get("max_tokens", 4096)
582
- )
583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584
  if YouTubeTranscriptApi is None:
585
  logger.warning("youtube_transcript_api not installed. Install with: pip install youtube-transcript-api")
586
 
@@ -597,6 +649,12 @@ class YouTubeVideoTool:
597
  Raises:
598
  ValueError: If the video ID cannot be extracted
599
  """
 
 
 
 
 
 
600
  # Regular expressions for different YouTube URL formats
601
  patterns = [
602
  r'(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/v\/|youtube\.com\/e\/|youtube\.com\/watch\?.*v=|youtube\.com\/watch\?.*&v=)([^&\n?#]+)',
@@ -606,8 +664,11 @@ class YouTubeVideoTool:
606
  for pattern in patterns:
607
  match = re.search(pattern, url)
608
  if match:
609
- return match.group(1)
 
 
610
 
 
611
  raise ValueError(f"Could not extract video ID from URL: {url}")
612
 
613
  def extract_transcript(self, video_id_or_url: str, language: Optional[str] = None) -> Dict[str, Any]:
@@ -625,26 +686,57 @@ class YouTubeVideoTool:
625
  Exception: If an error occurs during transcript extraction
626
  """
627
  if YouTubeTranscriptApi is None:
628
- raise ImportError("youtube_transcript_api not installed. Install with: pip install youtube-transcript-api")
 
 
 
 
 
 
 
629
 
 
630
  try:
631
  # Check if input is a URL or a video ID
632
- if "youtube.com" in video_id_or_url or "youtu.be" in video_id_or_url:
633
- video_id = self.extract_video_id(video_id_or_url)
 
 
 
 
 
 
 
 
 
 
634
  else:
635
  video_id = video_id_or_url
 
636
 
637
- # Get video metadata
638
- metadata = self.get_video_metadata(video_id)
 
639
 
640
- # Get transcript
641
- if language:
642
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=[language])
643
- else:
644
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
 
 
 
 
 
 
 
 
 
 
 
645
 
646
  # Format transcript with timestamps
647
- formatted_transcript = self._format_transcript(transcript_list)
648
 
649
  # Process transcript to identify speakers (if possible)
650
  processed_transcript = self._process_transcript_with_speakers(formatted_transcript)
@@ -656,33 +748,138 @@ class YouTubeVideoTool:
656
  "transcript": formatted_transcript,
657
  "processed_transcript": processed_transcript,
658
  "duration_seconds": metadata.get("duration", 0),
659
- "language": language or "auto-detected"
 
660
  }
661
 
662
  except TranscriptsDisabled as e:
663
- logger.error(f"Transcripts are disabled for this video: {str(e)}")
664
- return {
665
- "video_id": video_id,
666
- "error": "Transcripts are disabled for this video",
667
- "error_type": "TranscriptsDisabled"
668
- }
 
 
 
669
 
670
  except NoTranscriptFound as e:
671
- logger.error(f"No transcript found for this video: {str(e)}")
672
- return {
673
- "video_id": video_id,
674
- "error": "No transcript found for this video",
675
- "error_type": "NoTranscriptFound"
676
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677
 
678
  except Exception as e:
679
- logger.error(f"Error extracting transcript: {str(e)}")
680
- logger.error(traceback.format_exc())
681
- return {
682
- "video_id": video_id if 'video_id' in locals() else video_id_or_url,
683
- "error": f"Transcript extraction failed: {str(e)}",
684
- "error_type": type(e).__name__
685
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
686
 
687
  def get_video_metadata(self, video_id: str) -> Dict[str, Any]:
688
  """
@@ -694,13 +891,16 @@ class YouTubeVideoTool:
694
  Returns:
695
  Dictionary containing video metadata
696
  """
 
 
697
  try:
698
  # Use YouTube's oEmbed API to get basic metadata
699
  oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
700
- response = requests.get(oembed_url)
701
 
702
  if response.status_code == 200:
703
  metadata = response.json()
 
704
  return {
705
  "title": metadata.get("title", ""),
706
  "channel": metadata.get("author_name", ""),
@@ -708,23 +908,245 @@ class YouTubeVideoTool:
708
  "duration": 0 # YouTube's oEmbed API doesn't provide duration
709
  }
710
  else:
 
 
 
 
711
  # Fallback to minimal metadata
712
  return {
713
  "title": "Unknown",
714
  "channel": "Unknown",
715
  "thumbnail_url": "",
716
- "duration": 0
 
717
  }
718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719
  except Exception as e:
720
- logger.warning(f"Error fetching video metadata: {str(e)}")
 
 
 
 
721
  return {
722
  "title": "Unknown",
723
  "channel": "Unknown",
724
  "thumbnail_url": "",
725
- "duration": 0
 
726
  }
727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728
  def _format_transcript(self, transcript_list: List[Dict[str, Any]]) -> str:
729
  """
730
  Format transcript with timestamps.
@@ -735,20 +1157,30 @@ class YouTubeVideoTool:
735
  Returns:
736
  Formatted transcript string
737
  """
 
 
 
 
738
  formatted_lines = []
739
 
740
- for item in transcript_list:
741
- start_time = item['start']
742
- text = item['text']
743
-
744
- # Convert seconds to MM:SS format
745
- minutes = int(start_time // 60)
746
- seconds = int(start_time % 60)
747
- timestamp = f"[{minutes:02d}:{seconds:02d}]"
 
 
 
 
 
748
 
749
- formatted_lines.append(f"{timestamp} {text}")
750
-
751
- return "\n".join(formatted_lines)
 
752
 
753
  def _process_transcript_with_speakers(self, transcript: str) -> str:
754
  """
@@ -760,27 +1192,59 @@ class YouTubeVideoTool:
760
  Returns:
761
  Processed transcript with speaker identification if possible
762
  """
 
 
 
 
 
 
 
 
763
  try:
 
 
 
 
 
764
  # Use LLM to identify potential speakers in the transcript
 
 
 
 
 
 
 
 
 
 
765
  prompt = f"""
766
  Analyze this YouTube video transcript and identify different speakers if possible.
767
  Format the transcript with speaker labels (e.g., "Speaker 1:", "Speaker 2:").
768
  If you cannot confidently identify different speakers, return the transcript as is.
769
 
770
  Transcript:
771
- {transcript}
772
 
773
  Processed transcript with speakers:
774
  """
775
 
776
  prompt_template = PromptTemplate.from_template(prompt)
777
  chain = prompt_template | self.model | StrOutputParser()
 
 
778
  result = chain.invoke({})
779
 
 
 
 
 
 
 
780
  return result
781
 
782
  except Exception as e:
783
- logger.warning(f"Error processing transcript with speakers: {str(e)}")
 
784
  return transcript # Return original transcript if processing fails
785
 
786
 
 
16
  import os
17
  import tempfile
18
  import re
19
+ import time
20
+ from typing import Dict, Any, List, Optional, Union, BinaryIO, Tuple
21
  from pathlib import Path
22
+ from enum import Enum
23
+
24
+ # Configure module-level logger with more detailed format
25
+ logger = logging.getLogger("gaia_agent.tools.multimodal")
26
+
27
+ # Define error severity levels for better categorization
28
+ class ErrorSeverity(Enum):
29
+ """Enum for categorizing error severity levels."""
30
+ INFO = "INFO" # Informational, not critical
31
+ WARNING = "WARNING" # Potential issue, but operation can continue
32
+ ERROR = "ERROR" # Operation failed but system can continue
33
+ CRITICAL = "CRITICAL" # System cannot function properly
34
 
35
  try:
36
  from PIL import Image
 
48
  pdf2image = None
49
  docx2txt = None
50
 
51
+ # Import required modules
52
+ import requests
53
+ from requests.exceptions import RequestException, Timeout, ConnectionError as RequestsConnectionError
54
+
55
+ # Try to import YouTube transcript API
56
  try:
57
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, VideoUnavailable
58
+ # Some exceptions might not be available in all versions
59
+ try:
60
+ from youtube_transcript_api import NoTranscriptAvailable, TranslationLanguageNotAvailable
61
+ except ImportError:
62
+ NoTranscriptAvailable = Exception
63
+ TranslationLanguageNotAvailable = Exception
64
+
65
+ try:
66
+ from youtube_transcript_api import CookiePathInvalid, NotTranslatable
67
+ except ImportError:
68
+ CookiePathInvalid = Exception
69
+ NotTranslatable = Exception
70
+
71
+ # Define TooManyRequests if not available
72
+ TooManyRequests = Exception
73
+ except ImportError as e:
74
+ logger.error(f"Failed to import youtube_transcript_api: {str(e)}")
75
  YouTubeTranscriptApi = None
76
+ TranscriptsDisabled = Exception
77
+ NoTranscriptFound = Exception
78
+ VideoUnavailable = Exception
79
+ NoTranscriptAvailable = Exception
80
+ TranslationLanguageNotAvailable = Exception
81
+ CookiePathInvalid = Exception
82
+ NotTranslatable = Exception
83
+ TooManyRequests = Exception
84
 
85
  from agent.config import get_model_config, get_tool_config
86
  from langchain_openai import ChatOpenAI
87
  from langchain.prompts import PromptTemplate
88
  from langchain_core.output_parsers import StrOutputParser
89
 
 
 
90
  class ImageAnalyzer:
91
  """Tool for analyzing and describing images."""
92
 
 
614
  """
615
  self.config = config or get_tool_config().get("youtube_video", {})
616
  self.model_config = get_model_config()
 
 
 
 
 
617
 
618
+ # Initialize with default values
619
+ self.max_retries = self.config.get("max_retries", 3)
620
+ self.retry_delay = self.config.get("retry_delay", 2)
621
+ self.fallback_enabled = self.config.get("fallback_enabled", True)
622
+ self.timeout = self.config.get("request_timeout", 10)
623
+
624
+ # Initialize LLM for transcript processing
625
+ try:
626
+ self.model = ChatOpenAI(
627
+ model=self.model_config.get("text_model", "gpt-4o"),
628
+ temperature=self.model_config.get("temperature", 0.1),
629
+ max_tokens=self.model_config.get("max_tokens", 4096)
630
+ )
631
+ except Exception as e:
632
+ logger.error(f"Failed to initialize LLM model: {str(e)}", exc_info=True)
633
+ self.model = None
634
+
635
+ # Check for required dependencies
636
  if YouTubeTranscriptApi is None:
637
  logger.warning("youtube_transcript_api not installed. Install with: pip install youtube-transcript-api")
638
 
 
649
  Raises:
650
  ValueError: If the video ID cannot be extracted
651
  """
652
+ if not url:
653
+ logger.error("Empty URL provided to extract_video_id", extra={"severity": ErrorSeverity.ERROR.value})
654
+ raise ValueError("Empty URL provided")
655
+
656
+ logger.debug(f"Attempting to extract video ID from URL: {url}")
657
+
658
  # Regular expressions for different YouTube URL formats
659
  patterns = [
660
  r'(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/v\/|youtube\.com\/e\/|youtube\.com\/watch\?.*v=|youtube\.com\/watch\?.*&v=)([^&\n?#]+)',
 
664
  for pattern in patterns:
665
  match = re.search(pattern, url)
666
  if match:
667
+ video_id = match.group(1)
668
+ logger.debug(f"Successfully extracted video ID: {video_id}")
669
+ return video_id
670
 
671
+ logger.error(f"Could not extract video ID from URL: {url}", extra={"severity": ErrorSeverity.ERROR.value})
672
  raise ValueError(f"Could not extract video ID from URL: {url}")
673
 
674
  def extract_transcript(self, video_id_or_url: str, language: Optional[str] = None) -> Dict[str, Any]:
 
686
  Exception: If an error occurs during transcript extraction
687
  """
688
  if YouTubeTranscriptApi is None:
689
+ error_msg = "youtube_transcript_api not installed. Install with: pip install youtube-transcript-api"
690
+ logger.error(error_msg, extra={"severity": ErrorSeverity.CRITICAL.value})
691
+ return self._create_error_response(
692
+ video_id_or_url,
693
+ error_msg,
694
+ "ImportError",
695
+ severity=ErrorSeverity.CRITICAL
696
+ )
697
 
698
+ video_id = None
699
  try:
700
  # Check if input is a URL or a video ID
701
+ if isinstance(video_id_or_url, str) and ("youtube.com" in video_id_or_url or "youtu.be" in video_id_or_url):
702
+ try:
703
+ video_id = self.extract_video_id(video_id_or_url)
704
+ logger.info(f"Extracted video ID '{video_id}' from URL: {video_id_or_url}")
705
+ except ValueError as e:
706
+ logger.error(f"Failed to extract video ID: {str(e)}", extra={"severity": ErrorSeverity.ERROR.value})
707
+ return self._create_error_response(
708
+ video_id_or_url,
709
+ f"Invalid YouTube URL format: {str(e)}",
710
+ "ValueError",
711
+ severity=ErrorSeverity.ERROR
712
+ )
713
  else:
714
  video_id = video_id_or_url
715
+ logger.info(f"Using provided video ID: {video_id}")
716
 
717
+ # Validate video ID format
718
+ if not self._is_valid_video_id_format(video_id):
719
+ logger.warning(f"Video ID '{video_id}' has unusual format", extra={"severity": ErrorSeverity.WARNING.value})
720
 
721
+ # Get video metadata with retries
722
+ metadata = self._get_metadata_with_retries(video_id)
723
+
724
+ # Get transcript with retries and fallback
725
+ transcript_result = self._get_transcript_with_retries_and_fallback(video_id, language)
726
+
727
+ # Check if transcript extraction failed
728
+ if "error" in transcript_result:
729
+ # Merge metadata with error response
730
+ error_response = transcript_result
731
+ error_response.update({
732
+ "title": metadata.get("title", "Unknown"),
733
+ "channel": metadata.get("channel", "Unknown"),
734
+ "thumbnail_url": metadata.get("thumbnail_url", ""),
735
+ })
736
+ return error_response
737
 
738
  # Format transcript with timestamps
739
+ formatted_transcript = self._format_transcript(transcript_result["transcript_list"])
740
 
741
  # Process transcript to identify speakers (if possible)
742
  processed_transcript = self._process_transcript_with_speakers(formatted_transcript)
 
748
  "transcript": formatted_transcript,
749
  "processed_transcript": processed_transcript,
750
  "duration_seconds": metadata.get("duration", 0),
751
+ "language": transcript_result.get("language", language or "auto-detected"),
752
+ "transcript_source": transcript_result.get("source", "youtube_api")
753
  }
754
 
755
  except TranscriptsDisabled as e:
756
+ error_msg = f"Transcripts are disabled for this video: {str(e)}"
757
+ logger.error(error_msg, extra={"severity": ErrorSeverity.WARNING.value})
758
+ return self._create_error_response(
759
+ video_id or video_id_or_url,
760
+ error_msg,
761
+ "TranscriptsDisabled",
762
+ severity=ErrorSeverity.WARNING,
763
+ suggestion="This video has disabled transcripts. Try another video or use a different method to analyze the content."
764
+ )
765
 
766
  except NoTranscriptFound as e:
767
+ error_msg = f"No transcript found for this video: {str(e)}"
768
+ logger.error(error_msg, extra={"severity": ErrorSeverity.WARNING.value})
769
+ return self._create_error_response(
770
+ video_id or video_id_or_url,
771
+ error_msg,
772
+ "NoTranscriptFound",
773
+ severity=ErrorSeverity.WARNING,
774
+ suggestion="This video doesn't have captions. Try another video or use a different method to analyze the content."
775
+ )
776
+
777
+ except VideoUnavailable as e:
778
+ error_msg = f"Video is unavailable: {str(e)}"
779
+ logger.error(error_msg, extra={"severity": ErrorSeverity.ERROR.value})
780
+ return self._create_error_response(
781
+ video_id or video_id_or_url,
782
+ error_msg,
783
+ "VideoUnavailable",
784
+ severity=ErrorSeverity.ERROR,
785
+ suggestion="The video may be private, removed, or region-restricted."
786
+ )
787
+
788
+ except TooManyRequests as e:
789
+ error_msg = f"Too many requests to YouTube API: {str(e)}"
790
+ logger.error(error_msg, extra={"severity": ErrorSeverity.ERROR.value})
791
+ return self._create_error_response(
792
+ video_id or video_id_or_url,
793
+ error_msg,
794
+ "TooManyRequests",
795
+ severity=ErrorSeverity.ERROR,
796
+ suggestion="Try again later or reduce the frequency of requests."
797
+ )
798
+
799
+ except (RequestException, Timeout, RequestsConnectionError) as e:
800
+ error_msg = f"Network error while accessing YouTube: {str(e)}"
801
+ logger.error(error_msg, extra={"severity": ErrorSeverity.ERROR.value})
802
+ return self._create_error_response(
803
+ video_id or video_id_or_url,
804
+ error_msg,
805
+ type(e).__name__,
806
+ severity=ErrorSeverity.ERROR,
807
+ suggestion="Check your internet connection and try again."
808
+ )
809
 
810
  except Exception as e:
811
+ error_msg = f"Error extracting transcript: {str(e)}"
812
+ logger.error(error_msg, exc_info=True, extra={"severity": ErrorSeverity.ERROR.value})
813
+ return self._create_error_response(
814
+ video_id if video_id else video_id_or_url,
815
+ error_msg,
816
+ type(e).__name__,
817
+ severity=ErrorSeverity.ERROR
818
+ )
819
+
820
+ def _is_valid_video_id_format(self, video_id: str) -> bool:
821
+ """
822
+ Check if the video ID has a valid format.
823
+
824
+ Args:
825
+ video_id: YouTube video ID to validate
826
+
827
+ Returns:
828
+ True if the format appears valid, False otherwise
829
+ """
830
+ # YouTube IDs are typically 11 characters and contain alphanumeric chars, underscores, and hyphens
831
+ if not video_id or not isinstance(video_id, str):
832
+ return False
833
+
834
+ # Check length (most YouTube IDs are 11 characters)
835
+ if len(video_id) != 11:
836
+ return False
837
+
838
+ # Check for valid characters (alphanumeric, underscore, hyphen)
839
+ return bool(re.match(r'^[A-Za-z0-9_-]+$', video_id))
840
+
841
+ def _create_error_response(
842
+ self,
843
+ video_id_or_url: str,
844
+ error_message: str,
845
+ error_type: str,
846
+ severity: ErrorSeverity = ErrorSeverity.ERROR,
847
+ suggestion: Optional[str] = None
848
+ ) -> Dict[str, Any]:
849
+ """
850
+ Create a standardized error response.
851
+
852
+ Args:
853
+ video_id_or_url: The video ID or URL that caused the error
854
+ error_message: Detailed error message
855
+ error_type: Type of error
856
+ severity: Error severity level
857
+ suggestion: Optional suggestion for resolving the error
858
+
859
+ Returns:
860
+ Dictionary with standardized error information
861
+ """
862
+ # Extract video_id if URL was provided
863
+ video_id = video_id_or_url
864
+ if isinstance(video_id_or_url, str) and ("youtube.com" in video_id_or_url or "youtu.be" in video_id_or_url):
865
+ try:
866
+ video_id = self.extract_video_id(video_id_or_url)
867
+ except ValueError:
868
+ # Keep original value if extraction fails
869
+ pass
870
+
871
+ response = {
872
+ "video_id": video_id,
873
+ "error": error_message,
874
+ "error_type": error_type,
875
+ "severity": severity.value,
876
+ "success": False
877
+ }
878
+
879
+ if suggestion:
880
+ response["suggestion"] = suggestion
881
+
882
+ return response
883
 
884
  def get_video_metadata(self, video_id: str) -> Dict[str, Any]:
885
  """
 
891
  Returns:
892
  Dictionary containing video metadata
893
  """
894
+ logger.info(f"Getting metadata for video ID: {video_id}")
895
+
896
  try:
897
  # Use YouTube's oEmbed API to get basic metadata
898
  oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
899
+ response = requests.get(oembed_url, timeout=self.timeout)
900
 
901
  if response.status_code == 200:
902
  metadata = response.json()
903
+ logger.debug(f"Successfully retrieved metadata for video {video_id}")
904
  return {
905
  "title": metadata.get("title", ""),
906
  "channel": metadata.get("author_name", ""),
 
908
  "duration": 0 # YouTube's oEmbed API doesn't provide duration
909
  }
910
  else:
911
+ logger.warning(
912
+ f"Failed to get metadata: HTTP {response.status_code}",
913
+ extra={"severity": ErrorSeverity.WARNING.value}
914
+ )
915
  # Fallback to minimal metadata
916
  return {
917
  "title": "Unknown",
918
  "channel": "Unknown",
919
  "thumbnail_url": "",
920
+ "duration": 0,
921
+ "metadata_error": f"HTTP {response.status_code}"
922
  }
923
 
924
+ except Timeout as e:
925
+ logger.warning(f"Timeout while fetching video metadata: {str(e)}", extra={"severity": ErrorSeverity.WARNING.value})
926
+ return {
927
+ "title": "Unknown (Request Timeout)",
928
+ "channel": "Unknown",
929
+ "thumbnail_url": "",
930
+ "duration": 0,
931
+ "metadata_error": f"Request timeout: {str(e)}"
932
+ }
933
+
934
+ except RequestsConnectionError as e:
935
+ logger.warning(f"Connection error while fetching video metadata: {str(e)}", extra={"severity": ErrorSeverity.WARNING.value})
936
+ return {
937
+ "title": "Unknown (Connection Error)",
938
+ "channel": "Unknown",
939
+ "thumbnail_url": "",
940
+ "duration": 0,
941
+ "metadata_error": f"Connection error: {str(e)}"
942
+ }
943
+
944
  except Exception as e:
945
+ logger.warning(
946
+ f"Error fetching video metadata: {str(e)}",
947
+ exc_info=True,
948
+ extra={"severity": ErrorSeverity.WARNING.value}
949
+ )
950
  return {
951
  "title": "Unknown",
952
  "channel": "Unknown",
953
  "thumbnail_url": "",
954
+ "duration": 0,
955
+ "metadata_error": str(e)
956
  }
957
 
958
+ def _get_metadata_with_retries(self, video_id: str) -> Dict[str, Any]:
959
+ """
960
+ Get video metadata with retry mechanism.
961
+
962
+ Args:
963
+ video_id: YouTube video ID
964
+
965
+ Returns:
966
+ Dictionary containing video metadata
967
+ """
968
+ for attempt in range(1, self.max_retries + 1):
969
+ try:
970
+ metadata = self.get_video_metadata(video_id)
971
+ if metadata.get("title") != "Unknown" or attempt == self.max_retries:
972
+ return metadata
973
+
974
+ logger.warning(f"Metadata attempt {attempt} returned unknown title, retrying...")
975
+ time.sleep(self.retry_delay)
976
+
977
+ except Exception as e:
978
+ logger.warning(f"Metadata attempt {attempt} failed: {str(e)}")
979
+ if attempt < self.max_retries:
980
+ time.sleep(self.retry_delay)
981
+ else:
982
+ return {
983
+ "title": "Unknown",
984
+ "channel": "Unknown",
985
+ "thumbnail_url": "",
986
+ "duration": 0,
987
+ "metadata_error": f"All {self.max_retries} attempts failed"
988
+ }
989
+
990
+ # This should not be reached
991
+ return {
992
+ "title": "Unknown",
993
+ "channel": "Unknown",
994
+ "thumbnail_url": "",
995
+ "duration": 0
996
+ }
997
+
998
+ def _get_transcript_with_retries_and_fallback(self, video_id: str, language: Optional[str] = None) -> Dict[str, Any]:
999
+ """
1000
+ Get transcript with retries and fallback mechanisms.
1001
+
1002
+ Args:
1003
+ video_id: YouTube video ID
1004
+ language: Optional language code
1005
+
1006
+ Returns:
1007
+ Dictionary with transcript list and metadata or error information
1008
+ """
1009
+ logger.info(f"Attempting to get transcript for video {video_id} with language {language or 'auto'}")
1010
+
1011
+ # Try primary method with retries
1012
+ for attempt in range(1, self.max_retries + 1):
1013
+ try:
1014
+ logger.debug(f"Transcript extraction attempt {attempt}/{self.max_retries}")
1015
+
1016
+ if language:
1017
+ transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=[language])
1018
+ return {"transcript_list": transcript_list, "language": language, "source": "youtube_api"}
1019
+ else:
1020
+ transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
1021
+ return {"transcript_list": transcript_list, "source": "youtube_api"}
1022
+
1023
+ except (TranscriptsDisabled, NoTranscriptFound, VideoUnavailable) as e:
1024
+ # Don't retry for these specific exceptions
1025
+ logger.warning(f"Transcript unavailable (attempt {attempt}): {str(e)}")
1026
+ raise
1027
+
1028
+ except Exception as e:
1029
+ if attempt < self.max_retries:
1030
+ logger.warning(f"Transcript extraction failed (attempt {attempt}): {str(e)}. Retrying...")
1031
+ time.sleep(self.retry_delay)
1032
+ else:
1033
+ logger.error(f"All {self.max_retries} transcript extraction attempts failed: {str(e)}")
1034
+
1035
+ # Try fallback methods if enabled
1036
+ if self.fallback_enabled:
1037
+ return self._try_transcript_fallbacks(video_id, language)
1038
+
1039
+ # If no fallbacks or all fallbacks failed
1040
+ raise
1041
+
1042
+ # This should not be reached due to the exception handling above
1043
+ return {"error": "Failed to extract transcript after retries", "error_type": "ExtractionFailure"}
1044
+
1045
+ def _try_transcript_fallbacks(self, video_id: str, language: Optional[str] = None) -> Dict[str, Any]:
1046
+ """
1047
+ Try alternative methods to get transcript when primary method fails.
1048
+
1049
+ Args:
1050
+ video_id: YouTube video ID
1051
+ language: Optional language code
1052
+
1053
+ Returns:
1054
+ Dictionary with transcript list and metadata or error information
1055
+ """
1056
+ logger.info(f"Trying fallback methods for video {video_id}")
1057
+
1058
+ fallback_methods = [
1059
+ self._try_fallback_auto_generated,
1060
+ self._try_fallback_alternative_language,
1061
+ ]
1062
+
1063
+ for method in fallback_methods:
1064
+ try:
1065
+ result = method(video_id, language)
1066
+ if result and "transcript_list" in result:
1067
+ logger.info(f"Fallback method {method.__name__} succeeded")
1068
+ return result
1069
+ except Exception as e:
1070
+ logger.warning(f"Fallback method {method.__name__} failed: {str(e)}")
1071
+ continue
1072
+
1073
+ # All fallbacks failed
1074
+ return {
1075
+ "error": "Failed to extract transcript with all available methods",
1076
+ "error_type": "AllFallbacksFailed",
1077
+ "severity": ErrorSeverity.ERROR.value,
1078
+ "suggestion": "This video may not have any available transcripts or captions."
1079
+ }
1080
+
1081
+ def _try_fallback_auto_generated(self, video_id: str, language: Optional[str] = None) -> Dict[str, Any]:
1082
+ """
1083
+ Try to get auto-generated transcript as fallback.
1084
+
1085
+ Args:
1086
+ video_id: YouTube video ID
1087
+ language: Optional language code
1088
+
1089
+ Returns:
1090
+ Dictionary with transcript list and metadata or None if failed
1091
+ """
1092
+ logger.info(f"Trying to get auto-generated transcript for {video_id}")
1093
+ try:
1094
+ # Try to get auto-generated transcript
1095
+ transcript_list = YouTubeTranscriptApi.get_transcript(
1096
+ video_id,
1097
+ languages=['en'] if not language else [language],
1098
+ continue_after_error=True
1099
+ )
1100
+
1101
+ if transcript_list:
1102
+ return {
1103
+ "transcript_list": transcript_list,
1104
+ "source": "auto_generated",
1105
+ "language": language or "en"
1106
+ }
1107
+ return None
1108
+ except Exception as e:
1109
+ logger.warning(f"Auto-generated transcript fallback failed: {str(e)}")
1110
+ return None
1111
+
1112
+ def _try_fallback_alternative_language(self, video_id: str, language: Optional[str] = None) -> Dict[str, Any]:
1113
+ """
1114
+ Try to get transcript in alternative languages as fallback.
1115
+
1116
+ Args:
1117
+ video_id: YouTube video ID
1118
+ language: Optional language code
1119
+
1120
+ Returns:
1121
+ Dictionary with transcript list and metadata or None if failed
1122
+ """
1123
+ logger.info(f"Trying to get transcript in alternative languages for {video_id}")
1124
+ try:
1125
+ # Get available transcript languages
1126
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
1127
+
1128
+ # Try each available language
1129
+ for transcript in transcript_list:
1130
+ try:
1131
+ fetched_transcript = transcript.fetch()
1132
+ actual_language = transcript.language_code
1133
+
1134
+ logger.info(f"Found transcript in language: {actual_language}")
1135
+
1136
+ return {
1137
+ "transcript_list": fetched_transcript,
1138
+ "source": "alternative_language",
1139
+ "language": actual_language
1140
+ }
1141
+ except Exception as e:
1142
+ logger.debug(f"Failed to fetch transcript in {transcript.language_code}: {str(e)}")
1143
+ continue
1144
+
1145
+ return None
1146
+ except Exception as e:
1147
+ logger.warning(f"Alternative language fallback failed: {str(e)}")
1148
+ return None
1149
+
1150
  def _format_transcript(self, transcript_list: List[Dict[str, Any]]) -> str:
1151
  """
1152
  Format transcript with timestamps.
 
1157
  Returns:
1158
  Formatted transcript string
1159
  """
1160
+ if not transcript_list:
1161
+ logger.warning("Empty transcript list provided to _format_transcript")
1162
+ return ""
1163
+
1164
  formatted_lines = []
1165
 
1166
+ try:
1167
+ for item in transcript_list:
1168
+ start_time = item.get('start', 0)
1169
+ text = item.get('text', '')
1170
+
1171
+ # Convert seconds to MM:SS format
1172
+ minutes = int(start_time // 60)
1173
+ seconds = int(start_time % 60)
1174
+ timestamp = f"[{minutes:02d}:{seconds:02d}]"
1175
+
1176
+ formatted_lines.append(f"{timestamp} {text}")
1177
+
1178
+ return "\n".join(formatted_lines)
1179
 
1180
+ except Exception as e:
1181
+ logger.error(f"Error formatting transcript: {str(e)}", exc_info=True)
1182
+ # Return a basic format as fallback
1183
+ return "\n".join([f"[??:??] {item.get('text', '')}" for item in transcript_list])
1184
 
1185
  def _process_transcript_with_speakers(self, transcript: str) -> str:
1186
  """
 
1192
  Returns:
1193
  Processed transcript with speaker identification if possible
1194
  """
1195
+ if not transcript:
1196
+ logger.warning("Empty transcript provided to _process_transcript_with_speakers")
1197
+ return ""
1198
+
1199
+ if not self.model:
1200
+ logger.warning("LLM model not available for speaker identification")
1201
+ return transcript
1202
+
1203
  try:
1204
+ # Check if transcript is long enough to process
1205
+ if len(transcript) < 100:
1206
+ logger.info("Transcript too short for speaker identification")
1207
+ return transcript
1208
+
1209
  # Use LLM to identify potential speakers in the transcript
1210
+ logger.info("Processing transcript to identify speakers")
1211
+
1212
+ # Limit transcript length to avoid token limits
1213
+ max_length = 8000 # Adjust based on model token limits
1214
+ if len(transcript) > max_length:
1215
+ logger.warning(f"Transcript too long ({len(transcript)} chars), truncating to {max_length} chars for speaker identification")
1216
+ processed_transcript = transcript[:max_length] + "..."
1217
+ else:
1218
+ processed_transcript = transcript
1219
+
1220
  prompt = f"""
1221
  Analyze this YouTube video transcript and identify different speakers if possible.
1222
  Format the transcript with speaker labels (e.g., "Speaker 1:", "Speaker 2:").
1223
  If you cannot confidently identify different speakers, return the transcript as is.
1224
 
1225
  Transcript:
1226
+ {processed_transcript}
1227
 
1228
  Processed transcript with speakers:
1229
  """
1230
 
1231
  prompt_template = PromptTemplate.from_template(prompt)
1232
  chain = prompt_template | self.model | StrOutputParser()
1233
+
1234
+ # Set timeout to avoid hanging
1235
  result = chain.invoke({})
1236
 
1237
+ # Validate result
1238
+ if not result or len(result) < len(processed_transcript) / 2:
1239
+ logger.warning("Speaker identification returned suspiciously short result, using original transcript")
1240
+ return transcript
1241
+
1242
+ logger.info("Successfully processed transcript with speaker identification")
1243
  return result
1244
 
1245
  except Exception as e:
1246
+ logger.error(f"Error processing transcript with speakers: {str(e)}", exc_info=True,
1247
+ extra={"severity": ErrorSeverity.WARNING.value})
1248
  return transcript # Return original transcript if processing fails
1249
 
1250