Manasa1 commited on
Commit
17ce9b6
·
verified ·
1 Parent(s): d731eb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -35
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import streamlit as st
2
  from pytube import YouTube
 
 
3
  from phi.agent import Agent
4
  from phi.model.google import Gemini
5
  from phi.tools.duckduckgo import DuckDuckGo
@@ -40,10 +42,10 @@ def initialize_agent():
40
  # Initialize the agent
41
  multimodal_Agent = initialize_agent()
42
 
43
- # Function to get YouTube video captions using pytube
44
  def get_youtube_captions(youtube_url):
45
  """
46
- Retrieves YouTube video captions using pytube.
47
 
48
  Parameters:
49
  - youtube_url: The URL of the YouTube video.
@@ -51,28 +53,19 @@ def get_youtube_captions(youtube_url):
51
  Returns:
52
  - The captions of the video in SRT format, or a message if captions are not available.
53
  """
54
- yt = YouTube(youtube_url)
55
-
56
- # List available caption tracks
57
- caption_tracks = yt.caption_tracks
58
- available_languages = [track.language_code for track in caption_tracks]
59
-
60
- if available_languages:
61
- st.write(f"Available caption languages: {', '.join(available_languages)}")
62
-
63
- # You can set a fallback language or prompt the user to select
64
- language = 'en' # Change this to any available language from the list
65
 
66
- # Check if the chosen language exists in the video
67
- if language in yt.captions:
68
- caption = yt.captions[language]
69
- return caption.generate_srt_captions() # Return captions in SRT format
70
- else:
71
- return f"Captions not available in the selected language ({language})."
72
- else:
73
- return "No captions available for this video."
74
-
75
-
76
 
77
  # YouTube video URL input
78
  youtube_url = st.text_input(
@@ -83,13 +76,17 @@ youtube_url = st.text_input(
83
 
84
  if youtube_url:
85
  try:
86
- with st.spinner("Fetching captions from the YouTube video..."):
87
- # Get captions for the video
88
  captions = get_youtube_captions(youtube_url)
89
 
90
- # Display captions
91
- st.subheader("Video Captions (SRT format)")
92
- st.text(captions)
 
 
 
 
93
 
94
  # User query input
95
  user_query = st.text_area(
@@ -103,19 +100,25 @@ if youtube_url:
103
  st.warning("Please enter a question or insight to analyze the video.")
104
  else:
105
  try:
106
- with st.spinner("Analyzing captions and gathering insights..."):
107
- # Generate the analysis prompt using captions
 
 
 
 
 
 
108
  analysis_prompt = (
109
  f"""
110
- Analyze the content of the following YouTube video captions.
111
- Respond to the following query using the video captions and supplementary web research:
112
  {user_query}
113
  Provide a detailed, user-friendly, and actionable response.
114
  """
115
  )
116
 
117
  # AI agent processing
118
- response = multimodal_Agent.run(analysis_prompt, video_captions=captions)
119
 
120
  # Display the result
121
  st.subheader("Analysis Result")
@@ -123,8 +126,7 @@ if youtube_url:
123
 
124
  except Exception as error:
125
  st.error(f"An error occurred during analysis: {error}")
126
-
127
  except Exception as e:
128
- st.error(f"An error occurred while fetching captions: {e}")
129
  else:
130
  st.info("Enter a YouTube video link to begin analysis.")
 
1
  import streamlit as st
2
  from pytube import YouTube
3
+ from youtube_transcript_api import YouTubeTranscriptApi
4
+ from youtube_transcript_api.formatters import SRTFormatter
5
  from phi.agent import Agent
6
  from phi.model.google import Gemini
7
  from phi.tools.duckduckgo import DuckDuckGo
 
42
  # Initialize the agent
43
  multimodal_Agent = initialize_agent()
44
 
45
+ # Function to get captions using youtube-transcript-api
46
  def get_youtube_captions(youtube_url):
47
  """
48
+ Retrieves YouTube video captions using youtube-transcript-api.
49
 
50
  Parameters:
51
  - youtube_url: The URL of the YouTube video.
 
53
  Returns:
54
  - The captions of the video in SRT format, or a message if captions are not available.
55
  """
56
+ video_id = youtube_url.split("v=")[-1].split("&")[0]
57
+
58
+ try:
59
+ # Fetch transcript for the video
60
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
 
 
 
 
 
 
61
 
62
+ # Format the transcript in SRT format
63
+ formatter = SRTFormatter()
64
+ formatted_transcript = formatter.format_transcript(transcript)
65
+ return formatted_transcript
66
+
67
+ except Exception as e:
68
+ return f"Error: {str(e)}"
 
 
 
69
 
70
  # YouTube video URL input
71
  youtube_url = st.text_input(
 
76
 
77
  if youtube_url:
78
  try:
79
+ with st.spinner("Fetching and processing the YouTube video..."):
80
+ # Get captions using youtube-transcript-api
81
  captions = get_youtube_captions(youtube_url)
82
 
83
+ # Check if captions are available
84
+ if "Error" not in captions:
85
+ # Display captions
86
+ st.subheader("Video Captions:")
87
+ st.text(captions)
88
+ else:
89
+ st.error(captions)
90
 
91
  # User query input
92
  user_query = st.text_area(
 
100
  st.warning("Please enter a question or insight to analyze the video.")
101
  else:
102
  try:
103
+ with st.spinner("Analyzing video and gathering insights..."):
104
+ # Upload and process the transcript (captions)
105
+ processed_captions = upload_file(captions)
106
+ while processed_captions.state.name == "PROCESSING":
107
+ time.sleep(1)
108
+ processed_captions = get_file(processed_captions.name)
109
+
110
+ # Prompt generation for analysis
111
  analysis_prompt = (
112
  f"""
113
+ Analyze the content of the uploaded YouTube video.
114
+ Respond to the following query using video insights and supplementary web research:
115
  {user_query}
116
  Provide a detailed, user-friendly, and actionable response.
117
  """
118
  )
119
 
120
  # AI agent processing
121
+ response = multimodal_Agent.run(analysis_prompt, videos=[processed_captions])
122
 
123
  # Display the result
124
  st.subheader("Analysis Result")
 
126
 
127
  except Exception as error:
128
  st.error(f"An error occurred during analysis: {error}")
 
129
  except Exception as e:
130
+ st.error(f"An error occurred: {e}")
131
  else:
132
  st.info("Enter a YouTube video link to begin analysis.")