HarshanaLF commited on
Commit
60a521e
·
verified ·
1 Parent(s): 981ffea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -1
app.py CHANGED
@@ -3,6 +3,7 @@ from transformers import pipeline
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
  from youtube_transcript_api.formatters import TextFormatter
5
  import re
 
6
 
7
  # Define the models
8
  models = {
@@ -31,6 +32,7 @@ def get_youtube_transcript(video_url):
31
  if not video_id:
32
  return "Video ID could not be extracted. Please check the URL format."
33
 
 
34
  try:
35
  transcript = YouTubeTranscriptApi.get_transcript(video_id)
36
  formatter = TextFormatter()
@@ -39,9 +41,25 @@ def get_youtube_transcript(video_url):
39
  except Exception as e:
40
  error_message = str(e)
41
  if "Subtitles are disabled for this video" in error_message:
42
- return "Subtitles are disabled for this video. Transcript cannot be retrieved."
 
43
  return f"An error occurred while retrieving the transcript: {error_message}"
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Function to summarize YouTube video with selected model
46
  def summarize_youtube_video(url, model_name):
47
  transcript = get_youtube_transcript(url)
 
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
  from youtube_transcript_api.formatters import TextFormatter
5
  import re
6
+ from pytube import YouTube
7
 
8
  # Define the models
9
  models = {
 
32
  if not video_id:
33
  return "Video ID could not be extracted. Please check the URL format."
34
 
35
+ # Try to get transcript using YouTubeTranscriptApi
36
  try:
37
  transcript = YouTubeTranscriptApi.get_transcript(video_id)
38
  formatter = TextFormatter()
 
41
  except Exception as e:
42
  error_message = str(e)
43
  if "Subtitles are disabled for this video" in error_message:
44
+ # Try to get subtitles using pytube
45
+ return get_subtitles_with_pytube(video_url)
46
  return f"An error occurred while retrieving the transcript: {error_message}"
47
 
48
+ def get_subtitles_with_pytube(video_url):
49
+ video_id = extract_video_id(video_url)
50
+ if not video_id:
51
+ return "Video ID could not be extracted. Please check the URL format."
52
+
53
+ try:
54
+ yt = YouTube(video_url)
55
+ captions = yt.captions.get_by_language_code('en') # You can modify the language code
56
+ if captions:
57
+ return captions.generate_srt_captions()
58
+ return "No subtitles available in the selected language."
59
+ except Exception as e:
60
+ return f"An error occurred while retrieving subtitles with pytube: {str(e)}"
61
+
62
+
63
  # Function to summarize YouTube video with selected model
64
  def summarize_youtube_video(url, model_name):
65
  transcript = get_youtube_transcript(url)