Manasa1 commited on
Commit
a468187
·
verified ·
1 Parent(s): 6e37806

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -39
app.py CHANGED
@@ -40,34 +40,25 @@ def initialize_agent():
40
  # Initialize the agent
41
  multimodal_Agent = initialize_agent()
42
 
43
- # Function to download YouTube video using pytube
44
- def download_youtube_video(youtube_url):
45
  """
46
- Downloads a YouTube video using pytube.
47
 
48
  Parameters:
49
  - youtube_url: The URL of the YouTube video.
50
 
51
  Returns:
52
- - The path to the downloaded video file.
53
  """
54
- # Initialize YouTube object using pytube
55
  yt = YouTube(youtube_url)
56
 
57
- # Get the highest resolution stream available
58
- video_stream = yt.streams.get_highest_resolution()
59
-
60
- # Temporary file for the video
61
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
62
- temp_video_path = temp_video.name
63
-
64
- try:
65
- # Download the video to the temporary file
66
- video_stream.download(output_path=temp_video_path)
67
-
68
- return temp_video_path # Return the video file path
69
- except Exception as e:
70
- raise RuntimeError(f"An error occurred while downloading the video: {str(e)}")
71
 
72
  # YouTube video URL input
73
  youtube_url = st.text_input(
@@ -78,12 +69,13 @@ youtube_url = st.text_input(
78
 
79
  if youtube_url:
80
  try:
81
- with st.spinner("Downloading and processing the YouTube video..."):
82
- # Download video using pytube
83
- video_path = download_youtube_video(youtube_url)
84
 
85
- # Display video
86
- st.video(video_path, format="video/mp4", start_time=0)
 
87
 
88
  # User query input
89
  user_query = st.text_area(
@@ -97,25 +89,19 @@ if youtube_url:
97
  st.warning("Please enter a question or insight to analyze the video.")
98
  else:
99
  try:
100
- with st.spinner("Analyzing video and gathering insights..."):
101
- # Upload and process video file
102
- processed_video = upload_file(video_path)
103
- while processed_video.state.name == "PROCESSING":
104
- time.sleep(1)
105
- processed_video = get_file(processed_video.name)
106
-
107
- # Prompt generation for analysis
108
  analysis_prompt = (
109
  f"""
110
- Analyze the content of the uploaded YouTube video.
111
- Respond to the following query using video insights 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, videos=[processed_video])
119
 
120
  # Display the result
121
  st.subheader("Analysis Result")
@@ -123,10 +109,8 @@ if youtube_url:
123
 
124
  except Exception as error:
125
  st.error(f"An error occurred during analysis: {error}")
126
- finally:
127
- # Clean up temporary video file
128
- Path(video_path).unlink(missing_ok=True)
129
  except Exception as e:
130
- st.error(f"An error occurred while downloading the video: {e}")
131
  else:
132
  st.info("Enter a YouTube video link to begin analysis.")
 
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.
50
 
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
+ # Check if captions are available in English (you can modify this language)
57
+ if 'en' in yt.captions:
58
+ caption = yt.captions['en']
59
+ return caption.generate_srt_captions() # Return captions in SRT format
60
+ else:
61
+ return "No captions available for this video."
 
 
 
 
 
 
 
 
62
 
63
  # YouTube video URL input
64
  youtube_url = st.text_input(
 
69
 
70
  if youtube_url:
71
  try:
72
+ with st.spinner("Fetching captions from the YouTube video..."):
73
+ # Get captions for the video
74
+ captions = get_youtube_captions(youtube_url)
75
 
76
+ # Display captions
77
+ st.subheader("Video Captions (SRT format)")
78
+ st.text(captions)
79
 
80
  # User query input
81
  user_query = st.text_area(
 
89
  st.warning("Please enter a question or insight to analyze the video.")
90
  else:
91
  try:
92
+ with st.spinner("Analyzing captions and gathering insights..."):
93
+ # Generate the analysis prompt using captions
 
 
 
 
 
 
94
  analysis_prompt = (
95
  f"""
96
+ Analyze the content of the following YouTube video captions.
97
+ Respond to the following query using the video captions and supplementary web research:
98
  {user_query}
99
  Provide a detailed, user-friendly, and actionable response.
100
  """
101
  )
102
 
103
  # AI agent processing
104
+ response = multimodal_Agent.run(analysis_prompt, video_captions=captions)
105
 
106
  # Display the result
107
  st.subheader("Analysis Result")
 
109
 
110
  except Exception as error:
111
  st.error(f"An error occurred during analysis: {error}")
112
+
 
 
113
  except Exception as e:
114
+ st.error(f"An error occurred while fetching captions: {e}")
115
  else:
116
  st.info("Enter a YouTube video link to begin analysis.")