VPCSinfo commited on
Commit
f1018d4
·
1 Parent(s): f279b36

[FIX] update YouTube transcript extraction to handle missing captions and improve error handling

Browse files
Files changed (2) hide show
  1. Image/image.jpg +0 -0
  2. tool.py +12 -8
Image/image.jpg ADDED
tool.py CHANGED
@@ -5,6 +5,7 @@ from transformers import pipeline
5
  import requests
6
  import io
7
  from PIL import Image
 
8
  #from dotenv import load_dotenv
9
 
10
  #load_dotenv()
@@ -17,7 +18,7 @@ class TranscriptSummarizer(Tool):
17
 
18
  def __init__(self, *args, hf_api_key: str = None, **kwargs):
19
  super().__init__(*args, **kwargs)
20
- self.summarizer = pipeline("summarization", model="google/pegasus-xsum")
21
  self.api_url = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
22
  self.hf_api_key = hf_api_key
23
  self.headers = {"Authorization": f"Bearer {self.hf_api_key}"}
@@ -55,7 +56,7 @@ class TranscriptSummarizer(Tool):
55
  # Concatenate the summaries
56
  full_summary = "\n".join(summaries)
57
 
58
- key_entities = full_summary.split()[:15] # Extract first 3 words as key entities
59
  image_prompt = f"Generate an image related to: {' '.join(key_entities)}, cartoon style"
60
  image_bytes = self.query({"inputs": image_prompt})
61
  image = Image.open(io.BytesIO(image_bytes))
@@ -76,17 +77,20 @@ class YouTubeTranscriptExtractor(Tool):
76
 
77
  def forward(self, video_url: str) -> str:
78
  try:
79
- from pytubefix import YouTube
80
  # Create a YouTube object
81
  yt = YouTube(video_url)
82
  lang='en'
83
  # Get the video transcript
84
  try:
85
- transcript = yt.captions[lang].generate_srt_captions()
86
- except KeyError:
87
- # If the specified language is not available, get the first available transcript
88
- transcript = next(iter(yt.captions.values())).generate_srt_captions()
89
- lang = next(iter(yt.captions.keys()))
 
 
 
 
90
 
91
  # Clean up the transcript by removing timestamps and line numbers
92
  cleaned_transcript = ""
 
5
  import requests
6
  import io
7
  from PIL import Image
8
+ from pytubefix import YouTube
9
  #from dotenv import load_dotenv
10
 
11
  #load_dotenv()
 
18
 
19
  def __init__(self, *args, hf_api_key: str = None, **kwargs):
20
  super().__init__(*args, **kwargs)
21
+ self.summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
22
  self.api_url = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
23
  self.hf_api_key = hf_api_key
24
  self.headers = {"Authorization": f"Bearer {self.hf_api_key}"}
 
56
  # Concatenate the summaries
57
  full_summary = "\n".join(summaries)
58
 
59
+ key_entities = full_summary.split()[:15] # Extract first 15 words as key entities
60
  image_prompt = f"Generate an image related to: {' '.join(key_entities)}, cartoon style"
61
  image_bytes = self.query({"inputs": image_prompt})
62
  image = Image.open(io.BytesIO(image_bytes))
 
77
 
78
  def forward(self, video_url: str) -> str:
79
  try:
 
80
  # Create a YouTube object
81
  yt = YouTube(video_url)
82
  lang='en'
83
  # Get the video transcript
84
  try:
85
+ if lang in yt.captions:
86
+ transcript = yt.captions['en'].generate_srt_captions()
87
+ else:
88
+ transcript = yt.captions.all()[0].generate_srt_captions()
89
+ lang = yt.captions.all()[0].code
90
+ except StopIteration:
91
+ return "No transcript available for this video."
92
+ except Exception as e:
93
+ return f"An unexpected error occurred while accessing captions: {str(e)}"
94
 
95
  # Clean up the transcript by removing timestamps and line numbers
96
  cleaned_transcript = ""